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=202504 header.b=DtVlnYD+; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 4E78B5A0271 for ; Mon, 14 Apr 2025 04:19:19 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202504; t=1744597149; bh=qzGxiDnAhVrkbVCsQvx+sdG13c5/w/wI9j6iAQs5Qt8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=DtVlnYD+Mm8ylCICjAF6GEFa7l3mh0DjAjQywpfXI+TIUlRNWKl8CG6gps0RYiQHw atWtnyoFe8JVEqebQqxiz1mRx5IthdDj9xCNJ53LSpotZB5OFa0z5AHy3cRD6F+2s3 I23bYihKS4LJQZQyOYT8Apfa25akN/cNSmobVtllnQ7fxUhYTOAXRzflufZCVy28Em dxVpv5ZENgNSy4Pc+CGvNN2bi1d0DB4F09WnNHj1ta5lgzWp7p7GvFSjBXfVpJAX6S i6Wa3tVbHxLejDPaDVhhfhquduWWC7Kzkwce/gS9TmuYm4UhRo4zCK9ZdnIOozs0gJ p3pMPvdfAiqXw== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4ZbWF90KCbz4x0L; Mon, 14 Apr 2025 12:19:09 +1000 (AEST) Date: Mon, 14 Apr 2025 12:14:28 +1000 From: David Gibson To: Laurent Vivier Subject: Re: [PATCH v2 02/20] iov: Introduce iov_tail_drop() and iov_slice() Message-ID: References: <20250411131031.1398006-1-lvivier@redhat.com> <20250411131031.1398006-3-lvivier@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="Id2CYfstIx72Q7Fv" Content-Disposition: inline In-Reply-To: <20250411131031.1398006-3-lvivier@redhat.com> Message-ID-Hash: YLH7U6LPNG225YXKYQMIJXF3YAQ6OIZE X-Message-ID-Hash: YLH7U6LPNG225YXKYQMIJXF3YAQ6OIZE 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 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: --Id2CYfstIx72Q7Fv Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Apr 11, 2025 at 03:10:12PM +0200, Laurent Vivier wrote: > iov_tail_drop() discards a header from the iov_tail, >=20 > iov_slice() makes a new iov referencing a subset of the data > in the original iov. >=20 > Signed-off-by: Laurent Vivier Reviewed-by: David Gibson For the actual behaviour, couple of cosmetic nits noted below. > --- > iov.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > iov.h | 4 ++++ > 2 files changed, 72 insertions(+) >=20 > diff --git a/iov.c b/iov.c > index 8c63b7ea6e31..d2aca59e42ed 100644 > --- a/iov.c > +++ b/iov.c > @@ -156,6 +156,59 @@ size_t iov_size(const struct iovec *iov, size_t iov_= cnt) > return len; > } > =20 > +/** > + * iov_slice - Make a new iov referencing a subset of the data in the o= riginal iov > + * > + * @dst_iov: Pointer to the destination array of struct iovec descri= bing > + * the scatter/gather I/O vector to copy to. > + * @dst_iov_cnt: Maximum number of elements in the destination iov array. > + * @iov: Pointer to the source array of struct iovec describing > + * the scatter/gather I/O vector to copy from. > + * @iov_cnt: Maximum number of elements in the source iov array. > + * @offset: Offset within the source iov from where copying should = start. > + * @bytes: On input, total number of bytes to copy from @iov to @dst_io= v, > + * if @bytes is NULL, copy all the content of @iov, > + * on output actual number of bytes copied. > + * > + * Returns: The number of elements successfully copied to the desti= nation > + * iov array, a negative value if there is no enough room in the s/no/not/ > + * destination iov array > + */ > +/* cppcheck-suppress unusedFunction */ > +int iov_slice(struct iovec *dst_iov, size_t dst_iov_cnt, > + const struct iovec *iov, size_t iov_cnt, > + size_t offset, size_t *bytes) > +{ > + unsigned int i, j; > + size_t remaining =3D bytes ? *bytes : SIZE_MAX; > + > + i =3D iov_skip_bytes(iov, iov_cnt, offset, &offset); > + > + /* copying data */ I think this comment is still a bit misleading. > + for (j =3D 0; i < iov_cnt && j < dst_iov_cnt && remaining; i++) { > + size_t len =3D MIN(remaining, iov[i].iov_len - offset); > + > + dst_iov[j].iov_base =3D (char *)iov[i].iov_base + offset; > + dst_iov[j].iov_len =3D len; > + j++; > + remaining -=3D len; > + offset =3D 0; > + } > + if (j =3D=3D dst_iov_cnt) { > + if (bytes) { > + if (remaining) > + return -1; > + } else if (i !=3D iov_cnt) { > + return -1; > + } > + } > + > + if (bytes) > + *bytes -=3D remaining; > + > + return j; > +} > + > /** > * iov_tail_prune() - Remove any unneeded buffers from an IOV tail > * @tail: IO vector tail (modified) > @@ -191,6 +244,21 @@ size_t iov_tail_size(struct iov_tail *tail) > return iov_size(tail->iov, tail->cnt) - tail->off; > } > =20 > +/** > + * iov_tail_drop() - Discard a header from an IOV tail > + * @tail: IO vector tail > + * @len: length to move the head of the tail > + * > + * Returns: true if the tail still contains any bytes, otherwise false > + */ > +/* cppcheck-suppress unusedFunction */ > +bool iov_tail_drop(struct iov_tail *tail, size_t len) > +{ > + tail->off =3D tail->off + len; > + > + return iov_tail_prune(tail); > +} > + > /** > * iov_peek_header_() - Get pointer to a header from an IOV tail > * @tail: IOV tail to get header from > diff --git a/iov.h b/iov.h > index 9855bf0c0c32..3f8259aa8d28 100644 > --- a/iov.h > +++ b/iov.h > @@ -28,6 +28,9 @@ size_t iov_from_buf(const struct iovec *iov, size_t iov= _cnt, > size_t iov_to_buf(const struct iovec *iov, size_t iov_cnt, > size_t offset, void *buf, size_t bytes); > size_t iov_size(const struct iovec *iov, size_t iov_cnt); > +int iov_slice(struct iovec *dst_iov, size_t dst_iov_cnt, > + const struct iovec *iov, size_t iov_cnt, > + size_t offset, size_t *bytes); > =20 > /* > * DOC: Theory of Operation, struct iov_tail > @@ -72,6 +75,7 @@ struct iov_tail { > =20 > bool iov_tail_prune(struct iov_tail *tail); > size_t iov_tail_size(struct iov_tail *tail); > +bool iov_tail_drop(struct iov_tail *tail, size_t len); > void *iov_peek_header_(struct iov_tail *tail, size_t len, size_t align); > void *iov_remove_header_(struct iov_tail *tail, size_t len, size_t align= ); > =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 --Id2CYfstIx72Q7Fv Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmf8b4MACgkQzQJF27ox 2GcRyBAAoVUkG2pziBJoHSbZbMaYu9ggp0BADGoMFiQRsWAeK5wyZvZGAZDdvS1P JRF7HWOpql6MCLnmbmBFcRu8IedHXVQJU21mm6/N2wVSZoHe9NS+Typdh53oa8z+ 6iCv8JeKsXFXspz3bWMLhRWX8C85kFou+xMS26LzvefCUPzasXuFFgGUKd9/mROk 9kHPAUw+nWX8vPUGZux10Iamab88JhWhwd0ilqmYDzOPgdR86SP4Nwy1kaphVef0 FI3xmxpCxkkc7H1XR33BV8JVze/aYkLEZriI0RLuL8+KRwkCzkXw07C0YbyJIOT3 5EaVUWla+30A6ArLRtt/aB/vb1YalSogJnSh+3TXrXZMQSQxPb2+9Lc8HJnrlLwY 3IWDOHG02NQvnpH5v8w8qSW5rmujykUNsTvtmT3XlOEyXPKyVU0BHjZr+kFChRTj TzUD9z/c+P/n36jtcUTWV2MNMohWFlt3xu4v7ORikLG7W36B6uZMEcDMo2LK9KQa 8WSIWITV7+xFUaDd0Vln3+dU6GWOEOfw5v80hGlmSzN02dPmq8f0ULuP+O4EEsFH GrLfVLzRbjj1MSnxpIjqsqnS5d02USwwUwuqJpKziZ8lMZUIRnEq9gRBb3V2N09i 77vvnoXW49jzK/6xeCI5nLbA+GtscCs0vSeUZoAqrLr00eGGbWE= =LkXR -----END PGP SIGNATURE----- --Id2CYfstIx72Q7Fv--