qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Jagannathan Raman <jag.raman@oracle.com>
To: qemu-devel@nongnu.org
Cc: stefanha@redhat.com, mst@redhat.com, f4bug@amsat.org,
	pbonzini@redhat.com,  marcandre.lureau@redhat.com,
	thuth@redhat.com, bleal@redhat.com, berrange@redhat.com,
	eduardo@habkost.net, marcel.apfelbaum@gmail.com,
	eblake@redhat.com, armbru@redhat.com, quintela@redhat.com,
	dgilbert@redhat.com, imammedo@redhat.com, peterx@redhat.com,
	john.levon@nutanix.com, thanos.makatos@nutanix.com,
	elena.ufimtseva@oracle.com, john.g.johnson@oracle.com,
	kanth.ghatraju@oracle.com, jag.raman@oracle.com
Subject: [PATCH v9 02/17] qdev: unplug blocker for devices
Date: Tue,  3 May 2022 10:16:43 -0400	[thread overview]
Message-ID: <01e8950f954c291acd74c9caf1d2016e898cd80c.1651586203.git.jag.raman@oracle.com> (raw)
In-Reply-To: <cover.1651586203.git.jag.raman@oracle.com>

Add blocker to prevent hot-unplug of devices

TYPE_VFIO_USER_SERVER, which is introduced shortly, attaches itself to a
PCIDevice on which it depends. If the attached PCIDevice gets removed
while the server in use, it could cause it crash. To prevent this,
TYPE_VFIO_USER_SERVER adds an unplug blocker for the PCIDevice.

Signed-off-by: Elena Ufimtseva <elena.ufimtseva@oracle.com>
Signed-off-by: John G Johnson <john.g.johnson@oracle.com>
Signed-off-by: Jagannathan Raman <jag.raman@oracle.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 include/hw/qdev-core.h | 29 +++++++++++++++++++++++++++++
 hw/core/qdev.c         | 24 ++++++++++++++++++++++++
 softmmu/qdev-monitor.c |  4 ++++
 3 files changed, 57 insertions(+)

diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
index 92c3d65208..1b9fa25e5c 100644
--- a/include/hw/qdev-core.h
+++ b/include/hw/qdev-core.h
@@ -193,6 +193,7 @@ struct DeviceState {
     int instance_id_alias;
     int alias_required_for_version;
     ResettableState reset;
+    GSList *unplug_blockers;
 };
 
 struct DeviceListener {
@@ -419,6 +420,34 @@ void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,
 void qdev_machine_creation_done(void);
 bool qdev_machine_modified(void);
 
+/*
+ * qdev_add_unplug_blocker: Adds an unplug blocker to a device
+ *
+ * @dev: Device to be blocked from unplug
+ * @reason: Reason for blocking
+ */
+void qdev_add_unplug_blocker(DeviceState *dev, Error *reason);
+
+/*
+ * qdev_del_unplug_blocker: Removes an unplug blocker from a device
+ *
+ * @dev: Device to be unblocked
+ * @reason: Pointer to the Error used with qdev_add_unplug_blocker.
+ *          Used as a handle to lookup the blocker for deletion.
+ */
+void qdev_del_unplug_blocker(DeviceState *dev, Error *reason);
+
+/*
+ * qdev_unplug_blocked: Confirms if a device is blocked from unplug
+ *
+ * @dev: Device to be tested
+ * @reason: Returns one of the reasons why the device is blocked,
+ *          if any
+ *
+ * Returns: true if device is blocked from unplug, false otherwise
+ */
+bool qdev_unplug_blocked(DeviceState *dev, Error **errp);
+
 /**
  * GpioPolarity: Polarity of a GPIO line
  *
diff --git a/hw/core/qdev.c b/hw/core/qdev.c
index 84f3019440..0806d8fcaa 100644
--- a/hw/core/qdev.c
+++ b/hw/core/qdev.c
@@ -468,6 +468,28 @@ char *qdev_get_dev_path(DeviceState *dev)
     return NULL;
 }
 
+void qdev_add_unplug_blocker(DeviceState *dev, Error *reason)
+{
+    dev->unplug_blockers = g_slist_prepend(dev->unplug_blockers, reason);
+}
+
+void qdev_del_unplug_blocker(DeviceState *dev, Error *reason)
+{
+    dev->unplug_blockers = g_slist_remove(dev->unplug_blockers, reason);
+}
+
+bool qdev_unplug_blocked(DeviceState *dev, Error **errp)
+{
+    ERRP_GUARD();
+
+    if (dev->unplug_blockers) {
+        error_propagate(errp, error_copy(dev->unplug_blockers->data));
+        return true;
+    }
+
+    return false;
+}
+
 static bool device_get_realized(Object *obj, Error **errp)
 {
     DeviceState *dev = DEVICE(obj);
@@ -704,6 +726,8 @@ static void device_finalize(Object *obj)
 
     DeviceState *dev = DEVICE(obj);
 
+    g_assert(!dev->unplug_blockers);
+
     QLIST_FOREACH_SAFE(ngl, &dev->gpios, node, next) {
         QLIST_REMOVE(ngl, node);
         qemu_free_irqs(ngl->in, ngl->num_in);
diff --git a/softmmu/qdev-monitor.c b/softmmu/qdev-monitor.c
index 12fe60c467..9cfd59d17c 100644
--- a/softmmu/qdev-monitor.c
+++ b/softmmu/qdev-monitor.c
@@ -898,6 +898,10 @@ void qdev_unplug(DeviceState *dev, Error **errp)
     HotplugHandlerClass *hdc;
     Error *local_err = NULL;
 
+    if (qdev_unplug_blocked(dev, errp)) {
+        return;
+    }
+
     if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) {
         error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
         return;
-- 
2.20.1



  parent reply	other threads:[~2022-05-03 14:21 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-03 14:16 [PATCH v9 00/17] vfio-user server in QEMU Jagannathan Raman
2022-05-03 14:16 ` [PATCH v9 01/17] tests/avocado: Specify target VM argument to helper routines Jagannathan Raman
2022-05-03 14:16 ` Jagannathan Raman [this message]
2022-05-04 11:13   ` [PATCH v9 02/17] qdev: unplug blocker for devices Markus Armbruster
2022-05-04 14:00     ` Jag Raman
2022-05-03 14:16 ` [PATCH v9 03/17] remote/machine: add HotplugHandler for remote machine Jagannathan Raman
2022-05-03 14:16 ` [PATCH v9 04/17] remote/machine: add vfio-user property Jagannathan Raman
2022-05-03 14:16 ` [PATCH v9 05/17] configure: require cmake 3.19 or newer Jagannathan Raman
2022-05-05 15:31   ` Stefan Hajnoczi
2022-05-03 14:16 ` [PATCH v9 06/17] vfio-user: build library Jagannathan Raman
2022-05-05 15:39   ` Stefan Hajnoczi
2022-05-05 15:41     ` Daniel P. Berrangé
2022-05-05 16:17     ` Peter Maydell
2022-05-10 13:22       ` Daniel P. Berrangé
2022-05-10 16:19         ` Jag Raman
2022-05-03 14:16 ` [PATCH v9 07/17] vfio-user: define vfio-user-server object Jagannathan Raman
2022-05-04 11:45   ` Markus Armbruster
2022-05-04 15:24     ` Jag Raman
2022-05-05  5:52       ` Markus Armbruster
2022-05-05 15:14     ` Stefan Hajnoczi
2022-05-05 15:22       ` Markus Armbruster
2022-05-05 15:37         ` Jag Raman
2022-05-05 15:41   ` Stefan Hajnoczi
2022-05-03 14:16 ` [PATCH v9 08/17] vfio-user: instantiate vfio-user context Jagannathan Raman
2022-05-03 14:16 ` [PATCH v9 09/17] vfio-user: find and init PCI device Jagannathan Raman
2022-05-03 14:16 ` [PATCH v9 10/17] vfio-user: run vfio-user context Jagannathan Raman
2022-05-04 11:42   ` Markus Armbruster
2022-05-04 15:22     ` Jag Raman
2022-05-05  7:44       ` Markus Armbruster
2022-05-05 13:39         ` Jag Raman
2022-05-05 14:42           ` Markus Armbruster
2022-05-05 15:36             ` Jag Raman
2022-05-06  5:44               ` Markus Armbruster
2022-05-07 18:48                 ` Jag Raman
2022-05-03 14:16 ` [PATCH v9 11/17] vfio-user: handle PCI config space accesses Jagannathan Raman
2022-05-03 14:16 ` [PATCH v9 12/17] vfio-user: IOMMU support for remote device Jagannathan Raman
2022-05-05 16:12   ` Stefan Hajnoczi
2022-05-03 14:16 ` [PATCH v9 13/17] vfio-user: handle DMA mappings Jagannathan Raman
2022-05-03 14:16 ` [PATCH v9 14/17] vfio-user: handle PCI BAR accesses Jagannathan Raman
2022-05-05 15:43   ` Stefan Hajnoczi
2022-05-03 14:16 ` [PATCH v9 15/17] vfio-user: handle device interrupts Jagannathan Raman
2022-05-03 14:16 ` [PATCH v9 16/17] vfio-user: handle reset of remote device Jagannathan Raman
2022-05-03 14:16 ` [PATCH v9 17/17] vfio-user: avocado tests for vfio-user Jagannathan Raman
2022-05-05 16:04   ` Stefan Hajnoczi
2022-05-05 17:33     ` Jag Raman
2022-05-04 11:37 ` [PATCH v9 00/17] vfio-user server in QEMU Markus Armbruster
2022-05-04 14:26   ` Jag Raman
2022-05-04 15:03     ` Markus Armbruster

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=01e8950f954c291acd74c9caf1d2016e898cd80c.1651586203.git.jag.raman@oracle.com \
    --to=jag.raman@oracle.com \
    --cc=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=bleal@redhat.com \
    --cc=dgilbert@redhat.com \
    --cc=eblake@redhat.com \
    --cc=eduardo@habkost.net \
    --cc=elena.ufimtseva@oracle.com \
    --cc=f4bug@amsat.org \
    --cc=imammedo@redhat.com \
    --cc=john.g.johnson@oracle.com \
    --cc=john.levon@nutanix.com \
    --cc=kanth.ghatraju@oracle.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=quintela@redhat.com \
    --cc=stefanha@redhat.com \
    --cc=thanos.makatos@nutanix.com \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).