Blame SOURCES/kvm-softmmu-fix-device-deletion-events-with-device-JSON-.patch

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