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 0/5] KVM: x86/mmu: Allocate memslot rmaps on demand
Date: Wed,  4 Nov 2026 00:53:26 +0000	[thread overview]
Message-ID: <20261104005331.50600-1-griffoul@gmail.com> (raw)

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

When an L1 guest runs a nested guest, KVM allocates rmaps for all of
L1's memory the first time it creates a shadow root, one 8-byte head
per 4 KiB page. For an L1 with 4 TiB of memory that is 8 GiB, allocated
at once on the first nested VM-Enter, whether L2 maps 1% or all of L1's
memory. Sean pointed at this cost on Mushahid's lpage_info patch [1].

This RFC allocates rmaps in page-sized chunks instead, when the first
SPTE in a chunk's range is added. Each memslot keeps a per-level
directory of chunks, 8 bytes per 2 MiB at the 4 KiB level, so the 4 TiB
L1 above allocates 16 MiB on the first nested VM-Enter. A missing chunk
reads as empty rmaps and slot walks skip it. When L2 maps every gfn,
rmaps cost what they did before plus the directory.

Patches 1 and 2 prepare the slot walk and the debugfs rmap statistics,
with no functional change. Patch 3 does the chunking. Patches 4 and 5
add a selftest in which L2 writes pages around chunk boundaries and
checks the dirty log after the memslot is write-protected, including
eager splitting of nested huge pages.

I would like feedback on:
  - The data structure. A directory of page-sized chunks keeps lookups
    to one extra load. An xarray would be simpler to grow but adds a
    tree walk to every rmap lookup.
  - Freeing chunks once they are empty. That would let the shadow MMU
    page limit bound rmap memory too, but lockless aging reads chunks
    without mmu_lock, so freeing needs an RCU grace period. I left it
    out of this RFC.
  - Doing the same for gfn_write_track, 2 bytes per 4 KiB page, which
    is still allocated for every memslot on the first shadow root.

Limitations:
  - Chunks are only freed with the memslot.
  - Eager page splitting on dirty-log enable splits L2's huge mappings,
    so every 2 MiB that L2 mapped with a huge page then gets a chunk.

The series is based on kvm.git next.

[1] https://lore.kernel.org/all/arr1VgGZmgUmY4hc@google.com/

Fred


Fred Griffoul (5):
  KVM: x86/mmu: Walk memslot rmaps by index
  KVM: x86: Count rmaps per level in a debugfs helper
  KVM: x86/mmu: Allocate memslot rmaps in on-demand chunks
  KVM: selftests: Declare __tdp_map() in the x86 processor header
  KVM: selftests: Test dirty logging of sparse nested mappings

 arch/x86/include/asm/kvm_host.h               |  22 +-
 arch/x86/kvm/debugfs.c                        |  46 ++-
 arch/x86/kvm/mmu/mmu.c                        | 126 +++++++-
 arch/x86/kvm/x86.c                            |  22 +-
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/include/x86/processor.h     |   1 +
 .../kvm/x86/nested_dirty_log_sparse_test.c    | 275 ++++++++++++++++++
 7 files changed, 460 insertions(+), 33 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/x86/nested_dirty_log_sparse_test.c


base-commit: d21eef2084a9af87ac2a783bc7c2b175ba114e4e
-- 
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 Fred Griffoul [this message]
2026-11-04  0:53 ` [RFC PATCH 1/5] KVM: x86/mmu: Walk memslot rmaps by index Fred Griffoul
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-1-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®