]> www.infradead.org Git - users/dwmw2/qemu.git/commitdiff
hw/xen: Set XenBackendInstance in the XenDevice before realizing it
authorDavid Woodhouse <dwmw@amazon.co.uk>
Sun, 12 Nov 2023 21:49:21 +0000 (16:49 -0500)
committerDavid Woodhouse <dwmw@amazon.co.uk>
Mon, 13 Nov 2023 23:31:53 +0000 (18:31 -0500)
This allows a XenDevice implementation to know whether it was created
by QEMU, or merely discovered in XenStore after the toolstack created
it. This will allow us to create frontend/backend nodes only when we
should, rather than unconditionally attempting to overwrite them from
a driver domain which doesn't have privileges to do so.

As an added benefit, it also means we no longer have to call the
xen_backend_set_device() function from the device models immediately
after calling qdev_realize_and_unref(). Even though we could make
the argument that it's safe to do so, and the pointer to the unreffed
device *will* actually still be valid, it still made my skin itch to
look at it.

Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
hw/block/xen-block.c
hw/char/xen_console.c
hw/net/xen_nic.c
hw/xen/xen-bus.c
include/hw/xen/xen-backend.h
include/hw/xen/xen-bus.h

index aed1d5c330b2a5925b55a354ac6127232bea9675..cc7a5b75d2a50cc76f3b1f3edad3513397ebc22f 100644 (file)
@@ -1099,13 +1099,12 @@ static void xen_block_device_create(XenBackendInstance *backend,
 
     blockdev->iothread = iothread;
     blockdev->drive = drive;
+    xendev->backend = backend;
 
     if (!qdev_realize_and_unref(DEVICE(xendev), BUS(xenbus), errp)) {
         error_prepend(errp, "realization of device %s failed: ", type);
         goto fail;
     }
-
-    xen_backend_set_device(backend, xendev);
     return;
 
 fail:
index 5cbee2f184d8710810ab52f424bde8c0c7057b6d..bef8a3a62189cd1b7f8341e209de9199cdf2c4e0 100644 (file)
@@ -600,8 +600,8 @@ static void xen_console_device_create(XenBackendInstance *backend,
         goto fail;
     }
 
+    xendev->backend = backend;
     if (qdev_realize_and_unref(DEVICE(xendev), BUS(xenbus), errp)) {
-        xen_backend_set_device(backend, xendev);
         goto done;
     }
 
index af4ba3f1e66c13190eb1d0bf44f9e91d7a3080c6..afa10c96e887b9f4b3962644f85a340db45418b2 100644 (file)
@@ -627,8 +627,8 @@ static void xen_net_device_create(XenBackendInstance *backend,
     net->dev = number;
     memcpy(&net->conf.macaddr, &mac, sizeof(mac));
 
+    xendev->backend = backend;
     if (qdev_realize_and_unref(DEVICE(xendev), BUS(xenbus), errp)) {
-        xen_backend_set_device(backend, xendev);
         return;
     }
 
index fb82cc33e48bcd06dc9c46b942bd7d6ac0317613..e2c5006bfb4176f9944b4bad619429a64ddb1d33 100644 (file)
@@ -1080,6 +1080,10 @@ static void xen_device_realize(DeviceState *dev, Error **errp)
         }
     }
 
+    if (xendev->backend) {
+        xen_backend_set_device(xendev->backend, xendev);
+    }
+
     xendev->exit.notify = xen_device_exit;
     qemu_add_exit_notifier(&xendev->exit);
     return;
index 0f01631ae7a44bb8eb322869b1c4c958a5bbaf13..ea080ba7c988098744595fed767c6928fb6a6b24 100644 (file)
@@ -10,8 +10,6 @@
 
 #include "hw/xen/xen-bus.h"
 
-typedef struct XenBackendInstance XenBackendInstance;
-
 typedef void (*XenBackendDeviceCreate)(XenBackendInstance *backend,
                                        QDict *opts, Error **errp);
 typedef void (*XenBackendDeviceDestroy)(XenBackendInstance *backend,
index 38d40afa37989097b476174eb6985c160ed4eb85..5e55b984abad8c3403269f5431679c98073c81b2 100644 (file)
 #include "qom/object.h"
 
 typedef struct XenEventChannel XenEventChannel;
+typedef struct XenBackendInstance XenBackendInstance;
 
 struct XenDevice {
     DeviceState qdev;
+    XenBackendInstance *backend;
     domid_t frontend_id;
     char *name;
     struct qemu_xs_handle *xsh;