Skip to contentSkip to conversation

Deferred interrupt handling.

RFC Or Sagi 6 participants 20 messages Jul 18, 2007 – Jul 25, 2007 KVM

lore
Share & tools
lore

Or Sagi

author
#0 lore raw

Gentlepeople,

We're currently working on PCI device pass-through support for KVM. In this model all physical hardware access to specific devices will be performed by the VM, and not by the host.

In particular, this requires interrupt handling to be done by the guest -- The host shouldn't load the corresponding device driver or otherwise access the device. Since the host kernel is not aware of the device semantics it cannot acknowledge the interrupt at the device level.

As far as the host kernel is concerned the VM is a user level process. We require the ability to forward interrupt handling to such entities. The current kernel interrupt handling path doesn't allow deferring interrupt handling _and_ acknowledgement.

We believe that this requires changing the current interrupt path in several fashions:

0. Adding an IRQ_DEFERRED mechanism to the interrupt handling path. ISRs returning IRQ_DEFERRED will keep the interrupt masked until future acknowledge.
1. Deferred acknowledge mechanism which would acknowledge the APIC and unmask
the interrupt.

An alternative idea was suggested at
http://lists.xensource.com/archives/html/xen-devel/2007-05/msg01148.html --- which is based on reversing the polarity of the received interrupt. Once the guest acknowledges the interrupt at the device level, the polarity is reversed again by the host.

This may not be the best choice performance wise -- twice the interrupt count.

Another issue we are aware of is the effect deferred handling will have on other devices sharing the interrupt. This seems to be a problem regardless of the solution.

We wouldn't like to assume VT-d availability right now.

Any ideas ? Thoughts ?

-- Ors

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/

#1 lore raw
Quoted 4 lines

In particular, this requires interrupt handling to be done by the guest -- The host shouldn't load the corresponding device driver or otherwise access the device. Since the host kernel is not aware of the device semantics it cannot acknowledge the interrupt at the device level.

Tricky indeed.

Quoted 4 lines

As far as the host kernel is concerned the VM is a user level process. We require the ability to forward interrupt handling to such entities. The current kernel interrupt handling path doesn't allow deferring interrupt handling _and_ acknowledgement.

We don't support this model at all, and it doesn't appear to work anyway.

Quoted 3 lines

0. Adding an IRQ_DEFERRED mechanism to the interrupt handling path. ISRs returning IRQ_DEFERRED will keep the interrupt masked until future acknowledge.

Deadlock. If you get an IRQ for a guest and you block the IRQ until the guest handles it you may (eg if the IRQ is shared) get priority inversion with another interrupt source on the same line the guest requires first (eg disks and other I/O)

Quoted 3 lines

Another issue we are aware of is the effect deferred handling will have on other devices sharing the interrupt. This seems to be a problem regardless of the solution.

Yes. This is the big reason Linux has never supported this kind of model for anything but old ISA IRQ handling in vm86.

Quoted 4 lines

We wouldn't like to assume VT-d availability right now.

Any ideas ? Thoughts ?

Mask the interrupt in the main kernel, pass an event of some kind to the guest. You can describe most devices from guest to kernel in a safe form as

device, bar, offset, register size, mask, bits to set, bits to clear

(or bits to test when deciding if it is the irq source)

#19 lore raw
In reply to Or Sagi
Quoted 35 lines · Or Sagi

Gentlepeople,

We're currently working on PCI device pass-through support for KVM. In this model all physical hardware access to specific devices will be performed by the VM, and not by the host.

In particular, this requires interrupt handling to be done by the guest -- The host shouldn't load the corresponding device driver or otherwise access the device. Since the host kernel is not aware of the device semantics it cannot acknowledge the interrupt at the device level.

As far as the host kernel is concerned the VM is a user level process. We require the ability to forward interrupt handling to such entities. The current kernel interrupt handling path doesn't allow deferring interrupt handling _and_ acknowledgement.

We believe that this requires changing the current interrupt path in several fashions:

