KVM: Fix assigned device MSI-X entry setting leak
Subsystems (4)
PATCH v2 1/1 KVM: Fix assigned device MSI-X entry setting leak
We need to prioritize our matching when setting MSI-X vector entries. Unused entries should only be used if we don't find an exact match or else we risk duplicating entries. This was causing an ENOSPC return when trying to mask and unmask MSI-X vectors based on guest MSI-X table updates.
The new KVM_CAP_DEVICE_MSIX_MASK extension indicates the presence of this fix.
---
v2: Add capability indicating MSIX_MASKing now works.
The faulting sequence went something like:
Start:
[0] entry 0, vector A
[1] entry 1, vector B
[2] entry 2, vector C
Set entry 1 to 0:
[0] entry 0, vector A
[1] entry 1->1, vector B->0
[2] entry 2, vector C
Set entry 2 to 0:
[0] entry 0, vector A
[1] entry 1->2, vector 0->0 <- incorrectly matched
[2] entry 2, vector C
Set entry 2 to C:
[0] entry 0, vector A
[1] entry 2->2, vector 0->C <- incorrectly matched again
[2] entry 2, vector C
Set entry 1 to B:
[0] entry 0, vector A
[1] entry 2, vector C
[2] entry 2, vector C-ENOSPC
Documentation/virtual/kvm/api.txt | 5 +++-- arch/x86/kvm/x86.c | 1 + include/linux/kvm.h | 1 + virt/kvm/assigned-dev.c | 21 ++++++++++++++------- 4 files changed, 19 insertions(+), 9 deletions(-)
Documentation/virtual/kvm/api.txt +3 -2
@@ -1304,8 +1304,9 @@ Type: vm ioctl Parameters: struct kvm_assigned_msix_entry (in) Returns: 0 on success, -1 on error -Specifies the routing of an MSI-X assigned device interrupt to a GSI. Setting -the GSI vector to zero means disabling the interrupt. +Specifies the routing of an MSI-X assigned device interrupt to a GSI. If +KVM_CAP_DEVICE_MSIX_MASK is available, setting the GSI vector to zero means +disabling the interrupt. struct kvm_assigned_msix_entry { __u32 assigned_dev_id;
arch/x86/kvm/x86.c +1 -0
@@ -2057,6 +2057,7 @@ int kvm_dev_ioctl_check_extension(long ext)case KVM_CAP_XSAVE :case KVM_CAP_ASYNC_PF :case KVM_CAP_GET_TSC_KHZ : +case KVM_CAP_DEVICE_MSIX_MASK : r =1 ;break ;case KVM_CAP_COALESCED_MMIO :
include/linux/kvm.h +1 -0
@@ -558,6 +558,7 @@ struct kvm_ppc_pvinfo {#define KVM_CAP_PPC_PAPR 68#define KVM_CAP_S390_GMAP 71#define KVM_CAP_TSC_DEADLINE_TIMER 72 +#define KVM_CAP_DEVICE_MSIX_MASK 73#ifdef KVM_CAP_IRQ_ROUTING
virt/kvm/assigned-dev.c +14 -7
@@ -728,7 +728,7 @@ msix_nr_out:static int kvm_vm_ioctl_set_msix_entry (struct kvm *kvm,struct kvm_assigned_msix_entry *entry) { -int r =0 , i; +int r =0 , i, unused =-1 ;struct kvm_assigned_dev_kernel *adev;mutex_lock (&kvm->lock);
@@ -741,17 +741,24 @@ static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm, goto msix_entry_out; } -for (i =0 ; i < adev->entries_nr; i++) -if (adev->guest_msix_entries[i].vector ==0 || - adev->guest_msix_entries[i].entry == entry->entry) { +for (i =0 ; i < adev->entries_nr; i++) { +if (adev->guest_msix_entries[i].entry == entry->entry) { adev->guest_msix_entries[i].entry = entry->entry; adev->guest_msix_entries[i].vector = entry->gsi; adev->host_msix_entries[i].entry = entry->entry;break ; - } + }else if (unused <0 && !adev->guest_msix_entries[i].vector) + unused = i; + } +if (i == adev->entries_nr) { - r = -ENOSPC ; - goto msix_entry_out; +if (unused <0 ) { + r = -ENOSPC ; + goto msix_entry_out; + } + adev->guest_msix_entries[unused].entry = entry->entry; + adev->guest_msix_entries[unused].vector = entry->gsi; + adev->host_msix_entries[unused].entry = entry->entry; } msix_entry_out:
Quoted 42 lines · Alex Williamson
We need to prioritize our matching when setting MSI-X vector entries. Unused entries should only be used if we don't find an exact match or else we risk duplicating entries. This was causing an ENOSPC return when trying to mask and unmask MSI-X vectors based on guest MSI-X table updates.
The new
KVM_CAP_DEVICE_MSIX_MASKextension indicates the presence of this fix.Signed-off-by Alex Williamson <[email protected]>---
v2: Add capability indicating
MSIX_MASKingnow works.The faulting sequence went something like:
Start:
[0] entry 0, vector A
[1] entry 1, vector B
[2] entry 2, vector CSet entry 1 to 0:
[0] entry 0, vector A
[1] entry 1->1, vector B->0
[2] entry 2, vector CSet entry 2 to 0:
[0] entry 0, vector A
[1] entry 1->2, vector 0->0 <- incorrectly matched
[2] entry 2, vector CSet entry 2 to C:
[0] entry 0, vector A
[1] entry 2->2, vector 0->C <- incorrectly matched again
[2] entry 2, vector CSet entry 1 to B:
[0] entry 0, vector A
[1] entry 2, vector C
[2] entry 2, vector C-ENOSPC
Quoted 5 lines
Documentation/virtual/kvm/api.txt | 5 +++-- arch/x86/kvm/x86.c | 1 + include/linux/kvm.h | 1 + virt/kvm/assigned-dev.c | 21 ++++++++++++++------- 4 files changed, 19 insertions(+), 9 deletions(-)
I don't object to fixing this bug. But I don't think we need a capability for this. Here's why:
setting an entry to zero is not really matching what devices need, since that would lose interrupts. What devices need is mask entries to disable them, then unmask and get an interrupt if it was delayed.
So, if we are going to add a new capability, how about sticking a mask bit in the padding instead?
As was shown in the past this can even improve performance since we can propagate the mask bit back to the host device.
Quoted 90 lines
Documentation/virtual/kvm/api.txt +3
-2@@ -1304,8 +1304,9 @@ Type: vm ioctl Parameters: struct kvm_assigned_msix_entry (in) Returns: 0 on success, -1 on error -Specifies the routing of an MSI-X assigned device interrupt to a GSI. Setting -the GSI vector to zero means disabling the interrupt. +Specifies the routing of an MSI-X assigned device interrupt to a GSI. If +KVM_CAP_DEVICE_MSIX_MASK is available, setting the GSI vector to zero means +disabling the interrupt. struct kvm_assigned_msix_entry { __u32 assigned_dev_id;arch/x86/kvm/x86.c +1
-0@@ -2057,6 +2057,7 @@ int kvm_dev_ioctl_check_extension(long ext)case KVM_CAP_XSAVE :case KVM_CAP_ASYNC_PF :case KVM_CAP_GET_TSC_KHZ : +case KVM_CAP_DEVICE_MSIX_MASK : r =1 ;break ;case KVM_CAP_COALESCED_MMIO :include/linux/kvm.h +1
-0@@ -558,6 +558,7 @@ struct kvm_ppc_pvinfo {#define KVM_CAP_PPC_PAPR 68#define KVM_CAP_S390_GMAP 71#define KVM_CAP_TSC_DEADLINE_TIMER 72 +#define KVM_CAP_DEVICE_MSIX_MASK 73#ifdef KVM_CAP_IRQ_ROUTING virt/kvm/assigned-dev.c +14
-8@@ -728,7 +728,7 @@ msix_nr_out:static int kvm_vm_ioctl_set_msix_entry (struct kvm *kvm,struct kvm_assigned_msix_entry *entry) { -int r =0 , i; +int r =0 , i, unused =-1 ;struct kvm_assigned_dev_kernel *adev;mutex_lock (&kvm->lock);@@ -741,17 +741,24 @@ static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm, goto msix_entry_out; } -for (i =0 ; i < adev->entries_nr; i++) -if (adev->guest_msix_entries[i].vector ==0 || - adev->guest_msix_entries[i].entry == entry->entry) { +for (i =0 ; i < adev->entries_nr; i++) { +if (adev->guest_msix_entries[i].entry == entry->entry) { adev->guest_msix_entries[i].entry = entry->entry; adev->guest_msix_entries[i].vector = entry->gsi; adev->host_msix_entries[i].entry = entry->entry;break ; - } + }else if (unused <0 && !adev->guest_msix_entries[i].vector) + unused = i; + } +if (i == adev->entries_nr) { - r = -ENOSPC ; - goto msix_entry_out; +if (unused <0 ) { + r = -ENOSPC ; + goto msix_entry_out; + } + adev->guest_msix_entries[unused].entry = entry->entry; + adev->guest_msix_entries[unused].vector = entry->gsi; + adev->host_msix_entries[unused].entry = entry->entry; } msix_entry_out: --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/
Quoted 52 lines · Michael S. Tsirkin
Quoted 42 lines · Alex Williamson
We need to prioritize our matching when setting MSI-X vector entries. Unused entries should only be used if we don't find an exact match or else we risk duplicating entries. This was causing an ENOSPC return when trying to mask and unmask MSI-X vectors based on guest MSI-X table updates.
The new
KVM_CAP_DEVICE_MSIX_MASKextension indicates the presence of this fix.Signed-off-by Alex Williamson <[email protected]>---
v2: Add capability indicating
MSIX_MASKingnow works.The faulting sequence went something like:
Start:
[0] entry 0, vector A
[1] entry 1, vector B
[2] entry 2, vector CSet entry 1 to 0:
[0] entry 0, vector A
[1] entry 1->1, vector B->0
[2] entry 2, vector CSet entry 2 to 0:
[0] entry 0, vector A
[1] entry 1->2, vector 0->0 <- incorrectly matched
[2] entry 2, vector CSet entry 2 to C:
[0] entry 0, vector A
[1] entry 2->2, vector 0->C <- incorrectly matched again
[2] entry 2, vector CSet entry 1 to B:
[0] entry 0, vector A
[1] entry 2, vector C
[2] entry 2, vector C-ENOSPCQuoted 5 lines
Documentation/virtual/kvm/api.txt | 5 +++-- arch/x86/kvm/x86.c | 1 + include/linux/kvm.h | 1 + virt/kvm/assigned-dev.c | 21 ++++++++++++++------- 4 files changed, 19 insertions(+), 9 deletions(-)I don't object to fixing this bug. But I don't think we need a capability for this. Here's why:
Quoted 7 lines
setting an entry to zero is not really matching what devices need, since that would lose interrupts. What devices need is mask entries to disable them, then unmask and get an interrupt if it was delayed.
So, if we are going to add a new capability, how about sticking a mask bit in the padding instead?
I'll take a look. Are you suggesting then that we'd note the masked interrupt was deferred and inject when unmasked?
Quoted 2 lines
As was shown in the past this can even improve performance since we can propagate the mask bit back to the host device.
I'm dubious whether there's actually a use case that benefits from pushing the mask down to hardware. The typical mask case is a temporary mask to update the vector data/address then unmask. We're actually accelerating that by not pushing it down to hardware. Are there real world drivers that enable a vector and then mask it for an extended period of time? Thanks,
Alex
Quoted 90 lines
Quoted 90 lines
Documentation/virtual/kvm/api.txt +3
-2@@ -1304,8 +1304,9 @@ Type: vm ioctl Parameters: struct kvm_assigned_msix_entry (in) Returns: 0 on success, -1 on error -Specifies the routing of an MSI-X assigned device interrupt to a GSI. Setting -the GSI vector to zero means disabling the interrupt. +Specifies the routing of an MSI-X assigned device interrupt to a GSI. If +KVM_CAP_DEVICE_MSIX_MASK is available, setting the GSI vector to zero means +disabling the interrupt. struct kvm_assigned_msix_entry { __u32 assigned_dev_id;arch/x86/kvm/x86.c +1
-0@@ -2057,6 +2057,7 @@ int kvm_dev_ioctl_check_extension(long ext)case KVM_CAP_XSAVE :case KVM_CAP_ASYNC_PF :case KVM_CAP_GET_TSC_KHZ : +case KVM_CAP_DEVICE_MSIX_MASK : r =1 ;break ;case KVM_CAP_COALESCED_MMIO :include/linux/kvm.h +1
-0@@ -558,6 +558,7 @@ struct kvm_ppc_pvinfo {#define KVM_CAP_PPC_PAPR 68#define KVM_CAP_S390_GMAP 71#define KVM_CAP_TSC_DEADLINE_TIMER 72 +#define KVM_CAP_DEVICE_MSIX_MASK 73#ifdef KVM_CAP_IRQ_ROUTING virt/kvm/assigned-dev.c +14
-8@@ -728,7 +728,7 @@ msix_nr_out:static int kvm_vm_ioctl_set_msix_entry (struct kvm *kvm,struct kvm_assigned_msix_entry *entry) { -int r =0 , i; +int r =0 , i, unused =-1 ;struct kvm_assigned_dev_kernel *adev;mutex_lock (&kvm->lock);@@ -741,17 +741,24 @@ static int kvm_vm_ioctl_set_msix_entry(struct kvm *kvm, goto msix_entry_out; } -for (i =0 ; i < adev->entries_nr; i++) -if (adev->guest_msix_entries[i].vector ==0 || - adev->guest_msix_entries[i].entry == entry->entry) { +for (i =0 ; i < adev->entries_nr; i++) { +if (adev->guest_msix_entries[i].entry == entry->entry) { adev->guest_msix_entries[i].entry = entry->entry; adev->guest_msix_entries[i].vector = entry->gsi; adev->host_msix_entries[i].entry = entry->entry;break ; - } + }else if (unused <0 && !adev->guest_msix_entries[i].vector) + unused = i; + } +if (i == adev->entries_nr) { - r = -ENOSPC ; - goto msix_entry_out; +if (unused <0 ) { + r = -ENOSPC ; + goto msix_entry_out; + } + adev->guest_msix_entries[unused].entry = entry->entry; + adev->guest_msix_entries[unused].vector = entry->gsi; + adev->host_msix_entries[unused].entry = entry->entry; } msix_entry_out: --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/
Quoted 67 lines · Alex Williamson
Quoted 52 lines · Michael S. Tsirkin
Quoted 42 lines · Alex Williamson
We need to prioritize our matching when setting MSI-X vector entries. Unused entries should only be used if we don't find an exact match or else we risk duplicating entries. This was causing an ENOSPC return when trying to mask and unmask MSI-X vectors based on guest MSI-X table updates.
The new
KVM_CAP_DEVICE_MSIX_MASKextension indicates the presence of this fix.Signed-off-by Alex Williamson <[email protected]>---
v2: Add capability indicating
MSIX_MASKingnow works.The faulting sequence went something like:
Start:
[0] entry 0, vector A
[1] entry 1, vector B
[2] entry 2, vector CSet entry 1 to 0:
[0] entry 0, vector A
[1] entry 1->1, vector B->0
[2] entry 2, vector CSet entry 2 to 0:
[0] entry 0, vector A
[1] entry 1->2, vector 0->0 <- incorrectly matched
[2] entry 2, vector CSet entry 2 to C:
[0] entry 0, vector A
[1] entry 2->2, vector 0->C <- incorrectly matched again
[2] entry 2, vector CSet entry 1 to B:
[0] entry 0, vector A
[1] entry 2, vector C
[2] entry 2, vector C-ENOSPCQuoted 5 lines
Documentation/virtual/kvm/api.txt | 5 +++-- arch/x86/kvm/x86.c | 1 + include/linux/kvm.h | 1 + virt/kvm/assigned-dev.c | 21 ++++++++++++++------- 4 files changed, 19 insertions(+), 9 deletions(-)I don't object to fixing this bug. But I don't think we need a capability for this. Here's why:
Quoted 7 lines
setting an entry to zero is not really matching what devices need, since that would lose interrupts. What devices need is mask entries to disable them, then unmask and get an interrupt if it was delayed.
So, if we are going to add a new capability, how about sticking a mask bit in the padding instead?
I'll take a look. Are you suggesting then that we'd note the masked interrupt was deferred and inject when unmasked?
Quoted 2 lines
As was shown in the past this can even improve performance since we can propagate the mask bit back to the host device.
Quoted 6 lines
I'm dubious whether there's actually a use case that benefits from pushing the mask down to hardware. The typical mask case is a temporary mask to update the vector data/address then unmask. We're actually accelerating that by not pushing it down to hardware. Are there real world drivers that enable a vector and then mask it for an extended period of time?
I don't know. Shen Yang at some point said:
"We have also reproduced the issue on some large scale benchmark on the guest with "newer kernel like 2.6.30 on Xen, under very high interrupt rate, due to some "interrupt rate limitation mechanism in kernel.
And these patches did push the interrupts down - but not to hardware, masking interrupt as pending in kernel only sets a bit, it is only pushed down when we next get an interrupt while vector is masked.
Quoted 3 lines
Thanks,
Alex
Quoted 75 lines · Michael S. Tsirkin
Quoted 67 lines · Alex Williamson
Quoted 52 lines · Michael S. Tsirkin
Quoted 42 lines · Alex Williamson
We need to prioritize our matching when setting MSI-X vector entries. Unused entries should only be used if we don't find an exact match or else we risk duplicating entries. This was causing an ENOSPC return when trying to mask and unmask MSI-X vectors based on guest MSI-X table updates.
The new
KVM_CAP_DEVICE_MSIX_MASKextension indicates the presence of this fix.Signed-off-by Alex Williamson <[email protected]>---
v2: Add capability indicating
MSIX_MASKingnow works.The faulting sequence went something like:
Start:
[0] entry 0, vector A
[1] entry 1, vector B
[2] entry 2, vector CSet entry 1 to 0:
[0] entry 0, vector A
[1] entry 1->1, vector B->0
[2] entry 2, vector CSet entry 2 to 0:
[0] entry 0, vector A
[1] entry 1->2, vector 0->0 <- incorrectly matched
[2] entry 2, vector CSet entry 2 to C:
[0] entry 0, vector A
[1] entry 2->2, vector 0->C <- incorrectly matched again
[2] entry 2, vector CSet entry 1 to B:
[0] entry 0, vector A
[1] entry 2, vector C
[2] entry 2, vector C-ENOSPCQuoted 5 lines
Documentation/virtual/kvm/api.txt | 5 +++-- arch/x86/kvm/x86.c | 1 + include/linux/kvm.h | 1 + virt/kvm/assigned-dev.c | 21 ++++++++++++++------- 4 files changed, 19 insertions(+), 9 deletions(-)I don't object to fixing this bug. But I don't think we need a capability for this. Here's why:
Quoted 7 lines
setting an entry to zero is not really matching what devices need, since that would lose interrupts. What devices need is mask entries to disable them, then unmask and get an interrupt if it was delayed.
So, if we are going to add a new capability, how about sticking a mask bit in the padding instead?
I'll take a look. Are you suggesting then that we'd note the masked interrupt was deferred and inject when unmasked?
Quoted 2 lines
As was shown in the past this can even improve performance since we can propagate the mask bit back to the host device.
Quoted 6 lines
I'm dubious whether there's actually a use case that benefits from pushing the mask down to hardware. The typical mask case is a temporary mask to update the vector data/address then unmask. We're actually accelerating that by not pushing it down to hardware. Are there real world drivers that enable a vector and then mask it for an extended period of time?
Quoted 8 lines
I don't know. Shen Yang at some point said:
"We have also reproduced the issue on some large scale benchmark on the guest with "newer kernel like 2.6.30 on Xen, under very high interrupt rate, due to some "interrupt rate limitation mechanism in kernel.And these patches did push the interrupts down - but not to hardware, masking interrupt as pending in kernel only sets a bit, it is only pushed down when we next get an interrupt while vector is masked.
I thought the high rate masking was on older guests, but in any case a reasonable first step is emulated masking in kvm so that we can re-inject on unmask and not lose interrupts. That can either lead to lazy hardware masking with no additional change to the API or if someone takes the reigns on MSI-X table acceleration, we can head in that direction. The MSI-X table interaction I see on an average sized guest with an assigned 82576 PF certainly isn't calling out for more acceleration, but 10G devices and beyond could behave differently. Thanks,
Alex
Quoted 81 lines · Alex Williamson
Quoted 80 lines · Michael S. Tsirkin
Quoted 67 lines · Alex Williamson
Quoted 52 lines · Michael S. Tsirkin
Quoted 42 lines · Alex Williamson
We need to prioritize our matching when setting MSI-X vector entries. Unused entries should only be used if we don't find an exact match or else we risk duplicating entries. This was causing an ENOSPC return when trying to mask and unmask MSI-X vectors based on guest MSI-X table updates.
The new
KVM_CAP_DEVICE_MSIX_MASKextension indicates the presence of this fix.Signed-off-by Alex Williamson <[email protected]>---
v2: Add capability indicating
MSIX_MASKingnow works.The faulting sequence went something like:
Start:
[0] entry 0, vector A
[1] entry 1, vector B
[2] entry 2, vector CSet entry 1 to 0:
[0] entry 0, vector A
[1] entry 1->1, vector B->0
[2] entry 2, vector CSet entry 2 to 0:
[0] entry 0, vector A
[1] entry 1->2, vector 0->0 <- incorrectly matched
[2] entry 2, vector CSet entry 2 to C:
[0] entry 0, vector A
[1] entry 2->2, vector 0->C <- incorrectly matched again
[2] entry 2, vector CSet entry 1 to B:
[0] entry 0, vector A
[1] entry 2, vector C
[2] entry 2, vector C-ENOSPCQuoted 5 lines
Documentation/virtual/kvm/api.txt | 5 +++-- arch/x86/kvm/x86.c | 1 + include/linux/kvm.h | 1 + virt/kvm/assigned-dev.c | 21 ++++++++++++++------- 4 files changed, 19 insertions(+), 9 deletions(-)I don't object to fixing this bug. But I don't think we need a capability for this. Here's why:
Quoted 7 lines
setting an entry to zero is not really matching what devices need, since that would lose interrupts. What devices need is mask entries to disable them, then unmask and get an interrupt if it was delayed.
So, if we are going to add a new capability, how about sticking a mask bit in the padding instead?
I'll take a look. Are you suggesting then that we'd note the masked interrupt was deferred and inject when unmasked?
Quoted 2 lines
As was shown in the past this can even improve performance since we can propagate the mask bit back to the host device.
Quoted 6 lines
I'm dubious whether there's actually a use case that benefits from pushing the mask down to hardware. The typical mask case is a temporary mask to update the vector data/address then unmask. We're actually accelerating that by not pushing it down to hardware. Are there real world drivers that enable a vector and then mask it for an extended period of time?
I don't know. Shen Yang at some point said:
"We have also reproduced the issue on some large scale benchmark on the guest with "newer kernel like 2.6.30 on Xen, under very high interrupt rate, due to some "interrupt rate limitation mechanism in kernel.
Quoted 7 lines
Quoted 3 lines
And these patches did push the interrupts down - but not to hardware, masking interrupt as pending in kernel only sets a bit, it is only pushed down when we next get an interrupt while vector is masked.
I thought the high rate masking was on older guests, but in any case a reasonable first step is emulated masking in kvm so that we can re-inject on unmask and not lose interrupts.
Yes, I agree to that. That would also be good for emulated devices.
Quoted 9 lines
That can either lead to
lazy hardware masking with no additional change to the API or if someone takes the reigns on MSI-X table acceleration, we can head in that direction. The MSI-X table interaction I see on an average sized guest with an assigned 82576 PF certainly isn't calling out for more acceleration, but 10G devices and beyond could behave differently. Thanks,Alex