Xen regression fixes and cleanups

-----BEGIN PGP SIGNATURE-----
 
 iQJGBAABCAAwFiEEMUsIrNDeSBEzpfKGm+mA/QrAFUQFAmeIxhcSHGR3bXdAYW1h
 em9uLmNvLnVrAAoJEJvpgP0KwBVEbKoP/iqQ+PhbwT9+xz6lxW+g1Dx+YGrT/ugp
 d3xHn9AEkR0EHC42J6RB/llyWbKVD/IIhYwUk5GDm+4InGrtuQDhG6UqWxqvIRht
 0JuZvVm7x5akmKv73igxNqZHVg0ZEAS+EllBUaBYWj0pvpMbBK93Sdz9PXKxA7Nm
 dPeFrOpL2TAmnDCH1UuBbXypHEjAghmv7WFphMtk6qLX+wYVaK3F2J/ed2TNyT0V
 LliOdQH0Pxt445SSVJIZRe9bW3FH7qyvZV1gCnxSnqPUlN7vBhpjzgl4hWEzVYcp
 7X21ZAD9kPc81DJjYucbLjAbrqSmlDrJqL05qtRigfPcnqz2NoKrYxhj8B0F8mgt
 1IbymPyeab5gk5Hi1QgMmG5eobDDaglDSxpq6gRfJBiJW+1adif00z/HVvt5onS0
 uQ6i6w5NzQciBX77muAb2ZDEMysjk+3wSJMMpkfl90D0kjlMqeWWs4FH9ThasjC+
 EhQioUD0euedgnzOSfQjNNtAW4gzv9rcShkcV84bjxP/0Es+Pgx9f6wtCUTzdeqy
 Cid8/72lHIgrkZGfpv8BBZkA1XP09vgtUGKyAWm4yHOcB57l8cNiL1nKtqoCLwkQ
 8JWFWzFeEY19KoiRGY5saH6ExeOx8fmc/lYwqImZqFqvuFX4Vf2RJdTIRIYr7g05
 2QffxFmskg+A
 =Wz0V
 -----END PGP SIGNATURE-----

Merge tag 'pull-xenfv-20250116' of git://git.infradead.org/users/dwmw2/qemu into staging

Xen regression fixes and cleanups

# -----BEGIN PGP SIGNATURE-----
#
# iQJGBAABCAAwFiEEMUsIrNDeSBEzpfKGm+mA/QrAFUQFAmeIxhcSHGR3bXdAYW1h
# em9uLmNvLnVrAAoJEJvpgP0KwBVEbKoP/iqQ+PhbwT9+xz6lxW+g1Dx+YGrT/ugp
# d3xHn9AEkR0EHC42J6RB/llyWbKVD/IIhYwUk5GDm+4InGrtuQDhG6UqWxqvIRht
# 0JuZvVm7x5akmKv73igxNqZHVg0ZEAS+EllBUaBYWj0pvpMbBK93Sdz9PXKxA7Nm
# dPeFrOpL2TAmnDCH1UuBbXypHEjAghmv7WFphMtk6qLX+wYVaK3F2J/ed2TNyT0V
# LliOdQH0Pxt445SSVJIZRe9bW3FH7qyvZV1gCnxSnqPUlN7vBhpjzgl4hWEzVYcp
# 7X21ZAD9kPc81DJjYucbLjAbrqSmlDrJqL05qtRigfPcnqz2NoKrYxhj8B0F8mgt
# 1IbymPyeab5gk5Hi1QgMmG5eobDDaglDSxpq6gRfJBiJW+1adif00z/HVvt5onS0
# uQ6i6w5NzQciBX77muAb2ZDEMysjk+3wSJMMpkfl90D0kjlMqeWWs4FH9ThasjC+
# EhQioUD0euedgnzOSfQjNNtAW4gzv9rcShkcV84bjxP/0Es+Pgx9f6wtCUTzdeqy
# Cid8/72lHIgrkZGfpv8BBZkA1XP09vgtUGKyAWm4yHOcB57l8cNiL1nKtqoCLwkQ
# 8JWFWzFeEY19KoiRGY5saH6ExeOx8fmc/lYwqImZqFqvuFX4Vf2RJdTIRIYr7g05
# 2QffxFmskg+A
# =Wz0V
# -----END PGP SIGNATURE-----
# gpg: Signature made Thu 16 Jan 2025 03:40:55 EST
# gpg:                using RSA key 314B08ACD0DE481133A5F2869BE980FD0AC01544
# gpg:                issuer "dwmw@amazon.co.uk"
# gpg: Good signature from "David Woodhouse <dwmw@amazon.co.uk>" [unknown]
# gpg:                 aka "David Woodhouse <dwmw@amazon.com>" [unknown]
# gpg: WARNING: The key's User ID is not certified with a trusted signature!
# gpg:          There is no indication that the signature belongs to the owner.
# Primary key fingerprint: 314B 08AC D0DE 4811 33A5  F286 9BE9 80FD 0AC0 1544

