Skip to contentSkip to conversation

KVM: PPC: KVM module exit fixes

PATCH Superseded Fabiano Rosas 2 participants 7 messages Jan 24, 2022 – Jan 25, 2022

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover KVM: PPC: KVM module exit fixes

I stumbled upon another issue with our module exit so I'm sending another version to add a fix for it.

- patches 1 and 3 are already reviewed;

- patch 2 lacks a Reviewed-by. Nick asked about an issue Alexey might have encountered. I haven't heard of any issues with the module exit aside from the ones that this series fixes;

- patch 4 is new. It fixes an issue with module refcounting.

v1:
https://lore.kernel.org/r/[email protected]

Fabiano Rosas (4):
KVM: PPC: Book3S HV: Check return value of kvmppc_radix_init
KVM: PPC: Book3S HV: Delay setting of kvm ops
KVM: PPC: Book3S HV: Free allocated memory if module init fails
KVM: PPC: Decrement module refcount if init_vm fails

 arch/powerpc/kvm/book3s_hv.c | 28 ++++++++++++++++++++--------
 arch/powerpc/kvm/powerpc.c   |  7 ++++++-
 2 files changed, 26 insertions(+), 9 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/4 KVM: PPC: Book3S HV: Check return value of kvmppc_radix_init

The return of the function is being shadowed by the call to kvmppc_uvmem_init.

Fixes ca9f4942670c ("KVM: PPC: Book3S HV: Support for running secure guests")
---
 arch/powerpc/kvm/book3s_hv.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
arch/powerpc/kvm/book3s_hv.c +4 -1
index d1817cd9a691..3a3845f366d4 100644
@@ -6138,8 +6138,11 @@ static int kvmppc_book3s_init_hv(void)
 	if (r)
 		return r;
 
-	if (kvmppc_radix_possible())
+	if (kvmppc_radix_possible()) {
 		r = kvmppc_radix_init();
+		if (r)
+			return r;
+	}
 
 	r = kvmppc_uvmem_init();
 	if (r < 0)
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 2/4 KVM: PPC: Book3S HV: Delay setting of kvm ops

In reply to Fabiano Rosas

Delay the setting of kvm_hv_ops until after all init code has completed. This avoids leaving the ops still accessible if the init fails.

---
 arch/powerpc/kvm/book3s_hv.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
arch/powerpc/kvm/book3s_hv.c +7 -5
index 3a3845f366d4..b9aace212599 100644
@@ -6127,9 +6127,6 @@ static int kvmppc_book3s_init_hv(void)
 	}
 #endif
 
-	kvm_ops_hv.owner = THIS_MODULE;
-	kvmppc_hv_ops = &kvm_ops_hv;
-
 	init_default_hcalls();
 
 	init_vcore_lists();
@@ -6145,10 +6142,15 @@ static int kvmppc_book3s_init_hv(void)
 	}
 
 	r = kvmppc_uvmem_init();
-	if (r < 0)
+	if (r < 0) {
 		pr_err("KVM-HV: kvmppc_uvmem_init failed %d\n", r);
+		return r;
+	}
 
-	return r;
+	kvm_ops_hv.owner = THIS_MODULE;
+	kvmppc_hv_ops = &kvm_ops_hv;
+
+	return 0;
 }
 
 static void kvmppc_book3s_exit_hv(void)
Signature · 1 line
2.34.1
#3 lore raw

PATCH v2 3/4 KVM: PPC: Book3S HV: Free allocated memory if module init fails

In reply to Fabiano Rosas

The module's exit function is not called when the init fails, we need to do cleanup before returning.

---
 arch/powerpc/kvm/book3s_hv.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
arch/powerpc/kvm/book3s_hv.c +11 -4
index b9aace212599..87a49651a402 100644
@@ -6104,7 +6104,7 @@ static int kvmppc_book3s_init_hv(void)
 	if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
 		r = kvm_init_subcore_bitmap();
 		if (r)
-			return r;
+			goto err;
 	}
 
 	/*
@@ -6120,7 +6120,8 @@ static int kvmppc_book3s_init_hv(void)
 		np = of_find_compatible_node(NULL, NULL, "ibm,opal-intc");
 		if (!np) {
 			pr_err("KVM-HV: Cannot determine method for accessing XICS\n");
-			return -ENODEV;
+			r = -ENODEV;
+			goto err;
 		}
 		/* presence of intc confirmed - node can be dropped again */
 		of_node_put(np);
