Virtuozzo QEMU development (svt-core@virtuozzo.com)
 help / color / mirror / Atom feed
From: no-reply@sashiko.virtuozzo.com
To: svt-core@virtuozzo.com,
	 Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
Cc: andrey.drobyshev@virtuozzo.com
Subject: Re: [PATCH QEMU HCI-8.1 v2 15/24] kvm: tolerate ioctls after kvm_close()
Date: Tue, 13 Oct 2026 15:44:36 +0000	[thread overview]
Message-ID: <010701a135efe4c7-e45b9bd0-4d45-42e6-b3cd-cef25bccae49-000000@eu-central-1.amazonses.com> (raw)
In-Reply-To: <20261013-vfio_v1-v2-15-433455c0a294@virtuozzo.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] kvm: kvm_set_irq() and listeners still deref NULL kvm_state after kvm_close()
- [Medium] kvm: -ENODEV from closed-state guards still aborts in fail-fast callers; stale errno in ioeventfd
- [Low] kvm: closed-state sentinel reuses -ENODEV, which kvm_send_msi() cannot tell from a kernel -ENODEV
--

commit 32c3566efef848fa0bd62b2fabb9b95d82114443
Author: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>

kvm: tolerate ioctls after kvm_close()

This patch makes kvm_ioctl(), kvm_vm_ioctl() and kvm_vcpu_ioctl() return
-ENODEV once kvm_close() has run. kvm_send_msi() no longer warns on
-ENODEV. The aim is to stop a retired cpr-transfer source from crashing
on late interrupts.

> diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
> --- a/accel/kvm/kvm-all.c
> +++ b/accel/kvm/kvm-all.c

[ ... ]

> @@ -3389,6 +3394,11 @@ int kvm_vm_ioctl(KVMState *s, unsigned long type, ...)
>      arg = va_arg(ap, void *);
>      va_end(ap);
>  
> +    /* kvm_close() has been called: there is no VM to talk to any more */
> +    if (!s || s->vmfd == -1) {
> +        return -ENODEV;
> +    }
> +

[Severity: Medium]
Can callers that treat any negative return as fatal still bring down the
retired source? After kvm_close() the memory listeners are still
registered and kvm_enabled() is still true, so these callers now receive
-ENODEV:

kvm_mem_ioeventfd_del()
    if (r < 0) {
        fprintf(stderr, "%s: error deleting ioeventfd: %s (%d)\n",
                __func__, strerror(-r), -r);
        abort();
    }

kvm_set_irq()
    ret = kvm_vm_ioctl(s, s->irq_set_ioctl, &event);
    if (ret < 0) {
        perror("kvm_set_irq");
        abort();
    }

kvm_irqchip_commit_routes()
    ret = kvm_vm_ioctl(s, KVM_SET_GSI_ROUTING, s->irq_routes);
    assert(ret == 0);

kvm_update_clock() in hw/i386/kvm/clock.c
    ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
    if (ret < 0) {
        fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(-ret));
        abort();

The same applies to kvm_mem_ioeventfd_add(), the PIO ioeventfd variants and
kvm_log_clear().

Would a late legacy IRQ, a memory region update, a routing commit or a
clock query on the retired source still end in abort() or assert()? The
commit message says these cases now "fail gracefully".

Also, the new early return skips ioctl(), so errno is not updated. In
kvm_set_ioeventfd_mmio():

    ret = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &iofd);

    if (ret < 0) {
        return -errno;
    }

Doesn't this return a stale errno, which may be 0 or an unrelated value,
when kvm_vm_ioctl() returns -ENODEV from the new guard?

[Severity: Medium]
This isn't fully fixed for legacy interrupt callers either. The guard
helps only if the call reaches kvm_vm_ioctl(), but kvm_set_irq()
evaluates s->irq_set_ioctl as an argument before the call:

kvm_set_irq(s, irq, level)
    ret = kvm_vm_ioctl(s, s->irq_set_ioctl, &event);

With s == NULL this is a NULL dereference before the !s check can run. It
is reached with kvm_state == NULL from:

hw/i386/kvm/ioapic.c:kvm_ioapic_set_irq()
    delivered = kvm_set_irq(kvm_state, s->kvm_gsi_base + irq, level);

hw/i386/kvm/i8259.c:kvm_pic_set_irq()
    delivered = kvm_set_irq(kvm_state, irq, level);

