{"id":2225851,"url":"http://patchwork.ozlabs.org/api/patches/2225851/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20260421163438.3447280-3-clg@redhat.com/","project":{"id":14,"url":"http://patchwork.ozlabs.org/api/projects/14/?format=json","name":"QEMU Development","link_name":"qemu-devel","list_id":"qemu-devel.nongnu.org","list_email":"qemu-devel@nongnu.org","web_url":"","scm_url":"","webscm_url":"","list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<20260421163438.3447280-3-clg@redhat.com>","list_archive_url":null,"date":"2026-04-21T16:34:26","name":"[PULL,02/14] hw/vfio/helpers.c: extract kvm helpers in kvm-helpers.c","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"de31f215926bfbc000f9f6def85d0a76569ffaeb","submitter":{"id":85406,"url":"http://patchwork.ozlabs.org/api/people/85406/?format=json","name":"Cédric Le Goater","email":"clg@redhat.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20260421163438.3447280-3-clg@redhat.com/mbox/","series":[{"id":500853,"url":"http://patchwork.ozlabs.org/api/series/500853/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/list/?series=500853","date":"2026-04-21T16:34:24","name":"[PULL,01/14] hw/vfio/listener.c: remove CONFIG_KVM","version":1,"mbox":"http://patchwork.ozlabs.org/series/500853/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2225851/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2225851/checks/","tags":{},"related":[],"headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (1024-bit key;\n unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=aQQBaBSO;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org\n (client-ip=209.51.188.17; helo=lists1p.gnu.org;\n envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n receiver=patchwork.ozlabs.org)"],"Received":["from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17])\n\t(using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g0ShV6MHPz1yGs\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 22 Apr 2026 02:38:02 +1000 (AEST)","from localhost ([::1] helo=lists1p.gnu.org)\n\tby lists1p.gnu.org with esmtp (Exim 4.90_1)\n\t(envelope-from <qemu-devel-bounces@nongnu.org>)\n\tid 1wFE4x-0004sw-Rq; Tue, 21 Apr 2026 12:36:04 -0400","from eggs.gnu.org ([2001:470:142:3::10])\n by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <clg@redhat.com>) id 1wFE40-0004aG-OM\n for qemu-devel@nongnu.org; Tue, 21 Apr 2026 12:35:10 -0400","from us-smtp-delivery-124.mimecast.com ([170.10.129.124])\n by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <clg@redhat.com>) id 1wFE3q-0007KT-Cc\n for qemu-devel@nongnu.org; Tue, 21 Apr 2026 12:34:58 -0400","from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com\n (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by\n relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3,\n cipher=TLS_AES_256_GCM_SHA384) id us-mta-473-86wLxg-MNCC-FDZkFmCEaw-1; Tue,\n 21 Apr 2026 12:34:47 -0400","from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com\n (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17])\n (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest\n SHA256)\n (No client certificate requested)\n by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS\n id AD9D21800464; Tue, 21 Apr 2026 16:34:46 +0000 (UTC)","from corto.redhat.com (unknown [10.44.32.70])\n by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP\n id 9A61B1956095; Tue, 21 Apr 2026 16:34:44 +0000 (UTC)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n s=mimecast20190719; t=1776789293;\n h=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n to:to:cc:cc:mime-version:mime-version:content-type:content-type:\n content-transfer-encoding:content-transfer-encoding:\n in-reply-to:in-reply-to:references:references;\n bh=gZWM2kQuZy+59YJLKvLBs1e6rIWb4i3/E9198uvLvD8=;\n b=aQQBaBSOweyTE5ICGaC61E2inXtFkjqq+p0PmcRTLXfhE26SPhsevPy8w3NWKf8e/esMf3\n zwgCiGHRWdDQ9R87PWp58+Otr3uO+a7p28084prnpcFztUfwwe7kqO5kwgc1Y+yOchmHxb\n QRTwK6i14Cx6/tCUO4Vs/3pqvBGx3KY=","X-MC-Unique":"86wLxg-MNCC-FDZkFmCEaw-1","X-Mimecast-MFC-AGG-ID":"86wLxg-MNCC-FDZkFmCEaw_1776789286","From":"=?utf-8?q?C=C3=A9dric_Le_Goater?= <clg@redhat.com>","To":"qemu-devel@nongnu.org","Cc":"Alex Williamson <alex@shazbot.org>,\n Pierrick Bouvier <pierrick.bouvier@linaro.org>, =?utf-8?q?C=C3=A9dric_Le_Go?=\n\t=?utf-8?q?ater?= <clg@redhat.com>,\n =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= <philmd@linaro.org>","Subject":"[PULL 02/14] hw/vfio/helpers.c: extract kvm helpers in kvm-helpers.c","Date":"Tue, 21 Apr 2026 18:34:26 +0200","Message-ID":"<20260421163438.3447280-3-clg@redhat.com>","In-Reply-To":"<20260421163438.3447280-1-clg@redhat.com>","References":"<20260421163438.3447280-1-clg@redhat.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","X-Scanned-By":"MIMEDefang 3.0 on 10.30.177.17","Received-SPF":"pass client-ip=170.10.129.124; envelope-from=clg@redhat.com;\n helo=us-smtp-delivery-124.mimecast.com","X-Spam_score_int":"-20","X-Spam_score":"-2.1","X-Spam_bar":"--","X-Spam_report":"(-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001,\n DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,\n RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001,\n SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no","X-Spam_action":"no action","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"qemu development <qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<https://lists.nongnu.org/archive/html/qemu-devel>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org"},"content":"From: Pierrick Bouvier <pierrick.bouvier@linaro.org>\n\nBecause those functions use kvm specific types, they need to be isolated\nin another source file.\nThis allows us to link kvm-helpers only in configurations with\nCONFIG_KVM.\n\nReviewed-by: Cédric Le Goater <clg@redhat.com>\nReviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>\nSigned-off-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>\nTested-by: Cédric Le Goater <clg@redhat.com>\nTested-by: Philippe Mathieu-Daudé <philmd@linaro.org>\nLink: https://lore.kernel.org/qemu-devel/20260318174733.1717643-3-pierrick.bouvier@linaro.org\nSigned-off-by: Cédric Le Goater <clg@redhat.com>\n---\n hw/vfio/helpers.c     | 172 -------------------------------------\n hw/vfio/kvm-helpers.c | 192 ++++++++++++++++++++++++++++++++++++++++++\n hw/vfio/kvm-stubs.c   |  26 ++++++\n hw/vfio/meson.build   |   2 +\n 4 files changed, 220 insertions(+), 172 deletions(-)\n create mode 100644 hw/vfio/kvm-helpers.c\n create mode 100644 hw/vfio/kvm-stubs.c","diff":"diff --git a/hw/vfio/helpers.c b/hw/vfio/helpers.c\nindex 00d42d3b98ef98368bb30ff11098615c881cf0a8..65c6dba0428954170ce8a889edffa34ce9b24b13 100644\n--- a/hw/vfio/helpers.c\n+++ b/hw/vfio/helpers.c\n@@ -22,7 +22,6 @@\n #include \"qemu/osdep.h\"\n #include <sys/ioctl.h>\n \n-#include \"system/kvm.h\"\n #include \"exec/cpu-common.h\"\n #include \"hw/vfio/vfio-device.h\"\n #include \"hw/core/hw-error.h\"\n@@ -107,177 +106,6 @@ bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info,\n     return true;\n }\n \n-#ifdef CONFIG_KVM\n-/*\n- * We have a single VFIO pseudo device per KVM VM.  Once created it lives\n- * for the life of the VM.  Closing the file descriptor only drops our\n- * reference to it and the device's reference to kvm.  Therefore once\n- * initialized, this file descriptor is only released on QEMU exit and\n- * we'll re-use it should another vfio device be attached before then.\n- */\n-int vfio_kvm_device_fd = -1;\n-\n-/*\n- * Confidential virtual machines:\n- * During reset of confidential vms, the kvm vm file descriptor changes.\n- * In this case, the old vfio kvm file descriptor is\n- * closed and a new descriptor is created against the new kvm vm file\n- * descriptor.\n- */\n-\n-typedef struct VFIODeviceFd {\n-    int fd;\n-    QLIST_ENTRY(VFIODeviceFd) node;\n-} VFIODeviceFd;\n-\n-static QLIST_HEAD(, VFIODeviceFd) vfio_device_fds =\n-    QLIST_HEAD_INITIALIZER(vfio_device_fds);\n-\n-static void vfio_device_fd_list_add(int fd)\n-{\n-    VFIODeviceFd *file_fd;\n-    file_fd = g_malloc0(sizeof(*file_fd));\n-    file_fd->fd = fd;\n-    QLIST_INSERT_HEAD(&vfio_device_fds, file_fd, node);\n-}\n-\n-static void vfio_device_fd_list_remove(int fd)\n-{\n-    VFIODeviceFd *file_fd, *next;\n-\n-    QLIST_FOREACH_SAFE(file_fd, &vfio_device_fds, node, next) {\n-        if (file_fd->fd == fd) {\n-            QLIST_REMOVE(file_fd, node);\n-            g_free(file_fd);\n-            break;\n-        }\n-    }\n-}\n-\n-static int vfio_device_fd_rebind(NotifierWithReturn *notifier, void *data,\n-                                  Error **errp)\n-{\n-    VFIODeviceFd *file_fd;\n-    struct kvm_device_attr attr = {\n-        .group = KVM_DEV_VFIO_FILE,\n-        .attr = KVM_DEV_VFIO_FILE_ADD,\n-    };\n-    struct kvm_create_device cd = {\n-        .type = KVM_DEV_TYPE_VFIO,\n-    };\n-\n-    /* we are not interested in pre vmfd change notification */\n-    if (((VmfdChangeNotifier *)data)->pre) {\n-        return 0;\n-    }\n-\n-    if (kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &cd)) {\n-        error_setg_errno(errp, errno, \"Failed to create KVM VFIO device\");\n-        return -errno;\n-    }\n-\n-    if (vfio_kvm_device_fd != -1) {\n-        close(vfio_kvm_device_fd);\n-    }\n-\n-    vfio_kvm_device_fd = cd.fd;\n-\n-    QLIST_FOREACH(file_fd, &vfio_device_fds, node) {\n-        attr.addr = (uint64_t)(unsigned long)&file_fd->fd;\n-        if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {\n-            error_setg_errno(errp, errno,\n-                             \"Failed to add fd %d to KVM VFIO device\",\n-                             file_fd->fd);\n-            return -errno;\n-        }\n-    }\n-    return 0;\n-}\n-\n-static struct NotifierWithReturn vfio_vmfd_change_notifier = {\n-    .notify = vfio_device_fd_rebind,\n-};\n-\n-#endif\n-\n-void vfio_kvm_device_close(void)\n-{\n-#ifdef CONFIG_KVM\n-    kvm_close();\n-    if (vfio_kvm_device_fd != -1) {\n-        close(vfio_kvm_device_fd);\n-        vfio_kvm_device_fd = -1;\n-    }\n-#endif\n-}\n-\n-int vfio_kvm_device_add_fd(int fd, Error **errp)\n-{\n-#ifdef CONFIG_KVM\n-    struct kvm_device_attr attr = {\n-        .group = KVM_DEV_VFIO_FILE,\n-        .attr = KVM_DEV_VFIO_FILE_ADD,\n-        .addr = (uint64_t)(unsigned long)&fd,\n-    };\n-\n-    if (!kvm_enabled()) {\n-        return 0;\n-    }\n-\n-    if (vfio_kvm_device_fd < 0) {\n-        struct kvm_create_device cd = {\n-            .type = KVM_DEV_TYPE_VFIO,\n-        };\n-\n-        if (kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &cd)) {\n-            error_setg_errno(errp, errno, \"Failed to create KVM VFIO device\");\n-            return -errno;\n-        }\n-\n-        vfio_kvm_device_fd = cd.fd;\n-        /*\n-         * If the vm file descriptor changes, add a notifier so that we can\n-         * re-create the vfio_kvm_device_fd.\n-         */\n-        kvm_vmfd_add_change_notifier(&vfio_vmfd_change_notifier);\n-    }\n-\n-    if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {\n-        error_setg_errno(errp, errno, \"Failed to add fd %d to KVM VFIO device\",\n-                         fd);\n-        return -errno;\n-    }\n-\n-    vfio_device_fd_list_add(fd);\n-#endif\n-    return 0;\n-}\n-\n-int vfio_kvm_device_del_fd(int fd, Error **errp)\n-{\n-#ifdef CONFIG_KVM\n-    struct kvm_device_attr attr = {\n-        .group = KVM_DEV_VFIO_FILE,\n-        .attr = KVM_DEV_VFIO_FILE_DEL,\n-        .addr = (uint64_t)(unsigned long)&fd,\n-    };\n-\n-    if (vfio_kvm_device_fd < 0) {\n-        error_setg(errp, \"KVM VFIO device isn't created yet\");\n-        return -EINVAL;\n-    }\n-\n-    if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {\n-        error_setg_errno(errp, errno,\n-                         \"Failed to remove fd %d from KVM VFIO device\", fd);\n-        return -errno;\n-    }\n-\n-    vfio_device_fd_list_remove(fd);\n-#endif\n-    return 0;\n-}\n-\n struct vfio_device_info *vfio_get_device_info(int fd)\n {\n     struct vfio_device_info *info;\ndiff --git a/hw/vfio/kvm-helpers.c b/hw/vfio/kvm-helpers.c\nnew file mode 100644\nindex 0000000000000000000000000000000000000000..d71c9590aaa53dc4e436987a34575f57a6397c6b\n--- /dev/null\n+++ b/hw/vfio/kvm-helpers.c\n@@ -0,0 +1,192 @@\n+/*\n+ * low level and IOMMU backend agnostic helpers used by VFIO devices,\n+ * related to regions, interrupts, capabilities\n+ *\n+ * Copyright Red Hat, Inc. 2012\n+ *\n+ * Authors:\n+ *  Alex Williamson <alex.williamson@redhat.com>\n+ *\n+ * SPDX-License-Identifier: GPL-2.0-or-later\n+ *\n+ * Based on qemu-kvm device-assignment:\n+ *  Adapted for KVM by Qumranet.\n+ *  Copyright (c) 2007, Neocleus, Alex Novik (alex@neocleus.com)\n+ *  Copyright (c) 2007, Neocleus, Guy Zana (guy@neocleus.com)\n+ *  Copyright (C) 2008, Qumranet, Amit Shah (amit.shah@qumranet.com)\n+ *  Copyright (C) 2008, Red Hat, Amit Shah (amit.shah@redhat.com)\n+ *  Copyright (C) 2008, IBM, Muli Ben-Yehuda (muli@il.ibm.com)\n+ */\n+\n+#include \"qemu/osdep.h\"\n+#include <sys/ioctl.h>\n+\n+#include <linux/kvm.h>\n+#include \"system/kvm.h\"\n+#include \"exec/cpu-common.h\"\n+#include \"hw/vfio/vfio-device.h\"\n+#include \"hw/core/hw-error.h\"\n+#include \"qapi/error.h\"\n+#include \"vfio-helpers.h\"\n+\n+/*\n+ * We have a single VFIO pseudo device per KVM VM.  Once created it lives\n+ * for the life of the VM.  Closing the file descriptor only drops our\n+ * reference to it and the device's reference to kvm.  Therefore once\n+ * initialized, this file descriptor is only released on QEMU exit and\n+ * we'll re-use it should another vfio device be attached before then.\n+ */\n+int vfio_kvm_device_fd = -1;\n+\n+/*\n+ * Confidential virtual machines:\n+ * During reset of confidential vms, the kvm vm file descriptor changes.\n+ * In this case, the old vfio kvm file descriptor is\n+ * closed and a new descriptor is created against the new kvm vm file\n+ * descriptor.\n+ */\n+\n+typedef struct VFIODeviceFd {\n+    int fd;\n+    QLIST_ENTRY(VFIODeviceFd) node;\n+} VFIODeviceFd;\n+\n+static QLIST_HEAD(, VFIODeviceFd) vfio_device_fds =\n+    QLIST_HEAD_INITIALIZER(vfio_device_fds);\n+\n+static void vfio_device_fd_list_add(int fd)\n+{\n+    VFIODeviceFd *file_fd;\n+    file_fd = g_malloc0(sizeof(*file_fd));\n+    file_fd->fd = fd;\n+    QLIST_INSERT_HEAD(&vfio_device_fds, file_fd, node);\n+}\n+\n+static void vfio_device_fd_list_remove(int fd)\n+{\n+    VFIODeviceFd *file_fd, *next;\n+\n+    QLIST_FOREACH_SAFE(file_fd, &vfio_device_fds, node, next) {\n+        if (file_fd->fd == fd) {\n+            QLIST_REMOVE(file_fd, node);\n+            g_free(file_fd);\n+            break;\n+        }\n+    }\n+}\n+\n+static int vfio_device_fd_rebind(NotifierWithReturn *notifier, void *data,\n+                                  Error **errp)\n+{\n+    VFIODeviceFd *file_fd;\n+    struct kvm_device_attr attr = {\n+        .group = KVM_DEV_VFIO_FILE,\n+        .attr = KVM_DEV_VFIO_FILE_ADD,\n+    };\n+    struct kvm_create_device cd = {\n+        .type = KVM_DEV_TYPE_VFIO,\n+    };\n+\n+    /* we are not interested in pre vmfd change notification */\n+    if (((VmfdChangeNotifier *)data)->pre) {\n+        return 0;\n+    }\n+\n+    if (kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &cd)) {\n+        error_setg_errno(errp, errno, \"Failed to create KVM VFIO device\");\n+        return -errno;\n+    }\n+\n+    if (vfio_kvm_device_fd != -1) {\n+        close(vfio_kvm_device_fd);\n+    }\n+\n+    vfio_kvm_device_fd = cd.fd;\n+\n+    QLIST_FOREACH(file_fd, &vfio_device_fds, node) {\n+        attr.addr = (uint64_t)(unsigned long)&file_fd->fd;\n+        if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {\n+            error_setg_errno(errp, errno,\n+                             \"Failed to add fd %d to KVM VFIO device\",\n+                             file_fd->fd);\n+            return -errno;\n+        }\n+    }\n+    return 0;\n+}\n+\n+static struct NotifierWithReturn vfio_vmfd_change_notifier = {\n+    .notify = vfio_device_fd_rebind,\n+};\n+\n+void vfio_kvm_device_close(void)\n+{\n+    kvm_close();\n+    if (vfio_kvm_device_fd != -1) {\n+        close(vfio_kvm_device_fd);\n+        vfio_kvm_device_fd = -1;\n+    }\n+}\n+\n+int vfio_kvm_device_add_fd(int fd, Error **errp)\n+{\n+    struct kvm_device_attr attr = {\n+        .group = KVM_DEV_VFIO_FILE,\n+        .attr = KVM_DEV_VFIO_FILE_ADD,\n+        .addr = (uint64_t)(unsigned long)&fd,\n+    };\n+\n+    if (!kvm_enabled()) {\n+        return 0;\n+    }\n+\n+    if (vfio_kvm_device_fd < 0) {\n+        struct kvm_create_device cd = {\n+            .type = KVM_DEV_TYPE_VFIO,\n+        };\n+\n+        if (kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &cd)) {\n+            error_setg_errno(errp, errno, \"Failed to create KVM VFIO device\");\n+            return -errno;\n+        }\n+\n+        vfio_kvm_device_fd = cd.fd;\n+        /*\n+         * If the vm file descriptor changes, add a notifier so that we can\n+         * re-create the vfio_kvm_device_fd.\n+         */\n+        kvm_vmfd_add_change_notifier(&vfio_vmfd_change_notifier);\n+    }\n+\n+    if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {\n+        error_setg_errno(errp, errno, \"Failed to add fd %d to KVM VFIO device\",\n+                         fd);\n+        return -errno;\n+    }\n+\n+    vfio_device_fd_list_add(fd);\n+    return 0;\n+}\n+\n+int vfio_kvm_device_del_fd(int fd, Error **errp)\n+{\n+    struct kvm_device_attr attr = {\n+        .group = KVM_DEV_VFIO_FILE,\n+        .attr = KVM_DEV_VFIO_FILE_DEL,\n+        .addr = (uint64_t)(unsigned long)&fd,\n+    };\n+\n+    if (vfio_kvm_device_fd < 0) {\n+        error_setg(errp, \"KVM VFIO device isn't created yet\");\n+        return -EINVAL;\n+    }\n+\n+    if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {\n+        error_setg_errno(errp, errno,\n+                         \"Failed to remove fd %d from KVM VFIO device\", fd);\n+        return -errno;\n+    }\n+\n+    vfio_device_fd_list_remove(fd);\n+    return 0;\n+}\ndiff --git a/hw/vfio/kvm-stubs.c b/hw/vfio/kvm-stubs.c\nnew file mode 100644\nindex 0000000000000000000000000000000000000000..5a489d1b7110a5564054398cc354bb806ac39334\n--- /dev/null\n+++ b/hw/vfio/kvm-stubs.c\n@@ -0,0 +1,26 @@\n+/*\n+ * Stubs for kvm helpers\n+ *\n+ * SPDX-License-Identifier: GPL-2.0-or-later\n+ */\n+\n+#include \"qemu/osdep.h\"\n+\n+#include \"hw/vfio/vfio-device.h\"\n+#include \"qapi/error.h\"\n+#include \"vfio-helpers.h\"\n+\n+void vfio_kvm_device_close(void)\n+{\n+    return;\n+}\n+\n+int vfio_kvm_device_add_fd(int fd, Error **errp)\n+{\n+    return 0;\n+}\n+\n+int vfio_kvm_device_del_fd(int fd, Error **errp)\n+{\n+    return 0;\n+}\ndiff --git a/hw/vfio/meson.build b/hw/vfio/meson.build\nindex 82f68698fb8483196d5ced096f79d1024bf9c4bc..f2a7728d3d00f70b6a6f6e3ea24749e6246b5c43 100644\n--- a/hw/vfio/meson.build\n+++ b/hw/vfio/meson.build\n@@ -7,6 +7,8 @@ vfio_ss.add(files(\n   'container-legacy.c',\n   'helpers.c',\n ))\n+vfio_ss.add(when: 'CONFIG_KVM', if_true: files('kvm-helpers.c'))\n+stub_ss.add(files('kvm-stubs.c'))\n vfio_ss.add(when: 'CONFIG_PSERIES', if_true: files('spapr.c'))\n vfio_ss.add(when: 'CONFIG_VFIO_PCI', if_true: files(\n   'pci-quirks.c',\n","prefixes":["PULL","02/14"]}