From: Claudio Imbrenda <imbrenda@linux.ibm.com>
To: kvm@vger.kernel.org
Cc: linux-s390@vger.kernel.org, borntraeger@de.ibm.com,
frankja@linux.ibm.com, david@kernel.org, nrb@linux.ibm.com,
schlameuss@linux.ibm.com, gra@linux.ibm.com
Subject: [PATCH v2 08/10] KVM: s390: Improve unmapping of large ranges
Date: Fri, 23 Oct 2026 17:55:29 +0200 [thread overview]
Message-ID: <20261023155531.206113-9-imbrenda@linux.ibm.com> (raw)
In-Reply-To: <20261023155531.206113-1-imbrenda@linux.ibm.com>
When unmapping guest ranges, each individual leaf DAT entry is
invalidated. When unmapping large ranges, this results in a very large
amount of invalidations, which is time consuming. Moreover, since the
unmap callback is called by KVM while holding a spinlock, this also
leads to stall warnings.
Improve the situation by invalidating DAT entries higher in the tree,
instead of each leaf individually.
For small invalidations (up to 1 segment = 1M of guest memory), and
special cases (like protected VMs) regardless of the size, the old and
slow invalidation is performed; all leaf entries are invalidated one by
one.
For large invalidations (larger than 1M of guest memory), the new
strategy is used:
- Invalidate prefixes on the whole range, extending start and end to
1M granularity
- Iterate through the page table pointed to by each pmd and unshadow
entries as needed
- Detatch (invalidate) the pmd
- Save storage keys as needed
- Invalidate each (now detached) pte by simply overwriting it
- Reattach the old pmd to point to the updated page table
Fixes: a2c17f9270cc ("KVM: s390: New gmap code")
Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
---
arch/s390/kvm/gmap/gmap.c | 112 ++++++++++++++++++++++++++++++++++----
1 file changed, 101 insertions(+), 11 deletions(-)
diff --git a/arch/s390/kvm/gmap/gmap.c b/arch/s390/kvm/gmap/gmap.c
index c10681f882c2..01d25aa1f605 100644
--- a/arch/s390/kvm/gmap/gmap.c
+++ b/arch/s390/kvm/gmap/gmap.c
@@ -437,17 +437,8 @@ static long _gmap_unmap_crste(union crste *crstep, gfn_t gfn, gfn_t next, struct
return 0;
}
-/**
- * gmap_unmap_gfn_range() - Unmap a range of guest addresses.
- * @gmap: The gmap to act on.
- * @slot: The memslot in which the range is located.
- * @start: The first gfn to unmap.
- * @end: The gfn after the last one to unmap.
- *
- * Context: Called with the kvm mmu write lock held.
- * Return: false
- */
-bool gmap_unmap_gfn_range(struct gmap *gmap, struct kvm_memory_slot *slot, gfn_t start, gfn_t end)
+static bool gmap_unmap_gfn_range_slow(struct gmap *gmap, struct kvm_memory_slot *slot,
+ gfn_t start, gfn_t end)
{
const struct dat_walk_ops ops = {
.pte_entry = _gmap_unmap_pte,
@@ -465,6 +456,105 @@ bool gmap_unmap_gfn_range(struct gmap *gmap, struct kvm_memory_slot *slot, gfn_t
return false;
}
+static long _gmap_unmap_pud_fast(union crste *crstep, gfn_t gfn, gfn_t next, struct dat_walk *w)
+{
+ union crste old = *crstep;
+
+ /* We only care about large puds */
+ if (!old.h.fc)
+ return 0;
+ if (!gfn)
+ gmap_unmap_prefix(w->priv, 0, next);
+ if (old.s.fc1.vsie_notif)
+ gmap_handle_vsie_unshadow_event(w->priv, gfn);
+
+ idte_crste(crstep, gfn, w->datt.asce);
+ *crstep = _CRSTE_EMPTY(old.h.tt);
+
+ return 0;
+}
+
+static long _gmap_unmap_pmd_fast(union crste *crstep, gfn_t gfn, gfn_t next, struct dat_walk *w)
+{
+ union crste old = *crstep;
+ struct page_table *pt;
+ gfn_t first, last;
+ unsigned int i;
+
+ if (old.h.fc) {
+ if (old.s.fc1.vsie_notif)
+ gmap_handle_vsie_unshadow_event(w->priv, gfn);
+
+ idte_crste(crstep, gfn, w->datt.asce);
+ WRITE_ONCE(*crstep, _CRSTE_EMPTY(old.h.tt));
+
+ return 0;
+ }
+
+ if (old.h.i)
+ return 0;
+
+ pt = dereference_pmd(old.pmd);
+ first = max(gfn, w->start) & (PTRS_PER_PTE - 1);
+ last = (min(next, w->end) - 1) & (PTRS_PER_PTE - 1);
+
+ for (i = first; i <= last; i++)
+ if (pte_needs_unshadow(pt->ptes[i], _PTE_EMPTY, pt->pgstes[i]))
+ gmap_handle_vsie_unshadow_event(w->priv, gfn + i);
+
+ /* Detach the page table */
+ idte_crste(crstep, gfn, w->datt.asce);
+
+ for (i = first; i <= last; i++) {
+ if (!pt->ptes[i].h.i && w->datt.uses_skeys)
+ pt->pgstes[i] = dat_save_storage_key_into_pgste(pt->ptes[i], pt->pgstes[i]);
+ pt->ptes[i] = _PTE_EMPTY;
+ pt->pgstes[i].vsie_notif = 0;
+ pt->pgstes[i].vsie_gmem = 0;
+ pt->pgstes[i].zero = 0;
+ }
+
+ barrier();
+ /* Reattach the modified page table */
+ WRITE_ONCE(*crstep, old);
+
+ return 0;
+}
+
+static bool gmap_unmap_gfn_range_fast(struct gmap *gmap, gfn_t start, gfn_t end)
+{
+ const struct dat_walk_ops ops = {
+ .pud_entry = _gmap_unmap_pud_fast,
+ .pmd_entry = _gmap_unmap_pmd_fast,
+ };
+ bool failed;
+
+ lockdep_assert_held_write(&gmap->kvm->mmu_lock);
+
+ gmap_unmap_prefix(gmap, ALIGN_DOWN(start, _PAGE_ENTRIES), ALIGN(end, _PAGE_ENTRIES));
+
+ failed = _dat_walk_gfn_range(start, end, gmap->datt, &ops, DAT_WALK_IGN_HOLES, gmap);
+ KVM_BUG_ON(failed, gmap->kvm);
+ return false;
+}
+
+/**
+ * gmap_unmap_gfn_range() - Unmap a range of guest addresses.
+ * @gmap: The gmap to act on.
+ * @slot: The memslot in which the range is located.
+ * @start: The first gfn to unmap.
+ * @end: The gfn after the last one to unmap.
+ *
+ * Context: Called with the kvm mmu write lock held.
+ * Return: false
+ */
+bool gmap_unmap_gfn_range(struct gmap *gmap, struct kvm_memory_slot *slot, gfn_t start, gfn_t end)
+{
+ if ((end - start <= _PAGES_PER_SEGMENT) || export_on_unmap(gmap) || is_ucontrol(gmap))
+ return gmap_unmap_gfn_range_slow(gmap, slot, start, end);
+ return gmap_unmap_gfn_range_fast(gmap, start, end);
+}
+
static union pgste __pte_test_and_clear_softdirty(union pte *ptep, union pgste pgste, gfn_t gfn,
struct gmap *gmap)
{
--
2.56.0
next prev parent reply other threads:[~2026-10-23 15:55 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-23 15:55 [PATCH v2 00/10] KVM: s390: Misc bugfixes and improvements for 7.4 Claudio Imbrenda
2026-10-23 15:55 ` [PATCH v2 01/10] KVM: s390: Move some skey functions to headers Claudio Imbrenda
2026-10-26 8:32 ` Christoph Schlameuss
2026-10-23 15:55 ` [PATCH v2 02/10] KVM: s390: Refactor and improve DAT tables handling Claudio Imbrenda
2026-10-23 15:55 ` [PATCH v2 03/10] KVM: s390: Move prefix marker from PGSTEs to PTEs Claudio Imbrenda
2026-10-23 15:55 ` [PATCH v2 04/10] KVM: s390: Automatically enable storage keys for UCONTROL VMs Claudio Imbrenda
2026-10-23 15:55 ` [PATCH v2 05/10] KVM: s390: Replace ASCE usage with a struct with more flags Claudio Imbrenda
2026-10-23 15:55 ` [PATCH v2 06/10] KVM: s390: Rework some helper functions Claudio Imbrenda
2026-10-26 10:03 ` Christoph Schlameuss
2026-10-23 15:55 ` [PATCH v2 07/10] KVM: s390: Fix UCONTROL inconsistencies Claudio Imbrenda
2026-10-23 16:47 ` sashiko-bot
2026-10-23 15:55 ` Claudio Imbrenda [this message]
2026-10-23 17:17 ` [PATCH v2 08/10] KVM: s390: Improve unmapping of large ranges sashiko-bot
2026-10-23 15:55 ` [PATCH v2 09/10] KVM: s390: vsie: Fix pinning using the wrong flag Claudio Imbrenda
2026-10-23 15:55 ` [PATCH v2 10/10] KVM: s390: Enable 1M pages by default Claudio Imbrenda
2026-10-23 16:36 ` sashiko-bot
2026-10-26 10:31 ` Christian Borntraeger
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=20261023155531.206113-9-imbrenda@linux.ibm.com \
--to=imbrenda@linux.ibm.com \
--cc=borntraeger@de.ibm.com \
--cc=david@kernel.org \
--cc=frankja@linux.ibm.com \
--cc=gra@linux.ibm.com \
--cc=kvm@vger.kernel.org \
--cc=linux-s390@vger.kernel.org \
--cc=nrb@linux.ibm.com \
--cc=schlameuss@linux.ibm.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