* tag 'pull-xenfv-20250116' of git://git.infradead.org/users/dwmw2/qemu:
  system/runstate: Fix regression, clarify BQL status of exit notifiers
  hw/xen: Fix errp handling in xen_console
  hw/xen: Use xs_node_read() from xenstore_read_str() instead of open-coding it
  hw/xen: Use xs_node_read() from xen_netdev_get_name()
  hw/xen: Use xs_node_read() from xen_console_get_name()
  hw/xen: Use xs_node_read() from xs_node_vscanf()
  xen: do not use '%ms' scanf specifier
  hw/xen: Add xs_node_read() helper function

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
This commit is contained in:
Stefan Hajnoczi 2025-01-16 09:03:43 -05:00
commit 4d5d933bbc
11 changed files with 96 additions and 47 deletions

View File

@ -239,7 +239,8 @@ static void xen_block_connect(XenDevice *xendev, Error **errp)
return; return;
} }
if (xen_device_frontend_scanf(xendev, "protocol", "%ms", &str) != 1) { str = xen_device_frontend_read(xendev, "protocol");
if (!str) {
/* x86 defaults to the 32-bit protocol even for 64-bit guests. */ /* x86 defaults to the 32-bit protocol even for 64-bit guests. */
if (object_dynamic_cast(OBJECT(qdev_get_machine()), "x86-machine")) { if (object_dynamic_cast(OBJECT(qdev_get_machine()), "x86-machine")) {
protocol = BLKIF_PROTOCOL_X86_32; protocol = BLKIF_PROTOCOL_X86_32;

View File

@ -367,28 +367,28 @@ static char *xen_console_get_name(XenDevice *xendev, Error **errp)
if (con->dev == -1) { if (con->dev == -1) {
XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev))); XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
char fe_path[XENSTORE_ABS_PATH_MAX + 1];
int idx = (xen_mode == XEN_EMULATE) ? 0 : 1; int idx = (xen_mode == XEN_EMULATE) ? 0 : 1;
Error *local_err = NULL;
char *value; char *value;
/* Theoretically we could go up to INT_MAX here but that's overkill */ /* Theoretically we could go up to INT_MAX here but that's overkill */
while (idx < 100) { while (idx < 100) {
if (!idx) { if (!idx) {
snprintf(fe_path, sizeof(fe_path), value = xs_node_read(xenbus->xsh, XBT_NULL, NULL, &local_err,
"/local/domain/%u/console", xendev->frontend_id); "/local/domain/%u/console",
xendev->frontend_id);
} else { } else {
snprintf(fe_path, sizeof(fe_path), value = xs_node_read(xenbus->xsh, XBT_NULL, NULL, &local_err,
"/local/domain/%u/device/console/%u", "/local/domain/%u/device/console/%u",
xendev->frontend_id, idx); xendev->frontend_id, idx);
} }
value = qemu_xen_xs_read(xenbus->xsh, XBT_NULL, fe_path, NULL);
if (!value) { if (!value) {
if (errno == ENOENT) { if (errno == ENOENT) {
con->dev = idx; con->dev = idx;
error_free(local_err);
goto found; goto found;
} }
error_setg(errp, "cannot read %s: %s", fe_path, error_propagate(errp, local_err);
strerror(errno));
return NULL; return NULL;
} }
free(value); free(value);
@ -550,7 +550,8 @@ static void xen_console_device_create(XenBackendInstance *backend,
goto fail; goto fail;
} }
if (xs_node_scanf(xsh, XBT_NULL, fe, "type", errp, "%ms", &type) != 1) { type = xs_node_read(xsh, XBT_NULL, NULL, errp, "%s/%s", fe, "type");
if (!type) {
error_prepend(errp, "failed to read console device type: "); error_prepend(errp, "failed to read console device type: ");
goto fail; goto fail;
} }
@ -568,7 +569,8 @@ static void xen_console_device_create(XenBackendInstance *backend,
snprintf(label, sizeof(label), "xencons%ld", number); snprintf(label, sizeof(label), "xencons%ld", number);
if (xs_node_scanf(xsh, XBT_NULL, fe, "output", NULL, "%ms", &output) == 1) { output = xs_node_read(xsh, XBT_NULL, NULL, errp, "%s/%s", fe, "output");
if (output) {
/* /*
* FIXME: sure we want to support implicit * FIXME: sure we want to support implicit
* muxed monitors here? * muxed monitors here?
@ -579,19 +581,27 @@ static void xen_console_device_create(XenBackendInstance *backend,
output); output);
goto fail; goto fail;
} }
} else if (number) { } else if (errno != ENOENT) {
cd = serial_hd(number); error_prepend(errp, "console: No valid chardev found: ");
if (!cd) { goto fail;
error_prepend(errp, "console: No serial device #%ld found: ",
number);
goto fail;
}
} else { } else {
/* No 'output' node on primary console: use null. */ error_free(*errp);
cd = qemu_chr_new(label, "null", NULL); *errp = NULL;
if (!cd) {
error_setg(errp, "console: failed to create null device"); if (number) {
goto fail; cd = serial_hd(number);
if (!cd) {
error_setg(errp, "console: No serial device #%ld found",
number);
goto fail;
}
} else {
/* No 'output' node on primary console: use null. */
cd = qemu_chr_new(label, "null", NULL);
if (!cd) {
error_setg(errp, "console: failed to create null device");
goto fail;
}
} }
} }

View File

@ -510,23 +510,22 @@ static char *xen_netdev_get_name(XenDevice *xendev, Error **errp)
if (netdev->dev == -1) { if (netdev->dev == -1) {
XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev))); XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
char fe_path[XENSTORE_ABS_PATH_MAX + 1];
int idx = (xen_mode == XEN_EMULATE) ? 0 : 1; int idx = (xen_mode == XEN_EMULATE) ? 0 : 1;
Error *local_err = NULL;
char *value; char *value;
/* Theoretically we could go up to INT_MAX here but that's overkill */ /* Theoretically we could go up to INT_MAX here but that's overkill */
while (idx < 100) { while (idx < 100) {
snprintf(fe_path, sizeof(fe_path), value = xs_node_read(xenbus->xsh, XBT_NULL, NULL, &local_err,
"/local/domain/%u/device/vif/%u", "/local/domain/%u/device/vif/%u",
xendev->frontend_id, idx); xendev->frontend_id, idx);
value = qemu_xen_xs_read(xenbus->xsh, XBT_NULL, fe_path, NULL);
if (!value) { if (!value) {
if (errno == ENOENT) { if (errno == ENOENT) {
netdev->dev = idx; netdev->dev = idx;
error_free(local_err);
goto found; goto found;
} }
error_setg(errp, "cannot read %s: %s", fe_path, error_propagate(errp, local_err);
strerror(errno));
return NULL; return NULL;
} }
free(value); free(value);

View File

@ -38,7 +38,7 @@ xen_device_remove_watch(const char *type, char *name, const char *node, const ch
xs_node_create(const char *node) "%s" xs_node_create(const char *node) "%s"
xs_node_destroy(const char *node) "%s" xs_node_destroy(const char *node) "%s"
xs_node_vprintf(char *path, char *value) "%s %s" xs_node_vprintf(char *path, char *value) "%s %s"
xs_node_vscanf(char *path, char *value) "%s %s" xs_node_read(const char *path, const char *value) "%s %s"
xs_node_watch(char *path) "%s" xs_node_watch(char *path) "%s"
xs_node_unwatch(char *path) "%s" xs_node_unwatch(char *path) "%s"

View File

@ -105,25 +105,22 @@ int xs_node_vscanf(struct qemu_xs_handle *h, xs_transaction_t tid,
const char *node, const char *key, Error **errp, const char *node, const char *key, Error **errp,
const char *fmt, va_list ap) const char *fmt, va_list ap)
{ {
char *path, *value; char *value;
int rc; int rc;
path = (strlen(node) != 0) ? g_strdup_printf("%s/%s", node, key) : if (node && strlen(node) != 0) {
g_strdup(key); value = xs_node_read(h, tid, NULL, errp, "%s/%s", node, key);
value = qemu_xen_xs_read(h, tid, path, NULL); } else {
value = xs_node_read(h, tid, NULL, errp, "%s", key);
trace_xs_node_vscanf(path, value); }
if (value) { if (value) {
rc = vsscanf(value, fmt, ap); rc = vsscanf(value, fmt, ap);
} else { } else {
error_setg_errno(errp, errno, "failed to read from '%s'",
path);
rc = EOF; rc = EOF;
} }
free(value); free(value);
g_free(path);
return rc; return rc;
} }
@ -142,6 +139,28 @@ int xs_node_scanf(struct qemu_xs_handle *h, xs_transaction_t tid,
return rc; return rc;
} }
char *xs_node_read(struct qemu_xs_handle *h, xs_transaction_t tid,
unsigned int *len, Error **errp,
const char *path_fmt, ...)
{
char *path, *value;
va_list ap;
va_start(ap, path_fmt);
path = g_strdup_vprintf(path_fmt, ap);
va_end(ap);
value = qemu_xen_xs_read(h, tid, path, len);
trace_xs_node_read(path, value);
if (!value) {
error_setg_errno(errp, errno, "failed to read from '%s'", path);
}
g_free(path);
return value;
}
struct qemu_xs_watch *xs_node_watch(struct qemu_xs_handle *h, const char *node, struct qemu_xs_watch *xs_node_watch(struct qemu_xs_handle *h, const char *node,
const char *key, xs_watch_fn fn, const char *key, xs_watch_fn fn,
void *opaque, Error **errp) void *opaque, Error **errp)

View File

@ -156,8 +156,8 @@ again:
!strcmp(key[i], "hotplug-status")) !strcmp(key[i], "hotplug-status"))
continue; continue;
if (xs_node_scanf(xenbus->xsh, tid, path, key[i], NULL, "%ms", val = xs_node_read(xenbus->xsh, tid, NULL, NULL, "%s/%s", path, key[i]);
&val) == 1) { if (val) {
qdict_put_str(opts, key[i], val); qdict_put_str(opts, key[i], val);
free(val); free(val);
} }
@ -650,6 +650,16 @@ int xen_device_frontend_scanf(XenDevice *xendev, const char *key,
return rc; return rc;
} }
char *xen_device_frontend_read(XenDevice *xendev, const char *key)
{
XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(xendev)));
g_assert(xenbus->xsh);
return xs_node_read(xenbus->xsh, XBT_NULL, NULL, NULL, "%s/%s",
xendev->frontend_path, key);
}
static void xen_device_frontend_set_state(XenDevice *xendev, static void xen_device_frontend_set_state(XenDevice *xendev,
enum xenbus_state state, enum xenbus_state state,
bool publish) bool publish)

