mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Fred Griffoul <griffoul@gmail.com>
To: Sean Christopherson <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Cc: Mushahid Hussain <hmushi@amazon.co.uk>,
	James Houghton <jthoughton@google.com>,
	nh-open-source@amazon.com, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [RFC PATCH 1/5] KVM: x86/mmu: Walk memslot rmaps by index
Date: Wed,  4 Nov 2026 00:53:27 +0000	[thread overview]
Message-ID: <20261104005331.50600-2-griffoul@gmail.com> (raw)
In-Reply-To: <20261104005331.50600-1-griffoul@gmail.com>

From: Fred Griffoul <fgriffo@amazon.co.uk>

In preparation for storing memslot rmaps in chunks that are not
contiguous in memory, walk a slot's rmaps by index instead of stepping
a pointer through the array, and look each head up through a new
__idx_to_rmap() helper.

No functional change intended.

Signed-off-by: Fred Griffoul <fgriffo@amazon.co.uk>
---
 arch/x86/kvm/mmu/mmu.c | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)

diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index f4b12d6aef55..b3fcd6f2a991 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -1236,13 +1236,17 @@ unsigned int pte_list_count(struct kvm_rmap_head *rmap_head)
 	return desc->tail_count + desc->spte_count;
 }
 
+static struct kvm_rmap_head *__idx_to_rmap(const struct kvm_memory_slot *slot,
+					   int level, unsigned long idx)
+{
+	return &slot->arch.rmap[level - PG_LEVEL_4K][idx];
+}
+
 static struct kvm_rmap_head *gfn_to_rmap(gfn_t gfn, int level,
 					 const struct kvm_memory_slot *slot)
 {
-	unsigned long idx;
-
-	idx = gfn_to_index(gfn, slot->base_gfn, level);
-	return &slot->arch.rmap[level - PG_LEVEL_4K][idx];
+	return __idx_to_rmap(slot, level,
+			     gfn_to_index(gfn, slot->base_gfn, level));
 }
 
 static void rmap_remove(struct kvm *kvm, u64 *spte)
@@ -1577,17 +1581,21 @@ struct slot_rmap_walk_iterator {
 	struct kvm_rmap_head *rmap;
 	int level;
 
-	/* private field. */
-	struct kvm_rmap_head *end_rmap;
+	/* private fields. */
+	unsigned long idx;
+	unsigned long end_idx;
 };
 
 static void rmap_walk_init_level(struct slot_rmap_walk_iterator *iterator,
 				 int level)
 {
+	const struct kvm_memory_slot *slot = iterator->slot;
+
 	iterator->level = level;
 	iterator->gfn = iterator->start_gfn;
-	iterator->rmap = gfn_to_rmap(iterator->gfn, level, iterator->slot);
-	iterator->end_rmap = gfn_to_rmap(iterator->end_gfn, level, iterator->slot);
+	iterator->idx = gfn_to_index(iterator->start_gfn, slot->base_gfn, level);
+	iterator->end_idx = gfn_to_index(iterator->end_gfn, slot->base_gfn, level);
+	iterator->rmap = gfn_to_rmap(iterator->gfn, level, slot);
 }
 
 static void slot_rmap_walk_init(struct slot_rmap_walk_iterator *iterator,
@@ -1611,11 +1619,17 @@ static bool slot_rmap_walk_okay(struct slot_rmap_walk_iterator *iterator)
 
 static void slot_rmap_walk_next(struct slot_rmap_walk_iterator *iterator)
 {
-	while (++iterator->rmap <= iterator->end_rmap) {
+	struct kvm_rmap_head *rmap_head;
+
+	while (++iterator->idx <= iterator->end_idx) {
 		iterator->gfn += KVM_PAGES_PER_HPAGE(iterator->level);
 
-		if (atomic_long_read(&iterator->rmap->val))
+		rmap_head = __idx_to_rmap(iterator->slot, iterator->level,
+					  iterator->idx);
+		if (atomic_long_read(&rmap_head->val)) {
+			iterator->rmap = rmap_head;
 			return;
+		}
 	}
 
 	if (++iterator->level > iterator->end_level) {
-- 
2.47.3


  reply	other threads:[~2026-11-04  0:53 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-04  0:53 [RFC PATCH 0/5] KVM: x86/mmu: Allocate memslot rmaps on demand Fred Griffoul
2026-11-04  0:53 ` Fred Griffoul [this message]
2026-11-04  0:53 ` [RFC PATCH 2/5] KVM: x86: Count rmaps per level in a debugfs helper Fred Griffoul
2026-11-04  0:53 ` [RFC PATCH 3/5] KVM: x86/mmu: Allocate memslot rmaps in on-demand chunks Fred Griffoul
2026-11-04  0:53 ` [RFC PATCH 4/5] KVM: selftests: Declare __tdp_map() in the x86 processor header Fred Griffoul
2026-11-04  0:53 ` [RFC PATCH 5/5] KVM: selftests: Test dirty logging of sparse nested mappings Fred Griffoul

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=20261104005331.50600-2-griffoul@gmail.com \
    --to=griffoul@gmail.com \
    --cc=hmushi@amazon.co.uk \
    --cc=jthoughton@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nh-open-source@amazon.com \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.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

all inboxes | Powered by JetHome®