From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C122B1D86FF; Sat, 23 May 2026 00:18:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779495483; cv=none; b=oSKUlxHYHNhJhN5yIjmotvGfxRURA3SOSHQaY4gZa2ycbL0/qVMsAdBZt6TVqA+zvSlMPRvPd4WaOPVsEZffgPYgcX2yYzKtat51+M+FRAtx4cep1vkYSpoefyvFW6l2yvDR6JSu3ULMxZqDU579TgRnikyYh0LzwKfqhVuQuqc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779495483; c=relaxed/simple; bh=y3UInR9UUXj1QvhOJx1ThwJEEaF909///rvCuhskB+E=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=lP6fMJjbFlxacgXAIexT2SVUjM4BZ3UKsOFnwW6XCBEYva/u5l7Ndjc6uPT9dJOqseKMGBUJgIAfswofqQF5AaG0lo058iHxUWdoUPh2PuiRssEkjWm2bWFUyrAZGIWqwQCi0NnTIqakUZoCaW8hxLKF0JtpTTNm76HXNlRtHFc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iNW3ONMI; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="iNW3ONMI" Received: by smtp.kernel.org (Postfix) with ESMTPS id 84795C4AF13; Sat, 23 May 2026 00:18:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1779495483; bh=y3UInR9UUXj1QvhOJx1ThwJEEaF909///rvCuhskB+E=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=iNW3ONMIXTQT67b4u1PLfiQKfvp+YXFq85oNehedYy8YU4vL0rjwV6FYXOv9AG6Db 4W33LNH5BO2RWiqjk0/BA40vTmCAoSblCBF72aBYvz22nk27uzkIfDvGHxJOX3HOAb xTPKppP6SVReE/p1O1AXwq87rOnjzAKD33m9GbXJigSJn3dzcTf1/ENDXjy3V7j7Tw FZJIpsKlhy6p1d8K9Bgq93OAN3Vq6VF1tlZ1rOoF9L2B+KLKErQ1YVT3wDPG5965Ym z//axnRst0+n6FIWJU0mjvSSUClmDR+P7iDP6kEB4V5DWsvnBpsiVUts236LgRTmHD udSZbh6RZS8HQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 78526CD5BB0; Sat, 23 May 2026 00:18:03 +0000 (UTC) From: Ackerley Tng via B4 Relay Date: Fri, 22 May 2026 17:17:47 -0700 Subject: [PATCH v7 05/42] KVM: guest_memfd: Wire up kvm_get_memory_attributes() to per-gmem attributes Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260522-gmem-inplace-conversion-v7-5-2f0fae496530@google.com> References: <20260522-gmem-inplace-conversion-v7-0-2f0fae496530@google.com> In-Reply-To: <20260522-gmem-inplace-conversion-v7-0-2f0fae496530@google.com> 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, 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, liam@infradead.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 , Andrew Morton , Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Youngjun Park , Qi Zheng , Shakeel Butt , Kiryl Shutsemau , 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, linux-mm@kvack.org, linux-coco@lists.linux.dev, Ackerley Tng X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1779495480; l=3662; i=ackerleytng@google.com; s=20260225; h=from:subject:message-id; bh=OouColGXY1rz+TqhbKdalwaFwpqqCIpTBsSizuwNECM=; b=m9CYG0+92BUQHFPoJVWx5CaK0A66GGKjWj3gHwo+8ZsuTZcxAjT5j+rnJrQgEMXHpGsJv8kQe pcw6COMTdRhCORBOl30dALQ5P5jQqIyzSygFNmAO3A6IGl0MLvkFelX X-Developer-Key: i=ackerleytng@google.com; a=ed25519; pk=sAZDYXdm6Iz8FHitpHeFlCMXwabodTm7p8/3/8xUxuU= X-Endpoint-Received: by B4 Relay for ackerleytng@google.com/20260225 with auth_id=649 X-Original-From: Ackerley Tng Reply-To: ackerleytng@google.com From: Sean Christopherson Implement kvm_gmem_get_memory_attributes() for guest_memfd to allow the KVM core and architecture code to query per-GFN memory attributes. kvm_gmem_get_memory_attributes() finds the memory slot for a given GFN and queries the guest_memfd file's to determine if the page is marked as private. If vm_memory_attributes is not enabled, there is no shared/private tracking at the VM level. Install the guest_memfd implementation as long as guest_memfd is enabled to give guest_memfd a chance to respond on attributes. guest_memfd should look up attributes regardless of whether this memslot is gmem-only since attributes are now tracked by gmem regardless of whether mmap() is enabled. Signed-off-by: Sean Christopherson Co-developed-by: Ackerley Tng Signed-off-by: Ackerley Tng --- include/linux/kvm_host.h | 2 ++ virt/kvm/guest_memfd.c | 31 +++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 3 +++ 3 files changed, 36 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 29694b348df40..7de85474c75bd 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2557,6 +2557,8 @@ bool kvm_arch_post_set_memory_attributes(struct kvm *kvm, struct kvm_gfn_range *range); #endif /* CONFIG_KVM_VM_MEMORY_ATTRIBUTES */ +unsigned long kvm_gmem_get_memory_attributes(struct kvm *kvm, gfn_t gfn); + #ifdef CONFIG_KVM_GUEST_MEMFD int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, kvm_pfn_t *pfn, struct page **page, diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 117b726f670e8..c55879e033d96 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -509,6 +509,37 @@ static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma) return 0; } +unsigned long kvm_gmem_get_memory_attributes(struct kvm *kvm, gfn_t gfn) +{ + struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); + struct inode *inode; + + /* + * If this gfn has no associated memslot, there's no chance of the gfn + * being backed by private memory, since guest_memfd must be used for + * private memory, and guest_memfd must be associated with some memslot. + */ + if (!slot) + return 0; + + CLASS(gmem_get_file, file)(slot); + if (!file) + return 0; + + inode = file_inode(file); + + /* + * Rely on the maple tree's internal RCU lock to ensure a + * stable result. This result can become stale as soon as the + * lock is dropped, so the caller _must_ still protect + * consumption of private vs. shared by checking + * mmu_invalidate_retry_gfn() under mmu_lock to serialize + * against ongoing attribute updates. + */ + return kvm_gmem_get_attributes(inode, kvm_gmem_get_index(slot, gfn)); +} +EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_gmem_get_memory_attributes); + static struct file_operations kvm_gmem_fops = { .mmap = kvm_gmem_mmap, .open = generic_file_open, diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index ee26f1d9b5fda..4139e903f756a 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2653,6 +2653,9 @@ static void kvm_init_memory_attributes(void) if (vm_memory_attributes) static_call_update(__kvm_get_memory_attributes, kvm_get_vm_memory_attributes); + else if (IS_ENABLED(CONFIG_KVM_GUEST_MEMFD)) + static_call_update(__kvm_get_memory_attributes, + kvm_gmem_get_memory_attributes); else static_call_update(__kvm_get_memory_attributes, (void *)__static_call_return0); -- 2.54.0.794.g4f17f83d09-goog