Re: [PATCH MANUALSEL 5.14 1/5] KVM: X86: fix lazy allocation of rmaps

From: Paolo Bonzini
Date: Tue Oct 26 2021 - 12:14:42 EST


On 25/10/21 22:38, Sasha Levin wrote:
From: Paolo Bonzini <pbonzini@xxxxxxxxxx>

[ Upstream commit fa13843d1565d4c5b3aeb9be3343b313416bef46 ]

If allocation of rmaps fails, but some of the pointers have already been written,
those pointers can be cleaned up when the memslot is freed, or even reused later
for another attempt at allocating the rmaps. Therefore there is no need to
WARN, as done for example in memslot_rmap_alloc, but the allocation *must* be
skipped lest KVM will overwrite the previous pointer and will indeed leak memory.

Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx>
---
arch/x86/kvm/x86.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 4b0e866e9f08..60d9aa0ab389 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -11341,7 +11341,8 @@ static int memslot_rmap_alloc(struct kvm_memory_slot *slot,
int lpages = gfn_to_index(slot->base_gfn + npages - 1,
slot->base_gfn, level) + 1;
- WARN_ON(slot->arch.rmap[i]);
+ if (slot->arch.rmap[i])
+ continue;
slot->arch.rmap[i] = kvcalloc(lpages, sz, GFP_KERNEL_ACCOUNT);
if (!slot->arch.rmap[i]) {


NACK

There is no lazy allocation of rmaps in 5.14, and any failure to allocate goes straight to memslot_rmap_free followed by return -ENOMEM. So the WARN_ON is justified there.

Paolo