@@ -6133,12 +6134,12 @@ static int kvmppc_book3s_init_hv(void)
 
 	r = kvmppc_mmu_hv_init();
 	if (r)
-		return r;
+		goto err;
 
 	if (kvmppc_radix_possible()) {
 		r = kvmppc_radix_init();
 		if (r)
-			return r;
+			goto err;
 	}
 
 	r = kvmppc_uvmem_init();
@@ -6151,6 +6152,12 @@ static int kvmppc_book3s_init_hv(void)
 	kvmppc_hv_ops = &kvm_ops_hv;
 
 	return 0;
+
+err:
+	kvmhv_nested_exit();
+	kvmppc_radix_exit();
+
+	return r;
 }
 
 static void kvmppc_book3s_exit_hv(void)
Signature · 1 line
2.34.1
#4 lore raw

PATCH v2 4/4 KVM: PPC: Decrement module refcount if init_vm fails

In reply to Fabiano Rosas

We increment the reference count for KVM-HV/PR before the call to kvmppc_core_init_vm. If that function fails we need to decrement the refcount.

---
Caught this while testing Nick's LPID patches by looking at /sys/module/kvm_hv/refcnt

---
 arch/powerpc/kvm/powerpc.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/powerpc/kvm/powerpc.c +6 -1
index 2ad0ccd202d5..4285d0eac900 100644
@@ -431,6 +431,8 @@ int kvm_arch_check_processor_compat(void *opaque)
 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 {
 	struct kvmppc_ops *kvm_ops = NULL;
+	int r;
+
 	/*
 	 * if we have both HV and PR enabled, default is HV
 	 */
@@ -456,7 +458,10 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 		return -ENOENT;
 
 	kvm->arch.kvm_ops = kvm_ops;
-	return kvmppc_core_init_vm(kvm);
+	r = kvmppc_core_init_vm(kvm);
+	if (r)
+		module_put(kvm->arch.kvm_ops->owner);
+	return r;
 err_out:
 	return -EINVAL;
 }
Signature · 1 line
2.34.1
reviewer Reviewed-by on v2 4/4
#5 lore raw

Re: [PATCH v2 4/4] KVM: PPC: Decrement module refcount if init_vm fails

Excerpts from Fabiano Rosas's message of January 25, 2022 8:08 am:

Quoted 8 lines

We increment the reference count for KVM-HV/PR before the call to kvmppc_core_init_vm. If that function fails we need to decrement the refcount.

---
Caught this while testing Nick's LPID patches by looking at /sys/module/kvm_hv/refcnt

Nice catch. Is this the only change in the series?

You can just use kvm_ops->owner like try_module_get() does I think? Also try_module_get works on a NULL module same as module_put by the looks, so you could adjust that in this patch to remove the NULL check so it is consistent with the put.

Thanks,
Nick

Quoted 33 lines
---
 arch/powerpc/kvm/powerpc.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/powerpc/kvm/powerpc.c +6 -1
index 2ad0ccd202d5..4285d0eac900 100644
@@ -431,6 +431,8 @@ int kvm_arch_check_processor_compat(void *opaque)
 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 {
 	struct kvmppc_ops *kvm_ops = NULL;
+	int r;
+
 	/*
 	 * if we have both HV and PR enabled, default is HV
 	 */
@@ -456,7 +458,10 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 		return -ENOENT;
 
 	kvm->arch.kvm_ops = kvm_ops;
-	return kvmppc_core_init_vm(kvm);
+	r = kvmppc_core_init_vm(kvm);
+	if (r)
+		module_put(kvm->arch.kvm_ops->owner);
+	return r;
 err_out:
 	return -EINVAL;
 }
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH v2 4/4] KVM: PPC: Decrement module refcount if init_vm fails

Quoted 11 lines · Nicholas Piggin

Excerpts from Fabiano Rosas's message of January 25, 2022 8:08 am:

Quoted 8 lines

We increment the reference count for KVM-HV/PR before the call to kvmppc_core_init_vm. If that function fails we need to decrement the refcount.

---
Caught this while testing Nick's LPID patches by looking at /sys/module/kvm_hv/refcnt

Nice catch. Is this the only change in the series?

Yes.

Quoted 4 lines

You can just use kvm_ops->owner like try_module_get() does I think? Also try_module_get works on a NULL module same as module_put by the looks, so you could adjust that in this patch to remove the NULL check so it is consistent with the put.

Sure, I'll send a v3.