@samitouri / QOSamiQemu / commits / 6b375e13b2

char-fe: implement qemu_chr_fe_backend_name

A number of chardev users where calling qemu_chr_fe_get_driver() which is discouraged as it blocks backend hotswap. As the pattern is common and need for the string is transitory add a helper and keep qemu_chr_fe_get_driver() for those that really need the funkiness of poking around the backend setup. Suggested-by: Peter Maydell <peter.maydell@linaro.org> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-ID: <20260824-fixes-for-11-2-v2-4-352c6b890402@linaro.org> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>

Alex Bennée committed Aug 24, 2026 at 16:24 UTC 6b375e13b2db9c9ef5ec10d214cd764b9411dc20
5 files changed +17 -24
backends/cryptodev-vhost-user.c
+1 -7
@@ -373,13 +373,7 @@ cryptodev_vhost_user_get_chardev(Object *obj, Error **errp)
373 {
374 CryptoDevBackendVhostUser *s =
375 CRYPTODEV_BACKEND_VHOST_USER(obj);
376 - Chardev *chr = qemu_chr_fe_get_driver(&s->chr);
377 -
378 - if (chr && chr->label) {
379 - return g_strdup(chr->label);
380 - }
381 -
382 - return NULL;
376 + return qemu_chr_fe_backend_name(&s->chr);
377 }
378
379 static void cryptodev_vhost_user_finalize(Object *obj)
backends/rng-egd.c
+1 -7
@@ -126,13 +126,7 @@ static void rng_egd_set_chardev(Object *obj, const char *value, Error **errp)
126 static char *rng_egd_get_chardev(Object *obj, Error **errp)
127 {
128 RngEgd *s = RNG_EGD(obj);
129 - Chardev *chr = qemu_chr_fe_get_driver(&s->chr);
130 -
131 - if (chr && chr->label) {
132 - return g_strdup(chr->label);
133 - }
134 -
135 - return NULL;
129 + return qemu_chr_fe_backend_name(&s->chr);
130 }
131
132 static void rng_egd_finalize(Object *obj)
backends/vhost-user.c
+1 -7
@@ -152,13 +152,7 @@ static void set_chardev(Object *obj, const char *value, Error **errp)
152 static char *get_chardev(Object *obj, Error **errp)
153 {
154 VhostUserBackend *b = VHOST_USER_BACKEND(obj);
155 - Chardev *chr = qemu_chr_fe_get_driver(&b->chr);
156 -
157 - if (chr && chr->label) {
158 - return g_strdup(chr->label);
159 - }
160 -
161 - return NULL;
155 + return qemu_chr_fe_backend_name(&b->chr);
156 }
157
158 static void vhost_user_backend_class_init(ObjectClass *oc, const void *data)
hw/char/imx_serial.c
+2 -2
@@ -278,10 +278,10 @@ static void imx_serial_write(void *opaque, hwaddr offset,
278 uint64_t value, unsigned size)
279 {
280 IMXSerialState *s = (IMXSerialState *)opaque;
281 - Chardev *chr = qemu_chr_fe_get_driver(&s->chr);
281 + g_autofree char *label = qemu_chr_fe_backend_name(&s->chr);
282 unsigned char ch;
283
284 - trace_imx_serial_write(chr ? chr->label : "NODEV", offset, value);
284 + trace_imx_serial_write(label ? label : "NODEV", offset, value);
285
286 switch (offset >> 2) {
287 case 0x10: /* UTXD */
include/chardev/char-fe.h
+12 -1
@@ -52,7 +52,8 @@ void qemu_chr_fe_deinit(CharFrontend *c, bool del);
52 * associated Chardev.
53 * Note: avoid this function as the driver should never be accessed directly,
54 * especially by the frontends that support chardevice hotswap.
55 - * Consider qemu_chr_fe_backend_connected() to check for driver existence
55 + * Consider qemu_chr_fe_backend_connected() to check for driver
56 + * existence or qemu_chr_fe_backend_name() if you need the name.
57 */
58 Chardev *qemu_chr_fe_get_driver(CharFrontend *c);
59
@@ -70,6 +71,16 @@ bool qemu_chr_fe_backend_connected(CharFrontend *c);
71 */
72 bool qemu_chr_fe_backend_open(CharFrontend *c);
73
74 +/**
75 + * qemu_chr_fe_backend_name:
76 + *
77 + * Returns: caller freeable string or NULL
78 + */
79 +static inline char *qemu_chr_fe_backend_name(CharFrontend *c)
80 +{
81 + return (c->chr && c->chr->label) ? g_strdup(c->chr->label) : NULL;
82 +}
83 +
84 /**
85 * qemu_chr_fe_set_handlers_full:
86 * @c: a CharFrontend