0. Adding an IRQ_DEFERRED mechanism to the interrupt handling path. ISRs returning IRQ_DEFERRED will keep the interrupt masked until future acknowledge.
1. Deferred acknowledge mechanism which would acknowledge the APIC and unmask
the interrupt.

An alternative idea was suggested at
http://lists.xensource.com/archives/html/xen-devel/2007-05/msg01148.html --- which is based on reversing the polarity of the received interrupt. Once the guest acknowledges the interrupt at the device level, the polarity is reversed again by the host.

This may not be the best choice performance wise -- twice the interrupt count.

Quoted 7 lines

Another issue we are aware of is the effect deferred handling will have on other devices sharing the interrupt. This seems to be a problem regardless of the solution.

We wouldn't like to assume VT-d availability right now.

Any ideas ? Thoughts ?

What if you have two PCI devices sharing an interrupt, unless the host OS knows about all the devices that can generate that interrupt, how it is supposed to deal with it? It can't know that it should pass in IRQ to a guest OS, unless it can determine which device actually generated the interrupt, which probably requires some knowledge of the devices involved. Should the guest be able to register as a handler of that interrupt, so that the host has to call the guest's interrupt handler everytime an interrupt occours that the guest has registered as a potential handler of? There might not be any other choice. Maybe a driver that simply registers as using the interrupt and then passes the call to the guest somehow so the guest can determine if the device generated the interrupt and handle it, and then return back to the main interrupt handler. Of course this would mean the guest has to be scheduled and run during the host's interrupt handler which might just not be an option.

Sounds tricky.

--
Len Sorensen

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/

#2 lore raw
In reply to Alan Cox
Quoted 5 lines
Quoted 1 line

In particular, this requires interrupt handling to be done by the

guest --

Quoted 1 line

The host shouldn't load the corresponding device driver or otherwise

access

Quoted 1 line

the device. Since the host kernel is not aware of the device

semantics

Quoted 6 lines

it

Quoted 1 line

cannot acknowledge the interrupt at the device level.

Tricky indeed.

Quoted 1 line

As far as the host kernel is concerned the VM is a user level

process.

Quoted 12 lines

We

Quoted 1 line

require the ability to forward interrupt handling to such entities.

The

Quoted 1 line

current kernel interrupt handling path doesn't allow deferring

interrupt

Quoted 1 line

handling _and_ acknowledgement.

We don't support this model at all, and it doesn't appear to work anyway.

Quoted 1 line

0. Adding an IRQ_DEFERRED mechanism to the interrupt handling path.

ISRs

Quoted 8 lines
Quoted 2 lines

returning IRQ_DEFERRED will keep the interrupt masked until future acknowledge.

Deadlock. If you get an IRQ for a guest and you block the IRQ until the guest handles it you may (eg if the IRQ is shared) get priority inversion
with another interrupt source on the same line the guest requires first (eg disks and other I/O)

What if we will force the specific device to the end of the list. Once IRQ_NONE was returned by the other devices, we will mask the irq, forward the irq to the guest, issue a timer for 1msec. Motivation:
1msec is long enough for the guest to ack the irq + host unmask the irq +
cancell the timer. (ping round-trip for a guest is about 100msec) If the timer poped, it will unmask irqs + run over the device list to check
whether one of them has a pending irq.

This will solve the deadlock possiblity in a small price of potential latency.

...

Quoted 3 lines
Quoted 1 line

Any ideas ? Thoughts ?

Mask the interrupt in the main kernel, pass an event of some kind to

the

Quoted 1 line

guest. You can describe most devices from guest to kernel in a safe

form

Quoted 6 lines

as

device, bar, offset, register size, mask, bits to set, bits to clear

(or bits to test when deciding if it is the irq source)

The problem is that each device has its own bits and it cannot be a general solution.
Except that the device driver inside the guest should be changed because the host already
disabled the irq/status for them.

I know the above solution in not neat but we do want to contribute it. Any other ideas are welcome,
10x, Dor.

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/

#3 lore raw
Quoted 4 lines

What if we will force the specific device to the end of the list. Once IRQ_NONE was returned by the other devices, we will mask the irq, forward the irq to the guest, issue a timer for 1msec. Motivation:
1msec is long enough for the guest to ack the irq + host unmask the irq