View File

@ -22,6 +22,7 @@
#include "qemu/main-loop.h" #include "qemu/main-loop.h"
#include "hw/qdev-core.h" #include "hw/qdev-core.h"
#include "hw/xen/xen-legacy-backend.h" #include "hw/xen/xen-legacy-backend.h"
#include "hw/xen/xen-bus-helper.h"
#include "hw/xen/xen_pvdev.h" #include "hw/xen/xen_pvdev.h"
/* private */ /* private */
@ -81,12 +82,9 @@ int xenstore_write_str(const char *base, const char *node, const char *val)
char *xenstore_read_str(const char *base, const char *node) char *xenstore_read_str(const char *base, const char *node)
{ {
char abspath[XEN_BUFSIZE];
unsigned int len;
char *str, *ret = NULL; char *str, *ret = NULL;
snprintf(abspath, sizeof(abspath), "%s/%s", base, node); str = xs_node_read(xenstore, 0, NULL, NULL, "%s/%s", base, node);
str = qemu_xen_xs_read(xenstore, 0, abspath, &len);
if (str != NULL) { if (str != NULL) {
/* move to qemu-allocated memory to make sure /* move to qemu-allocated memory to make sure
* callers can safely g_free() stuff. */ * callers can safely g_free() stuff. */

View File

@ -38,6 +38,15 @@ int xs_node_scanf(struct qemu_xs_handle *h, xs_transaction_t tid,
const char *fmt, ...) const char *fmt, ...)
G_GNUC_SCANF(6, 7); G_GNUC_SCANF(6, 7);
/*
* Unlike other functions here, the printf-formatted path_fmt is for
* the XenStore path, not the contents of the node.
*/
char *xs_node_read(struct qemu_xs_handle *h, xs_transaction_t tid,
unsigned int *len, Error **errp,
const char *path_fmt, ...)
G_GNUC_PRINTF(5, 6);
/* Watch node/key unless node is empty, in which case watch key */ /* Watch node/key unless node is empty, in which case watch key */
struct qemu_xs_watch *xs_node_watch(struct qemu_xs_handle *h, const char *node, struct qemu_xs_watch *xs_node_watch(struct qemu_xs_handle *h, const char *node,
const char *key, xs_watch_fn fn, const char *key, xs_watch_fn fn,

View File

@ -91,6 +91,7 @@ void xen_device_frontend_printf(XenDevice *xendev, const char *key,
int xen_device_frontend_scanf(XenDevice *xendev, const char *key, int xen_device_frontend_scanf(XenDevice *xendev, const char *key,
const char *fmt, ...) const char *fmt, ...)
G_GNUC_SCANF(3, 4); G_GNUC_SCANF(3, 4);
char *xen_device_frontend_read(XenDevice *xendev, const char *key);
void xen_device_set_max_grant_refs(XenDevice *xendev, unsigned int nr_refs, void xen_device_set_max_grant_refs(XenDevice *xendev, unsigned int nr_refs,
Error **errp); Error **errp);

View File

@ -15,6 +15,7 @@ extern bool qemu_uuid_set;
const char *qemu_get_vm_name(void); const char *qemu_get_vm_name(void);
/* Exit notifiers will run with BQL held. */
void qemu_add_exit_notifier(Notifier *notify); void qemu_add_exit_notifier(Notifier *notify);
void qemu_remove_exit_notifier(Notifier *notify); void qemu_remove_exit_notifier(Notifier *notify);

View File

@ -850,6 +850,7 @@ void qemu_remove_exit_notifier(Notifier *notify)
static void qemu_run_exit_notifiers(void) static void qemu_run_exit_notifiers(void)
{ {
BQL_LOCK_GUARD();
notifier_list_notify(&exit_notifiers, NULL); notifier_list_notify(&exit_notifiers, NULL);
} }