On 3/18/26 18:47, Pierrick Bouvier wrote:
> We just need to add kvm_enabled() guard when calling concerned
> functions, but no need to extract those kvm functions since they are not
> using any kvm specific types that would not be visible at compilation
> time.
>
> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> Signed-off-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
> ---
> hw/vfio/pci.c | 28 ++++++++++------------------
> 1 file changed, 10 insertions(+), 18 deletions(-)
>
> diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
> index df617f1fe46..811e5001de5 100644
> --- a/hw/vfio/pci.c
> +++ b/hw/vfio/pci.c
> @@ -152,7 +152,6 @@ void vfio_pci_intx_eoi(VFIODevice *vbasedev)
>
> static bool vfio_intx_enable_kvm(VFIOPCIDevice *vdev, Error **errp)
> {
> -#ifdef CONFIG_KVM
> PCIDevice *pdev = PCI_DEVICE(vdev);
> int irq_fd = event_notifier_get_fd(&vdev->intx.interrupt);
>
> @@ -206,14 +205,10 @@ fail:
> qemu_set_fd_handler(irq_fd, vfio_intx_interrupt, NULL, vdev);
> vfio_device_irq_unmask(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX);
> return false;
> -#else
> - return true;
> -#endif
> }
>
> static bool vfio_cpr_intx_enable_kvm(VFIOPCIDevice *vdev, Error **errp)
> {
> -#ifdef CONFIG_KVM
> if (vdev->no_kvm_intx || !kvm_irqfds_enabled() ||
> vdev->intx.route.mode != PCI_INTX_ENABLED ||
> !kvm_resamplefds_enabled()) {
> @@ -236,14 +231,10 @@ static bool vfio_cpr_intx_enable_kvm(VFIOPCIDevice *vdev, Error **errp)
> vdev->intx.kvm_accel = true;
> trace_vfio_intx_enable_kvm(vdev->vbasedev.name);
> return true;
> -#else
> - return true;
> -#endif
> }
>
> static void vfio_intx_disable_kvm(VFIOPCIDevice *vdev)
> {
> -#ifdef CONFIG_KVM
> PCIDevice *pdev = PCI_DEVICE(vdev);
>
> if (!vdev->intx.kvm_accel) {
> @@ -277,7 +268,6 @@ static void vfio_intx_disable_kvm(VFIOPCIDevice *vdev)
> vfio_device_irq_unmask(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX);
>
> trace_vfio_intx_disable_kvm(vdev->vbasedev.name);
> -#endif
> }
>
> static void vfio_intx_update(VFIOPCIDevice *vdev, PCIINTxRoute *route)
> @@ -287,7 +277,9 @@ static void vfio_intx_update(VFIOPCIDevice *vdev, PCIINTxRoute *route)
> trace_vfio_intx_update(vdev->vbasedev.name,
> vdev->intx.route.irq, route->irq);
>
> - vfio_intx_disable_kvm(vdev);
> + if (kvm_enabled()) {
> + vfio_intx_disable_kvm(vdev);
> + }
>
> vdev->intx.route = *route;
>
> @@ -295,7 +287,7 @@ static void vfio_intx_update(VFIOPCIDevice *vdev, PCIINTxRoute *route)
> return;
> }
>
> - if (!vfio_intx_enable_kvm(vdev, &err)) {
> + if (kvm_enabled() && !vfio_intx_enable_kvm(vdev, &err)) {
> warn_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
> }
>
> @@ -350,16 +342,14 @@ static bool vfio_intx_enable(VFIOPCIDevice *vdev, Error **errp)
> vdev->intx.pin = pin - 1; /* Pin A (1) -> irq[0] */
> pci_config_set_interrupt_pin(pdev->config, pin);
>
> -#ifdef CONFIG_KVM
> /*
> * Only conditional to avoid generating error messages on platforms
> * where we won't actually use the result anyway.
> */
> - if (kvm_irqfds_enabled() && kvm_resamplefds_enabled()) {
> + if (kvm_enabled() && kvm_irqfds_enabled() && kvm_resamplefds_enabled()) {
> vdev->intx.route = pci_device_route_intx_to_irq(pdev,
> vdev->intx.pin);
> }
> -#endif
>
> if (!vfio_notifier_init(vdev, &vdev->intx.interrupt, "intx-interrupt", 0,
> errp)) {
> @@ -370,7 +360,7 @@ static bool vfio_intx_enable(VFIOPCIDevice *vdev, Error **errp)
>
>
> if (cpr_is_incoming()) {
> - if (!vfio_cpr_intx_enable_kvm(vdev, &err)) {
> + if (kvm_enabled() && !vfio_cpr_intx_enable_kvm(vdev, &err)) {
> warn_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
> }
> goto skip_signaling;
> @@ -383,7 +373,7 @@ static bool vfio_intx_enable(VFIOPCIDevice *vdev, Error **errp)
> return false;
> }
>
> - if (!vfio_intx_enable_kvm(vdev, &err)) {
> + if (kvm_enabled() && !vfio_intx_enable_kvm(vdev, &err)) {
> warn_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
> }
>
> @@ -400,7 +390,9 @@ static void vfio_intx_disable(VFIOPCIDevice *vdev)
> int fd;
>
> timer_del(vdev->intx.mmap_timer);
> - vfio_intx_disable_kvm(vdev);
> + if (kvm_enabled()) {
> + vfio_intx_disable_kvm(vdev);
> + }
> vfio_device_irq_disable(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX);
> vdev->intx.pending = false;
> pci_irq_deassert(pdev);
Reviewed-by: Cédric Le Goater <clg@redhat.com>
Thanks,
C.