From: "Aneesh Kumar K.V (Arm)" <aneesh.kumar@kernel.org>
To: linux-coco@lists.linux.dev
Cc: "Aneesh Kumar K.V (Arm)" <aneesh.kumar@kernel.org>,
Ackerley Tng <ackerleytng@google.com>,
Alex Williamson <alex@shazbot.org>,
David Woodhouse <dwmw2@infradead.org>,
David Hildenbrand <david@kernel.org>,
Jason Gunthorpe <jgg@ziepe.ca>,
"Joerg Roedel (AMD)" <joro@8bytes.org>,
Kevin Tian <kevin.tian@intel.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Robin Murphy <robin.murphy@arm.com>,
Sean Christopherson <seanjc@google.com>,
Will Deacon <will@kernel.org>, Alexey Kardashevskiy <aik@amd.com>,
Xu Yilun <yilun.xu@linux.intel.com>,
Catalin Marinas <catalin.marinas@arm.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
Steven Price <steven.price@arm.com>,
Fred Griffoul <griffoul@gmail.com>,
iommu@lists.linux.dev, kvm@vger.kernel.org,
patches@lists.linux.dev
Subject: [RFC PATCH v3 08/13] KVM: guest_memfd: Track private device ranges after attribute updates
Date: Sun, 1 Nov 2026 12:49:22 +0530 [thread overview]
Message-ID: <20261101071928.2462549-9-aneesh.kumar@kernel.org> (raw)
In-Reply-To: <20261101071928.2462549-1-aneesh.kumar@kernel.org>
The following device access callbacks need to report whether any private
ranges remain without acquiring the inode invalidate lock under a
provider lock.
Add device_has_private as a cached summary of the committed attributes.
Add an optional update_attributes() backing operation and call it after
storing new attributes on successful conversion. Device backing scans
the attribute tree to refresh the summary; folio and RAM PFN backing
leave the operation unset.
Run the hook after both private and shared conversions, before releasing
conversion locks. The scan must see the newly stored attributes, including
the removal of the last private range. Keep this bookkeeping separate
from the architecture mapping operations.
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Sean Christopherson <seanjc@google.com>
Cc: David Hildenbrand <david@kernel.org>
Cc: kvm@vger.kernel.org
Cc: patches@lists.linux.dev
Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
---
virt/kvm/guest_memfd.c | 29 ++++++++++++++++++++++++++++-
1 file changed, 28 insertions(+), 1 deletion(-)
diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
index 3bf26490f4e9..af1e4798ace1 100644
--- a/virt/kvm/guest_memfd.c
+++ b/virt/kvm/guest_memfd.c
@@ -44,6 +44,8 @@ struct kvm_gmem_backing_ops {
/* Prepare private RAM state after lookup, before returning a guest PFN. */
int (*prepare_private_pfn)(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn,
kvm_pfn_t nr_pages);
+ /* Called after the common code stores the new attributes. */
+ void (*update_attributes)(struct inode *inode);
};
struct gmem_inode {
@@ -56,6 +58,7 @@ struct gmem_inode {
const struct kvm_gmem_backing_ops *backing_ops;
struct guest_memfd_device device;
+ bool device_has_private;
u64 flags;
/*
@@ -80,6 +83,7 @@ static bool gmem_provider_is_device(struct gmem_inode *gi)
return gi->provider_ops && gi->provider_ops->device_memory;
}
+static void kvm_gmem_device_update_attributes(struct inode *inode);
static int kvm_gmem_device_make_private(struct inode *inode, pgoff_t start, pgoff_t end);
struct guest_memfd_device *kvm_gmem_get_device(struct inode *inode)
@@ -1182,6 +1186,7 @@ static const struct kvm_gmem_backing_ops kvm_gmem_device_backing_ops = {
.prepare_attributes = kvm_gmem_pfn_prepare_attributes,
.make_private = kvm_gmem_device_make_private,
.make_shared = kvm_gmem_noop_conversion,
+ .update_attributes = kvm_gmem_device_update_attributes,
};
static const struct kvm_gmem_backing_ops *
@@ -1241,8 +1246,11 @@ static int __kvm_gmem_set_attributes(struct inode *inode, pgoff_t start,
kvm_gmem_invalidate_start(inode, start, end, filter);
r = kvm_gmem_convert_attributes(inode, start, end, attrs);
- if (!r)
+ if (!r) {
mas_store_prealloc(&mas, xa_mk_value(attrs));
+ if (gi->backing_ops->update_attributes)
+ gi->backing_ops->update_attributes(inode);
+ }
if (r)
mas_destroy(&mas);
@@ -1502,6 +1510,24 @@ int __weak kvm_arch_gmem_device_map(struct kvm *kvm,
return -EOPNOTSUPP;
}
+static void kvm_gmem_device_update_attributes(struct inode *inode)
+{
+ struct gmem_inode *gi = GMEM_I(inode);
+ void *entry;
+ bool private = false;
+
+ MA_STATE(mas, &gi->attributes, 0, 0);
+
+ mas_for_each(&mas, entry, ULONG_MAX) {
+ if (kvm_gmem_get_attributes(&gi->vfs_inode, entry) &
+ KVM_MEMORY_ATTRIBUTE_PRIVATE) {
+ private = true;
+ break;
+ }
+ }
+ WRITE_ONCE(gi->device_has_private, private);
+}
+
/* The inode lock protects attributes and the offset-to-GPA bindings. */
static int kvm_gmem_device_make_private(struct inode *inode, pgoff_t start,
pgoff_t end)
@@ -2042,6 +2068,7 @@ static struct inode *kvm_gmem_alloc_inode(struct super_block *sb)
gi->provider_ops = NULL;
gi->backing_ops = NULL;
memset(&gi->device, 0, sizeof(gi->device));
+ gi->device_has_private = false;
INIT_LIST_HEAD(&gi->gmem_file_list);
return &gi->vfs_inode;
}
--
2.43.0
next prev parent reply other threads:[~2026-11-01 7:20 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-01 7:19 [RFC PATCH v3 00/13] KVM: guest_memfd: Add a common framework for device memory providers Aneesh Kumar K.V (Arm)
2026-11-01 7:19 ` [RFC PATCH v3 01/13] KVM: guest_memfd: Let providers prepare memory regions Aneesh Kumar K.V (Arm)
2026-11-01 7:19 ` [RFC PATCH v3 02/13] KVM: guest_memfd: Dispatch attribute conversion through backing operations Aneesh Kumar K.V (Arm)
2026-11-01 7:19 ` [RFC PATCH v3 03/13] KVM: guest_memfd: Let providers coordinate mapping changes Aneesh Kumar K.V (Arm)
2026-11-01 7:19 ` [RFC PATCH v3 04/13] KVM: guest_memfd: Dispatch revocation through backing operations Aneesh Kumar K.V (Arm)
2026-11-01 7:19 ` [RFC PATCH v3 05/13] KVM: guest_memfd: Split population by backing type Aneesh Kumar K.V (Arm)
2026-11-01 7:19 ` [RFC PATCH v3 06/13] KVM: guest_memfd: Support device-backed PFN providers Aneesh Kumar K.V (Arm)
2026-11-01 7:19 ` [RFC PATCH v3 07/13] KVM: guest_memfd: Support private/shared conversion of device memory Aneesh Kumar K.V (Arm)
2026-11-01 7:19 ` Aneesh Kumar K.V (Arm) [this message]
2026-11-01 7:19 ` [RFC PATCH v3 09/13] KVM: guest_memfd: Expose device provider private state Aneesh Kumar K.V (Arm)
2026-11-01 7:19 ` [RFC PATCH v3 10/13] KVM: guest_memfd: Support range revocation of device mappings Aneesh Kumar K.V (Arm)
2026-11-01 7:19 ` [RFC PATCH v3 11/13] KVM: guest_memfd: Add shared device mmap through PFN providers Aneesh Kumar K.V (Arm)
2026-11-01 7:19 ` [RFC PATCH v3 12/13] KVM: guest_memfd: Remove device mappings on final close Aneesh Kumar K.V (Arm)
2026-11-01 7:19 ` [RFC PATCH v3 13/13] KVM: guest_memfd: Invalidate guest_memfd mappings before removing memslot bindings Aneesh Kumar K.V (Arm)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261101071928.2462549-9-aneesh.kumar@kernel.org \
--to=aneesh.kumar@kernel.org \
--cc=ackerleytng@google.com \
--cc=aik@amd.com \
--cc=alex@shazbot.org \
--cc=catalin.marinas@arm.com \
--cc=david@kernel.org \
--cc=dwmw2@infradead.org \
--cc=griffoul@gmail.com \
--cc=iommu@lists.linux.dev \
--cc=jgg@ziepe.ca \
--cc=joro@8bytes.org \
--cc=kevin.tian@intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-coco@lists.linux.dev \
--cc=patches@lists.linux.dev \
--cc=pbonzini@redhat.com \
--cc=robin.murphy@arm.com \
--cc=seanjc@google.com \
--cc=steven.price@arm.com \
--cc=suzuki.poulose@arm.com \
--cc=will@kernel.org \
--cc=yilun.xu@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox