All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] KVM: x86: Fix mce_banks memory leak on mci_ctl2_banks allocation failure
@ 2022-08-19 18:22 Vipin Sharma
  2022-08-19 21:54 ` Jim Mattson
  2022-08-30 21:45 ` Sean Christopherson
  0 siblings, 2 replies; 4+ messages in thread
From: Vipin Sharma @ 2022-08-19 18:22 UTC (permalink / raw)
  To: seanjc, pbonzini, jmattson; +Cc: kvm, linux-kernel, Vipin Sharma

If mci_ctl2_banks allocation fails, kvm goes to fail_free_pio_data and
forgets about freeing mce_banks memory causing memory leak.

Individually check memory allocation status and free memory in the correct
order.

Fixes: 281b52780b57 ("KVM: x86: Add emulation for MSR_IA32_MCx_CTL2 MSRs.")
Signed-off-by: Vipin Sharma <vipinsh@google.com>
---
 arch/x86/kvm/x86.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index d7374d768296..4b2c7a4f175f 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -11560,15 +11560,19 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
 
 	vcpu->arch.mce_banks = kcalloc(KVM_MAX_MCE_BANKS * 4, sizeof(u64),
 				       GFP_KERNEL_ACCOUNT);
+	if (!vcpu->arch.mce_banks)
+		goto fail_free_pio_data;
+
 	vcpu->arch.mci_ctl2_banks = kcalloc(KVM_MAX_MCE_BANKS, sizeof(u64),
 					    GFP_KERNEL_ACCOUNT);
-	if (!vcpu->arch.mce_banks || !vcpu->arch.mci_ctl2_banks)
-		goto fail_free_pio_data;
+	if (!vcpu->arch.mci_ctl2_banks)
+		goto fail_free_mce_banks;
+
 	vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
 
 	if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask,
 				GFP_KERNEL_ACCOUNT))
-		goto fail_free_mce_banks;
+		goto fail_free_mci_ctl2_banks;
 
 	if (!alloc_emulate_ctxt(vcpu))
 		goto free_wbinvd_dirty_mask;
@@ -11614,9 +11618,10 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
 	kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt);
 free_wbinvd_dirty_mask:
 	free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
+fail_free_mci_ctl2_banks:
+	kfree(vcpu->arch.mci_ctl2_banks);
 fail_free_mce_banks:
 	kfree(vcpu->arch.mce_banks);
-	kfree(vcpu->arch.mci_ctl2_banks);
 fail_free_pio_data:
 	free_page((unsigned long)vcpu->arch.pio_data);
 fail_free_lapic:
-- 
2.37.1.595.g718a3a8f04-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] KVM: x86: Fix mce_banks memory leak on mci_ctl2_banks allocation failure
  2022-08-19 18:22 [PATCH] KVM: x86: Fix mce_banks memory leak on mci_ctl2_banks allocation failure Vipin Sharma
@ 2022-08-19 21:54 ` Jim Mattson
  2022-08-30 21:45 ` Sean Christopherson
  1 sibling, 0 replies; 4+ messages in thread
From: Jim Mattson @ 2022-08-19 21:54 UTC (permalink / raw)
  To: Vipin Sharma; +Cc: seanjc, pbonzini, kvm, linux-kernel

On Fri, Aug 19, 2022 at 11:23 AM Vipin Sharma <vipinsh@google.com> wrote:
>
> If mci_ctl2_banks allocation fails, kvm goes to fail_free_pio_data and
> forgets about freeing mce_banks memory causing memory leak.
>
> Individually check memory allocation status and free memory in the correct
> order.
>
> Fixes: 281b52780b57 ("KVM: x86: Add emulation for MSR_IA32_MCx_CTL2 MSRs.")
> Signed-off-by: Vipin Sharma <vipinsh@google.com>
> ---
Reviewed-by: Jim Mattson <jmattson@google.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] KVM: x86: Fix mce_banks memory leak on mci_ctl2_banks allocation failure
  2022-08-19 18:22 [PATCH] KVM: x86: Fix mce_banks memory leak on mci_ctl2_banks allocation failure Vipin Sharma
  2022-08-19 21:54 ` Jim Mattson
@ 2022-08-30 21:45 ` Sean Christopherson
  2022-09-08 15:20   ` Sean Christopherson
  1 sibling, 1 reply; 4+ messages in thread
From: Sean Christopherson @ 2022-08-30 21:45 UTC (permalink / raw)
  To: Vipin Sharma; +Cc: pbonzini, jmattson, kvm, linux-kernel

On Fri, Aug 19, 2022, Vipin Sharma wrote:
> If mci_ctl2_banks allocation fails, kvm goes to fail_free_pio_data and
> forgets about freeing mce_banks memory causing memory leak.
> 
> Individually check memory allocation status and free memory in the correct
> order.
> 
> Fixes: 281b52780b57 ("KVM: x86: Add emulation for MSR_IA32_MCx_CTL2 MSRs.")
> Signed-off-by: Vipin Sharma <vipinsh@google.com>
> ---

Pushed to branch `for_paolo/6.1` at:

    https://github.com/sean-jc/linux.git

Unless you hear otherwise, it will make its way to kvm/queue "soon".

Note, the commit IDs are not guaranteed to be stable.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] KVM: x86: Fix mce_banks memory leak on mci_ctl2_banks allocation failure
  2022-08-30 21:45 ` Sean Christopherson
@ 2022-09-08 15:20   ` Sean Christopherson
  0 siblings, 0 replies; 4+ messages in thread
From: Sean Christopherson @ 2022-09-08 15:20 UTC (permalink / raw)
  To: Vipin Sharma; +Cc: pbonzini, jmattson, kvm, linux-kernel

On Tue, Aug 30, 2022, Sean Christopherson wrote:
> On Fri, Aug 19, 2022, Vipin Sharma wrote:
> > If mci_ctl2_banks allocation fails, kvm goes to fail_free_pio_data and
> > forgets about freeing mce_banks memory causing memory leak.
> > 
> > Individually check memory allocation status and free memory in the correct
> > order.
> > 
> > Fixes: 281b52780b57 ("KVM: x86: Add emulation for MSR_IA32_MCx_CTL2 MSRs.")
> > Signed-off-by: Vipin Sharma <vipinsh@google.com>
> > ---
> 
> Pushed to branch `for_paolo/6.1` at:
> 
>     https://github.com/sean-jc/linux.git
> 
> Unless you hear otherwise, it will make its way to kvm/queue "soon".

Doh.  Dropping this as Paolo already sent a different fix[*] to Linus, commit
3c0ba05ce9c9 ("KVM: x86: fix memoryleak in kvm_arch_vcpu_create()").

Sorry :-(

[*] https://lore.kernel.org/all/20220901122300.22298-1-linmiaohe@huawei.com

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-09-08 15:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-19 18:22 [PATCH] KVM: x86: Fix mce_banks memory leak on mci_ctl2_banks allocation failure Vipin Sharma
2022-08-19 21:54 ` Jim Mattson
2022-08-30 21:45 ` Sean Christopherson
2022-09-08 15:20   ` Sean Christopherson

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.