Skip to contentSkip to conversation

KVM: arm64: Allow in-atomic injection of SPIs

PATCH Superseded Marc Zyngier 3 participants 8 messages May 26, 2020 – Jun 9, 2020

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Marc Zyngier

author maintainer
#0 lore raw

PATCH 1/1 KVM: arm64: Allow in-atomic injection of SPIs

On a system that uses SPIs to implement MSIs (as it would be the case on a GICv2 system exposing a GICv2m to its guests), we deny the possibility of injecting SPIs on the in-atomic fast-path.

This results in a very large amount of context-switches (roughly equivalent to twice the interrupt rate) on the host, and suboptimal performance for the guest (as measured with a test workload involving a virtio interface backed by vhost-net). Given that GICv2 systems are usually on the low-end of the spectrum performance wise, they could do without the aggravation.

We solved this for GICv3+ITS by having a translation cache. But SPIs do not need any extra infrastructure, and can be immediately injected in the virtual distributor as the locking is already heavy enough that we don't need to worry about anything.

This halves the number of context switches for the same workload.

---
 arch/arm64/kvm/vgic/vgic-irqfd.c | 20 ++++++++++++++++----
 arch/arm64/kvm/vgic/vgic-its.c   |  3 +--
 2 files changed, 17 insertions(+), 6 deletions(-)
arch/arm64/kvm/vgic/vgic-irqfd.c +16 -4
index d8cdfea5cc96..11a9f81115ab 100644
@@ -107,15 +107,27 @@ int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e,
 			      struct kvm *kvm, int irq_source_id, int level,
 			      bool line_status)
 {
-	if (e->type == KVM_IRQ_ROUTING_MSI && vgic_has_its(kvm) && level) {
+	if (!level)
+		return -EWOULDBLOCK;
+
+	switch (e->type) {
+	case KVM_IRQ_ROUTING_MSI: {
 		struct kvm_msi msi;
 
+		if (!vgic_has_its(kvm))
+			return -EINVAL;
+
 		kvm_populate_msi(e, &msi);
-		if (!vgic_its_inject_cached_translation(kvm, &msi))
-			return 0;
+		return vgic_its_inject_cached_translation(kvm, &msi);
 	}
 
-	return -EWOULDBLOCK;
+	case KVM_IRQ_ROUTING_IRQCHIP:
+		/* Injecting SPIs is always possible in atomic context */
+		return vgic_irqfd_set_irq(e, kvm, irq_source_id, 1, line_status);
+
+	default:
+		return -EWOULDBLOCK;
+	}
 }
 
 int kvm_vgic_setup_default_irq_routing(struct kvm *kvm)
arch/arm64/kvm/vgic/vgic-its.c +1 -2
index c012a52b19f5..40cbaca81333 100644
@@ -757,9 +757,8 @@ int vgic_its_inject_cached_translation(struct kvm *kvm, struct kvm_msi *msi)
 
 	db = (u64)msi->address_hi << 32 | msi->address_lo;
 	irq = vgic_its_check_cache(kvm, db, msi->devid, msi->data);
-
 	if (!irq)
-		return -1;
+		return -EWOULDBLOCK;
 
 	raw_spin_lock_irqsave(&irq->irq_lock, flags);
 	irq->pending_latch = true;
Signature · 1 line
2.26.2
reviewer
#1 lore raw
Quoted 30 lines · Marc Zyngier

On a system that uses SPIs to implement MSIs (as it would be the case on a GICv2 system exposing a GICv2m to its guests), we deny the possibility of injecting SPIs on the in-atomic fast-path.

This results in a very large amount of context-switches (roughly equivalent to twice the interrupt rate) on the host, and suboptimal performance for the guest (as measured with a test workload involving a virtio interface backed by vhost-net). Given that GICv2 systems are usually on the low-end of the spectrum performance wise, they could do without the aggravation.

We solved this for GICv3+ITS by having a translation cache. But SPIs do not need any extra infrastructure, and can be immediately injected in the virtual distributor as the locking is already heavy enough that we don't need to worry about anything.

This halves the number of context switches for the same workload.

---
  arch/arm64/kvm/vgic/vgic-irqfd.c | 20 ++++++++++++++++----
  arch/arm64/kvm/vgic/vgic-its.c   |  3 +--
  2 files changed, 17 insertions(+), 6 deletions(-)
arch/arm64/kvm/vgic/vgic-irqfd.c +0 -0
index d8cdfea5cc96..11a9f81115ab 100644

arch/arm64/kvm/vgic/vgic-irqfd.c int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e, · line 108

Quoted 2 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +0 -0
@@ -107,15 +107,27 @@ int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e,
  			      struct kvm *kvm, int irq_source_id, int level,
  			      bool line_status)

