kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Marcelo Tosatti <mtosatti@redhat.com>
To: Michael Ellerman <michael@ellerman.id.au>
Cc: kvm@vger.kernel.org, avi@redhat.com
Subject: Re: [PATCH] KVM: x86: Avoid NULL dereference in kvm_apic_accept_pic_intr()
Date: Tue, 7 Feb 2012 17:38:39 -0200	[thread overview]
Message-ID: <20120207193839.GA20281@amt.cnet> (raw)
In-Reply-To: <1328596327-18662-1-git-send-email-michael@ellerman.id.au>

On Tue, Feb 07, 2012 at 05:32:07PM +1100, Michael Ellerman wrote:
> A test case which does the following:
> 
>  ioctl(vmfd, KVM_CREATE_VCPU, 0);
>  ioctl(vmfd, KVM_CREATE_IRQCHIP);
>  ioctl(cpufd, KVM_RUN);
> 
> Can oops in kvm_apic_accept_pic_intr() because vcpu->arch.apic == NULL.
> 
> Because irqchip_in_kernel() is false when we create the vcpu we leave
> vcpu->arch.apic uninitialised (in kvm_arch_vcpu_init()). Then when we run,
> irqchip_in_kernel() is true, but we didn't do the correct initialisation.
> 
> The root of the problem seems to be that there is an assumption that
> KVM_CREATE_IRQCHIP will be called before any VCPUs are created. The
> documentation says "sets up future vcpus to have a local APIC".
> 
> So the simplest fix seems to be to enforce that ordering in the code.

Ugh. With your patch below there is still the window for a race:

kvm_arch_vcpu_init can create a vcpu without vcpu->arch.apic, 
block on mutex_lock(kvm->lock). Meanwhile a separate thread is on
KVM_CREATE_IRQCHIP holding kvm->lock, finds online_vcpus == 0 and 
proceeds. Then kvm_arch_vcpu_init finishes.

Moving mutex_lock(kvm->lock) to the beginning of
kvm_vm_ioctl_create_vcpu should fix it?

> 
> Signed-off-by: Michael Ellerman <michael@ellerman.id.au>
> ---
>  arch/x86/kvm/x86.c |    3 +++
>  1 files changed, 3 insertions(+), 0 deletions(-)
> 
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 14d6cad..27dd380 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -3110,6 +3110,9 @@ long kvm_arch_vm_ioctl(struct file *filp,
>  		r = -EEXIST;
>  		if (kvm->arch.vpic)
>  			goto create_irqchip_unlock;
> +		r = -EINVAL;
> +		if (atomic_read(&kvm->online_vcpus))
> +			goto create_irqchip_unlock;
>  		r = -ENOMEM;
>  		vpic = kvm_create_pic(kvm);
>  		if (vpic) {
> -- 
> 1.7.5.4
> 
> --
> To unsubscribe from this list: send the line "unsubscribe kvm" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

  reply	other threads:[~2012-02-07 19:40 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-07  6:32 [PATCH] KVM: x86: Avoid NULL dereference in kvm_apic_accept_pic_intr() Michael Ellerman
2012-02-07 19:38 ` Marcelo Tosatti [this message]
2012-02-08 10:41   ` Michael Ellerman
2012-02-08 12:13     ` Michael Ellerman
2012-03-04  9:51       ` Avi Kivity
2012-03-04 10:14         ` Avi Kivity

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120207193839.GA20281@amt.cnet \
    --to=mtosatti@redhat.com \
    --cc=avi@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=michael@ellerman.id.au \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).