Skip to contentSkip to conversation

KVM: s390: Add support for 2G hugepages

PATCH In review Claudio Imbrenda 3 participants 7 messages Jun 9 – Jun 10

Subsystems (4)
lore
Share & tools
lore

Claudio Imbrenda

author maintainer
#0 lore raw

v2 cover KVM: s390: Add support for 2G hugepages

Add support for 2G hugepages.

In theory, since kernel 7.0, userspace could already use 2G hugepages to back guest memory, but KVM would use 4k or 1M pages in gmap to actually map the guest.

This series allows KVM on s390 to actually use 2G hugepages in gmap to map guest memory that is backed by 2G hugepages in userspace.

Nested guests remain possible.

v1->v2:
* Fix possible race between clearing the GMAP_FLAG_ALLOW_HPAGE_2G flag and gmap_split_huge_pages().
* Add documentation for the KVM_CAP_S390_HPAGE_2G capability.

Claudio Imbrenda (4):
KVM: s390: Add module parameter to fence 2G hugepages
KVM: s390: Add capability to support 2G hugepages
KVM: s390: Allow for 2G hugepages
KVM: s390: Document the KVM_CAP_S390_HPAGE_2G capability

 Documentation/virt/kvm/api.rst | 15 ++++++++++++++
 arch/s390/kvm/gmap.c           | 24 ++++++++++++++++++++-
 arch/s390/kvm/kvm-s390.c       | 38 +++++++++++++++++++++++++++++++++-
 arch/s390/kvm/pv.c             |  5 ++++-
 include/uapi/linux/kvm.h       |  1 +
 5 files changed, 80 insertions(+), 3 deletions(-)
Signature · 1 line
2.54.0
author maintainer
#1 lore raw

PATCH v2 1/4 KVM: s390: Add module parameter to fence 2G hugepages

Add the hpage_2g module parameter to KVM to allow enabling or disabling 2G hugepages in KVM.

If hpage_2g is enabled but hpage is not enabled, print a message and disable hpage_2g.

Opportunistically fix the comment for the hpage module parameter.

---
 arch/s390/kvm/kvm-s390.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
arch/s390/kvm/kvm-s390.c +11 -1
index ffb20a64d328..801a622691b6 100644
@@ -204,11 +204,16 @@ static int nested;
 module_param(nested, int, S_IRUGO);
 MODULE_PARM_DESC(nested, "Nested virtualization support");
 
-/* allow 1m huge page guest backing, if !nested */
+/* allow 1m huge page guest backing */
 static int hpage;
 module_param(hpage, int, 0444);
 MODULE_PARM_DESC(hpage, "1m huge page backing support");
 
+/* allow 2g huge page guest backing */
+static int hpage_2g;
+module_param(hpage_2g, int, 0444);
+MODULE_PARM_DESC(hpage_2g, "2g huge page backing support");
+
 /* maximum percentage of steal time for polling.  >100 is treated like 100 */
 static u8 halt_poll_max_steal = 10;
 module_param(halt_poll_max_steal, byte, 0644);
@@ -5820,6 +5825,11 @@ static int __init kvm_s390_init(void)
 		return -ENODEV;
 	}
 
+	if (hpage_2g && !hpage) {
+		hpage_2g = 0;
+		pr_info("Disabling 2G hugepage support, since 1M hugepage support is not enabled.\n");
+	}
+
 	for (i = 0; i < 16; i++)
 		kvm_s390_fac_base[i] |=
 			stfle_fac_list[i] & nonhyp_mask(i);
Signature · 1 line
2.54.0
maintainer
#2 lore raw

Re: [PATCH v2 1/4] KVM: s390: Add module parameter to fence 2G hugepages

Quoted 8 lines · Claudio Imbrenda

Add the hpage_2g module parameter to KVM to allow enabling or disabling 2G hugepages in KVM.

If hpage_2g is enabled but hpage is not enabled, print a message and disable hpage_2g.

Opportunistically fix the comment for the hpage module parameter.

Instead of adding yet another parameter could we make it an int and say that hpage=2 equals edat1 and edat2?

author maintainer
#3 lore raw

PATCH v2 2/4 KVM: s390: Add capability to support 2G hugepages

In reply to Claudio Imbrenda

Add KVM_CAP_S390_HPAGE_2G to signal to userspace that 2G hugepages may be used to back the guest; restrictions apply similar to 1M hugepages.

Enable the (for now still ignored) GMAP_FLAG_ALLOW_HPAGE_2G flag for the guest gmap, and propagate / disable it as necessary.

---
 arch/s390/kvm/gmap.c     |  5 +++++
 arch/s390/kvm/kvm-s390.c | 26 ++++++++++++++++++++++++++
 arch/s390/kvm/pv.c       |  5 ++++-
 include/uapi/linux/kvm.h |  1 +
 4 files changed, 36 insertions(+), 1 deletion(-)
