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=ZC4MXIN7; 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 37C0F5A004C for ; Thu, 19 Sep 2024 12:43:07 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726742586; 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=EMW+SR4+hRhxpnZAGIkxSs3jSFN7aF5nWFPstn+9ynM=; b=ZC4MXIN7Qh1tHtsS7ZwySajULFwC7Cwmn4JF5hB0xRM9zJNJVSiUtx/uvPUnPwVIG8DEhs hPOBvypTHDt+T5NnhWNVY/Zway6e4M4zguMm+RGqRJ8fWyNknqBoEUYl+dGEWdeMjJXiXY IFqL0qVKgwqPylbAr7G1lM6AMOrlCBY= 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-563-UTquK1mqOVC_6lpc-JzIUw-1; Thu, 19 Sep 2024 06:43:04 -0400 X-MC-Unique: UTquK1mqOVC_6lpc-JzIUw-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (unknown [10.30.177.15]) (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 B9BA41953963; Thu, 19 Sep 2024 10:43:02 +0000 (UTC) Received: from blackfin.pond.sub.org (unknown [10.39.192.47]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1904D1956095; Thu, 19 Sep 2024 10:43:01 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id E9C5121E6A28; Thu, 19 Sep 2024 12:42:58 +0200 (CEST) From: Markus Armbruster To: David Gibson Subject: Re: [PATCH v3 2/2] util: Remove possible quadratic behaviour from write_remainder() In-Reply-To: <20240918104406.3050878-3-david@gibson.dropbear.id.au> (David Gibson's message of "Wed, 18 Sep 2024 20:44:06 +1000") References: <20240918104406.3050878-1-david@gibson.dropbear.id.au> <20240918104406.3050878-3-david@gibson.dropbear.id.au> Date: Thu, 19 Sep 2024 12:42:58 +0200 Message-ID: <87plozsz4d.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.15 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: SBTMS7XJEH4AR7QGOAZFQ53D7BI5YNTF X-Message-ID-Hash: SBTMS7XJEH4AR7QGOAZFQ53D7BI5YNTF X-Mailman-Approved-At: Thu, 19 Sep 2024 20:07:22 +0200 CC: passt-dev@passt.top, Stefano Brivio , Markus Armbruster 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 | 27 +++++++++++++++++---------- > 1 file changed, 17 insertions(+), 10 deletions(-) > > diff --git a/util.c b/util.c > index 7db7c2e7..87309c51 100644 > --- a/util.c > +++ b/util.c > @@ -597,10 +597,15 @@ int write_all_buf(int fd, const void *buf, size_t len) > size_t left = len; > > while (left) { > - ssize_t rc = write(fd, p, left); > + ssize_t rc; > + > + do > + rc = write(fd, p, left); > + while ((rc < 0) && errno == EINTR); > > if (rc < 0) > return -1; > + > p += rc; > left -= rc; > } Uh, shouldn't this be squashed into PATCH 1? > @@ -615,28 +620,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 > */ > 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); > if (rc < 0) > return -1; > > - skip += rc; > + skip = rc; > } > - > return 0; > }