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,
seiden@linux.ibm.com
Subject: [PATCH v1 08/12] KVM: s390: Properly adjust page size before calling _gmap_link()
Date: Fri, 27 Nov 2026 16:04:07 +0100 [thread overview]
Message-ID: <20261127150411.221072-9-imbrenda@linux.ibm.com> (raw)
In-Reply-To: <20261127150411.221072-1-imbrenda@linux.ibm.com>
Checking the size of the folio alone is not enough to determine the
maximum allowed page size for a mapping. Multiple vmas with different
permissions could be sharing a single large folio.
Fix by checking the actual size of the userspace mapping if the folio
was large enough, and adjust the size accordingly before attempting to
use large pmds or puds.
This reflects the behaviour on other architectures.
Fixes: 4204067f9982 ("KVM: s390: Add alignment checks for hugepages")
Fixes: a2c17f9270cc ("KVM: s390: New gmap code")
Cc: stable@vger.kernel.org
Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
---
arch/s390/kvm/gmap/gmap.c | 88 +++++++++++++++++++++++++++++++++++----
1 file changed, 79 insertions(+), 9 deletions(-)
diff --git a/arch/s390/kvm/gmap/gmap.c b/arch/s390/kvm/gmap/gmap.c
index 837815a7dffa..5a40f411bcc1 100644
--- a/arch/s390/kvm/gmap/gmap.c
+++ b/arch/s390/kvm/gmap/gmap.c
@@ -858,22 +858,92 @@ static int _gmap_link(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, int leve
return rc;
}
+/**
+ * gmap_get_userspace_page_size() - Check the actual page size of the mapping
+ * @kvm: the guest
+ * @gfn: the guest address whose page size is to be checked
+ * @slot: the KVM memslot containing @gfn
+ *
+ * Manually walk the userspace page tables to find out the page size.
+ *
+ * This is safe because kvm->mmu_lock is being held; being a (r/w) spinlock,
+ * preemption is disabled while it is being held.
+ * Due to RCU-deferred freeing of page tables, that is enough to guarantee
+ * that the userspace tables won't disappear while being walked.
+ *
+ * Context: kvm->mmu_lock must be held.
+ * Return:
+ * * TABLE_TYPE_REGION3 if userspace is mapping with a large pud,
+ * * TABLE_TYPE_SEGMENT if userspace is mapping with a large pmd,
+ * * TABLE_TYPE_PAGE_TABLE otherwise, including missing mappings.
+ */
+static int gmap_get_userspace_page_size(struct kvm *kvm, gfn_t gfn, struct kvm_memory_slot *slot)
+{
+ hva_t hva;
+ pgd_t pgd;
+ p4d_t p4d;
+ pud_t pud;
+ pmd_t pmd;
+
+ lockdep_assert_held(&kvm->mmu_lock);
+
+ hva = __gfn_to_hva_memslot(slot, gfn);
+
+ pgd = READ_ONCE(*pgd_offset(kvm->mm, hva));
+ if (pgd_none(pgd))
+ return TABLE_TYPE_PAGE_TABLE;
+
+ p4d = READ_ONCE(*p4d_offset(&pgd, hva));
+ if (p4d_none(p4d) || !p4d_present(p4d))
+ return TABLE_TYPE_PAGE_TABLE;
+
+ pud = READ_ONCE(*pud_offset(&p4d, hva));
+ if (pud_none(pud) || !pud_present(pud))
+ return TABLE_TYPE_PAGE_TABLE;
+ if (pud_leaf(pud))
+ return TABLE_TYPE_REGION3;
+
+ pmd = READ_ONCE(*pmd_offset(&pud, hva));
+ if (pmd_none(pmd) || !pmd_present(pmd))
+ return TABLE_TYPE_PAGE_TABLE;
+ if (pmd_leaf(pmd))
+ return TABLE_TYPE_SEGMENT;
+
+ return TABLE_TYPE_PAGE_TABLE;
+}
+
+static int gmap_determine_fault_page_size(struct gmap *gmap, struct guest_fault *f,
+ struct kvm_memory_slot *slot)
+{
+ unsigned int order;
+ int level;
+
+ if (!f->page)
+ return TABLE_TYPE_PAGE_TABLE;
+
+ order = folio_order(page_folio(f->page));
+ level = TABLE_TYPE_PAGE_TABLE;
+
+ if (order >= get_order(_REGION3_SIZE) && gmap_2g_allowed(gmap, f, slot))
+ level = TABLE_TYPE_REGION3;
+ else if (order >= get_order(_SEGMENT_SIZE) && gmap_1m_allowed(gmap, f, slot))
+ level = TABLE_TYPE_SEGMENT;
+
+ if (level == TABLE_TYPE_PAGE_TABLE)
+ return level;
+
+ return min(level, gmap_get_userspace_page_size(gmap->kvm, f->gfn, slot));
+}
+
int gmap_link(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, struct guest_fault *f,
struct kvm_memory_slot *slot)
{
- unsigned int order;
int level;
lockdep_assert_held(&gmap->kvm->mmu_lock);
- level = TABLE_TYPE_PAGE_TABLE;
- if (f->page) {
- order = folio_order(page_folio(f->page));
- if (order >= get_order(_REGION3_SIZE) && gmap_2g_allowed(gmap, f, slot))
- level = TABLE_TYPE_REGION3;
- else if (order >= get_order(_SEGMENT_SIZE) && gmap_1m_allowed(gmap, f, slot))
- level = TABLE_TYPE_SEGMENT;
- }
+ level = gmap_determine_fault_page_size(gmap, f, slot);
+
return _gmap_link(mc, gmap, level, f);
}
--
2.56.1
next prev parent reply other threads:[~2026-11-27 15:04 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 15:03 [PATCH v1 00/12] KVM: s390: More fixes Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 01/12] KVM: s390: ucontrol: Do not mask -ENOMEM when translating Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 02/12] KVM: s390: Fix potential leak in kvm_s390_get_io_int() Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 03/12] KVM: s390: Introduce gmap_enable_cmm() Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 04/12] KVM: s390: Allocate page tables when CMMA is used Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 05/12] KVM: s390: Fix dat_entry_walk() with DAT_WALK_CONTINUE Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 06/12] KVM: s390: Fix soft-dirty handling in _gmap_link() Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 07/12] KVM: s390: Fix race in gmap_clear_young_pte() Claudio Imbrenda
2026-11-27 15:04 ` Claudio Imbrenda [this message]
2026-11-27 15:04 ` [PATCH v1 09/12] KVM: s390: Fix *_test_and_clear_softdirty() functions Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 10/12] KVM: s390: Do not unnecessarily drop the soft-dirty bit Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 11/12] KVM: s390: Disallow 2G pages for guests up to 2G Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 12/12] KVM: s390: vsie: Fix improper shadowing of non-present host ptes Claudio Imbrenda
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=20261127150411.221072-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 \
--cc=seiden@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