From: Stefano Brivio <sbrivio@redhat.com>
To: Markus Armbruster <armbru@redhat.com>
Cc: David Gibson <david@gibson.dropbear.id.au>, passt-dev@passt.top
Subject: Re: [PATCH v3 2/2] util: Remove possible quadratic behaviour from write_remainder()
Date: Thu, 19 Sep 2024 18:19:25 +0200 [thread overview]
Message-ID: <20240919181925.171635b2@elisabeth> (raw)
In-Reply-To: <87plozsz4d.fsf@pond.sub.org>
On Thu, 19 Sep 2024 12:42:58 +0200
Markus Armbruster <armbru@redhat.com> wrote:
> David Gibson <david@gibson.dropbear.id.au> 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 <david@gibson.dropbear.id.au>
> > ---
> > 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?
Oh, oops, in theory yes, but while reviewing this I just had a look at
the result in util.c, and now it's merged. It's not a big issue I'd
say. Thanks for reporting anyway.
--
Stefano
next prev parent reply other threads:[~2024-09-19 16:19 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-18 10:44 [PATCH v3 0/2] util: Fix some problems in write_remainder() David Gibson
2024-09-18 10:44 ` [PATCH v3 1/2] util: Add helper to write() all of a buffer David Gibson
2024-09-18 10:44 ` [PATCH v3 2/2] util: Remove possible quadratic behaviour from write_remainder() David Gibson
2024-09-19 10:42 ` Markus Armbruster
2024-09-19 15:36 ` David Gibson
2024-09-19 16:19 ` Stefano Brivio [this message]
2024-09-18 18:13 ` [PATCH v3 0/2] util: Fix some problems in write_remainder() Stefano Brivio
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240919181925.171635b2@elisabeth \
--to=sbrivio@redhat.com \
--cc=armbru@redhat.com \
--cc=david@gibson.dropbear.id.au \
--cc=passt-dev@passt.top \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).