From: Janosch Frank <frankja@linux.ibm.com>
To: pbonzini@redhat.com
Cc: kvm@vger.kernel.org, frankja@linux.ibm.com, david@kernel.org,
borntraeger@linux.ibm.com, cohuck@redhat.com,
linux-s390@vger.kernel.org, imbrenda@linux.ibm.com,
Steffen Eiden <seiden@linux.ibm.com>
Subject: [GIT PULL 08/12] KVM: s390: Add alignment checks for hugepages
Date: Mon, 13 Apr 2026 12:46:36 +0200 [thread overview]
Message-ID: <20260413104721.203024-9-frankja@linux.ibm.com> (raw)
In-Reply-To: <20260413104721.203024-1-frankja@linux.ibm.com>
From: Claudio Imbrenda <imbrenda@linux.ibm.com>
When backing a guest page with a large page, check that the alignment
of the guest page matches the alignment of the host physical page
backing it within the large page.
Also check that the memslot is large enough to fit the large page.
Those checks are currently not needed, because memslots are guaranteed
to be 1m-aligned, but this will change.
Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
---
arch/s390/kvm/faultin.c | 2 +-
arch/s390/kvm/gmap.c | 32 ++++++++++++++++++++++++++------
arch/s390/kvm/gmap.h | 3 ++-
3 files changed, 29 insertions(+), 8 deletions(-)
diff --git a/arch/s390/kvm/faultin.c b/arch/s390/kvm/faultin.c
index e37cd18200f5..ddf0ca71f374 100644
--- a/arch/s390/kvm/faultin.c
+++ b/arch/s390/kvm/faultin.c
@@ -109,7 +109,7 @@ int kvm_s390_faultin_gfn(struct kvm_vcpu *vcpu, struct kvm *kvm, struct guest_fa
scoped_guard(read_lock, &kvm->mmu_lock) {
if (!mmu_invalidate_retry_gfn(kvm, inv_seq, f->gfn)) {
f->valid = true;
- rc = gmap_link(mc, kvm->arch.gmap, f);
+ rc = gmap_link(mc, kvm->arch.gmap, f, slot);
kvm_release_faultin_page(kvm, f->page, !!rc, f->write_attempt);
f->page = NULL;
}
diff --git a/arch/s390/kvm/gmap.c b/arch/s390/kvm/gmap.c
index ef0c6ebfdde2..e3c1b070a11d 100644
--- a/arch/s390/kvm/gmap.c
+++ b/arch/s390/kvm/gmap.c
@@ -613,17 +613,37 @@ int gmap_try_fixup_minor(struct gmap *gmap, struct guest_fault *fault)
return rc;
}
-static inline bool gmap_2g_allowed(struct gmap *gmap, gfn_t gfn)
+static inline bool gmap_2g_allowed(struct gmap *gmap, struct guest_fault *f,
+ struct kvm_memory_slot *slot)
{
return false;
}
-static inline bool gmap_1m_allowed(struct gmap *gmap, gfn_t gfn)
+/**
+ * gmap_1m_allowed() - Check whether a 1M hugepage is allowed.
+ * @gmap: The gmap of the guest.
+ * @f: Describes the fault that is being resolved.
+ * @slot: The memslot the faulting address belongs to.
+ *
+ * The function checks whether the GMAP_FLAG_ALLOW_HPAGE_1M flag is set for
+ * @gmap, whether the offset of the address in the 1M virtual frame is the
+ * same as the offset in the physical 1M frame, and finally whether the whole
+ * 1M page would fit in the given memslot.
+ *
+ * Return: true if a 1M hugepage is allowed to back the faulting address, false
+ * otherwise.
+ */
+static inline bool gmap_1m_allowed(struct gmap *gmap, struct guest_fault *f,
+ struct kvm_memory_slot *slot)
{
- return test_bit(GMAP_FLAG_ALLOW_HPAGE_1M, &gmap->flags);
+ return test_bit(GMAP_FLAG_ALLOW_HPAGE_1M, &gmap->flags) &&
+ !((f->gfn ^ f->pfn) & ~_SEGMENT_FR_MASK) &&
+ slot->base_gfn <= ALIGN_DOWN(f->gfn, _PAGES_PER_SEGMENT) &&
+ slot->base_gfn + slot->npages >= ALIGN(f->gfn + 1, _PAGES_PER_SEGMENT);
}
-int gmap_link(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, struct guest_fault *f)
+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 rc, level;
@@ -633,9 +653,9 @@ int gmap_link(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, struct guest_fau
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->gfn))
+ 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->gfn))
+ else if (order >= get_order(_SEGMENT_SIZE) && gmap_1m_allowed(gmap, f, slot))
level = TABLE_TYPE_SEGMENT;
}
rc = dat_link(mc, gmap->asce, level, uses_skeys(gmap), f);
diff --git a/arch/s390/kvm/gmap.h b/arch/s390/kvm/gmap.h
index ccb5cd751e31..a2f74587ddbf 100644
--- a/arch/s390/kvm/gmap.h
+++ b/arch/s390/kvm/gmap.h
@@ -90,7 +90,8 @@ struct gmap *gmap_new(struct kvm *kvm, gfn_t limit);
struct gmap *gmap_new_child(struct gmap *parent, gfn_t limit);
void gmap_remove_child(struct gmap *child);
void gmap_dispose(struct gmap *gmap);
-int gmap_link(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, struct guest_fault *fault);
+int gmap_link(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, struct guest_fault *fault,
+ struct kvm_memory_slot *slot);
void gmap_sync_dirty_log(struct gmap *gmap, gfn_t start, gfn_t end);
int gmap_set_limit(struct gmap *gmap, gfn_t limit);
int gmap_ucas_translate(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, gpa_t *gaddr);
--
2.53.0
next prev parent reply other threads:[~2026-04-13 10:47 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-13 10:46 [GIT PULL 00/12] KVM: s390: Changes for 7.1 Janosch Frank
2026-04-13 10:46 ` [GIT PULL 01/12] KVM: s390: only deliver service interrupt with payload Janosch Frank
2026-04-13 10:46 ` [GIT PULL 02/12] KVM: s390: Fix lpsw/e breaking event handling Janosch Frank
2026-04-13 10:46 ` [GIT PULL 03/12] KVM: s390: vsie: Allow non-zarch guests Janosch Frank
2026-04-13 10:46 ` [GIT PULL 04/12] KVM: s390: vsie: Disable some bits when in ESA mode Janosch Frank
2026-04-13 10:46 ` [GIT PULL 05/12] KVM: s390: vsie: Accommodate ESA prefix pages Janosch Frank
2026-04-13 10:46 ` [GIT PULL 06/12] KVM: s390: Add KVM capability for ESA mode guests Janosch Frank
2026-04-13 10:46 ` [GIT PULL 07/12] KVM: s390: Add some useful mask macros Janosch Frank
2026-04-13 10:46 ` Janosch Frank [this message]
2026-04-13 10:46 ` [GIT PULL 09/12] KVM: s390: Allow 4k granularity for memslots Janosch Frank
2026-04-13 10:46 ` [GIT PULL 10/12] KVM: selftests: Remove 1M alignment requirement for s390 Janosch Frank
2026-04-13 10:46 ` [GIT PULL 11/12] KVM: s390: selftests: enable some common memory-related tests Janosch Frank
2026-04-13 10:46 ` [GIT PULL 12/12] KVM: s390: ucontrol: Fix memslot handling Janosch Frank
2026-04-13 11:23 ` [GIT PULL 00/12] KVM: s390: Changes for 7.1 Paolo Bonzini
2026-04-13 12:28 ` Janosch Frank
2026-04-13 12:33 ` Paolo Bonzini
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=20260413104721.203024-9-frankja@linux.ibm.com \
--to=frankja@linux.ibm.com \
--cc=borntraeger@linux.ibm.com \
--cc=cohuck@redhat.com \
--cc=david@kernel.org \
--cc=imbrenda@linux.ibm.com \
--cc=kvm@vger.kernel.org \
--cc=linux-s390@vger.kernel.org \
--cc=pbonzini@redhat.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