From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: passt.top; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=aoIwzYze; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTP id 587945A004E for ; Wed, 18 Sep 2024 08:20:33 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726640432; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=8aVCAWrL6qAONjv5901v/cICCSdqqrA2Sv8j1t1oeXE=; b=aoIwzYze6eNDyceapMAklsY+PNUXs/RKuxw/oeOfrHloqRJjhd9fMSJkG6V7EbW4WhjiO2 hjOG/8/eL8IBrOo0wXlbNia4iT+mQ9h/Cr4uHYf11hXXPw7HztYbFeooP4NoSJw7bj15I7 mHy5Bx6DN94C/uU9ZUcKz52OLJg0L1U= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-63-HGKQ57dwPEaIR_Oy49eO5Q-1; Wed, 18 Sep 2024 02:20:30 -0400 X-MC-Unique: HGKQ57dwPEaIR_Oy49eO5Q-1 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 43F751955BC1; Wed, 18 Sep 2024 06:20:29 +0000 (UTC) Received: from blackfin.pond.sub.org (unknown [10.39.192.47]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A341F19560AA; Wed, 18 Sep 2024 06:20:28 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id DCFE721E6A28; Wed, 18 Sep 2024 08:20:25 +0200 (CEST) From: Markus Armbruster To: David Gibson Subject: Re: [PATCH v2 2/2] util: Remove possible quadratic behaviour from write_remainder() In-Reply-To: <20240917064555.1040365-3-david@gibson.dropbear.id.au> (David Gibson's message of "Tue, 17 Sep 2024 16:45:55 +1000") References: <20240917064555.1040365-1-david@gibson.dropbear.id.au> <20240917064555.1040365-3-david@gibson.dropbear.id.au> Date: Wed, 18 Sep 2024 08:20:25 +0200 Message-ID: <871q1h4h5i.fsf@pond.sub.org> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-MailFrom: armbru@redhat.com X-Mailman-Rule-Hits: nonmember-moderation X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation Message-ID-Hash: KC6JGKA54HRPJRH63UJ6Q4OSIXPN4Y4N X-Message-ID-Hash: KC6JGKA54HRPJRH63UJ6Q4OSIXPN4Y4N X-Mailman-Approved-At: Wed, 18 Sep 2024 08:29:50 +0200 CC: passt-dev@passt.top, Stefano Brivio 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: David Gibson writes: > write_remainder() steps through the buffers in an IO vector writing out > everything past a certain byte offset. However, on each iteration it > rescans the buffer from the beginning to find out where we're up to. With > an unfortunate set of write sizes this could lead to quadratic behaviour. > > In an even less likely set of circumstances (total vector length > maximum > size_t) the 'skip' variable could overflow. This is one factor in a > longstanding Coverity error we've seen (although I still can't figure out > the remainder of its complaint). > > Rework write_remainder() to always work out our new position in the vector > relative to our old/current position, rather than starting from the > beginning each time. As a bonus this seems to fix the Coverity error. > > Signed-off-by: David Gibson > --- > util.c | 20 +++++++++++--------- > 1 file changed, 11 insertions(+), 9 deletions(-) > > diff --git a/util.c b/util.c > index 7db7c2e7..43e5f5ec 100644 > --- a/util.c > +++ b/util.c > @@ -615,28 +615,30 @@ int write_all_buf(int fd, const void *buf, size_t len) > * > * Return: 0 on success, -1 on error (with errno set) > * > - * #syscalls write writev > + * #syscalls writev I'm not familiar with this annotation. I guess it's system calls used in directly this function, not including the ones used in callees. > */ > int write_remainder(int fd, const struct iovec *iov, size_t iovcnt, size_t skip) > { > - size_t offset, i; > + size_t i = 0, offset; > > - while ((i = iov_skip_bytes(iov, iovcnt, skip, &offset)) < iovcnt) { > + while ((i += iov_skip_bytes(iov + i, iovcnt - i, skip, &offset)) < iovcnt) { > ssize_t rc; > > if (offset) { > - rc = write(fd, (char *)iov[i].iov_base + offset, > - iov[i].iov_len - offset); > - } else { > - rc = writev(fd, &iov[i], iovcnt - i); > + /* Write the remainder of the partially written buffer */ > + if (write_all_buf(fd, (char *)iov[i].iov_base + offset, > + iov[i].iov_len - offset) < 0) > + return -1; > + i++; > } > > + /* Write as much of the remaining whole buffers as we can */ > + rc = writev(fd, &iov[i], iovcnt - i); Last argument can be zero now. Okay. > if (rc < 0) > return -1; > > - skip += rc; > + skip = rc; > } > - > return 0; > } We could use try to save system calls by only using writev(), with a suitably adjusted copy of @iov. But non-zero @offset should be rare, so I guess it's not worth the bother. Reviewed-by: Markus Armbruster