Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1195174 > unrolled thread

[PATCH 4/4] KVM: x86: clean/fix memory barriers in irqchip_in_kernel

Started byPaolo Bonzini <pbonzini@redhat.com>
First post2015-07-29 15:30 +0200
Last post2015-07-30 09:30 +0200
Articles 3 — 2 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [PATCH 4/4] KVM: x86: clean/fix memory barriers in irqchip_in_kernel Paolo Bonzini <pbonzini@redhat.com> - 2015-07-29 15:30 +0200
    Re: [PATCH 4/4] KVM: x86: clean/fix memory barriers in  irqchip_in_kernel Steve Rutherford <srutherford@google.com> - 2015-07-30 05:40 +0200
      Re: [PATCH 4/4] KVM: x86: clean/fix memory barriers in  irqchip_in_kernel Paolo Bonzini <pbonzini@redhat.com> - 2015-07-30 09:30 +0200

#1195174 — [PATCH 4/4] KVM: x86: clean/fix memory barriers in irqchip_in_kernel

FromPaolo Bonzini <pbonzini@redhat.com>
Date2015-07-29 15:30 +0200
Subject[PATCH 4/4] KVM: x86: clean/fix memory barriers in irqchip_in_kernel
Message-ID<pRzAK-Um-11@gated-at.bofh.it>
The memory barriers are trying to protect against concurrent RCU-based
interrupt injection, but the IRQ routing table is not valid at the time
kvm->arch.vpic is written.  Fix this by writing kvm->arch.vpic last.
kvm_destroy_pic then need not set kvm->arch.vpic to NULL; modify it
to take a struct kvm_pic* and reuse it if the IOAPIC creation fails.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kvm/i8259.c | 15 +++++----------
 arch/x86/kvm/irq.h   |  8 ++++----
 arch/x86/kvm/x86.c   | 17 ++++++-----------
 3 files changed, 15 insertions(+), 25 deletions(-)

diff --git a/arch/x86/kvm/i8259.c b/arch/x86/kvm/i8259.c
index fef922ff2635..7cc2360f1848 100644
--- a/arch/x86/kvm/i8259.c
+++ b/arch/x86/kvm/i8259.c
@@ -651,15 +651,10 @@ fail_unlock:
 	return NULL;
 }
 
-void kvm_destroy_pic(struct kvm *kvm)
+void kvm_destroy_pic(struct kvm_pic *vpic)
 {
-	struct kvm_pic *vpic = kvm->arch.vpic;
-
-	if (vpic) {
-		kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &vpic->dev_master);
-		kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &vpic->dev_slave);
-		kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS, &vpic->dev_eclr);
-		kvm->arch.vpic = NULL;
-		kfree(vpic);
-	}
+	kvm_io_bus_unregister_dev(vpic->kvm, KVM_PIO_BUS, &vpic->dev_master);
+	kvm_io_bus_unregister_dev(vpic->kvm, KVM_PIO_BUS, &vpic->dev_slave);
+	kvm_io_bus_unregister_dev(vpic->kvm, KVM_PIO_BUS, &vpic->dev_eclr);
+	kfree(vpic);
 }
diff --git a/arch/x86/kvm/irq.h b/arch/x86/kvm/irq.h
index ad68c73008c5..3d782a2c336a 100644
--- a/arch/x86/kvm/irq.h
+++ b/arch/x86/kvm/irq.h
@@ -74,7 +74,7 @@ struct kvm_pic {
 };
 
 struct kvm_pic *kvm_create_pic(struct kvm *kvm);
-void kvm_destroy_pic(struct kvm *kvm);
+void kvm_destroy_pic(struct kvm_pic *vpic);
 int kvm_pic_read_irq(struct kvm *kvm);
 void kvm_pic_update_irq(struct kvm_pic *s);
 
@@ -85,11 +85,11 @@ static inline struct kvm_pic *pic_irqchip(struct kvm *kvm)
 
 static inline int irqchip_in_kernel(struct kvm *kvm)
 {
-	int ret;
+	struct kvm_pic *vpic = pic_irqchip(kvm);
 
-	ret = (pic_irqchip(kvm) != NULL);
+	/* Read vpic before kvm->irq_routing.  */
 	smp_rmb();
-	return ret;
+	return vpic != NULL;
 }
 
 void kvm_pic_reset(struct kvm_kpic_state *s);
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 2d62229aac26..23e47a0b054b 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -3626,30 +3626,25 @@ long kvm_arch_vm_ioctl(struct file *filp,
 			r = kvm_ioapic_init(kvm);
 			if (r) {
 				mutex_lock(&kvm->slots_lock);
-				kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
-							  &vpic->dev_master);
-				kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
-							  &vpic->dev_slave);
-				kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
-							  &vpic->dev_eclr);
+				kvm_destroy_pic(vpic);
 				mutex_unlock(&kvm->slots_lock);
