* [PATCH 1/7] chardev/char-fe: Improve @docstrings
2025-10-22 14:48 [PATCH 0/7] chardev: Improve @docstring and clarify qemu_chr_write() uses Philippe Mathieu-Daudé
@ 2025-10-22 14:48 ` Philippe Mathieu-Daudé
2025-10-22 14:48 ` [PATCH 2/7] chardev/char-io: Add @docstrings for io_channel_send[_full]() Philippe Mathieu-Daudé
` (5 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-10-22 14:48 UTC (permalink / raw)
To: qemu-devel
Cc: Paolo Bonzini, Marc-André Lureau, Peter Maydell,
Alex Bennée, Philippe Mathieu-Daudé
Describe the @c (this is the *frontend*) and @s (the *backend*)
parameters. Fill qemu_chr_fe_[gs]et_msgfds() method docstrings.
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
include/chardev/char-fe.h | 24 +++++++++++++++++++++++-
1 file changed, 23 insertions(+), 1 deletion(-)
diff --git a/include/chardev/char-fe.h b/include/chardev/char-fe.h
index 7901856f951..c183432825b 100644
--- a/include/chardev/char-fe.h
+++ b/include/chardev/char-fe.h
@@ -26,6 +26,8 @@ struct CharFrontend {
/**
* qemu_chr_fe_init:
+ * @c: the character frontend
+ * @s: the character backend
*
* Initializes the frontend @c for the given Chardev backend @s. Call
* qemu_chr_fe_deinit() to remove the association and release the backend.
@@ -47,6 +49,7 @@ void qemu_chr_fe_deinit(CharFrontend *c, bool del);
/**
* qemu_chr_fe_get_driver:
+ * @c: the character frontend
*
* Returns: the driver associated with a CharFrontend or NULL if no
* associated Chardev.
@@ -58,6 +61,7 @@ Chardev *qemu_chr_fe_get_driver(CharFrontend *c);
/**
* qemu_chr_fe_backend_connected:
+ * @c: the character frontend
*
* Returns: true if there is a backend associated with @c.
*/
@@ -102,6 +106,7 @@ void qemu_chr_fe_set_handlers_full(CharFrontend *c,
/**
* qemu_chr_fe_set_handlers:
+ * @c: the character frontend
*
* Version of qemu_chr_fe_set_handlers_full() with sync_state = true.
*/
@@ -116,6 +121,7 @@ void qemu_chr_fe_set_handlers(CharFrontend *c,
/**
* qemu_chr_fe_take_focus:
+ * @c: the character frontend
*
* Take the focus (if the front end is muxed).
*
@@ -125,6 +131,7 @@ void qemu_chr_fe_take_focus(CharFrontend *c);
/**
* qemu_chr_fe_accept_input:
+ * @c: the character frontend
*
* Notify that the frontend is ready to receive data
*/
@@ -132,6 +139,7 @@ void qemu_chr_fe_accept_input(CharFrontend *c);
/**
* qemu_chr_fe_disconnect:
+ * @c: the character frontend
*
* Close a fd accepted by character backend.
* Without associated Chardev, do nothing.
@@ -148,6 +156,7 @@ int qemu_chr_fe_wait_connected(CharFrontend *c, Error **errp);
/**
* qemu_chr_fe_set_echo:
+ * @c: the character frontend
* @echo: true to enable echo, false to disable echo
*
* Ask the backend to override its normal echo setting. This only really
@@ -169,6 +178,7 @@ void qemu_chr_fe_set_open(CharFrontend *c, bool is_open);
/**
* qemu_chr_fe_printf:
+ * @c: the character frontend
* @fmt: see #printf
*
* Write to a character backend using a printf style interface. This
@@ -197,6 +207,7 @@ typedef gboolean (*FEWatchFunc)(void *do_not_use, GIOCondition condition, void *
/**
* qemu_chr_fe_add_watch:
+ * @c: the character frontend
* @cond: the condition to poll for
* @func: the function to call when the condition happens
* @user_data: the opaque pointer to pass to @func
@@ -219,6 +230,7 @@ guint qemu_chr_fe_add_watch(CharFrontend *c, GIOCondition cond,
/**
* qemu_chr_fe_write:
+ * @c: the character frontend to write to
* @buf: the data
* @len: the number of bytes to send
*
@@ -233,6 +245,7 @@ int qemu_chr_fe_write(CharFrontend *c, const uint8_t *buf, int len);
/**
* qemu_chr_fe_write_all:
+ * @c: the character frontend to write to
* @buf: the data
* @len: the number of bytes to send
*
@@ -248,6 +261,7 @@ int qemu_chr_fe_write_all(CharFrontend *c, const uint8_t *buf, int len);
/**
* qemu_chr_fe_read_all:
+ * @c: the character frontend to read from
* @buf: the data buffer
* @len: the number of bytes to read
*
@@ -260,6 +274,7 @@ int qemu_chr_fe_read_all(CharFrontend *c, uint8_t *buf, int len);
/**
* qemu_chr_fe_ioctl:
+ * @c: the character frontend to control
* @cmd: see CHR_IOCTL_*
* @arg: the data associated with @cmd
*
@@ -273,6 +288,7 @@ int qemu_chr_fe_ioctl(CharFrontend *c, int cmd, void *arg);
/**
* qemu_chr_fe_get_msgfd:
+ * @c: the character frontend to access
*
* For backends capable of fd passing, return the latest file descriptor passed
* by a client.
@@ -286,9 +302,12 @@ int qemu_chr_fe_get_msgfd(CharFrontend *c);
/**
* qemu_chr_fe_get_msgfds:
+ * @c: the character frontend
+ * @fds: an array of ancillary file descriptors to get
+ * @num: the maximum number of ancillary file descriptors to get in @fds
*
* For backends capable of fd passing, return the number of file received
- * descriptors and fills the fds array up to num elements
+ * descriptors and fills the fds array up to @num elements
*
* Returns: -1 if fd passing isn't supported or there are no pending file
* descriptors. If file descriptors are returned, subsequent calls to
@@ -299,6 +318,9 @@ int qemu_chr_fe_get_msgfds(CharFrontend *c, int *fds, int num);
/**
* qemu_chr_fe_set_msgfds:
+ * @c: the character frontend
+ * @fds: an array of ancillary file descriptors to set
+ * @num: the number of ancillary file descriptors to set
*
* For backends capable of fd passing, set an array of fds to be passed with
* the next send operation.
--
2.51.0
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH 2/7] chardev/char-io: Add @docstrings for io_channel_send[_full]()
2025-10-22 14:48 [PATCH 0/7] chardev: Improve @docstring and clarify qemu_chr_write() uses Philippe Mathieu-Daudé
2025-10-22 14:48 ` [PATCH 1/7] chardev/char-fe: Improve @docstrings Philippe Mathieu-Daudé
@ 2025-10-22 14:48 ` Philippe Mathieu-Daudé
2025-10-22 14:48 ` [PATCH 3/7] chardev/char-pty: Do not ignore chr_write() failures Philippe Mathieu-Daudé
` (4 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-10-22 14:48 UTC (permalink / raw)
To: qemu-devel
Cc: Paolo Bonzini, Marc-André Lureau, Peter Maydell,
Alex Bennée, Philippe Mathieu-Daudé
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
include/chardev/char-io.h | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/include/chardev/char-io.h b/include/chardev/char-io.h
index ac379ea70e3..f14d1d7ef00 100644
--- a/include/chardev/char-io.h
+++ b/include/chardev/char-io.h
@@ -38,8 +38,26 @@ GSource *io_add_watch_poll(Chardev *chr,
void remove_fd_in_watch(Chardev *chr);
+/**
+ * io_channel_send:
+ * @ioc: the IO channel object
+ * @buf: the data
+ * @len: the number of bytes to send
+ *
+ * Returns: the number of bytes consumed or -1 on error.
+ */
int io_channel_send(QIOChannel *ioc, const void *buf, size_t len);
+/**
+ * io_channel_send_full:
+ * @ioc: the IO channel object
+ * @buf: the data
+ * @len: the number of bytes to send
+ * @fds: an array of file handles to send
+ * @nfds: number of file handles in @fds
+ *
+ * Returns: the number of bytes consumed or -1 on error.
+ */
int io_channel_send_full(QIOChannel *ioc, const void *buf, size_t len,
int *fds, size_t nfds);
--
2.51.0
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH 3/7] chardev/char-pty: Do not ignore chr_write() failures
2025-10-22 14:48 [PATCH 0/7] chardev: Improve @docstring and clarify qemu_chr_write() uses Philippe Mathieu-Daudé
2025-10-22 14:48 ` [PATCH 1/7] chardev/char-fe: Improve @docstrings Philippe Mathieu-Daudé
2025-10-22 14:48 ` [PATCH 2/7] chardev/char-io: Add @docstrings for io_channel_send[_full]() Philippe Mathieu-Daudé
@ 2025-10-22 14:48 ` Philippe Mathieu-Daudé
2025-10-22 14:48 ` [PATCH 4/7] chardev/char: Allow partial writes in qemu_chr_write() Philippe Mathieu-Daudé
` (3 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-10-22 14:48 UTC (permalink / raw)
To: qemu-devel
Cc: Paolo Bonzini, Marc-André Lureau, Peter Maydell,
Alex Bennée, Philippe Mathieu-Daudé, qemu-stable
Cc: qemu-stable@nongnu.org
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
If ignoring this is deliberate, this must be described in a comment
to avoid any confusion.
---
chardev/char-pty.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/chardev/char-pty.c b/chardev/char-pty.c
index b066f014126..652b0bd9e73 100644
--- a/chardev/char-pty.c
+++ b/chardev/char-pty.c
@@ -125,7 +125,7 @@ static int char_pty_chr_write(Chardev *chr, const uint8_t *buf, int len)
rc = RETRY_ON_EINTR(g_poll(&pfd, 1, 0));
g_assert(rc >= 0);
if (!(pfd.revents & G_IO_HUP) && (pfd.revents & G_IO_OUT)) {
- io_channel_send(s->ioc, buf, len);
+ return io_channel_send(s->ioc, buf, len);
}
return len;
--
2.51.0
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH 4/7] chardev/char: Allow partial writes in qemu_chr_write()
2025-10-22 14:48 [PATCH 0/7] chardev: Improve @docstring and clarify qemu_chr_write() uses Philippe Mathieu-Daudé
` (2 preceding siblings ...)
2025-10-22 14:48 ` [PATCH 3/7] chardev/char-pty: Do not ignore chr_write() failures Philippe Mathieu-Daudé
@ 2025-10-22 14:48 ` Philippe Mathieu-Daudé
2025-10-22 14:49 ` [PATCH 5/7] chardev/char: Preserve %errno " Philippe Mathieu-Daudé
` (2 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-10-22 14:48 UTC (permalink / raw)
To: qemu-devel
Cc: Paolo Bonzini, Marc-André Lureau, Peter Maydell,
Alex Bennée, Philippe Mathieu-Daudé, qemu-stable
If qemu_chr_write_buffer() returned an error, but could
write some characters, return the number of character
written. Otherwise frontends able to recover and resume
writes re-write the partial chars already written.
Cc: qemu-stable@nongnu.org
Suggested-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
chardev/char.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/chardev/char.c b/chardev/char.c
index 30b21fedce4..5c8130b2435 100644
--- a/chardev/char.c
+++ b/chardev/char.c
@@ -189,7 +189,7 @@ int qemu_chr_write(Chardev *s, const uint8_t *buf, int len, bool write_all)
replay_char_write_event_save(res, offset);
}
- if (res < 0) {
+ if (res < 0 && offset == 0) {
return res;
}
return offset;
--
2.51.0
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH 5/7] chardev/char: Preserve %errno in qemu_chr_write()
2025-10-22 14:48 [PATCH 0/7] chardev: Improve @docstring and clarify qemu_chr_write() uses Philippe Mathieu-Daudé
` (3 preceding siblings ...)
2025-10-22 14:48 ` [PATCH 4/7] chardev/char: Allow partial writes in qemu_chr_write() Philippe Mathieu-Daudé
@ 2025-10-22 14:49 ` Philippe Mathieu-Daudé
2025-10-22 14:49 ` [PATCH 6/7] chardev/char-hub: Retry when qemu_chr_fe_write() can not write Philippe Mathieu-Daudé
2025-10-22 14:49 ` [PATCH 7/7] hw/char: Simplify when qemu_chr_fe_write() could " Philippe Mathieu-Daudé
6 siblings, 0 replies; 8+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-10-22 14:49 UTC (permalink / raw)
To: qemu-devel
Cc: Paolo Bonzini, Marc-André Lureau, Peter Maydell,
Alex Bennée, Philippe Mathieu-Daudé, qemu-stable
qemu_chr_write() dispatches to ChardevClass::chr_write(),
and is expected to propagate the backend error, not some
unrelated one produce by "best effort" logfile or replay.
Preserve and return the relevant %errno.
Cc: qemu-stable@nongnu.org
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
chardev/char.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/chardev/char.c b/chardev/char.c
index 5c8130b2435..2af402d9855 100644
--- a/chardev/char.c
+++ b/chardev/char.c
@@ -113,6 +113,7 @@ static int qemu_chr_write_buffer(Chardev *s,
int *offset, bool write_all)
{
ChardevClass *cc = CHARDEV_GET_CLASS(s);
+ int saved_errno;
int res = 0;
*offset = 0;
@@ -138,6 +139,7 @@ static int qemu_chr_write_buffer(Chardev *s,
break;
}
}
+ saved_errno = errno;
if (*offset > 0) {
/*
* If some data was written by backend, we should
@@ -154,6 +156,7 @@ static int qemu_chr_write_buffer(Chardev *s,
*/
qemu_chr_write_log(s, buf, len);
}
+ errno = saved_errno;
qemu_mutex_unlock(&s->chr_write_lock);
return res;
@@ -186,7 +189,9 @@ int qemu_chr_write(Chardev *s, const uint8_t *buf, int len, bool write_all)
res = qemu_chr_write_buffer(s, buf, len, &offset, write_all);
if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
+ int saved_errno = errno;
replay_char_write_event_save(res, offset);
+ errno = saved_errno;
}
if (res < 0 && offset == 0) {
--
2.51.0
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH 6/7] chardev/char-hub: Retry when qemu_chr_fe_write() can not write
2025-10-22 14:48 [PATCH 0/7] chardev: Improve @docstring and clarify qemu_chr_write() uses Philippe Mathieu-Daudé
` (4 preceding siblings ...)
2025-10-22 14:49 ` [PATCH 5/7] chardev/char: Preserve %errno " Philippe Mathieu-Daudé
@ 2025-10-22 14:49 ` Philippe Mathieu-Daudé
2025-10-22 14:49 ` [PATCH 7/7] hw/char: Simplify when qemu_chr_fe_write() could " Philippe Mathieu-Daudé
6 siblings, 0 replies; 8+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-10-22 14:49 UTC (permalink / raw)
To: qemu-devel
Cc: Paolo Bonzini, Marc-André Lureau, Peter Maydell,
Alex Bennée, Philippe Mathieu-Daudé
When qemu_chr_fe_write() can not write to a backend and there
is no error, it might return '0' to let the caller retry.
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
chardev/char-hub.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/chardev/char-hub.c b/chardev/char-hub.c
index d0967c22336..4bbde9fb033 100644
--- a/chardev/char-hub.c
+++ b/chardev/char-hub.c
@@ -65,7 +65,7 @@ static int hub_chr_write(Chardev *chr, const uint8_t *buf, int len)
continue;
}
r = qemu_chr_fe_write(&d->backends[i].fe, buf, len);
- if (r < 0) {
+ if (r <= 0) {
if (errno == EAGAIN) {
/* Set index and expect to be called soon on watch wake up */
d->be_eagain_ind = i;
--
2.51.0
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH 7/7] hw/char: Simplify when qemu_chr_fe_write() could not write
2025-10-22 14:48 [PATCH 0/7] chardev: Improve @docstring and clarify qemu_chr_write() uses Philippe Mathieu-Daudé
` (5 preceding siblings ...)
2025-10-22 14:49 ` [PATCH 6/7] chardev/char-hub: Retry when qemu_chr_fe_write() can not write Philippe Mathieu-Daudé
@ 2025-10-22 14:49 ` Philippe Mathieu-Daudé
6 siblings, 0 replies; 8+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-10-22 14:49 UTC (permalink / raw)
To: qemu-devel
Cc: Paolo Bonzini, Marc-André Lureau, Peter Maydell,
Alex Bennée, Philippe Mathieu-Daudé, Edgar E. Iglesias,
Alistair Francis, Palmer Dabbelt, qemu-arm, qemu-riscv
If no chars were written, avoid to access the FIFO.
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
hw/char/cadence_uart.c | 2 +-
hw/char/ibex_uart.c | 2 +-
hw/char/sifive_uart.c | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/hw/char/cadence_uart.c b/hw/char/cadence_uart.c
index 0dfa356b6d0..8908ebbe34a 100644
--- a/hw/char/cadence_uart.c
+++ b/hw/char/cadence_uart.c
@@ -316,7 +316,7 @@ static gboolean cadence_uart_xmit(void *do_not_use, GIOCondition cond,
ret = qemu_chr_fe_write(&s->chr, s->tx_fifo, s->tx_count);
- if (ret >= 0) {
+ if (ret > 0) {
s->tx_count -= ret;
memmove(s->tx_fifo, s->tx_fifo + ret, s->tx_count);
}
diff --git a/hw/char/ibex_uart.c b/hw/char/ibex_uart.c
index d6f0d18c777..b7843c7a741 100644
--- a/hw/char/ibex_uart.c
+++ b/hw/char/ibex_uart.c
@@ -161,7 +161,7 @@ static gboolean ibex_uart_xmit(void *do_not_use, GIOCondition cond,
ret = qemu_chr_fe_write(&s->chr, s->tx_fifo, s->tx_level);
- if (ret >= 0) {
+ if (ret > 0) {
s->tx_level -= ret;
memmove(s->tx_fifo, s->tx_fifo + ret, s->tx_level);
}
diff --git a/hw/char/sifive_uart.c b/hw/char/sifive_uart.c
index e7357d585a1..e5b381425a9 100644
--- a/hw/char/sifive_uart.c
+++ b/hw/char/sifive_uart.c
@@ -83,7 +83,7 @@ static gboolean sifive_uart_xmit(void *do_not_use, GIOCondition cond,
fifo8_num_used(&s->tx_fifo), &numptr);
ret = qemu_chr_fe_write(&s->chr, characters, numptr);
- if (ret >= 0) {
+ if (ret > 0) {
/* We wrote the data, actually pop the fifo */
fifo8_pop_bufptr(&s->tx_fifo, ret, NULL);
}
--
2.51.0
^ permalink raw reply related [flat|nested] 8+ messages in thread