From: Mark Cave-Ayland <mark.caveayland@nutanix.com>
To: John Levon <john.levon@nutanix.com>, qemu-devel@nongnu.org
Cc: "Thanos Makatos" <thanos.makatos@nutanix.com>,
"Peter Maydell" <peter.maydell@linaro.org>,
"Cédric Le Goater" <clg@redhat.com>
Subject: Re: [PATCH v3 4/5] vfio-user: simplify vfio_user_recv_one()
Date: Tue, 2 Dec 2025 11:11:53 +0000 [thread overview]
Message-ID: <03d9e117-d25d-46ed-aba6-03c9a1139eff@nutanix.com> (raw)
In-Reply-To: <20251201095621.2786318-5-john.levon@nutanix.com>
On 01/12/2025 09:56, John Levon wrote:
> This function was unnecessarily difficult to understand due to the
> separate handling of request and reply messages. Use common code for
> both where we can.
>
> Signed-off-by: John Levon <john.levon@nutanix.com>
> ---
> hw/vfio-user/proxy.c | 68 +++++++++++++++++++-------------------------
> 1 file changed, 30 insertions(+), 38 deletions(-)
>
> diff --git a/hw/vfio-user/proxy.c b/hw/vfio-user/proxy.c
> index 87e50501af..d1d63816b3 100644
> --- a/hw/vfio-user/proxy.c
> +++ b/hw/vfio-user/proxy.c
> @@ -281,15 +281,14 @@ static int vfio_user_recv_hdr(VFIOUserProxy *proxy, Error **errp,
> */
> static int vfio_user_recv_one(VFIOUserProxy *proxy, Error **errp)
> {
> - VFIOUserMsg *msg = NULL;
> g_autofree int *fdp = NULL;
> - VFIOUserFDs *reqfds;
> - VFIOUserHdr hdr;
> + VFIOUserMsg *msg = NULL;
> bool isreply = false;
> - int i, ret;
> - size_t msgleft, numfds = 0;
> + size_t msgleft = 0;
> + size_t numfds = 0;
> char *data = NULL;
> - char *buf = NULL;
> + VFIOUserHdr hdr;
> + int i, ret;
>
> /*
> * Complete any partial reads
> @@ -317,8 +316,8 @@ static int vfio_user_recv_one(VFIOUserProxy *proxy, Error **errp)
> }
>
> /*
> - * For replies, find the matching pending request.
> - * For requests, reap incoming FDs.
> + * Find the matching request if this is a reply, or initialize a new
> + * server->client request.
> */
> if (isreply) {
> QTAILQ_FOREACH(msg, &proxy->pending, next) {
> @@ -332,51 +331,44 @@ static int vfio_user_recv_one(VFIOUserProxy *proxy, Error **errp)
> }
> QTAILQ_REMOVE(&proxy->pending, msg, next);
>
> - /*
> - * Process any received FDs
> - */
> - if (numfds != 0) {
> - if (msg->fds == NULL || msg->fds->recv_fds < numfds) {
> - error_setg(errp, "unexpected FDs");
> - goto err;
> - }
> - msg->fds->recv_fds = numfds;
> - memcpy(msg->fds->fds, fdp, numfds * sizeof(int));
> - }
> - } else {
> - if (numfds != 0) {
> - reqfds = vfio_user_getfds(numfds);
> - memcpy(reqfds->fds, fdp, numfds * sizeof(int));
> - } else {
> - reqfds = NULL;
> - }
> - }
> -
> - /*
> - * Put the whole message into a single buffer.
> - */
> - if (isreply) {
> if (hdr.size > msg->rsize) {
> error_setg(errp, "reply larger than recv buffer");
> goto err;
> }
> - *msg->hdr = hdr;
> - data = (char *)msg->hdr + sizeof(hdr);
> } else {
> + void *buf;
> +
> if (hdr.size > proxy->max_xfer_size + sizeof(VFIOUserDMARW)) {
> error_setg(errp, "vfio_user_recv request larger than max");
> goto err;
> }
> +
> buf = g_malloc0(hdr.size);
> - memcpy(buf, &hdr, sizeof(hdr));
> - data = buf + sizeof(hdr);
> - msg = vfio_user_getmsg(proxy, (VFIOUserHdr *)buf, reqfds);
> + msg = vfio_user_getmsg(proxy, buf, NULL);
> msg->type = VFIO_MSG_REQ;
> }
>
> + *msg->hdr = hdr;
> + data = (char *)msg->hdr + sizeof(hdr);
> +
> + if (numfds != 0) {
> + if (msg->type == VFIO_MSG_REQ) {
> + msg->fds = vfio_user_getfds(numfds);
> + } else {
> + if (msg->fds == NULL || msg->fds->recv_fds < numfds) {
> + error_setg(errp, "unexpected FDs in reply");
> + goto err;
> + }
> + msg->fds->recv_fds = numfds;
> + }
> +
> + memcpy(msg->fds->fds, fdp, numfds * sizeof(int));
> + }
> +
> /*
> - * Read rest of message.
> + * Read rest of message into the data buffer.
> */
> +
> msgleft = hdr.size - sizeof(hdr);
> while (msgleft > 0) {
> ret = qio_channel_read(proxy->ioc, data, msgleft, errp);
Reviewed-by: Mark Cave-Ayland <mark.caveayland@nutanix.com>
ATB,
Mark.
next prev parent reply other threads:[~2025-12-02 11:12 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-01 9:56 [PATCH v3 0/5] vfio-user coverity fixes John Levon
2025-12-01 9:56 ` [PATCH v3 1/5] vfio-user: simplify vfio_user_process() John Levon
2025-12-02 10:42 ` Mark Cave-Ayland
2025-12-01 9:56 ` [PATCH v3 2/5] vfio-user: clarify partial message handling John Levon
2025-12-02 10:43 ` Mark Cave-Ayland
2025-12-01 9:56 ` [PATCH v3 3/5] vfio-user: refactor out header handling John Levon
2025-12-02 10:53 ` Mark Cave-Ayland
2025-12-03 7:40 ` Cédric Le Goater
2025-12-01 9:56 ` [PATCH v3 4/5] vfio-user: simplify vfio_user_recv_one() John Levon
2025-12-02 11:11 ` Mark Cave-Ayland [this message]
2025-12-01 9:56 ` [PATCH v3 5/5] vfio-user: recycle msg on failure John Levon
2025-12-02 11:14 ` Mark Cave-Ayland
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=03d9e117-d25d-46ed-aba6-03c9a1139eff@nutanix.com \
--to=mark.caveayland@nutanix.com \
--cc=clg@redhat.com \
--cc=john.levon@nutanix.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=thanos.makatos@nutanix.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).