arch/s390/kvm/gmap.c +5 -0
index 52d55ddea8d4..c9e348149ba1 100644
@@ -105,6 +105,11 @@ static void gmap_add_child(struct gmap *parent, struct gmap *child)
 	else
 		clear_bit(GMAP_FLAG_ALLOW_HPAGE_1M, &child->flags);
 
+	if (test_bit(GMAP_FLAG_ALLOW_HPAGE_2G, &parent->flags))
+		set_bit(GMAP_FLAG_ALLOW_HPAGE_2G, &child->flags);
+	else
+		clear_bit(GMAP_FLAG_ALLOW_HPAGE_2G, &child->flags);
+
 	if (kvm_is_ucontrol(parent->kvm))
 		clear_bit(GMAP_FLAG_OWNS_PAGETABLES, &child->flags);
 	list_add(&child->list, &parent->children);
arch/s390/kvm/kvm-s390.c +26 -0
index 801a622691b6..616d1db7c0d9 100644
@@ -645,6 +645,11 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 		if (hpage && !(kvm && kvm_is_ucontrol(kvm)))
 			r = 1;
 		break;
+	case KVM_CAP_S390_HPAGE_2G:
+		r = 0;
+		if (hpage_2g && !(kvm && kvm_is_ucontrol(kvm)))
+			r = 1;
+		break;
 	case KVM_CAP_S390_MEM_OP:
 		r = MEM_OP_MAX_SIZE;
 		break;
@@ -901,6 +906,27 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE %s",
 			 r ? "(not available)" : "(success)");
 		break;
+	case KVM_CAP_S390_HPAGE_2G:
+		mutex_lock(&kvm->lock);
+		if (kvm->created_vcpus) {
+			r = -EBUSY;
+		} else if (!hpage_2g || kvm->arch.use_cmma || kvm_is_ucontrol(kvm)) {
+			r = -EINVAL;
+		} else {
+			r = 0;
+			set_bit(GMAP_FLAG_ALLOW_HPAGE_2G, &kvm->arch.gmap->flags);
+			/*
+			 * We might have to create fake 4k page
+			 * tables. To avoid that the hardware works on
+			 * stale PGSTEs, we emulate these instructions.
+			 */
+			kvm->arch.use_skf = 0;
+			kvm->arch.use_pfmfi = 0;
+		}
+		mutex_unlock(&kvm->lock);
+		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE_2G %s",
+			 r ? "(not available)" : "(success)");
+		break;
 	case KVM_CAP_S390_USER_STSI:
 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
 		kvm->arch.user_stsi = 1;
arch/s390/kvm/pv.c +4 -1
index 4b865e75351c..1beacc841ca8 100644
@@ -740,7 +740,10 @@ int kvm_s390_pv_init_vm(struct kvm *kvm, u16 *rc, u16 *rrc)
 	uvcb.flags.ap_allow_instr = kvm->arch.model.uv_feat_guest.ap;
 	uvcb.flags.ap_instr_intr = kvm->arch.model.uv_feat_guest.ap_intr;
 
-	clear_bit(GMAP_FLAG_ALLOW_HPAGE_1M, &kvm->arch.gmap->flags);
+	scoped_guard(write_lock, &kvm->mmu_lock) {
+		clear_bit(GMAP_FLAG_ALLOW_HPAGE_1M, &kvm->arch.gmap->flags);
+		clear_bit(GMAP_FLAG_ALLOW_HPAGE_2G, &kvm->arch.gmap->flags);
+	}
 	gmap_split_huge_pages(kvm->arch.gmap);
 
 	cc = uv_call_sched(0, (u64)&uvcb);
include/uapi/linux/kvm.h +1 -0
index 6c8afa2047bf..419011097fa8 100644
@@ -996,6 +996,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_S390_USER_OPEREXEC 246
 #define KVM_CAP_S390_KEYOP 247
 #define KVM_CAP_S390_VSIE_ESAMODE 248
+#define KVM_CAP_S390_HPAGE_2G 249
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
Signature · 1 line
2.54.0
#4 lore raw

Re: [PATCH v2 2/4] KVM: s390: Add capability to support 2G hugepages

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Userspace can bypass the mutual exclusion between CMMA and huge pages (1M/2G) by enabling huge pages first, and then enabling CMMA. --

commit 18ee7269cd4f70997be26c3321bdbc7d2cc63a95
Author: Claudio Imbrenda <[email protected]>

KVM: s390: Add capability to support 2G hugepages

This commit adds KVM_CAP_S390_HPAGE_2G to signal to userspace that 2G hugepages may be used to back the guest. It implements the underlying GMAP_FLAG_ALLOW_HPAGE_2G flag and propagates it through the guest gmap.

Quoted 4 lines
arch/s390/kvm/kvm-s390.c +0 -0
index 801a622691b6a..616d1db7c0d9e 100644

[ ... ]

arch/s390/kvm/kvm-s390.c int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) · line 913

Quoted 9 lines
arch/s390/kvm/kvm-s390.c +5 -0
@@ -901,6 +906,27 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE %s",
 			 r ? "(not available)" : "(success)");
 		break;