... and you may also need to update the comment on top of it to reflect this change.

/**
* kvm_arch_set_irq_inatomic: fast-path for irqfd injection *
* Currently only direct MSI injection is supported. */

Thanks,
Zenghui

Quoted 45 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +16 -4
  {
-	if (e->type == KVM_IRQ_ROUTING_MSI && vgic_has_its(kvm) && level) {
+	if (!level)
+		return -EWOULDBLOCK;
+
+	switch (e->type) {
+	case KVM_IRQ_ROUTING_MSI: {
  		struct kvm_msi msi;
 
+		if (!vgic_has_its(kvm))
+			return -EINVAL;
+
  		kvm_populate_msi(e, &msi);
-		if (!vgic_its_inject_cached_translation(kvm, &msi))
-			return 0;
+		return vgic_its_inject_cached_translation(kvm, &msi);
  	}
 
-	return -EWOULDBLOCK;
+	case KVM_IRQ_ROUTING_IRQCHIP:
+		/* Injecting SPIs is always possible in atomic context */
+		return vgic_irqfd_set_irq(e, kvm, irq_source_id, 1, line_status);
+
+	default:
+		return -EWOULDBLOCK;
+	}
  }
 
  int kvm_vgic_setup_default_irq_routing(struct kvm *kvm)
arch/arm64/kvm/vgic/vgic-its.c +1 -2
index c012a52b19f5..40cbaca81333 100644
@@ -757,9 +757,8 @@ int vgic_its_inject_cached_translation(struct kvm *kvm, struct kvm_msi *msi)
 
  	db = (u64)msi->address_hi << 32 | msi->address_lo;
  	irq = vgic_its_check_cache(kvm, db, msi->devid, msi->data);
-
  	if (!irq)
-		return -1;
+		return -EWOULDBLOCK;
 
  	raw_spin_lock_irqsave(&irq->irq_lock, flags);
  	irq->pending_latch = true;
author maintainer
#2 lore raw

Hi Zenghui,

Quoted 38 lines · Zenghui Yu
Quoted 30 lines · Marc Zyngier

On a system that uses SPIs to implement MSIs (as it would be the case on a GICv2 system exposing a GICv2m to its guests), we deny the possibility of injecting SPIs on the in-atomic fast-path.

This results in a very large amount of context-switches (roughly equivalent to twice the interrupt rate) on the host, and suboptimal performance for the guest (as measured with a test workload involving a virtio interface backed by vhost-net). Given that GICv2 systems are usually on the low-end of the spectrum performance wise, they could do without the aggravation.

We solved this for GICv3+ITS by having a translation cache. But SPIs do not need any extra infrastructure, and can be immediately injected in the virtual distributor as the locking is already heavy enough that we don't need to worry about anything.

This halves the number of context switches for the same workload.

---
  arch/arm64/kvm/vgic/vgic-irqfd.c | 20 ++++++++++++++++----
  arch/arm64/kvm/vgic/vgic-its.c   |  3 +--
  2 files changed, 17 insertions(+), 6 deletions(-)
arch/arm64/kvm/vgic/vgic-irqfd.c +0 -0
index d8cdfea5cc96..11a9f81115ab 100644

arch/arm64/kvm/vgic/vgic-irqfd.c int kvm_arch_set_irq_inatomic(struct · line 109

Quoted 3 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +0 -0
@@ -107,15 +107,27 @@ int kvm_arch_set_irq_inatomic(struct 
 kvm_kernel_irq_routing_entry *e,
  			      struct kvm *kvm, int irq_source_id, int level,
  			      bool line_status)

... and you may also need to update the comment on top of it to reflect this change.

Quoted 5 lines

/**
* kvm_arch_set_irq_inatomic: fast-path for irqfd injection *
* Currently only direct MSI injection is supported. */

As far as I can tell, it is still valid (at least from the guest's perspective). You could in practice use that to deal with level interrupts, but we only inject the rising edge on this path, never the falling edge. So effectively, this is limited to edge interrupts, which is mostly MSIs.

Unless you are thinking of something else which I would have missed?

Thanks,

        M.
Signature · 1 line
Jazz is not dead. It just smells funny...
reviewer
#3 lore raw

Hi Marc,

Quoted 47 lines · Marc Zyngier

Hi Zenghui,

Quoted 38 lines · Zenghui Yu
Quoted 30 lines · Marc Zyngier

On a system that uses SPIs to implement MSIs (as it would be the case on a GICv2 system exposing a GICv2m to its guests), we deny the possibility of injecting SPIs on the in-atomic fast-path.

This results in a very large amount of context-switches (roughly equivalent to twice the interrupt rate) on the host, and suboptimal performance for the guest (as measured with a test workload involving a virtio interface backed by vhost-net). Given that GICv2 systems are usually on the low-end of the spectrum performance wise, they could do without the aggravation.

We solved this for GICv3+ITS by having a translation cache. But SPIs do not need any extra infrastructure, and can be immediately injected in the virtual distributor as the locking is already heavy enough that we don't need to worry about anything.

This halves the number of context switches for the same workload.

---
  arch/arm64/kvm/vgic/vgic-irqfd.c | 20 ++++++++++++++++---- arch/arm64/kvm/vgic/vgic-its.c   |  3 +--

  2 files changed, 17 insertions(+), 6 deletions(-)
arch/arm64/kvm/vgic/vgic-irqfd.c +0 -0
index d8cdfea5cc96..11a9f81115ab 100644

arch/arm64/kvm/vgic/vgic-irqfd.c int kvm_arch_set_irq_inatomic(struct · line 109

Quoted 3 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +0 -0
@@ -107,15 +107,27 @@ int kvm_arch_set_irq_inatomic(struct 
 kvm_kernel_irq_routing_entry *e,
                     struct kvm *kvm, int irq_source_id, int level,
                     bool line_status)

... and you may also need to update the comment on top of it to reflect this change.

Quoted 5 lines

/**
 * kvm_arch_set_irq_inatomic: fast-path for irqfd injection *
 * Currently only direct MSI injection is supported. */

Quoted 5 lines

As far as I can tell, it is still valid (at least from the guest's perspective). You could in practice use that to deal with level interrupts, but we only inject the rising edge on this path, never the falling edge. So effectively, this is limited to edge interrupts, which is mostly MSIs.

Oops... I had wrongly mixed MSI with the architecture-defined LPI, and was think that we should add something like "direct SPI injection is also supported now". Sorry.

Quoted 2 lines

Unless you are thinking of something else which I would have missed?

No, please ignore the noisy.

Thanks,
Zenghui

#4 lore raw
In reply to Marc Zyngier

Hi Marc,

arch/arm64/kvm/vgic/vgic-irqfd.c

Quoted 29 lines · Marc Zyngier

On a system that uses SPIs to implement MSIs (as it would be the case on a GICv2 system exposing a GICv2m to its guests), we deny the possibility of injecting SPIs on the in-atomic fast-path.

This results in a very large amount of context-switches (roughly equivalent to twice the interrupt rate) on the host, and suboptimal performance for the guest (as measured with a test workload involving a virtio interface backed by vhost-net). Given that GICv2 systems are usually on the low-end of the spectrum performance wise, they could do without the aggravation.

We solved this for GICv3+ITS by having a translation cache. But SPIs do not need any extra infrastructure, and can be immediately injected in the virtual distributor as the locking is already heavy enough that we don't need to worry about anything.

This halves the number of context switches for the same workload.

---
 arch/arm64/kvm/vgic/vgic-irqfd.c | 20 ++++++++++++++++----
 arch/arm64/kvm/vgic/vgic-its.c   |  3 +--
 2 files changed, 17 insertions(+), 6 deletions(-)
arch/arm64/kvm/vgic/vgic-irqfd.c +0 -0
index d8cdfea5cc96..11a9f81115ab 100644

There is still a comment above saying
* Currently only direct MSI injection is supported.

Quoted 4 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +0 -0
@@ -107,15 +107,27 @@ int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e,
 			      struct kvm *kvm, int irq_source_id, int level,
 			      bool line_status)
 {

arch/arm64/kvm/vgic/vgic-irqfd.c int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e, · line 118

Quoted 10 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +7 -1
-	if (e->type == KVM_IRQ_ROUTING_MSI && vgic_has_its(kvm) && level) {
+	if (!level)
+		return -EWOULDBLOCK;
+
+	switch (e->type) {
+	case KVM_IRQ_ROUTING_MSI: {
 		struct kvm_msi msi;
 
+		if (!vgic_has_its(kvm))
+			return -EINVAL;

Shouldn't we return -EWOULDBLOCK by default?
QEMU does not use that path with GICv2m but in kvm_set_routing_entry() I don't see any check related to the ITS.

arch/arm64/kvm/vgic/vgic-irqfd.c int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e,

Quoted 12 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +5 -3
+
 		kvm_populate_msi(e, &msi);
-		if (!vgic_its_inject_cached_translation(kvm, &msi))
-			return 0;
+		return vgic_its_inject_cached_translation(kvm, &msi);
 
 	}
 
-	return -EWOULDBLOCK;
+	case KVM_IRQ_ROUTING_IRQCHIP:
+		/* Injecting SPIs is always possible in atomic context */
+		return vgic_irqfd_set_irq(e, kvm, irq_source_id, 1, line_status);

what about the mutex_lock(&kvm->lock) called from within
vgic_irqfd_set_irq/kvm_vgic_inject_irq/vgic_lazy_init

Quoted 13 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +4 -0
+
+	default:
+		return -EWOULDBLOCK;
+	}
 }
 

int kvm_vgic_setup_default_irq_routing(struct kvm *kvm)

arch/arm64/kvm/vgic/vgic-its.c +0 -0
index c012a52b19f5..40cbaca81333 100644

arch/arm64/kvm/vgic/vgic-its.c int vgic_its_inject_cached_translation(struct kvm *kvm, struct kvm_msi *msi) · line 764

Quoted 10 lines
arch/arm64/kvm/vgic/vgic-its.c +1 -2
@@ -757,9 +757,8 @@ int vgic_its_inject_cached_translation(struct kvm *kvm, struct kvm_msi *msi)
 
 	db = (u64)msi->address_hi << 32 | msi->address_lo;
 	irq = vgic_its_check_cache(kvm, db, msi->devid, msi->data);
-
 	if (!irq)
-		return -1;
+		return -EWOULDBLOCK;
 
 	raw_spin_lock_irqsave(&irq->irq_lock, flags);
 	irq->pending_latch = true;

Thanks

Eric

author maintainer
#5 lore raw

Hi Eric,

Quoted 28 lines · Auger Eric

Hi Marc,

Quoted 25 lines · Marc Zyngier

On a system that uses SPIs to implement MSIs (as it would be the case on a GICv2 system exposing a GICv2m to its guests), we deny the possibility of injecting SPIs on the in-atomic fast-path.

This results in a very large amount of context-switches (roughly equivalent to twice the interrupt rate) on the host, and suboptimal performance for the guest (as measured with a test workload involving a virtio interface backed by vhost-net). Given that GICv2 systems are usually on the low-end of the spectrum performance wise, they could do without the aggravation.

We solved this for GICv3+ITS by having a translation cache. But SPIs do not need any extra infrastructure, and can be immediately injected in the virtual distributor as the locking is already heavy enough that we don't need to worry about anything.

This halves the number of context switches for the same workload.

---
 arch/arm64/kvm/vgic/vgic-irqfd.c | 20 ++++++++++++++++----
 arch/arm64/kvm/vgic/vgic-its.c   |  3 +--
 2 files changed, 17 insertions(+), 6 deletions(-)
Quoted 6 lines

arch/arm64/kvm/vgic/vgic-irqfd.c

Quoted 4 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +0 -0
index d8cdfea5cc96..11a9f81115ab 100644

There is still a comment above saying
* Currently only direct MSI injection is supported.

I believe this comment to be correct. There is no path other than MSI injection that leads us here. Case in point, we only ever inject a rising edge through this API, never a falling one.

Quoted 13 lines
Quoted 13 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +5 -1
@@ -107,15 +107,27 @@ int kvm_arch_set_irq_inatomic(struct 
 kvm_kernel_irq_routing_entry *e,
 			      struct kvm *kvm, int irq_source_id, int level,
 			      bool line_status)
 {
-	if (e->type == KVM_IRQ_ROUTING_MSI && vgic_has_its(kvm) && level) {
+	if (!level)
+		return -EWOULDBLOCK;
+
+	switch (e->type) {
+	case KVM_IRQ_ROUTING_MSI: {
 		struct kvm_msi msi;
 
Quoted 5 lines

arch/arm64/kvm/vgic/vgic-irqfd.c int kvm_arch_set_irq_inatomic(struct · line 119

Quoted 2 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +2 -0
+		if (!vgic_has_its(kvm))
+			return -EINVAL;

Shouldn't we return -EWOULDBLOCK by default?
QEMU does not use that path with GICv2m but in kvm_set_routing_entry() I don't see any check related to the ITS.

Fair enough. I really don't anticipate anyone to be using KVM_IRQ_ROUTING_MSI with anything but the ITS, but who knows, people are crazy! ;-)

Quoted 6 lines
Quoted 5 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +2 -2
+
 		kvm_populate_msi(e, &msi);
-		if (!vgic_its_inject_cached_translation(kvm, &msi))
-			return 0;
+		return vgic_its_inject_cached_translation(kvm, &msi);
Quoted 8 lines

arch/arm64/kvm/vgic/vgic-irqfd.c int kvm_arch_set_irq_inatomic(struct · line 127

Quoted 6 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +3 -1
 	}
 
-	return -EWOULDBLOCK;
+	case KVM_IRQ_ROUTING_IRQCHIP:
+		/* Injecting SPIs is always possible in atomic context */
+		return vgic_irqfd_set_irq(e, kvm, irq_source_id, 1, line_status);

what about the mutex_lock(&kvm->lock) called from within
vgic_irqfd_set_irq/kvm_vgic_inject_irq/vgic_lazy_init

Holy crap. The lazy GIC init strikes again :-(.
How about this on top of the existing patch:

arch/arm64/kvm/vgic/vgic-irqfd.c +9 -5
index 11a9f81115ab..6e5ca04d5589 100644
@@ -115,19 +115,23 @@ int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e,
 		struct kvm_msi msi;
 
 		if (!vgic_has_its(kvm))
-			return -EINVAL;
+			break;
 
 		kvm_populate_msi(e, &msi);
 		return vgic_its_inject_cached_translation(kvm, &msi);
 	}
 
 	case KVM_IRQ_ROUTING_IRQCHIP:
-		/* Injecting SPIs is always possible in atomic context */
+		/*
+		 * Injecting SPIs is always possible in atomic context
+		 * as long as the damn vgic is initialized.
+		 */
+		if (unlikely(!vgic_initialized(kvm)))
+			break;
 		return vgic_irqfd_set_irq(e, kvm, irq_source_id, 1, line_status);
-
-	default:
-		return -EWOULDBLOCK;
 	}
+
+	return -EWOULDBLOCK;
 }
 
 int kvm_vgic_setup_default_irq_routing(struct kvm *kvm)

Thanks,

        M.
Signature · 1 line
Jazz is not dead. It just smells funny...
#6 lore raw

Hi Marc,

Quoted 39 lines · Marc Zyngier

Hi Eric,

Quoted 28 lines · Auger Eric

Hi Marc,

Quoted 25 lines · Marc Zyngier

On a system that uses SPIs to implement MSIs (as it would be the case on a GICv2 system exposing a GICv2m to its guests), we deny the possibility of injecting SPIs on the in-atomic fast-path.

This results in a very large amount of context-switches (roughly equivalent to twice the interrupt rate) on the host, and suboptimal performance for the guest (as measured with a test workload involving a virtio interface backed by vhost-net). Given that GICv2 systems are usually on the low-end of the spectrum performance wise, they could do without the aggravation.

We solved this for GICv3+ITS by having a translation cache. But SPIs do not need any extra infrastructure, and can be immediately injected in the virtual distributor as the locking is already heavy enough that we don't need to worry about anything.

This halves the number of context switches for the same workload.

---
 arch/arm64/kvm/vgic/vgic-irqfd.c | 20 ++++++++++++++++---- arch/arm64/kvm/vgic/vgic-its.c   |  3 +--

 2 files changed, 17 insertions(+), 6 deletions(-)
Quoted 7 lines

arch/arm64/kvm/vgic/vgic-irqfd.c

Quoted 5 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +0 -0
index d8cdfea5cc96..11a9f81115ab 100644

There is still a comment above saying
 * Currently only direct MSI injection is supported.

Quoted 3 lines

I believe this comment to be correct. There is no path other than MSI injection that leads us here. Case in point, we only ever inject a rising edge through this API, never a falling one.

Isn't this path entered whenever you have either of the combo being used? KVM_SET_MSI_ROUTING + KVM_IRQFD
KVM_SET_GSI_ROUTING + KVM_IRQFD

I had in mind direct MSI injection was KVM_SIGNAL_MSI/ kvm_send_userspace_msi/kvm_set_msi

Quoted 60 lines
Quoted 14 lines
Quoted 14 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +5 -1
@@ -107,15 +107,27 @@ int kvm_arch_set_irq_inatomic(struct
 kvm_kernel_irq_routing_entry *e,
                    struct kvm *kvm, int irq_source_id, int level,
                    bool line_status)
  {
-    if (e->type == KVM_IRQ_ROUTING_MSI && vgic_has_its(kvm) && level) {
+    if (!level)
+        return -EWOULDBLOCK;
+
+    switch (e->type) {
+    case KVM_IRQ_ROUTING_MSI: {
          struct kvm_msi msi;
 
Quoted 5 lines

arch/arm64/kvm/vgic/vgic-irqfd.c int kvm_arch_set_irq_inatomic(struct · line 119

Quoted 2 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +2 -0
+        if (!vgic_has_its(kvm))
+            return -EINVAL;

Shouldn't we return -EWOULDBLOCK by default?
QEMU does not use that path with GICv2m but in kvm_set_routing_entry() I don't see any check related to the ITS.

Fair enough. I really don't anticipate anyone to be using KVM_IRQ_ROUTING_MSI with anything but the ITS, but who knows, people are crazy! ;-)

Quoted 8 lines
Quoted 8 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +2 -2
+
          kvm_populate_msi(e, &msi);
-        if (!vgic_its_inject_cached_translation(kvm, &msi))
-            return 0;
+        return vgic_its_inject_cached_translation(kvm, &msi);
 
      }
 
Quoted 7 lines

arch/arm64/kvm/vgic/vgic-irqfd.c int kvm_arch_set_irq_inatomic(struct · line 129

Quoted 5 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +3 -1
-    return -EWOULDBLOCK;
+    case KVM_IRQ_ROUTING_IRQCHIP:
+        /* Injecting SPIs is always possible in atomic context */
+        return vgic_irqfd_set_irq(e, kvm, irq_source_id, 1,
 line_status);

what about the     mutex_lock(&kvm->lock) called from within vgic_irqfd_set_irq/kvm_vgic_inject_irq/vgic_lazy_init

Holy crap. The lazy GIC init strikes again :-(.
How about this on top of the existing patch:

arch/arm64/kvm/vgic/vgic-irqfd.c +1 -1
index 11a9f81115ab..6e5ca04d5589 100644
@@ -115,19 +115,23 @@ int kvm_arch_set_irq_inatomic(struct
 kvm_kernel_irq_routing_entry *e,
          struct kvm_msi msi;
 
          if (!vgic_has_its(kvm))
-            return -EINVAL;
+            break;
 
          kvm_populate_msi(e, &msi);
          return vgic_its_inject_cached_translation(kvm, &msi);
      }
 
Quoted 8 lines

     case KVM_IRQ_ROUTING_IRQCHIP:
-        /* Injecting SPIs is always possible in atomic context */ +        /*
+         * Injecting SPIs is always possible in atomic context +         * as long as the damn vgic is initialized.
+         */
+        if (unlikely(!vgic_initialized(kvm)))
+            break;

Yes this should prevent the wait situation. But may be worth to deep into the call stack. Won't analyzers complain at some point?

Thanks

Eric

Quoted 15 lines

         return vgic_irqfd_set_irq(e, kvm, irq_source_id, 1, line_status); -
-    default:
-        return -EWOULDBLOCK;
     }
+
+    return -EWOULDBLOCK;
 }

 int kvm_vgic_setup_default_irq_routing(struct kvm *kvm)

Thanks,

        M.

author maintainer
#7 lore raw
Quoted 42 lines · Auger Eric

Hi Marc,

Quoted 39 lines · Marc Zyngier

Hi Eric,

Quoted 35 lines · Auger Eric

Hi Marc,

arch/arm64/kvm/vgic/vgic-irqfd.c

Quoted 30 lines · Marc Zyngier

On a system that uses SPIs to implement MSIs (as it would be the case on a GICv2 system exposing a GICv2m to its guests), we deny the possibility of injecting SPIs on the in-atomic fast-path.

This results in a very large amount of context-switches (roughly equivalent to twice the interrupt rate) on the host, and suboptimal performance for the guest (as measured with a test workload involving a virtio interface backed by vhost-net). Given that GICv2 systems are usually on the low-end of the spectrum performance wise, they could do without the aggravation.

We solved this for GICv3+ITS by having a translation cache. But SPIs do not need any extra infrastructure, and can be immediately injected in the virtual distributor as the locking is already heavy enough that we don't need to worry about anything.

This halves the number of context switches for the same workload.

---
 arch/arm64/kvm/vgic/vgic-irqfd.c | 20 ++++++++++++++++---- arch/arm64/kvm/vgic/vgic-its.c   |  3 +--

 2 files changed, 17 insertions(+), 6 deletions(-)
arch/arm64/kvm/vgic/vgic-irqfd.c +0 -0
index d8cdfea5cc96..11a9f81115ab 100644

There is still a comment above saying
 * Currently only direct MSI injection is supported.

Quoted 7 lines
Quoted 3 lines

I believe this comment to be correct. There is no path other than MSI injection that leads us here. Case in point, we only ever inject a rising edge through this API, never a falling one.

Isn't this path entered whenever you have either of the combo being used? KVM_SET_MSI_ROUTING + KVM_IRQFD
KVM_SET_GSI_ROUTING + KVM_IRQFD

I've always considered these to be MSIs, but maybe I'm narrow minded... ;-)

Quoted 2 lines

I had in mind direct MSI injection was KVM_SIGNAL_MSI/ kvm_send_userspace_msi/kvm_set_msi

Fair enough. Zengui was also unhappy about this comment, so I'll remove it. After all, we shouldn't care whether that's a MSI or not.

Quoted 61 lines
Quoted 61 lines
Quoted 15 lines
Quoted 15 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +5 -1
@@ -107,15 +107,27 @@ int kvm_arch_set_irq_inatomic(struct
 kvm_kernel_irq_routing_entry *e,
                    struct kvm *kvm, int irq_source_id, int level,
                    bool line_status)
  {
-    if (e->type == KVM_IRQ_ROUTING_MSI && vgic_has_its(kvm) && 
 level) {
+    if (!level)
+        return -EWOULDBLOCK;
+
+    switch (e->type) {
+    case KVM_IRQ_ROUTING_MSI: {
          struct kvm_msi msi;
 
Quoted 5 lines

arch/arm64/kvm/vgic/vgic-irqfd.c int kvm_arch_set_irq_inatomic(struct · line 120

Quoted 2 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +2 -0
+        if (!vgic_has_its(kvm))
+            return -EINVAL;

Shouldn't we return -EWOULDBLOCK by default?
QEMU does not use that path with GICv2m but in kvm_set_routing_entry() I don't see any check related to the ITS.

Fair enough. I really don't anticipate anyone to be using KVM_IRQ_ROUTING_MSI with anything but the ITS, but who knows, people are crazy! ;-)

Quoted 8 lines
Quoted 8 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +2 -2
+
          kvm_populate_msi(e, &msi);
-        if (!vgic_its_inject_cached_translation(kvm, &msi))
-            return 0;
+        return vgic_its_inject_cached_translation(kvm, &msi);
 
      }
 
Quoted 7 lines
Quoted 5 lines
arch/arm64/kvm/vgic/vgic-irqfd.c +3 -1
-    return -EWOULDBLOCK;
+    case KVM_IRQ_ROUTING_IRQCHIP:
+        /* Injecting SPIs is always possible in atomic context */
+        return vgic_irqfd_set_irq(e, kvm, irq_source_id, 1,

line_status);

what about the     mutex_lock(&kvm->lock) called from within vgic_irqfd_set_irq/kvm_vgic_inject_irq/vgic_lazy_init

Holy crap. The lazy GIC init strikes again :-(.
How about this on top of the existing patch:

arch/arm64/kvm/vgic/vgic-irqfd.c +1 -1
index 11a9f81115ab..6e5ca04d5589 100644
@@ -115,19 +115,23 @@ int kvm_arch_set_irq_inatomic(struct
 kvm_kernel_irq_routing_entry *e,
          struct kvm_msi msi;
 
          if (!vgic_has_its(kvm))
-            return -EINVAL;
+            break;
 
          kvm_populate_msi(e, &msi);
          return vgic_its_inject_cached_translation(kvm, &msi);
      }
 
Quoted 10 lines
Quoted 8 lines

     case KVM_IRQ_ROUTING_IRQCHIP:
-        /* Injecting SPIs is always possible in atomic context */ +        /*
+         * Injecting SPIs is always possible in atomic context +         * as long as the damn vgic is initialized.
+         */
+        if (unlikely(!vgic_initialized(kvm)))
+            break;

Yes this should prevent the wait situation. But may be worth to deep into the call stack. Won't analyzers complain at some point?

I have gone all the way in the call stack, to be sure. The init is the only point we use a mutex, and we're under a hard spinlock just after (all the injection proper is happening with interrupt disabled).

As for things like lockdep, they track the dynamic state, and not whether certain branches are simply "possible". A code analyser that wouldn't take control flow into account would be pretty broken! ;-)

Thanks,

        M.
Signature · 1 line
Jazz is not dead. It just smells funny...