The final return in kvm_set_irq() also reads s->irq_set_ioctl. Memory
listener callbacks such as kvm_coalesce_pio_del() also use kvm_state
directly:

    KVMState *s = kvm_state;

    if (s->coalesced_pio) {

kvm_close() sets kvm_state = NULL but does not unregister the memory
listeners or the irqchip users.

Can an emulated INTx device raising a legacy interrupt, or a memory
region removed during qemu_cleanup(), still hit the SIGSEGV this patch
is meant to remove?

The later patches in the series change only coalesced-MMIO ring handling
in kvm_close() and the CPR-exec and VFIO code. They do not address these
callers.

[Severity: Low]
Is -ENODEV distinct enough to mean "closed"? The check in kvm_vm_ioctl()
tests s->vmfd == -1, and kvm_accel_instance_init() also sets:

    s->fd = -1;
    s->vmfd = -1;

so callers that run before the VM is opened now get -ENODEV instead of
-EBADF. I did not find any caller in accel/kvm, target/i386 or hw/ that
depends on -EBADF.

[ ... ]

> diff --git a/hw/i386/kvm/apic.c b/hw/i386/kvm/apic.c
> --- a/hw/i386/kvm/apic.c
> +++ b/hw/i386/kvm/apic.c
> @@ -196,7 +196,11 @@ static void kvm_send_msi(MSIMessage *msg)
>      msg->address = kvm_swizzle_msi_ext_dest_id(msg->address);
>  
>      ret = kvm_irqchip_send_msi(kvm_state, *msg);
> -    if (ret < 0) {
> +    /*
> +     * -ENODEV means kvm_close() has already run (the VM was handed over
> +     * via CPR); the interrupt is meaningless here and not worth a warning.
> +     */
> +    if (ret < 0 && ret != -ENODEV) {

[Severity: Low]
This isn't a bug I could confirm, but the check suppresses the "MSI lost"
warning for any -ENODEV, not only the post-kvm_close() case. Since the
same value is also an errno the kernel could return from KVM_SIGNAL_MSI,
the two cases cannot be told apart here. I could not confirm that the
kernel returns -ENODEV for that ioctl.

Would testing kvm_state == NULL in kvm_send_msi() before the call be a
more precise way to detect the closed state?

>          fprintf(stderr, "KVM: injection failed, MSI lost (%s)\n",
>                  strerror(-ret));
>      }

-- 
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261013-vfio_v1-v2-0-433455c0a294@virtuozzo.com?part=15

  reply	other threads:[~2026-10-13 15:44 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-13 15:28 [PATCH QEMU HCI-8.1 v2 00/24] vfio: support qemu-update with passed through GPU's and vGPU's #VSTOR-103860 Andrey Drobyshev
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 01/24] migration: Plug memory leaks after migrate_set_error() Andrey Drobyshev
2026-10-13 15:30   ` no-reply
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 02/24] hw/vfio: Fix liveness check in vfio_connect_kvm_msi_virq() Andrey Drobyshev
2026-10-13 15:30   ` no-reply
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 03/24] vfio/pci: Reject invalid MSI-X Table and PBA BIR values Andrey Drobyshev
2026-10-13 15:31   ` no-reply
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 04/24] vfio/pci: Fix information leak in vfio_rom_read() Andrey Drobyshev
2026-10-13 15:31   ` no-reply
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 05/24] vfio/pci: Initialize rom_read_failed in vfio_pci_load_rom() Andrey Drobyshev
2026-10-13 15:33   ` no-reply
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 06/24] vfio/pci: Grow buffer in vfio_pci_host_match() Andrey Drobyshev
2026-10-13 15:32   ` no-reply
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 07/24] q35: Fix migration of SMRAM state Andrey Drobyshev
2026-10-13 15:35   ` no-reply
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 08/24] migration: ensure APIC is loaded prior to VFIO PCI devices Andrey Drobyshev
2026-10-13 15:33   ` no-reply
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 09/24] migration: Fix regression of passing error_fatal into vmstate_load_state() Andrey Drobyshev
2026-10-13 15:36   ` no-reply
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 10/24] migration: vmstate_save_state_v: fix double error_setg Andrey Drobyshev
2026-10-13 15:38   ` no-reply
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 11/24] migration: yank: Move register instance earlier Andrey Drobyshev
2026-10-13 15:41   ` no-reply
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 12/24] migration: Free cpr-transfer MigrationAddress along with gsource Andrey Drobyshev
2026-10-13 15:39   ` no-reply
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 13/24] migration: Move CPR HUP watch to cpr-transfer.c Andrey Drobyshev
2026-10-13 15:40   ` no-reply
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 14/24] migration: Fix crash on second migration when cancel early Andrey Drobyshev
2026-10-13 15:45   ` no-reply
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 15/24] kvm: tolerate ioctls after kvm_close() Andrey Drobyshev
2026-10-13 15:44   ` no-reply [this message]
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 16/24] kvm: fix coalesced MMIO flush racing with kvm_close() Andrey Drobyshev
2026-10-13 15:47   ` no-reply
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 17/24] migration/cpr: add cpr_is_in_progress() Andrey Drobyshev
2026-10-13 15:47   ` no-reply
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 18/24] vfio/migration: don't migrate device state during CPR Andrey Drobyshev
2026-10-13 15:53   ` no-reply
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 19/24] vfio/listener: skip dirty page tracking " Andrey Drobyshev
2026-10-13 15:50   ` no-reply
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 20/24] VZ: vfio/cpr: don't close KVM on cpr-exec Andrey Drobyshev
2026-10-13 15:53   ` no-reply
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 21/24] VZ: migration/cpr-exec: abort cpr-exec if memory can't be preserved Andrey Drobyshev
2026-10-13 15:55   ` no-reply
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 22/24] VZ: migration/cpr-exec: take the vhost-net devices back if exec fails Andrey Drobyshev
2026-10-13 15:55   ` no-reply
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 23/24] VZ: vfio/region: preserve BAR mappings across cpr-exec #VSTOR-103860 Andrey Drobyshev
2026-10-13 16:00   ` no-reply
2026-10-13 15:28 ` [PATCH QEMU HCI-8.1 v2 24/24] VZ: migration/cpr-exec: allow cpr-exec with VFIO devices #VSTOR-103860 Andrey Drobyshev
2026-10-13 15:57   ` no-reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=010701a135efe4c7-e45b9bd0-4d45-42e6-b3cd-cef25bccae49-000000@eu-central-1.amazonses.com \
    --to=no-reply@sashiko.virtuozzo.com \
    --cc=andrey.drobyshev@virtuozzo.com \
    --cc=svt-core@virtuozzo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox