Skip to contentSkip to conversation

KVM: Use IRQF_ONESHOT for assigned device MSI interrupts

PATCH In review Alex Williamson 6 participants 22 messages Jun 1, 2012 – Jun 18, 2012 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: Use IRQF_ONESHOT for assigned device MSI interrupts

The kernel no longer allows us to pass NULL for a hard interrupt handler without IRQF_ONESHOT. Should have been using this flag anyway.

---

 virt/kvm/assigned-dev.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
virt/kvm/assigned-dev.c +2 -2
index 01f572c..e804d14 100644
@@ -347,7 +347,7 @@ static int assigned_device_enable_host_msi(struct kvm *kvm,
 
 	dev->host_irq = dev->dev->irq;
 	if (request_threaded_irq(dev->host_irq, NULL,
-				 kvm_assigned_dev_thread_msi, 0,
+				 kvm_assigned_dev_thread_msi, IRQF_ONESHOT,
 				 dev->irq_name, dev)) {
 		pci_disable_msi(dev->dev);
 		return -EIO;
@@ -375,7 +375,7 @@ static int assigned_device_enable_host_msix(struct kvm *kvm,
 	for (i = 0; i < dev->entries_nr; i++) {
 		r = request_threaded_irq(dev->host_msix_entries[i].vector,
 					 NULL, kvm_assigned_dev_thread_msix,
-					 0, dev->irq_name, dev);
+					 IRQF_ONESHOT, dev->irq_name, dev);
 		if (r)
 			goto err;
 	}
#1 lore raw
Quoted 3 lines · Alex Williamson

The kernel no longer allows us to pass NULL for a hard interrupt handler without IRQF_ONESHOT. Should have been using this flag anyway.

This make the IRQ handling tail a bit slower (due to irq_finalize_oneshot). MSIs are edge-triggered, so there was no need for masking in theory. Hmm, can't we trust the information that an IRQ grabbed here is really a MSI type?

Jan

Quoted 32 lines

---

 virt/kvm/assigned-dev.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
virt/kvm/assigned-dev.c +2 -2
index 01f572c..e804d14 100644
@@ -347,7 +347,7 @@ static int assigned_device_enable_host_msi(struct kvm *kvm,
 
 	dev->host_irq = dev->dev->irq;
 	if (request_threaded_irq(dev->host_irq, NULL,
-				 kvm_assigned_dev_thread_msi, 0,
+				 kvm_assigned_dev_thread_msi, IRQF_ONESHOT,
 				 dev->irq_name, dev)) {
 		pci_disable_msi(dev->dev);
 		return -EIO;
@@ -375,7 +375,7 @@ static int assigned_device_enable_host_msix(struct kvm *kvm,
 	for (i = 0; i < dev->entries_nr; i++) {
 		r = request_threaded_irq(dev->host_msix_entries[i].vector,
 					 NULL, kvm_assigned_dev_thread_msix,
-					 0, dev->irq_name, dev);
+					 IRQF_ONESHOT, dev->irq_name, dev);
 		if (r)
 			goto err;
 	}
Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#2 lore raw
Quoted 8 lines · Jan Kiszka
Quoted 3 lines · Alex Williamson

The kernel no longer allows us to pass NULL for a hard interrupt handler without IRQF_ONESHOT. Should have been using this flag anyway.

This make the IRQ handling tail a bit slower (due to irq_finalize_oneshot). MSIs are edge-triggered, so there was no need for masking in theory.

Aren't these asynchronous since we can theoretically do irq_finalize_oneshot while the guest is servicing the device?

Quoted 2 lines

Hmm, can't we trust the information that an IRQ
grabbed here is really a MSI type?

Apparently not, comment added with this check (1c6c6952):

* The interrupt was requested with handler = NULL, so
* we use the default primary handler for it. But it
* does not have the oneshot flag set. In combination
* with level interrupts this is deadly, because the
* default primary handler just wakes the thread, then
* the irq lines is reenabled, but the device still
* has the level irq asserted. Rinse and repeat.... *
* While this works for edge type interrupts, we play
* it safe and reject unconditionally because we can't
* say for sure which type this interrupt really
* has. The type flags are unreliable as the
* underlying chip implementation can override them.

Thanks,

Alex

Quoted 33 lines
Quoted 32 lines

---

 virt/kvm/assigned-dev.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
virt/kvm/assigned-dev.c +2 -2
index 01f572c..e804d14 100644
@@ -347,7 +347,7 @@ static int assigned_device_enable_host_msi(struct kvm *kvm,
 
 	dev->host_irq = dev->dev->irq;
 	if (request_threaded_irq(dev->host_irq, NULL,
-				 kvm_assigned_dev_thread_msi, 0,
+				 kvm_assigned_dev_thread_msi, IRQF_ONESHOT,
 				 dev->irq_name, dev)) {
 		pci_disable_msi(dev->dev);
 		return -EIO;
@@ -375,7 +375,7 @@ static int assigned_device_enable_host_msix(struct kvm *kvm,
 	for (i = 0; i < dev->entries_nr; i++) {
 		r = request_threaded_irq(dev->host_msix_entries[i].vector,
 					 NULL, kvm_assigned_dev_thread_msix,
-					 0, dev->irq_name, dev);
+					 IRQF_ONESHOT, dev->irq_name, dev);
 		if (r)
 			goto err;
 	}
#3 lore raw
Quoted 6 lines · Alex Williamson
Quoted 5 lines · Jan Kiszka
Quoted 3 lines · Alex Williamson

The kernel no longer allows us to pass NULL for a hard interrupt handler without IRQF_ONESHOT. Should have been using this flag anyway.

Quoted 6 lines
Quoted 3 lines

This make the IRQ handling tail a bit slower (due to irq_finalize_oneshot). MSIs are edge-triggered, so there was no need for masking in theory.

Aren't these asynchronous since we can theoretically do irq_finalize_oneshot while the guest is servicing the device?

If it runs on a different CPU. But usually it's more efficient to have handler and user on the same CPU. And this work has to be processed somewhere.

Quoted 7 lines
Quoted 3 lines

Hmm, can't we trust the information that an IRQ
grabbed here is really a MSI type?

Apparently not, comment added with this check (1c6c6952):

Quoted 13 lines

* The interrupt was requested with handler = NULL, so
* we use the default primary handler for it. But it
* does not have the oneshot flag set. In combination
* with level interrupts this is deadly, because the
* default primary handler just wakes the thread, then
* the irq lines is reenabled, but the device still
* has the level irq asserted. Rinse and repeat.... *
* While this works for edge type interrupts, we play
* it safe and reject unconditionally because we can't
* say for sure which type this interrupt really
* has. The type flags are unreliable as the
* underlying chip implementation can override them.

I was talking about KVM here: Can't the KVM device assignment code ensure that only MSIs are registered as such so that the above concerns no longer apply?

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#4 lore raw
Quoted 11 lines · Jan Kiszka
Quoted 10 lines · Alex Williamson
Quoted 8 lines · Jan Kiszka
Quoted 3 lines · Alex Williamson

The kernel no longer allows us to pass NULL for a hard interrupt handler without IRQF_ONESHOT. Should have been using this flag anyway.

This make the IRQ handling tail a bit slower (due to irq_finalize_oneshot). MSIs are edge-triggered, so there was no need for masking in theory.

Quoted 6 lines
Quoted 2 lines

Aren't these asynchronous since we can theoretically do irq_finalize_oneshot while the guest is servicing the device?

If it runs on a different CPU. But usually it's more efficient to have handler and user on the same CPU. And this work has to be processed somewhere.

Yes we need more CPUs and it's better to not do work if we don't have to. I don't want to go off on too much of a tangent, but I'm not entirely convinced that host to guest handler locality is all that important. There's just not enough touched by the host handler to make much difference. Guest handler and guest user locality is obviously important, just as it is on bare metal.

Quoted 20 lines
Quoted 6 lines
Quoted 2 lines

Hmm, can't we trust the information that an IRQ
grabbed here is really a MSI type?

Apparently not, comment added with this check (1c6c6952):

Quoted 13 lines

* The interrupt was requested with handler = NULL, so
* we use the default primary handler for it. But it
* does not have the oneshot flag set. In combination
* with level interrupts this is deadly, because the
* default primary handler just wakes the thread, then
* the irq lines is reenabled, but the device still
* has the level irq asserted. Rinse and repeat.... *
* While this works for edge type interrupts, we play
* it safe and reject unconditionally because we can't
* say for sure which type this interrupt really
* has. The type flags are unreliable as the
* underlying chip implementation can override them.

Quoted 3 lines

I was talking about KVM here: Can't the KVM device assignment code ensure that only MSIs are registered as such so that the above concerns no longer apply?

Is that making assumptions about how the chipset handles an MSI? Are you suggesting we need a request_edge_threaded_only_irq() API? Thanks,

Alex

#5 lore raw
Quoted 20 lines · Alex Williamson
Quoted 20 lines
Quoted 19 lines
Quoted 2 lines

Hmm, can't we trust the information that an IRQ
grabbed here is really a MSI type?

Apparently not, comment added with this check (1c6c6952):

* The interrupt was requested with handler = NULL, so
* we use the default primary handler for it. But it
* does not have the oneshot flag set. In combination
* with level interrupts this is deadly, because the
* default primary handler just wakes the thread, then
* the irq lines is reenabled, but the device still
* has the level irq asserted. Rinse and repeat.... *
* While this works for edge type interrupts, we play
* it safe and reject unconditionally because we can't
* say for sure which type this interrupt really
* has. The type flags are unreliable as the
* underlying chip implementation can override them.

Quoted 5 lines
Quoted 3 lines

I was talking about KVM here: Can't the KVM device assignment code ensure that only MSIs are registered as such so that the above concerns no longer apply?

Is that making assumptions about how the chipset handles an MSI? Are

I suppose the nature of MSIs removes any need for assumptions about the handling.

Quoted 1 line

you suggesting we need a request_edge_threaded_only_irq() API? Thanks,

I'm just wondering if that restriction for threaded IRQs is really necessary for all use cases we have. Threaded MSIs do not appear to me like have to be handled that conservatively, but maybe I'm missing some detail.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#6 lore raw
Quoted 8 lines · Jan Kiszka
Quoted 2 lines

you suggesting we need a request_edge_threaded_only_irq() API? Thanks,

I'm just wondering if that restriction for threaded IRQs is really necessary for all use cases we have. Threaded MSIs do not appear to me like have to be handled that conservatively, but maybe I'm missing some detail.

btw, I'm hoping we can unthread assigned MSIs. If the delivery is unicast, we can precalculate everything and all the handler has to do is set the IRR, KVM_REQ_EVENT, and kick the vcpu. All of these can be done from interrupt context with just RCU locking.

Signature · 1 line
error compiling committee.c: too many arguments to function
#7 lore raw
Quoted 10 lines · Avi Kivity
Quoted 8 lines · Jan Kiszka
Quoted 2 lines

you suggesting we need a request_edge_threaded_only_irq() API? Thanks,

I'm just wondering if that restriction for threaded IRQs is really necessary for all use cases we have. Threaded MSIs do not appear to me like have to be handled that conservatively, but maybe I'm missing some detail.

Quoted 4 lines

btw, I'm hoping we can unthread assigned MSIs. If the delivery is unicast, we can precalculate everything and all the handler has to do is set the IRR, KVM_REQ_EVENT, and kick the vcpu. All of these can be done from interrupt context with just RCU locking.

There is really no need to run MSI/MSI-X interrupts threaded for KVM. I'm running the patch below for quite some time and it works like a charm.

Thanks,

tglx
----

virt/kvm/assigned-dev.c +7 -8
Index: linux-2.6/virt/kvm/assigned-dev.c
===================================================================
@@ -105,7 +105,7 @@ static irqreturn_t kvm_assigned_dev_thre
 }
 
 #ifdef __KVM_HAVE_MSI
-static irqreturn_t kvm_assigned_dev_thread_msi(int irq, void *dev_id)
+static irqreturn_t kvm_assigned_dev_msi_handler(int irq, void *dev_id)
 {
 	struct kvm_assigned_dev_kernel *assigned_dev = dev_id;
 
@@ -117,7 +117,7 @@ static irqreturn_t kvm_assigned_dev_thre
 #endif
 
 #ifdef __KVM_HAVE_MSIX
-static irqreturn_t kvm_assigned_dev_thread_msix(int irq, void *dev_id)
+static irqreturn_t kvm_assigned_dev_msix_handler(int irq, void *dev_id)
 {
 	struct kvm_assigned_dev_kernel *assigned_dev = dev_id;
 	int index = find_index_from_host_irq(assigned_dev, irq);
@@ -346,9 +346,8 @@ static int assigned_device_enable_host_m
 	}
 
 	dev->host_irq = dev->dev->irq;
-	if (request_threaded_irq(dev->host_irq, NULL,
-				 kvm_assigned_dev_thread_msi, 0,
-				 dev->irq_name, dev)) {
+	if (request_irq(dev->host_irq, kvm_assigned_dev_msi_handler, 0,
+			dev->irq_name, dev)) {
 		pci_disable_msi(dev->dev);
 		return -EIO;
 	}
@@ -373,9 +372,9 @@ static int assigned_device_enable_host_m
 		return r;
 
 	for (i = 0; i < dev->entries_nr; i++) {
-		r = request_threaded_irq(dev->host_msix_entries[i].vector,
-					 NULL, kvm_assigned_dev_thread_msix,
-					 0, dev->irq_name, dev);
+		r = request_irq(dev->host_msix_entries[i].vector,
+				kvm_assigned_dev_msix_handler, 0,
+				dev->irq_name, dev);
 		if (r)
 			goto err;
 	}
#8 lore raw
Quoted 62 lines · Thomas Gleixner
Quoted 10 lines · Avi Kivity
Quoted 8 lines · Jan Kiszka
Quoted 2 lines

you suggesting we need a request_edge_threaded_only_irq() API? Thanks,

I'm just wondering if that restriction for threaded IRQs is really necessary for all use cases we have. Threaded MSIs do not appear to me like have to be handled that conservatively, but maybe I'm missing some detail.

Quoted 4 lines

btw, I'm hoping we can unthread assigned MSIs. If the delivery is unicast, we can precalculate everything and all the handler has to do is set the IRR, KVM_REQ_EVENT, and kick the vcpu. All of these can be done from interrupt context with just RCU locking.

There is really no need to run MSI/MSI-X interrupts threaded for KVM. I'm running the patch below for quite some time and it works like a charm.

Thanks,

tglx
----
Index: linux-2.6/virt/kvm/assigned-dev.c
===================================================================

virt/kvm/assigned-dev.c +4 -5
@@ -105,7 +105,7 @@ static irqreturn_t kvm_assigned_dev_thre
 }
 
 #ifdef __KVM_HAVE_MSI
-static irqreturn_t kvm_assigned_dev_thread_msi(int irq, void *dev_id)
+static irqreturn_t kvm_assigned_dev_msi_handler(int irq, void *dev_id)
 {
 	struct kvm_assigned_dev_kernel *assigned_dev = dev_id;
 
@@ -117,7 +117,7 @@ static irqreturn_t kvm_assigned_dev_thre
 #endif
 
 #ifdef __KVM_HAVE_MSIX
-static irqreturn_t kvm_assigned_dev_thread_msix(int irq, void *dev_id)
+static irqreturn_t kvm_assigned_dev_msix_handler(int irq, void *dev_id)
 {
 	struct kvm_assigned_dev_kernel *assigned_dev = dev_id;
 	int index = find_index_from_host_irq(assigned_dev, irq);
@@ -346,9 +346,8 @@ static int assigned_device_enable_host_m
 	}
 
 	dev->host_irq = dev->dev->irq;
-	if (request_threaded_irq(dev->host_irq, NULL,
-				 kvm_assigned_dev_thread_msi, 0,
-				 dev->irq_name, dev)) {
+	if (request_irq(dev->host_irq, kvm_assigned_dev_msi_handler, 0,
+			dev->irq_name, dev)) {
 		pci_disable_msi(dev->dev);
 		return -EIO;
 	}
@@ -373,9 +372,9 @@ static int assigned_device_enable_host_m
 		return r;
 

virt/kvm/assigned-dev.c static int assigned_device_enable_host_m · line 380

Quoted 10 lines
virt/kvm/assigned-dev.c +3 -3
 	for (i = 0; i < dev->entries_nr; i++) {
-		r = request_threaded_irq(dev->host_msix_entries[i].vector,
-					 NULL, kvm_assigned_dev_thread_msix,
-					 0, dev->irq_name, dev);
+		r = request_irq(dev->host_msix_entries[i].vector,
+				kvm_assigned_dev_msix_handler, 0,
+				dev->irq_name, dev);
 		if (r)
 			goto err;
 	}

This may work in practice but has two conceptual problems:
- we do not want to run a potential broadcast to all VCPUs to run in a host IRQ handler
- crazy user space could have configured the route to end up in the PIC or IOAPIC, and both are not hard-IRQ safe (this should probably be caught on setup)

So this shortcut requires some checks before being applied to a specific MSI/MSI-X vector.

Taking KVM aside, my general question remains if threaded MSI handlers of all devices really need to apply IRQF_ONESHOT though they should have no use for it.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#9 lore raw
Quoted 8 lines · Jan Kiszka

On 2012-06-04 13:21, Thomas Gleixner wrote:
So this shortcut requires some checks before being applied to a specific MSI/MSI-X vector.

Taking KVM aside, my general question remains if threaded MSI handlers of all devices really need to apply IRQF_ONESHOT though they should have no use for it.

In theory no, but we had more than one incident, where threaded irqs w/o a primary handler and w/o IRQF_ONEHSOT lead to full system starvation. Linus requested this sanity check and I think it's sane and required.

In fact it's a non issue for MSI. MSI uses handle_edge_irq which does not mask the interrupt. IRQF_ONESHOT is a noop for that flow handler.

Thanks,

tglx

#10 lore raw
Quoted 6 lines · Thomas Gleixner
Quoted 5 lines · Jan Kiszka

On 2012-06-04 13:21, Thomas Gleixner wrote:
So this shortcut requires some checks before being applied to a specific MSI/MSI-X vector.

Quoted 8 lines
Quoted 3 lines

Taking KVM aside, my general question remains if threaded MSI handlers of all devices really need to apply IRQF_ONESHOT though they should have no use for it.

In theory no, but we had more than one incident, where threaded irqs w/o a primary handler and w/o IRQF_ONEHSOT lead to full system starvation. Linus requested this sanity check and I think it's sane and required.

OK.

Quoted 3 lines

In fact it's a non issue for MSI. MSI uses handle_edge_irq which does not mask the interrupt. IRQF_ONESHOT is a noop for that flow handler.

Isn't irq_finalize_oneshot processes for all flows?

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#11 lore raw
Quoted 16 lines · Jan Kiszka
Quoted 6 lines · Thomas Gleixner
Quoted 5 lines · Jan Kiszka

On 2012-06-04 13:21, Thomas Gleixner wrote:
So this shortcut requires some checks before being applied to a specific MSI/MSI-X vector.

Quoted 8 lines
Quoted 3 lines

Taking KVM aside, my general question remains if threaded MSI handlers of all devices really need to apply IRQF_ONESHOT though they should have no use for it.

In theory no, but we had more than one incident, where threaded irqs w/o a primary handler and w/o IRQF_ONEHSOT lead to full system starvation. Linus requested this sanity check and I think it's sane and required.

Quoted 7 lines

OK.

Quoted 3 lines

In fact it's a non issue for MSI. MSI uses handle_edge_irq which does not mask the interrupt. IRQF_ONESHOT is a noop for that flow handler.

Isn't irq_finalize_oneshot processes for all flows?

Right, forgot about that. The only way we can avoid that, is that we get a hint from the underlying irq chip/ handler setup with an extra flag to tell the core, that it's safe to avoid the ONESHOT/finalize magic.

Thanks,

tglx

#12 lore raw
In reply to Jan Kiszka
Quoted 81 lines · Jan Kiszka
Quoted 62 lines · Thomas Gleixner
Quoted 10 lines · Avi Kivity
Quoted 8 lines · Jan Kiszka
Quoted 2 lines

you suggesting we need a request_edge_threaded_only_irq() API? Thanks,

I'm just wondering if that restriction for threaded IRQs is really necessary for all use cases we have. Threaded MSIs do not appear to me like have to be handled that conservatively, but maybe I'm missing some detail.

Quoted 4 lines

btw, I'm hoping we can unthread assigned MSIs. If the delivery is unicast, we can precalculate everything and all the handler has to do is set the IRR, KVM_REQ_EVENT, and kick the vcpu. All of these can be done from interrupt context with just RCU locking.

There is really no need to run MSI/MSI-X interrupts threaded for KVM. I'm running the patch below for quite some time and it works like a charm.

Thanks,

tglx
----
Index: linux-2.6/virt/kvm/assigned-dev.c
===================================================================

virt/kvm/assigned-dev.c +4 -5
@@ -105,7 +105,7 @@ static irqreturn_t kvm_assigned_dev_thre
 }
 
 #ifdef __KVM_HAVE_MSI
-static irqreturn_t kvm_assigned_dev_thread_msi(int irq, void *dev_id)
+static irqreturn_t kvm_assigned_dev_msi_handler(int irq, void *dev_id)
 {
 	struct kvm_assigned_dev_kernel *assigned_dev = dev_id;
 
@@ -117,7 +117,7 @@ static irqreturn_t kvm_assigned_dev_thre
 #endif
 
 #ifdef __KVM_HAVE_MSIX
-static irqreturn_t kvm_assigned_dev_thread_msix(int irq, void *dev_id)
+static irqreturn_t kvm_assigned_dev_msix_handler(int irq, void *dev_id)
 {
 	struct kvm_assigned_dev_kernel *assigned_dev = dev_id;
 	int index = find_index_from_host_irq(assigned_dev, irq);
@@ -346,9 +346,8 @@ static int assigned_device_enable_host_m
 	}
 
 	dev->host_irq = dev->dev->irq;
-	if (request_threaded_irq(dev->host_irq, NULL,
-				 kvm_assigned_dev_thread_msi, 0,
-				 dev->irq_name, dev)) {
+	if (request_irq(dev->host_irq, kvm_assigned_dev_msi_handler, 0,
+			dev->irq_name, dev)) {
 		pci_disable_msi(dev->dev);
 		return -EIO;
 	}
@@ -373,9 +372,9 @@ static int assigned_device_enable_host_m
 		return r;
 

virt/kvm/assigned-dev.c static int assigned_device_enable_host_m · line 380

Quoted 10 lines
virt/kvm/assigned-dev.c +3 -3
 	for (i = 0; i < dev->entries_nr; i++) {
-		r = request_threaded_irq(dev->host_msix_entries[i].vector,
-					 NULL, kvm_assigned_dev_thread_msix,
-					 0, dev->irq_name, dev);
+		r = request_irq(dev->host_msix_entries[i].vector,
+				kvm_assigned_dev_msix_handler, 0,
+				dev->irq_name, dev);
 		if (r)
 			goto err;
 	}

This may work in practice but has two conceptual problems:
- we do not want to run a potential broadcast to all VCPUs to run in a host IRQ handler
- crazy user space could have configured the route to end up in the PIC or IOAPIC, and both are not hard-IRQ safe (this should probably be caught on setup)

Quoted 2 lines

So this shortcut requires some checks before being applied to a specific MSI/MSI-X vector.

I did this in the past:
https://lkml.org/lkml/2012/1/18/287

Have no hw to test this atm but if there are any takers wanting to play with it I can update and post.

Signature · 1 line
mst
#13 lore raw
Quoted 82 lines · Michael S. Tsirkin
Quoted 81 lines · Jan Kiszka
Quoted 62 lines · Thomas Gleixner
Quoted 10 lines · Avi Kivity
Quoted 8 lines · Jan Kiszka
Quoted 2 lines

you suggesting we need a request_edge_threaded_only_irq() API? Thanks,

I'm just wondering if that restriction for threaded IRQs is really necessary for all use cases we have. Threaded MSIs do not appear to me like have to be handled that conservatively, but maybe I'm missing some detail.

Quoted 4 lines

btw, I'm hoping we can unthread assigned MSIs. If the delivery is unicast, we can precalculate everything and all the handler has to do is set the IRR, KVM_REQ_EVENT, and kick the vcpu. All of these can be done from interrupt context with just RCU locking.

There is really no need to run MSI/MSI-X interrupts threaded for KVM. I'm running the patch below for quite some time and it works like a charm.

Thanks,

tglx
----
Index: linux-2.6/virt/kvm/assigned-dev.c
===================================================================

virt/kvm/assigned-dev.c +4 -5
@@ -105,7 +105,7 @@ static irqreturn_t kvm_assigned_dev_thre
 }
 
 #ifdef __KVM_HAVE_MSI
-static irqreturn_t kvm_assigned_dev_thread_msi(int irq, void *dev_id)
+static irqreturn_t kvm_assigned_dev_msi_handler(int irq, void *dev_id)
 {
 	struct kvm_assigned_dev_kernel *assigned_dev = dev_id;
 
@@ -117,7 +117,7 @@ static irqreturn_t kvm_assigned_dev_thre
 #endif
 
 #ifdef __KVM_HAVE_MSIX
-static irqreturn_t kvm_assigned_dev_thread_msix(int irq, void *dev_id)
+static irqreturn_t kvm_assigned_dev_msix_handler(int irq, void *dev_id)
 {
 	struct kvm_assigned_dev_kernel *assigned_dev = dev_id;
 	int index = find_index_from_host_irq(assigned_dev, irq);
@@ -346,9 +346,8 @@ static int assigned_device_enable_host_m
 	}
 
 	dev->host_irq = dev->dev->irq;
-	if (request_threaded_irq(dev->host_irq, NULL,
-				 kvm_assigned_dev_thread_msi, 0,
-				 dev->irq_name, dev)) {
+	if (request_irq(dev->host_irq, kvm_assigned_dev_msi_handler, 0,
+			dev->irq_name, dev)) {
 		pci_disable_msi(dev->dev);
 		return -EIO;
 	}
@@ -373,9 +372,9 @@ static int assigned_device_enable_host_m
 		return r;
 

virt/kvm/assigned-dev.c static int assigned_device_enable_host_m · line 380

Quoted 10 lines
virt/kvm/assigned-dev.c +3 -3
 	for (i = 0; i < dev->entries_nr; i++) {
-		r = request_threaded_irq(dev->host_msix_entries[i].vector,
-					 NULL, kvm_assigned_dev_thread_msix,
-					 0, dev->irq_name, dev);
+		r = request_irq(dev->host_msix_entries[i].vector,
+				kvm_assigned_dev_msix_handler, 0,
+				dev->irq_name, dev);
 		if (r)
 			goto err;
 	}

This may work in practice but has two conceptual problems:
- we do not want to run a potential broadcast to all VCPUs to run in a host IRQ handler
- crazy user space could have configured the route to end up in the PIC or IOAPIC, and both are not hard-IRQ safe (this should probably be caught on setup)

Quoted 8 lines
Quoted 2 lines

So this shortcut requires some checks before being applied to a specific MSI/MSI-X vector.

I did this in the past:
https://lkml.org/lkml/2012/1/18/287

Have no hw to test this atm but if there are any takers wanting to play with it I can update and post.

Just add check that allow only unicasts, and this should be fine.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#14 lore raw
Quoted 89 lines · Jan Kiszka
Quoted 88 lines · Michael S. Tsirkin
Quoted 81 lines · Jan Kiszka
Quoted 62 lines · Thomas Gleixner
Quoted 10 lines · Avi Kivity
Quoted 8 lines · Jan Kiszka
Quoted 2 lines

you suggesting we need a request_edge_threaded_only_irq() API? Thanks,

I'm just wondering if that restriction for threaded IRQs is really necessary for all use cases we have. Threaded MSIs do not appear to me like have to be handled that conservatively, but maybe I'm missing some detail.

Quoted 4 lines

btw, I'm hoping we can unthread assigned MSIs. If the delivery is unicast, we can precalculate everything and all the handler has to do is set the IRR, KVM_REQ_EVENT, and kick the vcpu. All of these can be done from interrupt context with just RCU locking.

There is really no need to run MSI/MSI-X interrupts threaded for KVM. I'm running the patch below for quite some time and it works like a charm.

Thanks,

tglx
----
Index: linux-2.6/virt/kvm/assigned-dev.c
===================================================================

virt/kvm/assigned-dev.c +4 -5
@@ -105,7 +105,7 @@ static irqreturn_t kvm_assigned_dev_thre
 }
 
 #ifdef __KVM_HAVE_MSI
-static irqreturn_t kvm_assigned_dev_thread_msi(int irq, void *dev_id)
+static irqreturn_t kvm_assigned_dev_msi_handler(int irq, void *dev_id)
 {
 	struct kvm_assigned_dev_kernel *assigned_dev = dev_id;
 
@@ -117,7 +117,7 @@ static irqreturn_t kvm_assigned_dev_thre
 #endif
 
 #ifdef __KVM_HAVE_MSIX
-static irqreturn_t kvm_assigned_dev_thread_msix(int irq, void *dev_id)
+static irqreturn_t kvm_assigned_dev_msix_handler(int irq, void *dev_id)
 {
 	struct kvm_assigned_dev_kernel *assigned_dev = dev_id;
 	int index = find_index_from_host_irq(assigned_dev, irq);
@@ -346,9 +346,8 @@ static int assigned_device_enable_host_m
 	}
 
 	dev->host_irq = dev->dev->irq;
-	if (request_threaded_irq(dev->host_irq, NULL,
-				 kvm_assigned_dev_thread_msi, 0,
-				 dev->irq_name, dev)) {
+	if (request_irq(dev->host_irq, kvm_assigned_dev_msi_handler, 0,
+			dev->irq_name, dev)) {
 		pci_disable_msi(dev->dev);
 		return -EIO;
 	}
@@ -373,9 +372,9 @@ static int assigned_device_enable_host_m
 		return r;
 

virt/kvm/assigned-dev.c static int assigned_device_enable_host_m · line 380

Quoted 10 lines
virt/kvm/assigned-dev.c +3 -3
 	for (i = 0; i < dev->entries_nr; i++) {
-		r = request_threaded_irq(dev->host_msix_entries[i].vector,
-					 NULL, kvm_assigned_dev_thread_msix,
-					 0, dev->irq_name, dev);
+		r = request_irq(dev->host_msix_entries[i].vector,
+				kvm_assigned_dev_msix_handler, 0,
+				dev->irq_name, dev);
 		if (r)
 			goto err;
 	}

This may work in practice but has two conceptual problems:
- we do not want to run a potential broadcast to all VCPUs to run in a host IRQ handler
- crazy user space could have configured the route to end up in the PIC or IOAPIC, and both are not hard-IRQ safe (this should probably be caught on setup)

Quoted 2 lines

So this shortcut requires some checks before being applied to a specific MSI/MSI-X vector.

I did this in the past:
https://lkml.org/lkml/2012/1/18/287

Quoted 6 lines
Quoted 2 lines

Have no hw to test this atm but if there are any takers wanting to play with it I can update and post.

Just add check that allow only unicasts, and this should be fine.

Jan

If I code it up you can test it?

Quoted 7 lines
Signature · 6 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#15 lore raw
Quoted 90 lines · Michael S. Tsirkin
Quoted 89 lines · Jan Kiszka
Quoted 88 lines · Michael S. Tsirkin
Quoted 81 lines · Jan Kiszka
Quoted 62 lines · Thomas Gleixner
Quoted 10 lines · Avi Kivity
Quoted 8 lines · Jan Kiszka
Quoted 2 lines

you suggesting we need a request_edge_threaded_only_irq() API? Thanks,

I'm just wondering if that restriction for threaded IRQs is really necessary for all use cases we have. Threaded MSIs do not appear to me like have to be handled that conservatively, but maybe I'm missing some detail.

Quoted 4 lines

btw, I'm hoping we can unthread assigned MSIs. If the delivery is unicast, we can precalculate everything and all the handler has to do is set the IRR, KVM_REQ_EVENT, and kick the vcpu. All of these can be done from interrupt context with just RCU locking.

There is really no need to run MSI/MSI-X interrupts threaded for KVM. I'm running the patch below for quite some time and it works like a charm.

Thanks,

tglx
----
Index: linux-2.6/virt/kvm/assigned-dev.c
===================================================================

virt/kvm/assigned-dev.c +4 -5
@@ -105,7 +105,7 @@ static irqreturn_t kvm_assigned_dev_thre
 }
 
 #ifdef __KVM_HAVE_MSI
-static irqreturn_t kvm_assigned_dev_thread_msi(int irq, void *dev_id)
+static irqreturn_t kvm_assigned_dev_msi_handler(int irq, void *dev_id)
 {
 	struct kvm_assigned_dev_kernel *assigned_dev = dev_id;
 
@@ -117,7 +117,7 @@ static irqreturn_t kvm_assigned_dev_thre
 #endif
 
 #ifdef __KVM_HAVE_MSIX
-static irqreturn_t kvm_assigned_dev_thread_msix(int irq, void *dev_id)
+static irqreturn_t kvm_assigned_dev_msix_handler(int irq, void *dev_id)
 {
 	struct kvm_assigned_dev_kernel *assigned_dev = dev_id;
 	int index = find_index_from_host_irq(assigned_dev, irq);
@@ -346,9 +346,8 @@ static int assigned_device_enable_host_m
 	}
 
 	dev->host_irq = dev->dev->irq;
-	if (request_threaded_irq(dev->host_irq, NULL,
-				 kvm_assigned_dev_thread_msi, 0,
-				 dev->irq_name, dev)) {
+	if (request_irq(dev->host_irq, kvm_assigned_dev_msi_handler, 0,
+			dev->irq_name, dev)) {
 		pci_disable_msi(dev->dev);
 		return -EIO;
 	}
@@ -373,9 +372,9 @@ static int assigned_device_enable_host_m
 		return r;
 

virt/kvm/assigned-dev.c static int assigned_device_enable_host_m · line 380

Quoted 10 lines
virt/kvm/assigned-dev.c +3 -3
 	for (i = 0; i < dev->entries_nr; i++) {
-		r = request_threaded_irq(dev->host_msix_entries[i].vector,
-					 NULL, kvm_assigned_dev_thread_msix,
-					 0, dev->irq_name, dev);
+		r = request_irq(dev->host_msix_entries[i].vector,
+				kvm_assigned_dev_msix_handler, 0,
+				dev->irq_name, dev);
 		if (r)
 			goto err;
 	}

This may work in practice but has two conceptual problems:
- we do not want to run a potential broadcast to all VCPUs to run in a host IRQ handler
- crazy user space could have configured the route to end up in the PIC or IOAPIC, and both are not hard-IRQ safe (this should probably be caught on setup)

Quoted 2 lines

So this shortcut requires some checks before being applied to a specific MSI/MSI-X vector.

I did this in the past:
https://lkml.org/lkml/2012/1/18/287

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Have no hw to test this atm but if there are any takers wanting to play with it I can update and post.

Just add check that allow only unicasts, and this should be fine.

Jan

If I code it up you can test it?

Yep, no problem.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#16 lore raw
In reply to Jan Kiszka
Quoted 26 lines · Jan Kiszka
Quoted 25 lines · Thomas Gleixner
Quoted 10 lines · Avi Kivity
Quoted 8 lines · Jan Kiszka
Quoted 2 lines

you suggesting we need a request_edge_threaded_only_irq() API? Thanks,

I'm just wondering if that restriction for threaded IRQs is really necessary for all use cases we have. Threaded MSIs do not appear to me like have to be handled that conservatively, but maybe I'm missing some detail.

Quoted 4 lines

btw, I'm hoping we can unthread assigned MSIs. If the delivery is unicast, we can precalculate everything and all the handler has to do is set the IRR, KVM_REQ_EVENT, and kick the vcpu. All of these can be done from interrupt context with just RCU locking.

There is really no need to run MSI/MSI-X interrupts threaded for KVM. I'm running the patch below for quite some time and it works like a charm.

Thanks,

tglx
----

....

Quoted 4 lines

This may work in practice but has two conceptual problems:
- we do not want to run a potential broadcast to all VCPUs to run in a host IRQ handler

I'm not sure why this one is a problem: injecting an interrupt once you know the vcpu seems really cheap.
It's true that scanning vcpus might take a bit more time when there are lots of them but it's a single
linear scan that we do anyway.

And we also inject msi from irqfd callback with interrupts disabled which seems equivalent.

Pls correct me if I'm wrong.

Quoted 3 lines

- crazy user space could have configured the route to end up in the PIC or IOAPIC, and both are not hard-IRQ safe (this should probably be caught on setup)

Yes this needs to be fixed up.

Quoted 18 lines

So this shortcut requires some checks before being applied to a specific MSI/MSI-X vector.

Taking KVM aside, my general question remains if threaded MSI handlers of all devices really need to apply IRQF_ONESHOT though they should have no use for it.

Jan

Signature · 7 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
#17 lore raw
Quoted 36 lines · Michael S. Tsirkin
Quoted 26 lines · Jan Kiszka
Quoted 25 lines · Thomas Gleixner
Quoted 10 lines · Avi Kivity
Quoted 8 lines · Jan Kiszka
Quoted 2 lines

you suggesting we need a request_edge_threaded_only_irq() API? Thanks,

I'm just wondering if that restriction for threaded IRQs is really necessary for all use cases we have. Threaded MSIs do not appear to me like have to be handled that conservatively, but maybe I'm missing some detail.

Quoted 4 lines

btw, I'm hoping we can unthread assigned MSIs. If the delivery is unicast, we can precalculate everything and all the handler has to do is set the IRR, KVM_REQ_EVENT, and kick the vcpu. All of these can be done from interrupt context with just RCU locking.

There is really no need to run MSI/MSI-X interrupts threaded for KVM. I'm running the patch below for quite some time and it works like a charm.

Thanks,

tglx
----

....

Quoted 4 lines

This may work in practice but has two conceptual problems:
- we do not want to run a potential broadcast to all VCPUs to run in a host IRQ handler

Quoted 8 lines

I'm not sure why this one is a problem: injecting an interrupt once you know the vcpu seems really cheap.
It's true that scanning vcpus might take a bit more time when there are lots of them but it's a single
linear scan that we do anyway.

And we also inject msi from irqfd callback with interrupts disabled which seems equivalent.

Interesting, need to check.

Quoted 2 lines

Pls correct me if I'm wrong.

Well, IIRC, the "don't loop over all vcpus with IRQs or preemption disabled" was one argument against direct legacy interrupt injection as well. That's what I kept in mind from those discussions. Maybe Avi can comment on the current position.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#18 lore raw
Quoted 8 lines · Jan Kiszka
Quoted 3 lines

Pls correct me if I'm wrong.

Well, IIRC, the "don't loop over all vcpus with IRQs or preemption disabled" was one argument against direct legacy interrupt injection as well. That's what I kept in mind from those discussions. Maybe Avi can comment on the current position.

It's still my position.

IMO we need something like struct gfn_to_hva_cache for interrupts. If it's in the cache, we fast-path it from the interrupt handler. If not, fall back to a workqueue and let it refill the cache.

Signature · 1 line
error compiling committee.c: too many arguments to function
#19 lore raw
Quoted 10 lines · Avi Kivity
Quoted 8 lines · Jan Kiszka
Quoted 3 lines

Pls correct me if I'm wrong.

Well, IIRC, the "don't loop over all vcpus with IRQs or preemption disabled" was one argument against direct legacy interrupt injection as well. That's what I kept in mind from those discussions. Maybe Avi can comment on the current position.

Quoted 5 lines

It's still my position.

IMO we need something like struct gfn_to_hva_cache for interrupts. If it's in the cache, we fast-path it from the interrupt handler. If not, fall back to a workqueue and let it refill the cache.

And you class the irqfd behaviour of injecting multicast with interrupts disabled a bug then?

Quoted 2 lines
Signature · 1 line
error compiling committee.c: too many arguments to function
#20 lore raw
Quoted 20 lines

-----Original Message-----
From: Michael S. Tsirkin [mailto:[email protected]]
Sent: Monday, June 11, 2012 6:22 PM
To: Avi Kivity
Cc: Jan Kiszka; Thomas Gleixner; Alex Williamson; [email protected]; [email protected]; [email protected]; Ren, Yongjie
Subject: Re: [PATCH] KVM: Use IRQF_ONESHOT for assigned device MSI interrupts

Quoted 10 lines · Avi Kivity
Quoted 8 lines · Jan Kiszka
Quoted 3 lines

Pls correct me if I'm wrong.

Well, IIRC, the "don't loop over all vcpus with IRQs or preemption disabled" was one argument against direct legacy interrupt injection as well. That's what I kept in mind from those discussions. Maybe Avi can comment on the current position.

Quoted 9 lines
Quoted 5 lines

It's still my position.

IMO we need something like struct gfn_to_hva_cache for interrupts. If it's in the cache, we fast-path it from the interrupt handler. If not, fall back to a workqueue and let it refill the cache.

And you class the irqfd behaviour of injecting multicast with interrupts disabled a bug then?

Hi Avi & Michael,
Any more news on this issue ?

Quoted 2 lines
Quoted 2 lines

--
error compiling committee.c: too many arguments to function

#21 lore raw
In reply to Michael S. Tsirkin
Quoted 11 lines · Michael S. Tsirkin
Quoted 10 lines · Avi Kivity
Quoted 8 lines · Jan Kiszka
Quoted 3 lines

Pls correct me if I'm wrong.

Well, IIRC, the "don't loop over all vcpus with IRQs or preemption disabled" was one argument against direct legacy interrupt injection as well. That's what I kept in mind from those discussions. Maybe Avi can comment on the current position.

Quoted 8 lines
Quoted 5 lines

It's still my position.

IMO we need something like struct gfn_to_hva_cache for interrupts. If it's in the cache, we fast-path it from the interrupt handler. If not, fall back to a workqueue and let it refill the cache.

And you class the irqfd behaviour of injecting multicast with interrupts disabled a bug then?

Yes (a minor one).

Signature · 1 line
error compiling committee.c: too many arguments to function