From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202510 header.b=xJUQ8Ncp; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 9528E5A026F for ; Wed, 05 Nov 2025 05:01:12 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202510; t=1762315270; bh=mCzH7Pa7I/SLM0r48f+qT451yqSp/omatIkmD2UTSUA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=xJUQ8Ncp7WnPO12QdaG0IOf1CTEdMExc3ynqIb4bEo6pxFDEBtY/tJpnMUVFrGRD9 qlAWHFr0iabK0jf/+wcPWkcjykbZqA78m11irhZ5sZMhxoaMITDO7TcJHZKKRPpsNC pWwgGhRpkfqpXW0weUqCJgNVlNkjsG5INIFEfwhaRnCJX5TWrL+ucDY7xCfNtqBQ0F 0AHNMbhE1o9gx6j5VWPEZxDNrPMmAFxz2sU+sP60LghsY9E/qqYSQFY+aocAXCGlA0 eAS601a+7uYtvYqQHSlRaLcaaJiwIl34+T1MnSSsorqYOuRewJ1TCJLrPP/IgP1SIT AW+GqtSRNcmxw== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4d1WpG4Htbz4wMG; Wed, 05 Nov 2025 15:01:10 +1100 (AEDT) Date: Wed, 5 Nov 2025 15:01:04 +1100 From: David Gibson To: Stefano Brivio Subject: Re: [RFC PATCH 3/3] iov, vu_common: Make iov_from_buf() fill destination iov entirely Message-ID: References: <20251103101629.1412331-1-sbrivio@redhat.com> <20251103101629.1412331-4-sbrivio@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="44Nc4MvINAFjVylc" Content-Disposition: inline In-Reply-To: <20251103101629.1412331-4-sbrivio@redhat.com> Message-ID-Hash: GMTJB2VCY2JC64LCSJB7GDNBFW4NZDVD X-Message-ID-Hash: GMTJB2VCY2JC64LCSJB7GDNBFW4NZDVD X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top, Laurent Vivier X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --44Nc4MvINAFjVylc Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Nov 03, 2025 at 11:16:29AM +0100, Stefano Brivio wrote: > ...and, for consistency, rename 'bytes' to 'copy' in iov_to_buf(). >=20 > Two commits ago, I changed vhost-user functions to use iov_from_buf() > to copy only up to the size of source buffers, instead of using the > size of the destination vhost-user buffers. >=20 > This change pads the rest with zeroes, which is not strictly needed, > but looks definitely cleaner. This seems like a useful thing, but maybe it would be clearer as a separate iov_memset() / iov_from_zero() rather than built into iov_from_buf(). >=20 > Signed-off-by: Stefano Brivio > --- > iov.c | 54 ++++++++++++++++++++++++++++++++++------------------- > iov.h | 4 ++-- > vu_common.c | 2 +- > 3 files changed, 38 insertions(+), 22 deletions(-) >=20 > diff --git a/iov.c b/iov.c > index dc1b6b1..557be55 100644 > --- a/iov.c > +++ b/iov.c > @@ -59,35 +59,51 @@ size_t iov_skip_bytes(const struct iovec *iov, size_t= n, > * @iov_cnt: Number of elements in the iovec array > * @offset: Destination offset in iovec array > * @buf: Source buffer > - * @bytes: Bytes to copy > + * @copy: Bytes to copy > + * @fill: Bytes to zero-fill after copied bytes > * > - * Return: number of bytes copied > + * Return: number of bytes filled, with data or zeroes > */ > size_t iov_from_buf(const struct iovec *iov, size_t iov_cnt, > - size_t offset, const void *buf, size_t bytes) > + size_t offset, const void *buf, size_t copy, size_t fill) > { > + size_t copied, filled; > unsigned int i; > - size_t copied; > =20 > - if (__builtin_constant_p(bytes) && iov_cnt && > - offset <=3D iov[0].iov_len && bytes <=3D iov[0].iov_len - offset) { > - memcpy((char *)iov[0].iov_base + offset, buf, bytes); > + if (__builtin_constant_p(copy) && iov_cnt && > + offset <=3D iov[0].iov_len && > + (copy + fill) <=3D iov[0].iov_len - offset) { > + memcpy((char *)iov[0].iov_base + offset, buf, copy); > + memset((char *)iov[0].iov_base + offset + copy, 0, fill); > =20 > - return bytes; > + return copy + fill; > } > =20 > i =3D iov_skip_bytes(iov, iov_cnt, offset, &offset); > =20 > - for (copied =3D 0; copied < bytes && i < iov_cnt; i++) { > - size_t len =3D MIN(iov[i].iov_len - offset, bytes - copied); > + for (copied =3D 0; copied < copy && i < iov_cnt; i++) { > + size_t len =3D MIN(iov[i].iov_len - offset, copy - copied); > =20 > memcpy((char *)iov[i].iov_base + offset, (char *)buf + copied, > len); > copied +=3D len; > + > + if (copied < copy) > + offset =3D 0; /* More to copy in the next iteration */ > + else > + offset =3D len; /* Start of zero-filling, see below */ > + } > + > + for (filled =3D 0; filled < fill && i < iov_cnt; i++) { > + size_t len =3D MIN(iov[i].iov_len - offset, fill - filled); > + > + memcpy((char *)iov[i].iov_base + offset, > + (char *)buf + copied + filled, len); > + filled +=3D len; > offset =3D 0; > } > =20 > - return copied; > + return copied + filled; > } > =20 > /** > @@ -96,27 +112,27 @@ size_t iov_from_buf(const struct iovec *iov, size_t = iov_cnt, > * @iov_cnt: Number of elements in iovec array > * @offset: Source offset altogether, counted in flattened iovec > * @buf: Destination buffer > - * @bytes: Bytes to copy > + * @copy: Bytes to copy > * > * Return: number of bytes copied > */ > size_t iov_to_buf(const struct iovec *iov, size_t iov_cnt, > - size_t offset, void *buf, size_t bytes) > + size_t offset, void *buf, size_t copy) > { > unsigned int i; > size_t copied; > =20 > - if (__builtin_constant_p(bytes) && iov_cnt && > - offset <=3D iov[0].iov_len && bytes <=3D iov[0].iov_len - offset) { > - memcpy(buf, (char *)iov[0].iov_base + offset, bytes); > + if (__builtin_constant_p(copy) && iov_cnt && > + offset <=3D iov[0].iov_len && copy <=3D iov[0].iov_len - offset) { > + memcpy(buf, (char *)iov[0].iov_base + offset, copy); > =20 > - return bytes; > + return copy; > } > =20 > i =3D iov_skip_bytes(iov, iov_cnt, offset, &offset); > =20 > - for (copied =3D 0; copied < bytes && i < iov_cnt; i++) { > - size_t len =3D MIN(iov[i].iov_len - offset, bytes - copied); > + for (copied =3D 0; copied < copy && i < iov_cnt; i++) { > + size_t len =3D MIN(iov[i].iov_len - offset, copy - copied); > =20 > ASSERT(iov[i].iov_base); > =20 > diff --git a/iov.h b/iov.h > index ba1fda5..9b5d910 100644 > --- a/iov.h > +++ b/iov.h > @@ -24,9 +24,9 @@ > size_t iov_skip_bytes(const struct iovec *iov, size_t n, > size_t skip, size_t *offset); > size_t iov_from_buf(const struct iovec *iov, size_t iov_cnt, > - size_t offset, const void *buf, size_t bytes); > + size_t offset, const void *buf, size_t copy, size_t = fill); > size_t iov_to_buf(const struct iovec *iov, size_t iov_cnt, > - size_t offset, void *buf, size_t bytes); > + size_t offset, void *buf, size_t copy); > size_t iov_size(const struct iovec *iov, size_t iov_cnt); > =20 > /* > diff --git a/vu_common.c b/vu_common.c > index 21cfb2a..d941b72 100644 > --- a/vu_common.c > +++ b/vu_common.c > @@ -274,7 +274,7 @@ int vu_send_single(const struct ctx *c, const void *b= uf, size_t size) > =20 > /* copy data from the buffer to the iovec */ > iov_from_buf(in_sg, elem_cnt, sizeof(struct virtio_net_hdr_mrg_rxbuf), > - buf, size); > + buf, size, vu_buf_size - size); > =20 > if (*c->pcap) { > pcap_iov(in_sg, elem_cnt, > --=20 > 2.43.0 >=20 --=20 David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson --44Nc4MvINAFjVylc Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmkKy/8ACgkQzQJF27ox 2GfQrQ//SI015S40j3pM+9Gwc7DMGGA9T9derj8HtMqjlAx9Ue6YN75zKMzg+XMC OuXcDKQBzNmO1AarNzfytc3byMjK0AAAfNSu/+CQJk5yPc+wnNBh0D56E86/n4p6 F8Dl6EzPaR4rpEmqU0x1hRjaUq8q9v9Qpt90H4rqpxEs2pr7jrTezgsOBJbvCYqv ZIbc+u+0nnrDAq/DnttfGlwl3FwEpkmVgASgka6Mm5ts0NLRfXn27UUgjie8IVhf St/HkiIHNyLCxPnT1PtgO+ZoVZ+BjRYZBPVrTFb/vENKZ105F8jNHYCOsifKTXTR bCkxSwzbqes4puDX8XxcxNlkW1N/vYk/s7aJ02keKxY4TEZb2n7ZPL08PcmF/Gt7 3rt2TRxfuOWKdRDKqmLwrPEHqkyGZYVYM7axirab1Q53EOTJC5B4+oGPhoICsgM7 4BkiZTN4vxOaKSqwy6RgytPgwHyJrJi1Y09IfnlFqcxdTeNS7yup6q1V6no6IrsW h3jG7haY4qoLEuy5wJJFJoMg7l3egvVafv6EWrgcAljVFVGj+TisC1Q5EWYuklNj Ss9ZH+8Zu0uB2Wg7lgwV5VUfebomz/qhDYJgAqbDRquWV0tJO/g1ae/m9BXnttv1 5pV8FZBg68ErrM6PXfjCZuNMI/u9A34cCmgMugcYwUk4TYWBFUU= =qt6s -----END PGP SIGNATURE----- --44Nc4MvINAFjVylc--