+	case KVM_CAP_S390_HPAGE_2G:
+		mutex_lock(&kvm->lock);
+		if (kvm->created_vcpus) {
+			r = -EBUSY;
+		} else if (!hpage_2g || kvm->arch.use_cmma || kvm_is_ucontrol(kvm)) {

[Severity: High]
This is a pre-existing issue, but does KVM_S390_VM_MEM_ENABLE_CMMA allow userspace to bypass this mutual exclusion?

While this handler correctly rejects the capability if kvm->arch.use_cmma is already set, the inverse path in kvm_s390_set_mem_control() for KVM_S390_VM_MEM_ENABLE_CMMA unconditionally sets kvm->arch.use_cmma = 1 without verifying if huge pages have been allowed:

kvm_s390_set_mem_control() {
...

        case KVM_S390_VM_MEM_ENABLE_CMMA:
...
                kvm->arch.use_cmma = 1;

...
}

Since both ioctls only require that no vCPUs have been created, could userspace bypass this safety restriction by enabling huge pages first, and then enabling CMMA?

If so, this might allow the VM to run in an invalid state where the hardware is instructed to use CMMA on huge pages, potentially leading to issues when it attempts to access non-existent 4K Page Status Table Entries.

Quoted 13 lines
arch/s390/kvm/kvm-s390.c +13 -0
+			r = -EINVAL;
+		} else {
+			r = 0;
+			set_bit(GMAP_FLAG_ALLOW_HPAGE_2G, &kvm->arch.gmap->flags);
+			/*
+			 * We might have to create fake 4k page
+			 * tables. To avoid that the hardware works on
+			 * stale PGSTEs, we emulate these instructions.
+			 */
+			kvm->arch.use_skf = 0;
+			kvm->arch.use_pfmfi = 0;
+		}
+		mutex_unlock(&kvm->lock);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author maintainer
#5 lore raw

PATCH v2 3/4 KVM: s390: Allow for 2G hugepages

In reply to Claudio Imbrenda

Change gmap_2g_allowed() to perform the necessary checks to allow for 2G hugepages to be used, instead of returning false. The GMAP_FLAG_ALLOW_HPAGE_2G gmap flag is now taken into account.

Also add appropriate kerneldoc comments.

---
 arch/s390/kvm/gmap.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
arch/s390/kvm/gmap.c +18 -1
index c9e348149ba1..c5a8132eaa22 100644
@@ -635,10 +635,27 @@ int gmap_try_fixup_minor(struct gmap *gmap, struct guest_fault *fault)
 	return rc;
 }
 
+/**
+ * gmap_2g_allowed() - Check whether a 2G 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_2G flag is set for
+ * @gmap, whether the offset of the address in the 2G virtual frame is the
+ * same as the offset in the physical 2G frame, and finally whether the whole
+ * 2G page would fit in the given memslot.
+ *
+ * Return: true if a 2G hugepage is allowed to back the faulting address, false
+ *         otherwise.
+ */
 static inline bool gmap_2g_allowed(struct gmap *gmap, struct guest_fault *f,
 				   struct kvm_memory_slot *slot)
 {
-	return false;
+	return test_bit(GMAP_FLAG_ALLOW_HPAGE_2G, &gmap->flags) &&
+	       !((f->gfn ^ f->pfn) & ~_REGION3_FR_MASK) &&
+	       slot->base_gfn <= ALIGN_DOWN(f->gfn, _PAGES_PER_REGION3) &&
+	       slot->base_gfn + slot->npages >= ALIGN(f->gfn + 1, _PAGES_PER_REGION3);
 }
 
 /**
Signature · 1 line
2.54.0
author maintainer
#6 lore raw

PATCH v2 4/4 KVM: s390: Document the KVM_CAP_S390_HPAGE_2G capability

In reply to Claudio Imbrenda

Document the KVM_CAP_S390_HPAGE_2G capability, which behaves very similarly to the existing KVM_CAP_S390_HPAGE_1M.

---
 Documentation/virt/kvm/api.rst | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
Documentation/virt/kvm/api.rst +15 -0
index 52bbbb553ce1..3fdd0729f6ee 100644
@@ -8904,6 +8904,21 @@ helpful if user space wants to emulate instructions which are not
 This capability can be enabled dynamically even if VCPUs were already
 created and are running.
 
+7.47 KVM_CAP_S390_HPAGE_2G
+--------------------------
+
+:Architectures: s390
+:Parameters: none
+:Returns: 0 on success; -EINVAL if hpage_2g module parameter was not set,
+          cmma is enabled, or the VM has the KVM_VM_S390_UCONTROL
+          flag set; -EBUSY if vCPUs were already created for the VM.
+
+With this capability the KVM support for memory backing with 2g pages
+through hugetlbfs can be enabled for a VM. After the capability is
+enabled, cmma can't be enabled anymore and pfmfi and the storage key
+interpretation are disabled. If cmma has already been enabled or the
+hpage_2g module parameter is not set to 1, -EINVAL is returned.
+
 8. Other capabilities.
 ======================
 
Signature · 1 line
2.54.0