diff mbox series

[1/4] KVM: x86: add start_assignment hook to kvm_x86_ops

Message ID 20210510172817.934490238@redhat.com (mailing list archive)
State New, archived
Headers show
Series VMX: configure posted interrupt descriptor when assigning device (v3) | expand

Commit Message

Marcelo Tosatti May 10, 2021, 5:26 p.m. UTC
Add a start_assignment hook to kvm_x86_ops, which is called when 
kvm_arch_start_assignment is done.

The hook is required to update the wakeup vector of a sleeping vCPU
when a device is assigned to the guest.

Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>

Comments

Peter Xu May 11, 2021, 4:26 p.m. UTC | #1
On Mon, May 10, 2021 at 02:26:47PM -0300, Marcelo Tosatti wrote:
> Add a start_assignment hook to kvm_x86_ops, which is called when 
> kvm_arch_start_assignment is done.
> 
> The hook is required to update the wakeup vector of a sleeping vCPU
> when a device is assigned to the guest.
> 
> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
> 
> Index: kvm/arch/x86/include/asm/kvm_host.h
> ===================================================================
> --- kvm.orig/arch/x86/include/asm/kvm_host.h
> +++ kvm/arch/x86/include/asm/kvm_host.h
> @@ -1322,6 +1322,7 @@ struct kvm_x86_ops {
>  
>  	int (*update_pi_irte)(struct kvm *kvm, unsigned int host_irq,
>  			      uint32_t guest_irq, bool set);
> +	void (*start_assignment)(struct kvm *kvm);
>  	void (*apicv_post_state_restore)(struct kvm_vcpu *vcpu);
>  	bool (*dy_apicv_has_pending_interrupt)(struct kvm_vcpu *vcpu);
>  
> Index: kvm/arch/x86/kvm/svm/svm.c
> ===================================================================
> --- kvm.orig/arch/x86/kvm/svm/svm.c
> +++ kvm/arch/x86/kvm/svm/svm.c
> @@ -4601,6 +4601,7 @@ static struct kvm_x86_ops svm_x86_ops __
>  	.deliver_posted_interrupt = svm_deliver_avic_intr,
>  	.dy_apicv_has_pending_interrupt = svm_dy_apicv_has_pending_interrupt,
>  	.update_pi_irte = svm_update_pi_irte,
> +	.start_assignment = NULL,

Can this be dropped (as default NULL)?

>  	.setup_mce = svm_setup_mce,
>  
>  	.smi_allowed = svm_smi_allowed,
> Index: kvm/arch/x86/kvm/vmx/vmx.c
> ===================================================================
> --- kvm.orig/arch/x86/kvm/vmx/vmx.c
> +++ kvm/arch/x86/kvm/vmx/vmx.c
> @@ -7732,6 +7732,7 @@ static struct kvm_x86_ops vmx_x86_ops __
>  	.nested_ops = &vmx_nested_ops,
>  
>  	.update_pi_irte = pi_update_irte,
> +	.start_assignment = NULL,

Same here?

>  
>  #ifdef CONFIG_X86_64
>  	.set_hv_timer = vmx_set_hv_timer,
> Index: kvm/arch/x86/kvm/x86.c
> ===================================================================
> --- kvm.orig/arch/x86/kvm/x86.c
> +++ kvm/arch/x86/kvm/x86.c
> @@ -11295,7 +11295,11 @@ bool kvm_arch_can_dequeue_async_page_pre
>  
>  void kvm_arch_start_assignment(struct kvm *kvm)
>  {
> -	atomic_inc(&kvm->arch.assigned_device_count);
> +	int ret;
> +
> +	ret = atomic_inc_return(&kvm->arch.assigned_device_count);
> +	if (ret == 1)
> +		static_call_cond(kvm_x86_start_assignment)(kvm);

Maybe "ret" can be dropped too?

void kvm_arch_start_assignment(struct kvm *kvm)
{
	if (atomic_inc_return(&kvm->arch.assigned_device_count) == 1)
		static_call_cond(kvm_x86_start_assignment)(kvm);
}

Otherwise looks good to me.  Thanks,
Marcelo Tosatti May 11, 2021, 5:29 p.m. UTC | #2
On Tue, May 11, 2021 at 12:26:08PM -0400, Peter Xu wrote:
> On Mon, May 10, 2021 at 02:26:47PM -0300, Marcelo Tosatti wrote:
> > Add a start_assignment hook to kvm_x86_ops, which is called when 
> > kvm_arch_start_assignment is done.
> > 
> > The hook is required to update the wakeup vector of a sleeping vCPU
> > when a device is assigned to the guest.
> > 
> > Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
> > 
> > Index: kvm/arch/x86/include/asm/kvm_host.h
> > ===================================================================
> > --- kvm.orig/arch/x86/include/asm/kvm_host.h
> > +++ kvm/arch/x86/include/asm/kvm_host.h
> > @@ -1322,6 +1322,7 @@ struct kvm_x86_ops {
> >  
> >  	int (*update_pi_irte)(struct kvm *kvm, unsigned int host_irq,
> >  			      uint32_t guest_irq, bool set);
> > +	void (*start_assignment)(struct kvm *kvm);
> >  	void (*apicv_post_state_restore)(struct kvm_vcpu *vcpu);
> >  	bool (*dy_apicv_has_pending_interrupt)(struct kvm_vcpu *vcpu);
> >  
> > Index: kvm/arch/x86/kvm/svm/svm.c
> > ===================================================================
> > --- kvm.orig/arch/x86/kvm/svm/svm.c
> > +++ kvm/arch/x86/kvm/svm/svm.c
> > @@ -4601,6 +4601,7 @@ static struct kvm_x86_ops svm_x86_ops __
> >  	.deliver_posted_interrupt = svm_deliver_avic_intr,
> >  	.dy_apicv_has_pending_interrupt = svm_dy_apicv_has_pending_interrupt,
> >  	.update_pi_irte = svm_update_pi_irte,
> > +	.start_assignment = NULL,
> 
> Can this be dropped (as default NULL)?

Done.

> 
> >  	.setup_mce = svm_setup_mce,
> >  
> >  	.smi_allowed = svm_smi_allowed,
> > Index: kvm/arch/x86/kvm/vmx/vmx.c
> > ===================================================================
> > --- kvm.orig/arch/x86/kvm/vmx/vmx.c
> > +++ kvm/arch/x86/kvm/vmx/vmx.c
> > @@ -7732,6 +7732,7 @@ static struct kvm_x86_ops vmx_x86_ops __
> >  	.nested_ops = &vmx_nested_ops,
> >  
> >  	.update_pi_irte = pi_update_irte,
> > +	.start_assignment = NULL,
> 
> Same here?

Done.

> >  
> >  #ifdef CONFIG_X86_64
> >  	.set_hv_timer = vmx_set_hv_timer,
> > Index: kvm/arch/x86/kvm/x86.c
> > ===================================================================
> > --- kvm.orig/arch/x86/kvm/x86.c
> > +++ kvm/arch/x86/kvm/x86.c
> > @@ -11295,7 +11295,11 @@ bool kvm_arch_can_dequeue_async_page_pre
> >  
> >  void kvm_arch_start_assignment(struct kvm *kvm)
> >  {
> > -	atomic_inc(&kvm->arch.assigned_device_count);
> > +	int ret;
> > +
> > +	if (atomic_inc_return(&kvm->arch.assigned_device_count) == 1)
> > +	if (ret == 1)
> > +		static_call_cond(kvm_x86_start_assignment)(kvm);
> 
> Maybe "ret" can be dropped too?
> 
> void kvm_arch_start_assignment(struct kvm *kvm)
> {
> 	if (atomic_inc_return(&kvm->arch.assigned_device_count) == 1)
> 		static_call_cond(kvm_x86_start_assignment)(kvm);
> }
> 
> Otherwise looks good to me.  Thanks,

Done.
diff mbox series

Patch

Index: kvm/arch/x86/include/asm/kvm_host.h
===================================================================
--- kvm.orig/arch/x86/include/asm/kvm_host.h
+++ kvm/arch/x86/include/asm/kvm_host.h
@@ -1322,6 +1322,7 @@  struct kvm_x86_ops {
 
 	int (*update_pi_irte)(struct kvm *kvm, unsigned int host_irq,
 			      uint32_t guest_irq, bool set);
+	void (*start_assignment)(struct kvm *kvm);
 	void (*apicv_post_state_restore)(struct kvm_vcpu *vcpu);
 	bool (*dy_apicv_has_pending_interrupt)(struct kvm_vcpu *vcpu);
 
Index: kvm/arch/x86/kvm/svm/svm.c
===================================================================
--- kvm.orig/arch/x86/kvm/svm/svm.c
+++ kvm/arch/x86/kvm/svm/svm.c
@@ -4601,6 +4601,7 @@  static struct kvm_x86_ops svm_x86_ops __
 	.deliver_posted_interrupt = svm_deliver_avic_intr,
 	.dy_apicv_has_pending_interrupt = svm_dy_apicv_has_pending_interrupt,
 	.update_pi_irte = svm_update_pi_irte,
+	.start_assignment = NULL,
 	.setup_mce = svm_setup_mce,
 
 	.smi_allowed = svm_smi_allowed,
Index: kvm/arch/x86/kvm/vmx/vmx.c
===================================================================
--- kvm.orig/arch/x86/kvm/vmx/vmx.c
+++ kvm/arch/x86/kvm/vmx/vmx.c
@@ -7732,6 +7732,7 @@  static struct kvm_x86_ops vmx_x86_ops __
 	.nested_ops = &vmx_nested_ops,
 
 	.update_pi_irte = pi_update_irte,
+	.start_assignment = NULL,
 
 #ifdef CONFIG_X86_64
 	.set_hv_timer = vmx_set_hv_timer,
Index: kvm/arch/x86/kvm/x86.c
===================================================================
--- kvm.orig/arch/x86/kvm/x86.c
+++ kvm/arch/x86/kvm/x86.c
@@ -11295,7 +11295,11 @@  bool kvm_arch_can_dequeue_async_page_pre
 
 void kvm_arch_start_assignment(struct kvm *kvm)
 {
-	atomic_inc(&kvm->arch.assigned_device_count);
+	int ret;
+
+	ret = atomic_inc_return(&kvm->arch.assigned_device_count);
+	if (ret == 1)
+		static_call_cond(kvm_x86_start_assignment)(kvm);
 }
 EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
 
Index: kvm/arch/x86/include/asm/kvm-x86-ops.h
===================================================================
--- kvm.orig/arch/x86/include/asm/kvm-x86-ops.h
+++ kvm/arch/x86/include/asm/kvm-x86-ops.h
@@ -99,6 +99,7 @@  KVM_X86_OP_NULL(post_block)
 KVM_X86_OP_NULL(vcpu_blocking)
 KVM_X86_OP_NULL(vcpu_unblocking)
 KVM_X86_OP_NULL(update_pi_irte)
+KVM_X86_OP_NULL(start_assignment)
 KVM_X86_OP_NULL(apicv_post_state_restore)
 KVM_X86_OP_NULL(dy_apicv_has_pending_interrupt)
 KVM_X86_OP_NULL(set_hv_timer)