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

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