From: David Woodhouse The XenbusStateInitWait transition tells the frontend that the backend's parameters are readable, but xen_device_realize() set it before calling the implementation's realize method — which is what writes the feature nodes ('max-ring-page-order' et al. for xen-block, 'feature-rx-copy' for xen-net). Linux blkfront reads 'max-ring-page-order' with xenbus_read_unsigned(): no wait, no retry, default 0. Lose the race and it silently negotiates a single-page ring — 32 request slots instead of 256. A device hot-plugged into a running guest loses essentially every time; at boot the frontend doesn't come looking until long after realize finished. Netfront reads its feature nodes the same way. Commit 240cc11369fc ("hw/xen: Avoid crash when backend watch fires too early") introduced this by moving the realize call after the block of code which both populates the initial xenstore nodes *and* sets InitWait. The crash it fixed was real — xen-block's realize processes watch events via blkconf_geometry()'s aio_poll(), and the handlers fell over when the nodes didn't yet exist — but only the node population needed to come first. So keep the node population and the 'online' write before realize, and move only the InitWait transition after it. A watch event during realize now finds the frontend in Initialising and no backend 'state' node, which the handlers tolerate; none of the three realize implementations reads the backend state. Verified against the original 2023 crash reproducer as well as the ring-order negotiation. Fixes: 240cc11369fc ("hw/xen: Avoid crash when backend watch fires too early") Reported-by: Mark Syms Signed-off-by: David Woodhouse --- hw/xen/xen-bus.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/hw/xen/xen-bus.c b/hw/xen/xen-bus.c index 17bbbe2ea71..b75ce7f0957 100644 --- a/hw/xen/xen-bus.c +++ b/hw/xen/xen-bus.c @@ -1082,7 +1082,6 @@ static void xen_device_realize(DeviceState *dev, Error **errp) xen_device_backend_printf(xendev, "hotplug-status", "connected"); xen_device_backend_set_online(xendev, true); - xen_device_backend_set_state(xendev, XenbusStateInitWait); if (!xen_device_frontend_exists(xendev)) { xen_device_frontend_printf(xendev, "backend", "%s", @@ -1093,6 +1092,19 @@ static void xen_device_realize(DeviceState *dev, Error **errp) xen_device_frontend_set_state(xendev, XenbusStateInitialising, true); } + /* + * The implementation's realize method may write feature nodes to + * the backend area (e.g. 'max-ring-page-order' for xen-block, + * 'feature-rx-copy' for xen-net). It must be called after the + * backend and frontend areas are populated, because it can invoke + * aio_poll() (e.g. through blkconf_geometry()) and thus process + * watch events which need to read the 'state' nodes. But it must + * be called *before* the backend state is set to InitWait below, + * because InitWait is the frontend's cue that the backend's + * feature nodes can be read; a frontend which reads them promptly + * (as Linux blkfront and netfront do, without waiting or retrying) + * must not find them missing. + */ if (xendev_class->realize) { xendev_class->realize(xendev, errp); if (*errp) { @@ -1100,6 +1112,8 @@ static void xen_device_realize(DeviceState *dev, Error **errp) } } + xen_device_backend_set_state(xendev, XenbusStateInitWait); + xendev->exit.notify = xen_device_exit; qemu_add_exit_notifier(&xendev->exit); return; -- 2.43.0