chardev fixes for 6.1
From: Marc-André Lureau <[email protected]>
The following changes since commit bccabb3a5d60182645c7749e89f21a9ff307a9eb:
Update version for v6.1.0-rc2 release (2021-08-04 16:56:14 +0100)
are available in the Git repository at:
[email protected]:marcandre.lureau/qemu.git tags/chr-fix-pull-request
for you to fetch changes up to a68403b0a6843f106e381b0bbeaacb29f6d27255:
chardev: report a simpler error about duplicated id (2021-08-05 16:15:33 +0400)
----------------------------------------------------------------
Chardev-related fixes
Hi
Here are some bug fixes worthy for 6.1.
thanks
----------------------------------------------------------------
Marc-André Lureau (8):
util: fix abstract socket path copy chardev/socket: print a more correct command-line address
chardev: mark explicitly first argument as poisoned
chardev: fix fd_chr_add_watch() when in != out
chardev: fix qemu_chr_open_fd() being called with fd=-1
chardev: fix qemu_chr_open_fd() with fd_in==fd_out
chardev: give some context on chardev-add error
chardev: report a simpler error about duplicated id
include/chardev/char-fe.h | 8 ++- chardev/char-fd.c | 119 ++++++++++++++++++++++++++++++++++---- chardev/char-fe.c | 2 +- chardev/char-socket.c | 4 +- chardev/char.c | 20 +++++-- hw/char/cadence_uart.c | 2 +- hw/char/cmsdk-apb-uart.c | 2 +- hw/char/ibex_uart.c | 2 +- hw/char/nrf51_uart.c | 2 +- hw/char/serial.c | 2 +- hw/char/virtio-console.c | 2 +- hw/usb/redirect.c | 2 +- hw/virtio/vhost-user.c | 2 +- monitor/monitor.c | 2 +- net/vhost-user.c | 4 +- util/qemu-sockets.c | 5 +- 16 files changed, 146 insertions(+), 34 deletions(-)
Signature · 1 line
2.32.0.264.g75ae10bc75
[PULL 1/8] util: fix abstract socket path copy
From: Marc-André Lureau <[email protected]>
Commit 776b97d360 "qemu-sockets: add abstract UNIX domain socket support" neglected to update socket_sockaddr_to_address_unix() and copied the whole sun_path without taking "salen" into account.
Later, commit 3b14b4ec49 "sockets: Fix socket_sockaddr_to_address_unix() for abstract sockets" handled the abstract UNIX path, by stripping the leading \0 character and fixing address details, but didn't use salen either.
Not taking "salen" into account may result in incorrect "path" being returned in monitors commands, as we read past the address which is not necessarily \0-terminated.
--- util/qemu-sockets.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
util/qemu-sockets.c +4 -1
@@ -1345,13 +1345,16 @@ socket_sockaddr_to_address_unix(struct sockaddr_storage *sa,SocketAddress *addr;struct sockaddr_un *su = (struct sockaddr_un *)sa; +assert (salen >=sizeof (su->sun_family) +1 && + salen <=sizeof (struct sockaddr_un )); + addr =g_new0 (SocketAddress,1 ); addr->type =SOCKET_ADDRESS_TYPE_UNIX ;#ifdef CONFIG_LINUX if (!su->sun_path[0 ]) {/* Linux abstract socket */ addr->u.q_unix.path =g_strndup (su->sun_path +1 , -sizeof (su->sun_path) -1 ); + salen -sizeof (su->sun_family) -1 ); addr->u.q_unix.has_abstract = true; addr->u.q_unix.abstract = true; addr->u.q_unix.has_tight = true;
Signature · 1 line
2.32.0.264.g75ae10bc75
[PULL 2/8] chardev/socket: print a more correct command-line address
In reply to [email protected]From: Marc-André Lureau <[email protected]>
Better reflect the command line version of the socket address arguments, following the now recommended long-form opt=on syntax.
Complement/fixes commit 9d902d51 "chardev: do not use short form boolean options in non-QemuOpts character device descriptions".
--- chardev/char-socket.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
chardev/char-socket.c +2 -2
@@ -468,9 +468,9 @@ static char *qemu_chr_socket_address(SocketChardev *s, const char *prefix)#ifdef CONFIG_LINUX if (sa->has_abstract && sa->abstract) { - abstract =",abstract" ; + abstract =",abstract=on" ;if (sa->has_tight && sa->tight) { - tight =",tight" ; + tight =",tight=on" ; } }#endif
Signature · 1 line
2.32.0.264.g75ae10bc75
[PULL 4/8] chardev: fix fd_chr_add_watch() when in != out
In reply to [email protected]From: Marc-André Lureau <[email protected]>
Create child sources for the different streams, and dispatch on the parent source with the synthesized conditions.
--- chardev/char-fd.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-)
chardev/char-fd.c +77 -1
@@ -28,6 +28,7 @@#include "qemu/sockets.h" #include "qapi/error.h" #include "chardev/char.h" +#include "chardev/char-fe.h" #include "io/channel-file.h" #include "chardev/char-fd.h"
@@ -80,10 +81,85 @@ static int fd_chr_read_poll(void *opaque)return s->max_size; } +typedef struct FDSource { +GSource parent; + +GIOCondition cond; +}FDSource ; + +static gboolean +fd_source_prepare (GSource *source, +gint *timeout_) +{ +FDSource *src = (FDSource *)source; + +return src->cond !=0 ; +} + +static gboolean +fd_source_check (GSource *source) +{ +FDSource *src = (FDSource *)source; + +return src->cond !=0 ; +} + +static gboolean +fd_source_dispatch (GSource *source,GSourceFunc callback, +gpointer user_data) +{ +FDSource *src = (FDSource *)source; +FEWatchFunc func = (FEWatchFunc )callback; +gboolean ret =G_SOURCE_CONTINUE ; + +if (src->cond) { + ret =func (NULL , src->cond, user_data); + src->cond =0 ; + } + +return ret; +} + +static GSourceFuncs fd_source_funcs = { + fd_source_prepare, + fd_source_check, + fd_source_dispatch, +NULL ,NULL ,NULL +}; + +static GSource *fd_source_new (FDChardev *chr) +{ +return g_source_new (&fd_source_funcs,sizeof (FDSource)); +} + +static gboolean child_func (GIOChannel *source, +GIOCondition condition, +gpointer data) +{ +FDSource *parent = data; + + parent->cond |= condition; + +return G_SOURCE_CONTINUE ; +} +static GSource *fd_chr_add_watch (Chardev *chr,GIOCondition cond) {FDChardev *s =FD_CHARDEV (chr); -return qio_channel_create_watch (s->ioc_out, cond); +g_autoptr (GSource) source =fd_source_new (s); + +if (s->ioc_out) { +g_autoptr (GSource) child =qio_channel_create_watch (s->ioc_out, cond & ~G_IO_IN ); +g_source_set_callback (child, (GSourceFunc )child_func, source,NULL ); +g_source_add_child_source (source, child); + } +if (s->ioc_in) { +g_autoptr (GSource) child =qio_channel_create_watch (s->ioc_in, cond & ~G_IO_OUT ); +g_source_set_callback (child, (GSourceFunc )child_func, source,NULL ); +g_source_add_child_source (source, child); + } + +return g_steal_pointer (&source); }static void fd_chr_update_read_handler (Chardev *chr)
Signature · 1 line
2.32.0.264.g75ae10bc75
[PULL 3/8] chardev: mark explicitly first argument as poisoned
In reply to [email protected]From: Marc-André Lureau <[email protected]>
Since commit 9894dc0cdcc397ee5b26370bc53da6d360a363c2 "char: convert from GIOChannel to QIOChannel", the first argument to the watch callback can actually be a QIOChannel, which is not a GIOChannel (but a QEMU Object).
Even though we never used that pointer, change the callback type to warn the users. Possibly a better fix later, we may want to store the callback and call it from intermediary functions.
--- include/chardev/char-fe.h | 8 +++++++- chardev/char-fe.c | 2 +- hw/char/cadence_uart.c | 2 +- hw/char/cmsdk-apb-uart.c | 2 +- hw/char/ibex_uart.c | 2 +- hw/char/nrf51_uart.c | 2 +- hw/char/serial.c | 2 +- hw/char/virtio-console.c | 2 +- hw/usb/redirect.c | 2 +- hw/virtio/vhost-user.c | 2 +- monitor/monitor.c | 2 +- net/vhost-user.c | 4 ++-- 12 files changed, 19 insertions(+), 13 deletions(-)
include/chardev/char-fe.h +7 -1
@@ -174,6 +174,9 @@ void qemu_chr_fe_set_open(CharBackend *be, int fe_open); void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...) GCC_FMT_ATTR(2, 3); + +typedef gboolean (*FEWatchFunc )(void *do_not_use,GIOCondition condition,void *data); +/** * qemu_chr_fe_add_watch: * @cond: the condition to poll for
@@ -188,10 +191,13 @@ void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...)* Note that you are responsible to update the front-end sources if * you are switching the main context with qemu_chr_fe_set_handlers(). * +* Warning: DO NOT use the first callback argument (it may be either +* a GIOChannel or a QIOChannel, depending on the underlying chardev) +* * Returns: the source tag */ guint qemu_chr_fe_add_watch (CharBackend *be,GIOCondition cond, -GIOFunc func,void *user_data); +FEWatchFunc func,void *user_data);/** * qemu_chr_fe_write:
chardev/char-fe.c +1 -1
@@ -354,7 +354,7 @@ void qemu_chr_fe_set_open(CharBackend *be, int fe_open) }guint qemu_chr_fe_add_watch (CharBackend *be,GIOCondition cond, -GIOFunc func,void *user_data) +FEWatchFunc func,void *user_data) {Chardev *s = be->chr;GSource *src;
hw/char/cadence_uart.c +1 -1
@@ -288,7 +288,7 @@ static void uart_write_rx_fifo(void *opaque, const uint8_t *buf, int size)uart_update_status (s); } -static gboolean cadence_uart_xmit (GIOChannel *chan,GIOCondition cond, +static gboolean cadence_uart_xmit (void *do_not_use,GIOCondition cond,void *opaque) {CadenceUARTState *s = opaque;
hw/char/cmsdk-apb-uart.c +1 -1
@@ -191,7 +191,7 @@ static uint64_t uart_read(void *opaque, hwaddr offset, unsigned size)/* Try to send tx data, and arrange to be called back later if * we can't (ie the char backend is busy/blocking). */ -static gboolean uart_transmit (GIOChannel *chan,GIOCondition cond,void *opaque) +static gboolean uart_transmit (void *do_not_use,GIOCondition cond,void *opaque) {CMSDKAPBUART *s =CMSDK_APB_UART (opaque);int ret;
hw/char/ibex_uart.c +1 -1
@@ -135,7 +135,7 @@ static void ibex_uart_receive(void *opaque, const uint8_t *buf, int size)ibex_uart_update_irqs (s); } -static gboolean ibex_uart_xmit (GIOChannel *chan,GIOCondition cond, +static gboolean ibex_uart_xmit (void *do_not_use,GIOCondition cond,void *opaque) {IbexUartState *s = opaque;
hw/char/nrf51_uart.c +1 -1
@@ -75,7 +75,7 @@ static uint64_t uart_read(void *opaque, hwaddr addr, unsigned int size)return r; } -static gboolean uart_transmit (GIOChannel *chan,GIOCondition cond,void *opaque) +static gboolean uart_transmit (void *do_not_use,GIOCondition cond,void *opaque) {NRF51UARTState *s =NRF51_UART (opaque);int r;
hw/char/serial.c +1 -1
@@ -220,7 +220,7 @@ static void serial_update_msl(SerialState *s) } } -static gboolean serial_watch_cb (GIOChannel *chan,GIOCondition cond, +static gboolean serial_watch_cb (void *do_not_use,GIOCondition cond,void *opaque) {SerialState *s = opaque;
hw/char/virtio-console.c +1 -1
@@ -38,7 +38,7 @@ struct VirtConsole {* Callback function that's called from chardevs when backend becomes * writable. */ -static gboolean chr_write_unblocked (GIOChannel *chan,GIOCondition cond, +static gboolean chr_write_unblocked (void *do_not_use,GIOCondition cond,void *opaque) {VirtConsole *vcon = opaque;
hw/usb/redirect.c +1 -1
@@ -270,7 +270,7 @@ static int usbredir_read(void *priv, uint8_t *data, int count)return count; } -static gboolean usbredir_write_unblocked (GIOChannel *chan,GIOCondition cond, +static gboolean usbredir_write_unblocked (void *do_not_use,GIOCondition cond,void *opaque) {USBRedirDevice *dev = opaque;
hw/virtio/vhost-user.c +1 -1
@@ -303,7 +303,7 @@ struct vhost_user_read_cb_data {int ret; }; -static gboolean vhost_user_read_cb (GIOChannel *source,GIOCondition condition, +static gboolean vhost_user_read_cb (void *do_not_use,GIOCondition condition,gpointer opaque) {struct vhost_user_read_cb_data *data = opaque;
monitor/monitor.c +1 -1
@@ -156,7 +156,7 @@ static inline bool monitor_is_hmp_non_interactive(const Monitor *mon)static void monitor_flush_locked (Monitor *mon); -static gboolean monitor_unblocked (GIOChannel *chan,GIOCondition cond, +static gboolean monitor_unblocked (void *do_not_use,GIOCondition cond,void *opaque) {Monitor *mon = opaque;
net/vhost-user.c +2 -2
@@ -208,8 +208,8 @@ static NetClientInfo net_vhost_user_info = { .set_vnet_le = vhost_user_set_vnet_endianness, }; -static gboolean net_vhost_user_watch (GIOChannel *chan,GIOCondition cond, -void *opaque) +static gboolean net_vhost_user_watch (void *do_not_use,GIOCondition cond, +void *opaque) {NetVhostUserState *s = opaque;
Signature · 1 line
2.32.0.264.g75ae10bc75
[PULL 5/8] chardev: fix qemu_chr_open_fd() being called with fd=-1
In reply to [email protected]From: Marc-André Lureau <[email protected]>
The "file" chardev may call qemu_chr_open_fd() with fd_in=-1. This may cause invalid system calls, as the QIOChannel is assumed to be properly initialized later on.
--- chardev/char-fd.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-)
chardev/char-fd.c +17 -9
@@ -39,6 +39,10 @@ static int fd_chr_write(Chardev *chr, const uint8_t *buf, int len) {FDChardev *s =FD_CHARDEV (chr); +if (!s->ioc_out) { +return -1 ; + } +return io_channel_send (s->ioc_out, buf, len); }
@@ -209,15 +213,19 @@ void qemu_chr_open_fd(Chardev *chr,FDChardev *s =FD_CHARDEV (chr);char *name; - s->ioc_in =QIO_CHANNEL (qio_channel_file_new_fd (fd_in)); - name =g_strdup_printf ("chardev-file-in-%s" , chr->label); -qio_channel_set_name (QIO_CHANNEL (s->ioc_in), name); -g_free (name); - s->ioc_out =QIO_CHANNEL (qio_channel_file_new_fd (fd_out)); - name =g_strdup_printf ("chardev-file-out-%s" , chr->label); -qio_channel_set_name (QIO_CHANNEL (s->ioc_out), name); -g_free (name); -qemu_set_nonblock (fd_out); +if (fd_in >=0 ) { + s->ioc_in =QIO_CHANNEL (qio_channel_file_new_fd (fd_in)); + name =g_strdup_printf ("chardev-file-in-%s" , chr->label); +qio_channel_set_name (QIO_CHANNEL (s->ioc_in), name); +g_free (name); + } +if (fd_out >=0 ) { + s->ioc_out =QIO_CHANNEL (qio_channel_file_new_fd (fd_out)); + name =g_strdup_printf ("chardev-file-out-%s" , chr->label); +qio_channel_set_name (QIO_CHANNEL (s->ioc_out), name); +g_free (name); +qemu_set_nonblock (fd_out); + } }static void char_fd_class_init (ObjectClass *oc,void *data)
Signature · 1 line
2.32.0.264.g75ae10bc75
[PULL 7/8] chardev: give some context on chardev-add error
In reply to [email protected]From: Marc-André Lureau <[email protected]>
Description from Daniel P. Berrangé:
Quoted 10 lines
The original code reported:
"attempt to add duplicate property 'char2' to object (type 'container')"
Since adding yank support, the current code reports
"duplicate yank instance"
With this patch applied it now reports:
Quoted 8 lines
"Failed to add chardev 'char2': duplicate yank instance"
This is marginally better, but still not great, not that the original error was great either.
It would be nice if we could report
"chardev with id 'char2' already exists"
Related to:
https://bugzilla.redhat.com/show_bug.cgi?id=1984721
--- chardev/char.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
chardev/char.c +9 -6
@@ -1031,27 +1031,26 @@ Chardev *qemu_chardev_new(const char *id, const char *typename,ChardevReturn *qmp_chardev_add (const char *id,ChardevBackend *backend,Error **errp) { +ERRP_GUARD ();const ChardevClass *cc;ChardevReturn *ret; -Chardev *chr; +g_autoptr (Chardev) chr =NULL ; cc =char_get_class (ChardevBackendKind_str (backend->type), errp);if (!cc) { -return NULL ; + goto err; } chr =chardev_new (id,object_class_get_name (OBJECT_CLASS (cc)), backend,NULL , false, errp);if (!chr) { -return NULL ; + goto err; }if (!object_property_try_add_child (get_chardevs_root (), id,OBJECT (chr), errp)) { -object_unref (OBJECT (chr)); -return NULL ; + goto err; } -object_unref (OBJECT (chr)); ret =g_new0 (ChardevReturn,1 );if (CHARDEV_IS_PTY (chr)) {
@@ -1060,6 +1059,10 @@ ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend, }return ret; + +err: +error_prepend (errp,"Failed to add chardev '%s': " , id); +return NULL ; } ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend,
Signature · 1 line
2.32.0.264.g75ae10bc75
[PULL 6/8] chardev: fix qemu_chr_open_fd() with fd_in==fd_out
In reply to [email protected]From: Marc-André Lureau <[email protected]>
The "serial" chardev calls qemu_chr_open_fd() with the same fd. This may lead to double-close as each QIOChannel owns the fd.
Instead, share the reference to the same QIOChannel.
--- chardev/char-fd.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
chardev/char-fd.c +15 -4
@@ -211,20 +211,31 @@ void qemu_chr_open_fd(Chardev *chr, int fd_in, int fd_out) {FDChardev *s =FD_CHARDEV (chr); -char *name; + g_autofree char *name = NULL; + +if (fd_out >=0 ) { +qemu_set_nonblock (fd_out); + } + +if (fd_out == fd_in && fd_in >=0 ) { + s->ioc_in =QIO_CHANNEL (qio_channel_file_new_fd (fd_in)); + name =g_strdup_printf ("chardev-file-%s" , chr->label); +qio_channel_set_name (QIO_CHANNEL (s->ioc_in), name); + s->ioc_out =QIO_CHANNEL (object_ref (s->ioc_in)); +return ; + }if (fd_in >=0 ) { s->ioc_in =QIO_CHANNEL (qio_channel_file_new_fd (fd_in)); name =g_strdup_printf ("chardev-file-in-%s" , chr->label);qio_channel_set_name (QIO_CHANNEL (s->ioc_in), name); -g_free (name); } +if (fd_out >=0 ) { s->ioc_out =QIO_CHANNEL (qio_channel_file_new_fd (fd_out)); +g_free (name); name =g_strdup_printf ("chardev-file-out-%s" , chr->label);qio_channel_set_name (QIO_CHANNEL (s->ioc_out), name); -g_free (name); -qemu_set_nonblock (fd_out); } }
Signature · 1 line
2.32.0.264.g75ae10bc75
[PULL 8/8] chardev: report a simpler error about duplicated id
In reply to [email protected]From: Marc-André Lureau <[email protected]>
Report:
"Chardev with id 'char2' already exists"
Rather than:
"Failed to add chardev 'char2': duplicate yank instance"
--- chardev/char.c | 5 +++++ 1 file changed, 5 insertions(+)
chardev/char.c +5 -0
@@ -1036,6 +1036,11 @@ ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,ChardevReturn *ret;g_autoptr (Chardev) chr =NULL ; +if (qemu_chr_find (id)) { +error_setg (errp,"Chardev with id '%s' already exists" , id); +return NULL ; + } + cc =char_get_class (ChardevBackendKind_str (backend->type), errp);if (!cc) { goto err;
Signature · 1 line
2.32.0.264.g75ae10bc75
Peter Maydell
AppliedQuoted 15 lines · On Thu, 5 Aug 2021 at 13:53, <[email protected]> wrote:
From: Marc-André Lureau <[email protected]>
The following changes since commit
bccabb3a5d60182645c7749e89f21a9ff307a9eb:Update version for v6.1.0-rc2 release (2021-08-04 16:56:14 +0100)
are available in the Git repository at:
[email protected]:marcandre.lureau/qemu.git tags/chr-fix-pull-request
for you to fetch changes up to
a68403b0a6843f106e381b0bbeaacb29f6d27255:chardev: report a simpler error about duplicated id (2021-08-05 16:15:33 +0400)
Quoted 8 lines
----------------------------------------------------------------
Chardev-related fixesHi
Here are some bug fixes worthy for 6.1.
thanks
Applied, thanks.
Please update the changelog at https://wiki.qemu.org/ChangeLog/6.1
for any user-visible changes.
-- PMM