It makes no difference. The deadlock isn't fixable by timing hacks. Consider the following sequence

Guest0 - blocked on I/O

IRQ14 from your hardware
Block IRQ14
Sent to guest (guest is blocked)

IRQ14 from hard disk
Ignored (as blocked)

Deadlock

#4 lore raw
Quoted 12 lines · Alan Cox
Quoted 5 lines

What if we will force the specific device to the end of the list. Once IRQ_NONE was returned by the other devices, we will mask the irq, forward the irq to the guest, issue a timer for 1msec. Motivation:
1msec is long enough for the guest to ack the irq + host unmask the irq

It makes no difference. The deadlock isn't fixable by timing hacks. Consider the following sequence

Guest0 - blocked on I/O

Quoted 9 lines

IRQ14 from your hardware
Block IRQ14
Sent to guest (guest is blocked)

IRQ14 from hard disk
Ignored (as blocked)

Deadlock

IMO the only reasonable solution is to disallow interrupt forwarding with shared irqs. If someone later comes up with a bright idea, we can implement it. Otherwise the problem will solve itself with hardware moving to msi.

Signature · 8 lines
Do not meddle in the internals of kernels, for they are subtle and quick to panic.


-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
#5 lore raw
Quoted 2 lines

IMO the only reasonable solution is to disallow interrupt forwarding with shared irqs. If someone later comes up with a bright idea, we can

Which means you are back to ISA bus devices. Even checking if an IRQ is currently unshared isn't simple as with hotplug this may change.

Quoted 2 lines

implement it. Otherwise the problem will solve itself with hardware moving to msi.