-				kfree(vpic);
 				goto create_irqchip_unlock;
 			}
 		} else
 			goto create_irqchip_unlock;
-		smp_wmb();
-		kvm->arch.vpic = vpic;
-		smp_wmb();
 		r = kvm_setup_default_irq_routing(kvm);
 		if (r) {
 			mutex_lock(&kvm->slots_lock);
 			mutex_lock(&kvm->irq_lock);
 			kvm_ioapic_destroy(kvm);
-			kvm_destroy_pic(kvm);
+			kvm_destroy_pic(vpic);
 			mutex_unlock(&kvm->irq_lock);
 			mutex_unlock(&kvm->slots_lock);
+			goto create_irqchip_unlock;
 		}
+		/* Write kvm->irq_routing before kvm->arch.vpic.  */
+		smp_wmb();
+		kvm->arch.vpic = vpic;
 	create_irqchip_unlock:
 		mutex_unlock(&kvm->lock);
 		break;
-- 
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [next] | [standalone]


#1195701 — Re: [PATCH 4/4] KVM: x86: clean/fix memory barriers in irqchip_in_kernel

FromSteve Rutherford <srutherford@google.com>
Date2015-07-30 05:40 +0200
SubjectRe: [PATCH 4/4] KVM: x86: clean/fix memory barriers in irqchip_in_kernel
Message-ID<pRMRk-365-15@gated-at.bofh.it>
In reply to#1195174
On Wed, Jul 29, 2015 at 03:28:58PM +0200, Paolo Bonzini wrote:
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 2d62229aac26..23e47a0b054b 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -3626,30 +3626,25 @@ long kvm_arch_vm_ioctl(struct file *filp,
>  			r = kvm_ioapic_init(kvm);
>  			if (r) {
>  				mutex_lock(&kvm->slots_lock);
> -				kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
> -							  &vpic->dev_master);
> -				kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
> -							  &vpic->dev_slave);
> -				kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
> -							  &vpic->dev_eclr);
> +				kvm_destroy_pic(vpic);
>  				mutex_unlock(&kvm->slots_lock);
> -				kfree(vpic);
>  				goto create_irqchip_unlock;
>  			}
>  		} else
>  			goto create_irqchip_unlock;
> -		smp_wmb();
> -		kvm->arch.vpic = vpic;
> -		smp_wmb();
>  		r = kvm_setup_default_irq_routing(kvm);
>  		if (r) {
>  			mutex_lock(&kvm->slots_lock);
>  			mutex_lock(&kvm->irq_lock);
>  			kvm_ioapic_destroy(kvm);
> -			kvm_destroy_pic(kvm);
> +			kvm_destroy_pic(vpic);
>  			mutex_unlock(&kvm->irq_lock);
>  			mutex_unlock(&kvm->slots_lock);
> +			goto create_irqchip_unlock;
>  		}
> +		/* Write kvm->irq_routing before kvm->arch.vpic.  */
> +		smp_wmb();
I assume this pairs with irqchip_in_kernel? 
> +		kvm->arch.vpic = vpic;
>  	create_irqchip_unlock:
>  		mutex_unlock(&kvm->lock);
>  		break;
> -- 
> 1.8.3.1
> 
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1195758 — Re: [PATCH 4/4] KVM: x86: clean/fix memory barriers in irqchip_in_kernel

FromPaolo Bonzini <pbonzini@redhat.com>
Date2015-07-30 09:30 +0200
SubjectRe: [PATCH 4/4] KVM: x86: clean/fix memory barriers in irqchip_in_kernel
Message-ID<pRQrU-8ly-9@gated-at.bofh.it>
In reply to#1195701

On 30/07/2015 05:32, Steve Rutherford wrote:
>> > +		/* Write kvm->irq_routing before kvm->arch.vpic.  */
>> > +		smp_wmb();
> I assume this pairs with irqchip_in_kernel? 

Yes, see the comment added there by this same patch ("read
kvm->arch.vpic before kvm->irq_routing").

Paolo
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web