Skip to content

Commit 918450a

Browse files
V4belgregkh
authored andcommitted
KVM: arm64: Reassign nested_mmus array behind mmu_lock
commit 7054335 upstream. kvm->arch.nested_mmus[] is walked under kvm->mmu_lock, including from the MMU notifier path (kvm_unmap_gfn_range() -> kvm_nested_s2_unmap()), which can run at any time. kvm_vcpu_init_nested() reallocates the array and frees the old buffer while holding only kvm->arch.config_lock, so such a walker can reference the freed array. Allocate the new array outside of mmu_lock, as the allocation can sleep. Under the lock, copy the existing entries, fix up the back pointers and reassign the array. Free the old buffer after dropping the lock, as kvfree() can sleep as well. Fixes: 4f128f8 ("KVM: arm64: nv: Support multiple nested Stage-2 mmu structures") Signed-off-by: Hyunwoo Kim <imv4bel@gmail.com> Reviewed-by: Oliver Upton <oupton@kernel.org> Link: https://patch.msgid.link/aiKIVVeIr1aAB1yp@v4bel Signed-off-by: Marc Zyngier <maz@kernel.org> Cc: stable@vger,kernel.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 2bbc395 commit 918450a

1 file changed

Lines changed: 20 additions & 13 deletions

File tree

arch/arm64/kvm/nested.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,28 @@ int kvm_vcpu_init_nested(struct kvm_vcpu *vcpu)
8989
* again, and there is no reason to affect the whole VM for this.
9090
*/
9191
num_mmus = atomic_read(&kvm->online_vcpus) * S2_MMU_PER_VCPU;
92-
tmp = kvrealloc(kvm->arch.nested_mmus,
93-
size_mul(sizeof(*kvm->arch.nested_mmus), num_mmus),
94-
GFP_KERNEL_ACCOUNT | __GFP_ZERO);
95-
if (!tmp)
96-
return -ENOMEM;
9792

98-
swap(kvm->arch.nested_mmus, tmp);
93+
if (num_mmus > kvm->arch.nested_mmus_size) {
94+
tmp = kvcalloc(num_mmus, sizeof(*tmp), GFP_KERNEL_ACCOUNT);
95+
if (!tmp)
96+
return -ENOMEM;
9997

100-
/*
101-
* If we went through a realocation, adjust the MMU back-pointers in
102-
* the previously initialised kvm_pgtable structures.
103-
*/
104-
if (kvm->arch.nested_mmus != tmp)
105-
for (int i = 0; i < kvm->arch.nested_mmus_size; i++)
106-
kvm->arch.nested_mmus[i].pgt->mmu = &kvm->arch.nested_mmus[i];
98+
write_lock(&kvm->mmu_lock);
99+
100+
if (kvm->arch.nested_mmus_size) {
101+
memcpy(tmp, kvm->arch.nested_mmus,
102+
size_mul(sizeof(*tmp), kvm->arch.nested_mmus_size));
103+
104+
for (int i = 0; i < kvm->arch.nested_mmus_size; i++)
105+
tmp[i].pgt->mmu = &tmp[i];
106+
}
107+
108+
swap(kvm->arch.nested_mmus, tmp);
109+
110+
write_unlock(&kvm->mmu_lock);
111+
112+
kvfree(tmp);
113+
}
107114

108115
for (int i = kvm->arch.nested_mmus_size; !ret && i < num_mmus; i++)
109116
ret = init_nested_s2_mmu(kvm, &kvm->arch.nested_mmus[i]);

0 commit comments

Comments
 (0)