public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Marcelo Tosatti <mtosatti@redhat.com>
To: "Michael S. Tsirkin" <mst@redhat.com>
Cc: avi@redhat.com, kvm@vger.kernel.org, gleb@redhat.com
Subject: Re: [patch 1/3] KVM: x86: disallow multiple KVM_CREATE_IRQCHIP
Date: Thu, 29 Oct 2009 12:10:30 -0200	[thread overview]
Message-ID: <20091029141030.GA3158@amt.cnet> (raw)
In-Reply-To: <20091029093246.GA4140@redhat.com>

On Thu, Oct 29, 2009 at 11:32:48AM +0200, Michael S. Tsirkin wrote:
> On Wed, Oct 28, 2009 at 06:42:38PM -0200, Marcelo Tosatti wrote:
> > Otherwise kvm will leak memory on multiple KVM_CREATE_IRQCHIP. 
> > Also serialize multiple accesses with kvm->lock.
> > 
> > Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
> > 
> > Index: kvm/arch/x86/kvm/x86.c
> > ===================================================================
> > --- kvm.orig/arch/x86/kvm/x86.c
> > +++ kvm/arch/x86/kvm/x86.c
> > @@ -2362,25 +2362,38 @@ long kvm_arch_vm_ioctl(struct file *filp
> >  		if (r)
> >  			goto out;
> >  		break;
> > -	case KVM_CREATE_IRQCHIP:
> > +	case KVM_CREATE_IRQCHIP: {
> > +		struct kvm_pic *vpic;
> > +
> > +		mutex_lock(&kvm->lock);
> > +		r = -EEXIST;
> > +		if (kvm->arch.vpic)
> > +			goto create_irqchip_unlock;
> >  		r = -ENOMEM;
> > -		kvm->arch.vpic = kvm_create_pic(kvm);
> > -		if (kvm->arch.vpic) {
> > +		vpic = kvm_create_pic(kvm);
> > +		if (vpic) {
> >  			r = kvm_ioapic_init(kvm);
> >  			if (r) {
> > -				kfree(kvm->arch.vpic);
> > -				kvm->arch.vpic = NULL;
> > -				goto out;
> > +				kfree(vpic);
> > +				goto create_irqchip_unlock;
> >  			}
> >  		} else
> > -			goto out;
> > +			goto create_irqchip_unlock;
> > +		kvm->arch.vpic = vpic;
> > +		smp_wmb();
> 
> Hmm, I think we want the reverse order:
> 		smp_wmb();
> 		kvm->arch.vpic = vpic;
> 
> The point is preventing vpic pointer
> from being written to memory before vpic data itself.
> Right?

Point is preventing arch.vpic assignment (and everything before) from
reordering with kvm_setup_default_irq_routing (you cannot have a present
irq route without arch.vioapic or arch.vpic set, since kvm_set_irq
assumes they are present).

But, now that you say, we also want a smp_wmb before vpic assignment so
the irqchip_in_kernel() test is safe (that is, everything including
vioapic is in place before irqchip_in_kernel() can succeed).

I'll resend, thanks.

> BTW, the reason that we have wmb here but no read barrier anywhere is
> that this code is x86 specific and reads are not reordered across a
> dependency on x86. Writes are also not reordered on x86, so this really
> acts as a compiler barrier, but I agree it's better to be portable.  So
> maybe, for documentation purposes, we should add read_barrier_depends
> within irqchip_in_kernel()?
> 
> >  		r = kvm_setup_default_irq_routing(kvm);
> >  		if (r) {
> > +			mutex_lock(&kvm->irq_lock);
> >  			kfree(kvm->arch.vpic);
> >  			kfree(kvm->arch.vioapic);
> > -			goto out;
> > +			kvm->arch.vpic = NULL;
> > +			kvm->arch.vioapic = NULL;
> > +			mutex_unlock(&kvm->irq_lock);
> >  		}
> > +	create_irqchip_unlock:
> > +		mutex_unlock(&kvm->lock);
> >  		break;
> > +	}
> >  	case KVM_CREATE_PIT:
> >  		u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
> >  		goto create_pit;
> > 

  reply	other threads:[~2009-10-29 14:11 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-27 15:10 [patch 0/3] ioctl fixes Marcelo Tosatti
2009-10-27 15:10 ` [patch 1/3] KVM: x86: disallow multiple KVM_CREATE_IRQCHIP Marcelo Tosatti
2009-10-27 17:49   ` Michael S. Tsirkin
2009-10-27 15:10 ` [patch 2/3] KVM: x86: disallow KVM_{SET,GET}_LAPIC without in kernel irqchip Marcelo Tosatti
2009-10-27 17:50   ` Michael S. Tsirkin
2009-10-28  9:32     ` Avi Kivity
2009-10-28 10:20       ` Michael S. Tsirkin
2009-10-28 10:30         ` Gleb Natapov
2009-10-28 10:32           ` Michael S. Tsirkin
2009-10-28 10:39             ` Gleb Natapov
2009-10-28 10:34         ` Avi Kivity
2009-10-27 15:10 ` [patch 3/3] KVM: only clear irq_source_id if irqchip is present Marcelo Tosatti
2009-10-28 20:42 ` [patch 0/3] ioctl fixes v2 Marcelo Tosatti
2009-10-28 20:42   ` [patch 1/3] KVM: x86: disallow multiple KVM_CREATE_IRQCHIP Marcelo Tosatti
2009-10-29  9:32     ` Michael S. Tsirkin
2009-10-29 14:10       ` Marcelo Tosatti [this message]
2009-10-28 20:42   ` [patch 2/3] KVM: x86: disallow KVM_{SET,GET}_LAPIC without allocated in-kernel lapic Marcelo Tosatti
2009-10-28 20:42   ` [patch 3/3] KVM: only clear irq_source_id if irqchip is present Marcelo Tosatti
2009-10-29 15:44   ` [patch 0/3] ioctl fixes v3 Marcelo Tosatti
2009-10-29 15:44     ` [patch 1/3] KVM: x86: disallow multiple KVM_CREATE_IRQCHIP Marcelo Tosatti
2009-10-29 15:44     ` [patch 2/3] KVM: x86: disallow KVM_{SET,GET}_LAPIC without allocated in-kernel lapic Marcelo Tosatti
2009-10-29 15:44     ` [patch 3/3] KVM: only clear irq_source_id if irqchip is present Marcelo Tosatti
2009-11-02  9:53     ` [patch 0/3] ioctl fixes v3 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=20091029141030.GA3158@amt.cnet \
    --to=mtosatti@redhat.com \
    --cc=avi@redhat.com \
    --cc=gleb@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=mst@redhat.com \
    /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