Only if MSI ever works properly on the bridges and hardware 8(

#6 lore raw
Quoted 8 lines · Alan Cox
Quoted 3 lines

IMO the only reasonable solution is to disallow interrupt forwarding with shared irqs. If someone later comes up with a bright idea, we can

Which means you are back to ISA bus devices. Even checking if an IRQ is currently unshared isn't simple as with hotplug this may change.

Hotplug is user-controllable, so if the user refrains from adding pci devices after assigning a device to the guest, it should work. I think that USB interrupts are assigned to the controller, not the device, so USB hotplug can be ruled out.

I admit this is fairly weak.

Quoted 6 lines
Quoted 3 lines

implement it. Otherwise the problem will solve itself with hardware moving to msi.

Only if MSI ever works properly on the bridges and hardware 8(

Oh, I've no doubt it will be made to work -- there's money to be saved in those irq lines. And msi makes sense technically as well.

Signature · 8 lines
Do not meddle in the internals of kernels, for they are subtle and quick to panic.


-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
#7 lore raw
Quoted 4 lines

Hotplug is user-controllable, so if the user refrains from adding pci devices after assigning a device to the guest, it should work. I think that USB interrupts are assigned to the controller, not the device, so USB hotplug can be ruled out.

Cardbus is more problematic.

Alan

#8 lore raw
In reply to Avi Kivity
Quoted 22 lines
Quoted 1 line · Alan Cox
Quoted 1 line

What if we will force the specific device to the end of the list.

Once

Quoted 3 lines
Quoted 3 lines

IRQ_NONE was returned by the other devices, we will mask the irq, forward the irq to the guest, issue a timer for 1msec. Motivation:
1msec is long enough for the guest to ack the irq + host unmask the

irq

Quoted 15 lines
Quoted 1 line

It makes no difference. The deadlock isn't fixable by timing hacks. Consider the following sequence

Guest0 - blocked on I/O

IRQ14 from your hardware
Block IRQ14
Sent to guest (guest is blocked)

IRQ14 from hard disk
Ignored (as blocked)

But now the timer will pop and the hard disk will get its irq. The guest will be released right after.

Quoted 8 lines
Quoted 2 lines

Deadlock

IMO the only reasonable solution is to disallow interrupt forwarding with shared irqs. If someone later comes up with a bright idea, we can implement it. Otherwise the problem will solve itself with hardware moving to msi.

I though of that but the problem is that we'd like to use it with current hardware
devices that are shared.
:(

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/

#9 lore raw
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Guest0 - blocked on I/O

IRQ14 from your hardware
Block IRQ14
Sent to guest (guest is blocked)

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

IRQ14 from hard disk
Ignored (as blocked)

But now the timer will pop and the hard disk will get its irq. The guest will be released right after.

How do you plan to do this ? If you unmask the interrupt then it will immediately jam solid with IRQs from your hardware and the line will be disabled.

#14 lore raw
In reply to Avi Kivity
Quoted 4 lines · Avi Kivity

IMO the only reasonable solution is to disallow interrupt forwarding with shared irqs. If someone later comes up with a bright idea, we can implement it. Otherwise the problem will solve itself with hardware moving to msi.

Disallowing shared IRQs means disallowing PCI devices in general, so now you are back to ISA only devices, which it sounds like were the only things already allowed by vm86 or whatever it was for passing through.

MSI would be good, but how long until all devices support that? Certainly does appear to have potential though for allowing more interesting virtual machines.

--
Len Sorensen

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/

#10 lore raw
In reply to Alan Cox
Quoted 11 lines
Quoted 11 lines
Quoted 9 lines
Quoted 9 lines

Guest0 - blocked on I/O

IRQ14 from your hardware
Block IRQ14
Sent to guest (guest is blocked)

IRQ14 from hard disk
Ignored (as blocked)

Quoted 6 lines
Quoted 2 lines

But now the timer will pop and the hard disk will get its irq. The guest will be released right after.

How do you plan to do this ? If you unmask the interrupt then it will immediately jam solid with IRQs from your hardware and the line will be disabled.

Hope it should work like the following [Please correct me if I'm wrong]:
- Make the device the last irqaction in the list
- Our dummy handler will always return IRQ_HANDLED in case any other previous
irqaction did not return such. It will also issue the timer and mask
the irq in this case.
The line is temporarily jammed but not disabled - note_interrupt()
will not consider
our irq unhandled and won't disable it.
btw, if I'm not mistaken only after bad 99900/100000 the irq is
disabled.
- If the timer pops before the guest acks the irq, the timer handler will
ack the irq and unmask it. The timer's job is only to prevent
deadlocks.

Maybe it's better to code it first then send RFC.
Or wanted to get a feed back before hand to hear opinions and to know whether to use the
deferred option or the irq polarity option. Both of them can lead to the above deadlock
without the timer hack.
Best regards, Dor.

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/

#11 lore raw
Quoted 5 lines

- Make the device the last irqaction in the list
- Our dummy handler will always return IRQ_HANDLED in case any other previous
irqaction did not return such. It will also issue the timer and mask
the irq in this case.

Ok

Quoted 2 lines

btw, if I'm not mistaken only after bad 99900/100000 the irq is
disabled.

(and with GIT providing that they occurred in a short time period)

Quoted 4 lines

- If the timer pops before the guest acks the irq, the timer handler will
ack the irq and unmask it. The timer's job is only to prevent
deadlocks.

Ok I see what you are doing. It's either inspired or insane and I am not quite sure which of the two. I agree it should work although may cause performance crunches now and then and will also need care getting the locking right.

Alan

author
#13 lore raw
In reply to Dor Laor
Quoted 18 lines

Hope it should work like the following [Please correct me if I'm wrong]:
- Make the device the last irqaction in the list
- Our dummy handler will always return IRQ_HANDLED in case any other previous
irqaction did not return such. It will also issue the timer and mask
the irq in this case.
The line is temporarily jammed but not disabled - note_interrupt()
will not consider
our irq unhandled and won't disable it.
btw, if I'm not mistaken only after bad 99900/100000 the irq is
disabled.
- If the timer pops before the guest acks the irq, the timer handler will
ack the irq and unmask it. The timer's job is only to prevent
deadlocks.

Quoted 1 line

Maybe it's better to code it first then send RFC.

Definitely. We've already agreed that it's correct for the (rare, unfortunately) non shared scenario. Let's see how much do we have to bend reality to fit once it's in code form.

-- Ors.

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/

#15 lore raw
In reply to Lennart Sorensen
Quoted 8 lines · Lennart Sorensen
Quoted 5 lines · Avi Kivity

IMO the only reasonable solution is to disallow interrupt forwarding with shared irqs. If someone later comes up with a bright idea, we can implement it. Otherwise the problem will solve itself with hardware moving to msi.

Quoted 5 lines

Disallowing shared IRQs means disallowing PCI devices in general, so now you are back to ISA only devices, which it sounds like were the only things already allowed by vm86 or whatever it was for passing through.

No, it means disallowing pci devices that use shared irqs, and allowing pci devices that use non-shared irqs.

Quoted 3 lines

Certainly does appear to have potential though for allowing more interesting virtual machines.

IMO pass-through is counter to most of the benefits of virtualization. But users seem to want it.

Signature · 8 lines
error compiling committee.c: too many arguments to function


-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
#16 lore raw
Quoted 2 lines · Avi Kivity

No, it means disallowing pci devices that use shared irqs, and allowing pci devices that use non-shared irqs.

Most machiens I see today have almost no chance of having PCI devices without shared IRQs. This probably means any implementation will only work on a small set of machines with very specific setup in terms of which PCI slots they install cards in, and only as long as you don't allow any type of hotplugging of devices (or ever changing hardware at all). May not be worth implementing if it has such a limited use case. The MSI setup on the other hand does sound like it might have potential for working in general.

Quoted 2 lines

IMO pass-through is counter to most of the benefits of virtualization. But users seem to want it.

Certainly if you can have the host manage the device and provide virutal access instead, that is much better.

--
Len Sorensen

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/

#17 lore raw
Quoted 6 lines · Lennart Sorensen
Quoted 3 lines · Avi Kivity

No, it means disallowing pci devices that use shared irqs, and allowing pci devices that use non-shared irqs.

Quoted 10 lines

Most machiens I see today have almost no chance of having PCI devices without shared IRQs. This probably means any implementation will only work on a small set of machines with very specific setup in terms of which PCI slots they install cards in, and only as long as you don't allow any type of hotplugging of devices (or ever changing hardware at all). May not be worth implementing if it has such a limited use case. The MSI setup on the other hand does sound like it might have potential for working in general.

Looking at two random servers here and a desktop, interrupts are unshared except for usb. A laptop was not so lucky. So "no chance" is a bit extreme.

I agree it's far from optimal, but it is less limited than you imply.

Signature · 8 lines
error compiling committee.c: too many arguments to function


-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
#18 lore raw
Quoted 5 lines · Avi Kivity

Looking at two random servers here and a desktop, interrupts are unshared except for usb. A laptop was not so lucky. So "no chance" is a bit extreme.

I agree it's far from optimal, but it is less limited than you imply.

Well with 4 PCI interrupts, it doesn't take that many PCI devices before you must be sharing. Of course it seems some higher end systems split up the PCI bus in some way and run different IRQs from the APIC to different slots. I suspect that is how I see IRQ's of 177, 185, 193, 201, 209, etc on some systems.

If you have a server with an IO APIC then I suppose you have a rather good chance of avoiding shared IRQs for the most part.

--
Len Sorensen

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#12 lore raw
In reply to Alan Cox

Hi!

I guess this will also allow UIO to work without _any_ kernel parts, with only slight performance penalty in 'almost-never-happens' deadlock case?

(Greg, details are below, and better description is in the lkml thread).
Pavel

Quoted 21 lines
Quoted 4 lines

- Our dummy handler will always return IRQ_HANDLED in case any other previous
irqaction did not return such. It will also issue the timer and mask
the irq in this case.

Ok

Quoted 2 lines

btw, if I'm not mistaken only after bad 99900/100000 the irq is
disabled.

(and with GIT providing that they occurred in a short time period)

Quoted 4 lines

- If the timer pops before the guest acks the irq, the timer handler will
ack the irq and unmask it. The timer's job is only to prevent
deadlocks.

Ok I see what you are doing. It's either inspired or insane and I am not quite sure which of the two. I agree it should work although may cause performance crunches now and then and will also need care getting the locking right.

Signature · 8 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/