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

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