cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
432cb7
From afe1a63fe0cf863e024889edd82b9a380bfa8230 Mon Sep 17 00:00:00 2001
432cb7
From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= <berrange@redhat.com>
432cb7
Date: Wed, 5 Jan 2022 12:38:47 +0000
432cb7
Subject: [PATCH 2/6] softmmu: fix device deletion events with -device JSON
432cb7
 syntax
432cb7
MIME-Version: 1.0
432cb7
Content-Type: text/plain; charset=UTF-8
432cb7
Content-Transfer-Encoding: 8bit
432cb7
432cb7
RH-Author: Kevin Wolf <kwolf@redhat.com>
432cb7
RH-MergeRequest: 103: Fix hot unplug of devices created with -device JSON syntax
432cb7
RH-Commit: [1/1] 64cbc78bcb46bdb24d5f589ceb5ad598c388e447
432cb7
RH-Bugzilla: 2033279
432cb7
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
432cb7
RH-Acked-by: Thomas Huth <thuth@redhat.com>
432cb7
RH-Acked-by: Jano Tomko <None>
432cb7
RH-Acked-by: Daniel P. Berrangé <berrange@redhat.com>
432cb7
432cb7
The -device JSON syntax impl leaks a reference on the created
432cb7
DeviceState instance. As a result when you hot-unplug the
432cb7
device, the device_finalize method won't be called and thus
432cb7
it will fail to emit the required DEVICE_DELETED event.
432cb7
432cb7
A 'json-cli' feature was previously added against the
432cb7
'device_add' QMP command QAPI schema to indicated to mgmt
432cb7
apps that -device supported JSON syntax. Given the hotplug
432cb7
bug that feature flag is not usable for its purpose, so
432cb7
we add a new 'json-cli-hotplug' feature to indicate the
432cb7
-device supports JSON without breaking hotplug.
432cb7
432cb7
Fixes: 5dacda5167560b3af8eadbce5814f60ba44b467e
432cb7
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/802
432cb7
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
432cb7
Message-Id: <20220105123847.4047954-2-berrange@redhat.com>
432cb7
Reviewed-by: Laurent Vivier <lvivier@redhat.com>
432cb7
Tested-by: Ján Tomko <jtomko@redhat.com>
432cb7
Reviewed-by: Thomas Huth <thuth@redhat.com>
432cb7
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
432cb7
(cherry picked from commit 64b4529a432507ee84a924be69a03432639e87ba)
432cb7
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
432cb7
---
432cb7
 qapi/qdev.json                 |  5 ++++-
432cb7
 softmmu/vl.c                   |  4 +++-
432cb7
 tests/qtest/device-plug-test.c | 19 +++++++++++++++++++
432cb7
 3 files changed, 26 insertions(+), 2 deletions(-)
432cb7
432cb7
diff --git a/qapi/qdev.json b/qapi/qdev.json
432cb7
index 69656b14df..26cd10106b 100644
432cb7
--- a/qapi/qdev.json
432cb7
+++ b/qapi/qdev.json
432cb7
@@ -44,6 +44,9 @@
432cb7
 # @json-cli: If present, the "-device" command line option supports JSON
432cb7
 #            syntax with a structure identical to the arguments of this
432cb7
 #            command.
432cb7
+# @json-cli-hotplug: If present, the "-device" command line option supports JSON
432cb7
+#                    syntax without the reference counting leak that broke
432cb7
+#                    hot-unplug
432cb7
 #
432cb7
 # Notes:
432cb7
 #
432cb7
@@ -74,7 +77,7 @@
432cb7
 { 'command': 'device_add',
432cb7
   'data': {'driver': 'str', '*bus': 'str', '*id': 'str'},
432cb7
   'gen': false, # so we can get the additional arguments
432cb7
-  'features': ['json-cli'] }
432cb7
+  'features': ['json-cli', 'json-cli-hotplug'] }
432cb7
 
432cb7
 ##
432cb7
 # @device_del:
432cb7
diff --git a/softmmu/vl.c b/softmmu/vl.c
432cb7
index d46b8fb4ab..b3829e2edd 100644
432cb7
--- a/softmmu/vl.c
432cb7
+++ b/softmmu/vl.c
432cb7
@@ -2690,6 +2690,7 @@ static void qemu_create_cli_devices(void)
432cb7
     qemu_opts_foreach(qemu_find_opts("device"),
432cb7
                       device_init_func, NULL, &error_fatal);
432cb7
     QTAILQ_FOREACH(opt, &device_opts, next) {
432cb7
+        DeviceState *dev;
432cb7
         loc_push_restore(&opt->loc);
432cb7
         /*
432cb7
          * TODO Eventually we should call qmp_device_add() here to make sure it
432cb7
@@ -2698,7 +2699,8 @@ static void qemu_create_cli_devices(void)
432cb7
          * from the start, so call qdev_device_add_from_qdict() directly for
432cb7
          * now.
432cb7
          */
432cb7
-        qdev_device_add_from_qdict(opt->opts, true, &error_fatal);
432cb7
+        dev = qdev_device_add_from_qdict(opt->opts, true, &error_fatal);
432cb7
+        object_unref(OBJECT(dev));
432cb7
         loc_pop(&opt->loc);
432cb7
     }
432cb7
     rom_reset_order_override();
432cb7
diff --git a/tests/qtest/device-plug-test.c b/tests/qtest/device-plug-test.c
432cb7
index 559d47727a..ad79bd4c14 100644
432cb7
--- a/tests/qtest/device-plug-test.c
432cb7
+++ b/tests/qtest/device-plug-test.c
432cb7
@@ -77,6 +77,23 @@ static void test_pci_unplug_request(void)
432cb7
     qtest_quit(qtest);
432cb7
 }
432cb7
 
432cb7
+static void test_pci_unplug_json_request(void)
432cb7
+{
432cb7
+    QTestState *qtest = qtest_initf(
432cb7
+        "-device '{\"driver\": \"virtio-mouse-pci\", \"id\": \"dev0\"}'");
432cb7
+
432cb7
+    /*
432cb7
+     * Request device removal. As the guest is not running, the request won't
432cb7
+     * be processed. However during system reset, the removal will be
432cb7
+     * handled, removing the device.
432cb7
+     */
432cb7
+    device_del(qtest, "dev0");
432cb7
+    system_reset(qtest);
432cb7
+    wait_device_deleted_event(qtest, "dev0");
432cb7
+
432cb7
+    qtest_quit(qtest);
432cb7
+}
432cb7
+
432cb7
 static void test_ccw_unplug(void)
432cb7
 {
432cb7
     QTestState *qtest = qtest_initf("-device virtio-balloon-ccw,id=dev0");
432cb7
@@ -145,6 +162,8 @@ int main(int argc, char **argv)
432cb7
      */
432cb7
     qtest_add_func("/device-plug/pci-unplug-request",
432cb7
                    test_pci_unplug_request);
432cb7
+    qtest_add_func("/device-plug/pci-unplug-json-request",
432cb7
+                   test_pci_unplug_json_request);
432cb7
 
432cb7
     if (!strcmp(arch, "s390x")) {
432cb7
         qtest_add_func("/device-plug/ccw-unplug",
432cb7
-- 
432cb7
2.27.0
432cb7