From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 3190C5A0271 for ; Wed, 7 Feb 2024 02:04:57 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1707267892; bh=bKecNIysw4nhvzMFS/vG9U/a6bZ+1P92cER3LZpU2dM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=fREr8yu07OPnO4Tfzl6CWF6o9VYPEvlOkh/Eu6B5t0YaAtP/DVL7h4l/ZXgq8XRmP +hhYOxTLHBmJkAapKvSoMjXqzqv81+ufs18N2QeqweHMeDcZJlEv1Yhi9SjF1bJgix pd73jneGwSCguP2lMP+9qQCGQrZjBaiVr7qwrYorOC4HKHwtxyPGUsdpYbS2Tu5Z+B gzAsvZck76ekRdt1tPf43aXzRM7jv615AA05eiDCzH9wBzUCIShyP2sSZJ8Jcgmuyw ygA2jyQGPhN8ajpg+cHyX7OyYQwnSPP9R2EoZ68seg5YjzSYZBnbT0eYXkabWlxd7Y MTn2oHtdlX/AA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4TV22r34KHz4wct; Wed, 7 Feb 2024 12:04:52 +1100 (AEDT) Date: Wed, 7 Feb 2024 12:01:20 +1100 From: David Gibson To: Laurent Vivier Subject: Re: [PATCH 01/24] iov: add some functions to manage iovec Message-ID: References: <20240202141151.3762941-1-lvivier@redhat.com> <20240202141151.3762941-2-lvivier@redhat.com> <0f7bdf00-58cb-4207-94f5-b13d34b1177f@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="pyl+gU8/teoRFQ03" Content-Disposition: inline In-Reply-To: <0f7bdf00-58cb-4207-94f5-b13d34b1177f@redhat.com> Message-ID-Hash: PPYM2TZ4VJ6R4UQELN4F3CC7RJXZOUJ7 X-Message-ID-Hash: PPYM2TZ4VJ6R4UQELN4F3CC7RJXZOUJ7 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: --pyl+gU8/teoRFQ03 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Feb 06, 2024 at 03:28:10PM +0100, Laurent Vivier wrote: > On 2/5/24 06:57, David Gibson wrote: > > On Fri, Feb 02, 2024 at 03:11:28PM +0100, Laurent Vivier wrote: > > ... > > > diff --git a/iov.c b/iov.c > > > new file mode 100644 > > > index 000000000000..38a8e7566021 > > > --- /dev/null > > > +++ b/iov.c > > >=20 > > > + for (i =3D 0, done =3D 0; (offset || done < bytes) && i < iov_cnt; = i++) { > > Not immediately seeing why you need the 'offset ||' part of the conditi= on. >=20 > In fact the loop has two purposes: >=20 > 1- scan the the iovec to reach byte offset in the iov (so until offset is= 0) >=20 > 2- copy the bytes (until done =3D=3D byte) >=20 > It could be written like this: >=20 > for (i =3D 0; offset && i < iov_cnt && offset >=3D iov[i].iov_len ; i++) > =A0=A0=A0=A0=A0=A0=A0 offset -=3D iov[i].iov_len; >=20 > for (done =3D 0; done < bytes && i < iov_cnt; i++) { > =A0=A0=A0 size_t len =3D MIN(iov[i].iov_len - offset, bytes - done); > =A0=A0=A0 memcpy((char *)iov[i].iov_base + offset, (char *)buf + done, le= n); > =A0=A0=A0 done +=3D len; > } Right, but done starts at 0 and will remain zero until you reach the first segment where you need to copy something. So, unless bytes =3D=3D 0, then done < bytes will always be true when offset !=3D 0. And if bytes *is* zero, then there's nothing to do, so there's no need to step through the vector at all. > ... >=20 > > > +unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt, > > > + const struct iovec *iov, unsigned int iov_cnt, > > > + size_t offset, size_t bytes) > > > +{ > > > + size_t len; > > > + unsigned int i, j; > > > + for (i =3D 0, j =3D 0; > > > + i < iov_cnt && j < dst_iov_cnt && (offset || bytes); i++) { > > > + if (offset >=3D iov[i].iov_len) { > > > + offset -=3D iov[i].iov_len; > > > + continue; > > > + } > > > + len =3D MIN(bytes, 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++; > > > + bytes -=3D len; > > > + offset =3D 0; > > > + } > > > + return j; > > > +} > > Small concern about the interface to iov_copy(). If dst_iov_cnt < > > iov_cnt and the chunk of the input iovec you want doesn't fit in the > > destination it will silently truncate - you can't tell if this has > > happened from the return value. If the assumption is that dst_iov_cnt = =3D iov_cnt, > > then there's not really any need to pass it. >=20 > In fact this function will be removed with "vhost-user: use guest buffer > directly in vu_handle_tx()" as it is not needed when we use directly guest > buffers. So I don't think we need to improve this. Ok, fair enough. > If I remember correctly I think it behaves like passt already does with > socket backend: if it doesn't fit, it's dropped silently. >=20 > I've fixed all your other comments. >=20 > Thanks, > Laurent >=20 --=20 David Gibson | 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 --pyl+gU8/teoRFQ03 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmXC1lsACgkQzQJF27ox 2GeDpA//QtyeCVFGWsZ1kbijQBg524UFoO4frcG37x8DCvletmxgkEIM6XxqQDAI pRcI14PMwr1tyN42OCRssjBzY1/cGOoihAKm6y1RSacH/EOlw0onpR9famYGai+/ 73Qm4V2cLF44TEo0Bpn6JJHk4AVWK5hVjJtt9ftfG4j58TLz7gN9wT0ToVFO/yRr 5QkVlvdiF85kdER/YvM85Bf4895rFq9B9JilwE0IZYUmAwHpFxePdG8fa55DCzS/ GRann5ExgrehBdQcWnwDOljaUBcQnC6VDXhPwrlHt5jtM2/bVUlplcHwUzm/jJUc WHN6JFWOUUVqdeSryoe3cnHj3Tj3M7FcvdSW+VKkc9nPjKX+4gVvod4KkZ3JLmaP ubyhmqfGb5MGHj3NJOgUj4ujiY+gfQLXPFjer2OzEb0gz8i+Km/mlwAdW1pfAmyS vFj8h/9gp5xyh7/J/vkw5+Q7qabiS+wh/bJrZEiWUq09w2lmmuXOYrE6B2Yjut4w CvB5Ba9mjI+UWHvNWYrubESurqInNkrS7DXgLE7KpvNCl8dDKrVBU7ZRj4HVd0xl XddNJZ071Oyyl1FSh+4rI7h37ltW3By6awiTsAsBPrzIxZMCl4sFSJqkV0iit5mb UHcjyEzSYeFRkn2dnmw/Qs0ZCMX9MeV1eEmRQo2+6HD8AEoDaKI= =0inX -----END PGP SIGNATURE----- --pyl+gU8/teoRFQ03--