vfio/pci: Propagate ACPI notifications to user-space via eventfd
Subsystems (13)
PATCH v6 1/1 vfio/pci: Propagate ACPI notifications to user-space via eventfd
To allow pass-through devices receiving ACPI notifications, permit to register ACPI notify handler (via VFIO_DEVICE_SET_IRQS) for a given device. The handler role is to receive and propagate such ACPI notifications to the user-space through the user provided eventfd. This allows VMM to receive and propagate them further to the VM, where the actual driver for pass-through device resides and can react to device specific notifications accordingly.
The eventfd usage ensures VMM and device isolation: it allows to use a dedicated channel associated with the device for such events, such that the VMM has direct access.
Since the eventfd counter is used as ACPI notification value placeholder, the eventfd signaling needs to be serialized in order to not end up with notification values being coalesced. Therefore ACPI notification values are buffered and signalized one by one, when the previous notification value has been consumed.
---
Changelog v5..v6
- Get rid of #if IS_ENABLED(CONFIG_ACPI) from vfio_pci_intrs.c
- There is no need to keep vfio_acpi_notification struct definition in
header, make it private and move to cpi_notify.c
- Add lockdep_asserts in acpi_notification_dequeue
- notification_list_lock was not initialized which triggers some
warning with CONFIG_DEBUG_MUTEXES enabled (it was not logical error
since mentioned mutex was zeroed thanks to kzalloc it is just
DEBUG_MUTEXES performs extra checks and sets lock->magic).
- Change notification_lock from mutex to semaphore (notification_sem).
The logic was working fine with the mutex but enabling lockdep support
made me realize that the mutex has to be released by the same task
which acquires it and it is checked with lockdep enabled. Semaphore
does not have such a restriction.
- In order to correctly handle some sequences such as:
* user configures a notification eventfd
* a notification fires
* the user closes the eventfd without reading it, and then tries
to swap in a different eventfd ctx
separate the entire eventfd context together with notification_sem
from the main vfio_acpi_notification struct. Thanks to that entire,
separate eventfd ctx could be swapped leaving the previous eventfd ctx untouched. The new eventfd has its separate notification_sem
initialized and will not depend on previous eventfd ctx. Additionally
when the last handle to old eventfd will be released and EPOLLHUP
triggered, old eventfd ctx will be cleaned-up.
- Together with the above add support for required EPOLLHUP handling.
- Add missing fput invocations.
- Move some common code into vfio_acpi_eventfd_init and use it during registration and eventfd swapping.
- v5: https://patchwork.kernel.org/project/kvm/patch/[email protected]/
Changelog v4..v5
Address Alex Williamson's feedback:
- s/vfio_acpi_notify.{c,o}/acpi_notify.{c,o}
- Do not put acpi_notify to its own module but fold it into main
vfio.ko. Additionally select it from VFIO_PCI_CORE instead of VFIO_PCI.
- Cleanup acpi notify under igate mutex (in vfio_pci_core_close_device).
- Add extra check for ACPI companion in vfio_pci_get_irq_count and
extend vfio_pci_ioctl_get_irq_info.
- Drop acpi.h include - linux/vfio_acpi_notify.h includes it already.
- Send device check notification value for DATA_NONE and non-zero count
and for DATA_BOOL and non-zero count (as for loopback testing).
- Drop some redundant !acpi_notify->acpi_notify_trigger checks.
- Move some common code to new helper functions:
1) acpi_notification_dequeue
2) vfio_acpi_notify_cleanup and rename previous
vfio_acpi_notify_cleanup into vfio_remove_acpi_notify which uses it
- Add rate limited logging for dropped notifications.
- Move vdev->acpi_notification pointer cleanup to the
vfio_acpi_notify_cleanup function this also fixes two bigger issues
caught by Alex.
- Allow the eventfd to be swapped.
- s/GFP_KERNEL/GFP_KERNEL_ACCOUNT.
- s/VFIO_PCI_ACPI_NTFY_IRQ_INDEX/VFIO_PCI_ACPI_IRQ_INDEX.
- Add header protection for multiple includes.
- v4: https://patchwork.kernel.org/project/kvm/patch/[email protected]/
Changelog v3..v4
Address Alex Williamson feedback:
- Instead of introducing new ioctl used for eventfd registration, take
advantage of VFIO_DEVICE_SET_IRQS which already supports virtual IRQs
for things like error notification and device release requests.
- Introduced mechanism preventing creation of large queues.
Other:
- Move the implementation into the newly introduced VFIO_ACPI_NOTIFY
helper module. It is actually not bound to VFIO_PCI but VFIO_PCI
enables it whenever ACPI support is enabled. This change is introduced
since ACPI notifications are not limited to PCI devices, making it PCI independent will allow to re-use it also for other VFIO_* like
supports: e.g. VFIO_PLATFORM in the future if needed. Moving it out of drivers/vfio/pci/ was also suggested offline.
- s/notify_val_next/node
- v3: https://patchwork.kernel.org/project/kvm/patch/[email protected]/
Changelog v2..v3:
- Fix compilation warnings when building with "W=1"
Changelog v1..v2:
- The v2 implementation is actually completely different then v1:
instead of using acpi netlink events for propagating ACPI
notifications to the user space take advantage of eventfd, which can
provide better VMM and device isolation: it allows to use a dedicated
channel associated with the device for such events, such that the VMM
has direct access.
- Using eventfd counter as notification value placeholder was suggested
in v1 and requires additional serialization logic introduced in v2.
- Since the vfio-pci supports non-ACPI platforms address !CONFIG_ACPI
case.
- v1 discussion: https://patchwork.kernel.org/project/kvm/patch/[email protected]/
--- drivers/vfio/Kconfig | 5 + drivers/vfio/Makefile | 1 + drivers/vfio/acpi_notify.c | 324 ++++++++++++++++++++++++++++++ drivers/vfio/pci/Kconfig | 1 + drivers/vfio/pci/vfio_pci_core.c | 13 ++ drivers/vfio/pci/vfio_pci_intrs.c | 80 ++++++++ include/linux/vfio_acpi_notify.h | 36 ++++ include/linux/vfio_pci_core.h | 1 + include/uapi/linux/vfio.h | 1 + 9 files changed, 462 insertions(+) create mode 100644 drivers/vfio/acpi_notify.c create mode 100644 include/linux/vfio_acpi_notify.h
drivers/vfio/Kconfig +5 -0
@@ -12,6 +12,11 @@ menuconfig VFIO If you don't know what to do here, say N. if VFIO +config VFIO_ACPI_NOTIFY + bool + depends on ACPI + default n + config VFIO_CONTAINER bool "Support for the VFIO container /dev/vfio/vfio" select VFIO_IOMMU_TYPE1 if MMU && (X86 || S390 || ARM || ARM64)
drivers/vfio/Makefile +1 -0
@@ -7,6 +7,7 @@ vfio-y += vfio_main.o \ vfio-$(CONFIG_IOMMUFD) += iommufd.o vfio-$(CONFIG_VFIO_CONTAINER) += container.o vfio-$(CONFIG_VFIO_VIRQFD) += virqfd.o +vfio-$(CONFIG_VFIO_ACPI_NOTIFY) += acpi_notify.o obj-$(CONFIG_VFIO_IOMMU_TYPE1) += vfio_iommu_type1.o obj-$(CONFIG_VFIO_IOMMU_SPAPR_TCE) += vfio_iommu_spapr_tce.o
drivers/vfio/acpi_notify.c new file +324 -0
324 lines not on this page · raw message
drivers/vfio/pci/Kconfig +1 -0
@@ -4,6 +4,7 @@ config VFIO_PCI_CORE tristate select VFIO_VIRQFD select IRQ_BYPASS_MANAGER + select VFIO_ACPI_NOTIFY if ACPI config VFIO_PCI_MMAP def_bool y if !S390
drivers/vfio/pci/vfio_pci_core.c +13 -0
@@ -27,6 +27,7 @@#include <linux/vgaarb.h> #include <linux/nospec.h> #include <linux/sched/mm.h> +#include <linux/vfio_acpi_notify.h> #if IS_ENABLED (CONFIG_EEH )#include <asm/eeh.h> #endif
@@ -683,6 +684,7 @@ void vfio_pci_core_close_device(struct vfio_device *core_vdev) {struct vfio_pci_core_device *vdev = container_of(core_vdev, struct vfio_pci_core_device, vdev); +struct acpi_device *adev =ACPI_COMPANION (&vdev->pdev->dev);if (vdev->sriov_pf_core_dev) {mutex_lock (&vdev->sriov_pf_core_dev->vf_token->lock);
@@ -704,6 +706,8 @@ void vfio_pci_core_close_device(struct vfio_device *core_vdev)eventfd_ctx_put (vdev->req_trigger); vdev->req_trigger =NULL ; } +if (adev) +vfio_remove_acpi_notify (&vdev->acpi_notification, adev);mutex_unlock (&vdev->igate); }EXPORT_SYMBOL_GPL (vfio_pci_core_close_device);
@@ -725,6 +729,8 @@ EXPORT_SYMBOL_GPL(vfio_pci_core_finish_enable);static int vfio_pci_get_irq_count (struct vfio_pci_core_device *vdev,int irq_type) { +struct acpi_device *adev =ACPI_COMPANION (&vdev->pdev->dev); +if (irq_type ==VFIO_PCI_INTX_IRQ_INDEX ) {u8 pin;
@@ -761,6 +767,8 @@ static int vfio_pci_get_irq_count(struct vfio_pci_core_device *vdev, int irq_typreturn 1 ; }else if (irq_type ==VFIO_PCI_REQ_IRQ_INDEX ) {return 1 ; + }else if (adev && irq_type ==VFIO_PCI_ACPI_IRQ_INDEX ) { +return 1 ; }return 0 ;
@@ -1084,6 +1092,7 @@ static int vfio_pci_ioctl_get_irq_info(struct vfio_pci_core_device *vdev, struct vfio_irq_info __user *arg) { unsigned long minsz = offsetofend(struct vfio_irq_info, count); +struct acpi_device *adev =ACPI_COMPANION (&vdev->pdev->dev);struct vfio_irq_info info;if (copy_from_user (&info, arg, minsz))
@@ -1096,6 +1105,10 @@ static int vfio_pci_ioctl_get_irq_info(struct vfio_pci_core_device *vdev, case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX:case VFIO_PCI_REQ_IRQ_INDEX :break ; +case VFIO_PCI_ACPI_IRQ_INDEX : +if (adev) +break ; +return -EINVAL ;case VFIO_PCI_ERR_IRQ_INDEX :if (pci_is_pcie (vdev->pdev))break ;
drivers/vfio/pci/vfio_pci_intrs.c +80 -0
98 lines not on this page · raw message
include/linux/vfio_acpi_notify.h new file +36 -0
@@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* VFIO ACPI notification replication +* +* Author: Grzegorz Jaszczyk <[email protected]> +*/ +#include <linux/acpi.h> +#include <linux/eventfd.h> +#include <linux/poll.h> + +#ifndef VFIO_ACPI_NOTIFY_H +#define VFIO_ACPI_NOTIFY_H + +struct vfio_acpi_notification ; + +#if IS_ENABLED (CONFIG_ACPI ) +void vfio_acpi_notify (struct acpi_device *adev,u32 event,void *data); +int vfio_register_acpi_notify_handler (struct vfio_acpi_notification **acpi_notify, +struct acpi_device *adev,int32_t fd); +void vfio_remove_acpi_notify (struct vfio_acpi_notification **acpi_notify, +struct acpi_device *adev); +#else +static inline void vfio_acpi_notify (struct acpi_device *adev,u32 event,void *data) {} +static inline int +vfio_register_acpi_notify_handler (struct vfio_acpi_notification **acpi_notify, +struct acpi_device *adev,int32_t fd) +{ +return -ENODEV ; +} + +static inline void +vfio_remove_acpi_notify (struct vfio_acpi_notification **acpi_notify, +struct acpi_device *adev) {} +#endif /* CONFIG_ACPI */ + +#endif /* VFIO_ACPI_NOTIFY_H */
include/linux/vfio_pci_core.h +1 -0
@@ -96,6 +96,7 @@ struct vfio_pci_core_device {struct mutex vma_lock;struct list_head vma_list;struct rw_semaphore memory_lock; +struct vfio_acpi_notification *acpi_notification; };/* Will be exported for vfio pci drivers usage */
include/uapi/linux/vfio.h +1 -0
@@ -625,6 +625,7 @@ enum {VFIO_PCI_MSIX_IRQ_INDEX ,VFIO_PCI_ERR_IRQ_INDEX ,VFIO_PCI_REQ_IRQ_INDEX , +VFIO_PCI_ACPI_IRQ_INDEX , VFIO_PCI_NUM_IRQS };
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Christian Brauner
maintainerRe: [PATCH 0/2] eventfd: simplify signal helpers
Quoted 5 lines · Alex Williamson
Quoted 2 lines · On Thu, 13 Jul 2023 12:05:36 +0200 Christian Brauner <[email protected]> wrote:
Hey everyone,
Quoted 7 lines
Quoted 2 lines
This simplifies the
eventfd_signal()andeventfd_signal_mask()helpers by removing the count argument which is effectively unused.We have a patch under review which does in fact make use of the signaling value:
Huh, thanks for the link.
Quoting from
https://patchwork.kernel.org/project/kvm/patch/[email protected]/#25266856
Quoted 3 lines
Reading an eventfd returns an 8-byte value, we generally only use it as a counter, but it's been discussed previously and IIRC, it's possible to use that value as a notification value.
So the goal is to pipe a specific value through eventfd? But it is explicitly a counter. The whole thing is written around a counter and each write and signal adds to the counter.
The consequences are pretty well described in the cover letter of v6 https://lore.kernel.org/all/[email protected]/
Quoted 5 lines
Since the eventfd counter is used as ACPI notification value placeholder, the eventfd signaling needs to be serialized in order to not end up with notification values being coalesced. Therefore ACPI notification values are buffered and signalized one by one, when the previous notification value has been consumed.
But isn't this a good indication that you really don't want an eventfd but something that's explicitly designed to associate specific data with a notification? Using eventfd in that manner requires serialization, buffering, and enforces ordering.
I have no skin in the game aside from having to drop this conversion which I'm fine to do if there are actually users for this btu really, that looks a lot like abusing an api that really wasn't designed for this.
Re: [PATCH 0/2] eventfd: simplify signal helpers
Quoted 4 lines · Christian Brauner
I have no skin in the game aside from having to drop this conversion which I'm fine to do if there are actually users for this btu really, that looks a lot like abusing an api that really wasn't designed for this.
Yeah, I think so too. The ACPI thing should use its own FD if it wants to feed actual data..
Jason
Re: [PATCH 0/2] eventfd: simplify signal helpers
In reply to Christian Braunerpt., 14 lip 2023 o 09:05 Christian Brauner <[email protected]> napisał(a):
Quoted 42 lines · Alex Williamson
Quoted 5 lines
Quoted 2 lines · On Thu, 13 Jul 2023 12:05:36 +0200 Christian Brauner…
Hey everyone,
Quoted 7 lines
Quoted 2 lines
This simplifies the
eventfd_signal()andeventfd_signal_mask()helpers by removing the count argument which is effectively unused.We have a patch under review which does in fact make use of the signaling value:
https://lore.kernel.org/all/20230630155936.3015595-1-jaz-nYOzD4b6Jr9Wk0Htik3J/[email protected]/
Huh, thanks for the link.
Quoting from
https://patchwork.kernel.org/project/kvm/patch/[email protected]/#25266856Quoted 3 lines
Reading an eventfd returns an 8-byte value, we generally only use it as a counter, but it's been discussed previously and IIRC, it's possible to use that value as a notification value.
So the goal is to pipe a specific value through eventfd? But it is explicitly a counter. The whole thing is written around a counter and each write and signal adds to the counter.
The consequences are pretty well described in the cover letter of
v6 https://lore.kernel.org/all/20230630155936.3015595-1-jaz-nYOzD4b6Jr9Wk0Htik3J/[email protected]/Quoted 5 lines
Since the eventfd counter is used as ACPI notification value placeholder, the eventfd signaling needs to be serialized in order to not end up with notification values being coalesced. Therefore ACPI notification values are buffered and signalized one by one, when the previous notification value has been consumed.
But isn't this a good indication that you really don't want an eventfd but something that's explicitly designed to associate specific data with a notification? Using eventfd in that manner requires serialization, buffering, and enforces ordering.
Quoted 4 lines
I have no skin in the game aside from having to drop this conversion which I'm fine to do if there are actually users for this btu really, that looks a lot like abusing an api that really wasn't designed for this.
https://patchwork.kernel.org/project/kvm/patch/[email protected]/
was posted at the beginig of March and one of the main things we've discussed was the mechanism for propagating acpi notification value. We've endup with eventfd as the best mechanism and have actually been using it from v2. I really do not want to waste this effort, I think we are quite advanced with v6 now. Additionally we didn't actually modify any part of eventfd support that was in place, we only used it in a specific (and discussed beforehand) way.
Re: [PATCH 0/2] eventfd: simplify signal helpers
Quoted 42 lines · On Mon, 17 Jul 2023 10:29:34 +0200 Grzegorz Jaszczyk <[email protected]> wrote:
pt., 14 lip 2023 o 09:05 Christian Brauner <[email protected]> napisał(a):
Quoted 41 lines · Alex Williamson
Quoted 5 lines
Quoted 2 lines · On Thu, 13 Jul 2023 12:05:36 +0200 Christian Brauner <[email protected]> wrote:
Hey everyone,
Quoted 7 lines
Quoted 2 lines
This simplifies the
eventfd_signal()andeventfd_signal_mask()helpers by removing the count argument which is effectively unused.We have a patch under review which does in fact make use of the signaling value:
Huh, thanks for the link.
Quoting from
https://patchwork.kernel.org/project/kvm/patch/[email protected]/#25266856Quoted 3 lines
Reading an eventfd returns an 8-byte value, we generally only use it as a counter, but it's been discussed previously and IIRC, it's possible to use that value as a notification value.
So the goal is to pipe a specific value through eventfd? But it is explicitly a counter. The whole thing is written around a counter and each write and signal adds to the counter.
The consequences are pretty well described in the cover letter of v6 https://lore.kernel.org/all/[email protected]/
Quoted 5 lines
Since the eventfd counter is used as ACPI notification value placeholder, the eventfd signaling needs to be serialized in order to not end up with notification values being coalesced. Therefore ACPI notification values are buffered and signalized one by one, when the previous notification value has been consumed.
But isn't this a good indication that you really don't want an eventfd but something that's explicitly designed to associate specific data with a notification? Using eventfd in that manner requires serialization, buffering, and enforces ordering.
What would that mechanism be? We've been iterating on getting the serialization and buffering correct, but I don't know of another means that combines the notification with a value, so we'd likely end up with an eventfd only for notification and a separate ring buffer for notification values.
As this series demonstrates, the current in-kernel users only increment the counter and most userspace likely discards the counter value, which makes the counter largely a waste. While perhaps unconventional, there's no requirement that the counter may only be incremented by one, nor any restriction that I see in how userspace must interpret the counter value.
As I understand the ACPI notification proposal that Grzegorz links below, a notification with an interpreted value allows for a more direct userspace implementation when dealing with a series of discrete notification with value events. Thanks,
Alex
Quoted 5 lines
Quoted 4 lines
I have no skin in the game aside from having to drop this conversion which I'm fine to do if there are actually users for this btu really, that looks a lot like abusing an api that really wasn't designed for this.
Quoted 8 lines
https://patchwork.kernel.org/project/kvm/patch/[email protected]/
was posted at the beginig of March and one of the main things we've discussed was the mechanism for propagating acpi notification value. We've endup with eventfd as the best mechanism and have actually been using it from v2. I really do not want to waste this effort, I think we are quite advanced with v6 now. Additionally we didn't actually modify any part of eventfd support that was in place, we only used it in a specific (and discussed beforehand) way.
--
To unsubscribe, send a message with 'unsubscribe linux-aio' in the body to [email protected]. For more info on Linux AIO,
see: http://www.kvack.org/aio/
Don't email: <a href=mailto:"[email protected]">[email protected]</a>
Re: [PATCH 0/2] eventfd: simplify signal helpers
Quoted 5 lines · Alex Williamson
What would that mechanism be? We've been iterating on getting the serialization and buffering correct, but I don't know of another means that combines the notification with a value, so we'd likely end up with an eventfd only for notification and a separate ring buffer for notification values.
All FDs do this. You just have to make a FD with custom file_operations that does what this wants. The uAPI shouldn't be able to tell if the FD is backing it with an eventfd or otherwise. Have the kernel return the FD instead of accepting it. Follow the basic design of eg mlx5vf_save_fops
Jason
--
To unsubscribe, send a message with 'unsubscribe linux-aio' in the body to [email protected]. For more info on Linux AIO,
see: http://www.kvack.org/aio/
Don't email: <a href=mailto:"[email protected]">[email protected]</a>
Re: [PATCH 0/2] eventfd: simplify signal helpers
Quoted 8 lines · On Mon, 17 Jul 2023 19:12:16 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 5 lines · Alex Williamson
What would that mechanism be? We've been iterating on getting the serialization and buffering correct, but I don't know of another means that combines the notification with a value, so we'd likely end up with an eventfd only for notification and a separate ring buffer for notification values.
Quoted 5 lines
All FDs do this. You just have to make a FD with custom
file_operationsthat does what this wants. The uAPI shouldn't be able to tell if the FD is backing it with an eventfd or otherwise. Have the kernel return the FD instead of accepting it. Follow the basic design of egmlx5vf_save_fops
Sure, userspace could poll on any fd and read a value from it, but at that point we're essentially duplicating a lot of what eventfd provides for a minor(?) semantic difference over how the counter value is interpreted. Using an actual eventfd allows the ACPI notification to work as just another interrupt index within the existing vfio IRQ uAPI. Thanks,
Alex
--
To unsubscribe, send a message with 'unsubscribe linux-aio' in the body to [email protected]. For more info on Linux AIO,
see: http://www.kvack.org/aio/
Don't email: <a href=mailto:"[email protected]">[email protected]</a>
Re: [PATCH 0/2] eventfd: simplify signal helpers
Quoted 17 lines · Alex Williamson
Quoted 8 lines · On Mon, 17 Jul 2023 19:12:16 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 5 lines · Alex Williamson
What would that mechanism be? We've been iterating on getting the serialization and buffering correct, but I don't know of another means that combines the notification with a value, so we'd likely end up with an eventfd only for notification and a separate ring buffer for notification values.
Quoted 5 lines
All FDs do this. You just have to make a FD with custom
file_operationsthat does what this wants. The uAPI shouldn't be able to tell if the FD is backing it with an eventfd or otherwise. Have the kernel return the FD instead of accepting it. Follow the basic design of egmlx5vf_save_fops
Quoted 6 lines
Sure, userspace could poll on any fd and read a value from it, but at that point we're essentially duplicating a lot of what eventfd provides for a minor(?) semantic difference over how the counter value is interpreted. Using an actual eventfd allows the ACPI notification to work as just another interrupt index within the existing vfio IRQ uAPI.
Yes, duplicated, sort of, whatever the "ack" is to allow pushing a new value can be revised to run as part of the read.
But I don't really view it as a minor difference. eventfd is a counter. It should not be abused otherwise, even if it can be made to work.
It really isn't an IRQ if it is pushing an async message w/data.
Jason
--
To unsubscribe, send a message with 'unsubscribe linux-aio' in the body to [email protected]. For more info on Linux AIO,
see: http://www.kvack.org/aio/
Don't email: <a href=mailto:"[email protected]">[email protected]</a>