* [PATCH] KVM: arm64: Fix nested S2 MMU structures reallocation
@ 2025-02-04 14:55 Marc Zyngier
2025-02-04 14:58 ` Alexander Potapenko
2025-02-04 15:03 ` Marc Zyngier
0 siblings, 2 replies; 3+ messages in thread
From: Marc Zyngier @ 2025-02-04 14:55 UTC (permalink / raw)
To: kvmarm, linux-arm-kernel
Cc: Joey Gouly, Suzuki K Poulose, Oliver Upton, Zenghui Yu,
Alexander Potapenko
For each vcpu that userspace creates, we allocate a number of
s2_mmu structures that will eventually contain our shadow S2
page tables.
Since this is a dynamically allocated array, we reallocate
the array and initialise the newly allocated elements. Once
everything is correctly initialised, we adjust pointer and size
in the kvm structure, and move on.
But should that initialisation fail *and* the reallocation triggered
a copy to another location, we end-up returning early, with the
kvm structure still containing the (now stale) old pointer. Weeee!
Cure it by assigning the pointer early, and use this to perform
the initialisation. If everything succeeds, we adjust the size.
Otherwise, we just leave the size as it was, no harm done, and the
new memory is as good as the ol' one (we hope...).
Fixes: 4f128f8e1aaac ("KVM: arm64: nv: Support multiple nested Stage-2 mmu structures")
Reported-by: Alexander Potapenko <glider@google.com>
Signed-off-by: Marc Zyngier <maz@kernel.org>
---
arch/arm64/kvm/nested.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c
index d3214c3a40393..6969c273b39e1 100644
--- a/arch/arm64/kvm/nested.c
+++ b/arch/arm64/kvm/nested.c
@@ -93,20 +93,22 @@ int kvm_vcpu_init_nested(struct kvm_vcpu *vcpu)
if (!tmp)
return -ENOMEM;
+ swap(kvm->arch.nested_mmus, tmp);
+
/*
* If we went through a realocation, adjust the MMU back-pointers in
* the previously initialised kvm_pgtable structures.
*/
if (kvm->arch.nested_mmus != tmp)
for (int i = 0; i < kvm->arch.nested_mmus_size; i++)
- tmp[i].pgt->mmu = &tmp[i];
+ kvm->arch.nested_mmus[i].pgt->mmu = &kvm->arch.nested_mmus[i];
for (int i = kvm->arch.nested_mmus_size; !ret && i < num_mmus; i++)
- ret = init_nested_s2_mmu(kvm, &tmp[i]);
+ ret = init_nested_s2_mmu(kvm, &kvm->arch.nested_mmus[i]);
if (ret) {
for (int i = kvm->arch.nested_mmus_size; i < num_mmus; i++)
- kvm_free_stage2_pgd(&tmp[i]);
+ kvm_free_stage2_pgd(&kvm->arch.nested_mmus[i]);
free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
vcpu->arch.ctxt.vncr_array = NULL;
@@ -115,7 +117,6 @@ int kvm_vcpu_init_nested(struct kvm_vcpu *vcpu)
}
kvm->arch.nested_mmus_size = num_mmus;
- kvm->arch.nested_mmus = tmp;
return 0;
}
--
2.39.2
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] KVM: arm64: Fix nested S2 MMU structures reallocation
2025-02-04 14:55 [PATCH] KVM: arm64: Fix nested S2 MMU structures reallocation Marc Zyngier
@ 2025-02-04 14:58 ` Alexander Potapenko
2025-02-04 15:03 ` Marc Zyngier
1 sibling, 0 replies; 3+ messages in thread
From: Alexander Potapenko @ 2025-02-04 14:58 UTC (permalink / raw)
To: Marc Zyngier
Cc: kvmarm, linux-arm-kernel, Joey Gouly, Suzuki K Poulose,
Oliver Upton, Zenghui Yu
On Tue, Feb 4, 2025 at 3:56 PM Marc Zyngier <maz@kernel.org> wrote:
>
> For each vcpu that userspace creates, we allocate a number of
> s2_mmu structures that will eventually contain our shadow S2
> page tables.
>
> Since this is a dynamically allocated array, we reallocate
> the array and initialise the newly allocated elements. Once
> everything is correctly initialised, we adjust pointer and size
> in the kvm structure, and move on.
>
> But should that initialisation fail *and* the reallocation triggered
> a copy to another location, we end-up returning early, with the
> kvm structure still containing the (now stale) old pointer. Weeee!
>
> Cure it by assigning the pointer early, and use this to perform
> the initialisation. If everything succeeds, we adjust the size.
> Otherwise, we just leave the size as it was, no harm done, and the
> new memory is as good as the ol' one (we hope...).
>
> Fixes: 4f128f8e1aaac ("KVM: arm64: nv: Support multiple nested Stage-2 mmu structures")
> Reported-by: Alexander Potapenko <glider@google.com>
> Signed-off-by: Marc Zyngier <maz@kernel.org>
Tested-by: Alexander Potapenko <glider@google.com>
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] KVM: arm64: Fix nested S2 MMU structures reallocation
2025-02-04 14:55 [PATCH] KVM: arm64: Fix nested S2 MMU structures reallocation Marc Zyngier
2025-02-04 14:58 ` Alexander Potapenko
@ 2025-02-04 15:03 ` Marc Zyngier
1 sibling, 0 replies; 3+ messages in thread
From: Marc Zyngier @ 2025-02-04 15:03 UTC (permalink / raw)
To: kvmarm, linux-arm-kernel, Marc Zyngier
Cc: Joey Gouly, Suzuki K Poulose, Oliver Upton, Zenghui Yu,
Alexander Potapenko
On Tue, 04 Feb 2025 14:55:54 +0000, Marc Zyngier wrote:
> For each vcpu that userspace creates, we allocate a number of
> s2_mmu structures that will eventually contain our shadow S2
> page tables.
>
> Since this is a dynamically allocated array, we reallocate
> the array and initialise the newly allocated elements. Once
> everything is correctly initialised, we adjust pointer and size
> in the kvm structure, and move on.
>
> [...]
Applied to fixes, thanks!
[1/1] KVM: arm64: Fix nested S2 MMU structures reallocation
commit: 5417a2e9b130a78bf48cb4cf92630efcee5ccf38
Cheers,
M.
--
Without deviation from the norm, progress is not possible.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-02-04 15:06 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-04 14:55 [PATCH] KVM: arm64: Fix nested S2 MMU structures reallocation Marc Zyngier
2025-02-04 14:58 ` Alexander Potapenko
2025-02-04 15:03 ` Marc Zyngier
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox