From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: linux-kernel@vger.kernel.org,
Andrew Morton <akpm@linux-foundation.org>,
Kees Cook <keescook@chromium.org>
Subject: Re: [PATCH] minmax: clamp more efficiently by avoiding extra comparison
Date: Fri, 23 Sep 2022 12:48:48 +0200 [thread overview]
Message-ID: <CAHmME9p+SKTHzniw=TsC=vnQsyCuBa08+LcK-CsFcjA58K+ifA@mail.gmail.com> (raw)
In-Reply-To: <CAHmME9osJiKg8-o-OdfCPS6t_fZ=zgGKZdy0CgybaK2NDv8XLA@mail.gmail.com>
Hey again,
On Fri, Sep 23, 2022 at 12:40 PM Jason A. Donenfeld <Jason@zx2c4.com> wrote:
>
> Hi Andy,
>
> On Fri, Sep 23, 2022 at 12:36 PM Andy Shevchenko
> <andriy.shevchenko@linux.intel.com> wrote:
> >
> > On Fri, Sep 23, 2022 at 12:06:21PM +0200, Jason A. Donenfeld wrote:
> > > Currently the clamp algorithm does:
> > >
> > > if (val > hi)
> > > val = hi;
> > > if (val < lo)
> > > val = lo;
> > >
> > > But since hi > lo by definition, this can be made more efficient with:
> >
> > It's strongly speaking, but we have to proof that, right?
> > So, while I haven't checked the code, this change should also
> > include (does it?) the corresponding compile-time checks (for
> > constant arguments) in similar way how it's done for GENMASK().
> >
> > Otherwise I have no objections.
>
> I think most cases are with compile time constants, but some cases are
> with variables. What should we do in that case? Checking variables at
> runtime incurs the same cost as the old code. I guess we could do this
> fast thing for constants and the slower old thing for non-constants?
> Or not do either, keep this commit as is, and just accept that if you
> pass bogus bounds to clamp, you're going to end up with something
> weird, which is already the case now so not a big deal?
Actually, yea, I think we should keep this commit as-is and not add
additional checking becauseeeee not only is hi>lo by definition, but
both for the old code and for the new code, the result of lo>hi is
total nonsense:
Assuming hi > lo, these snippets all yield the same result:
if (val > hi)
val = hi;
if (val < lo)
val = lo;
if (val > hi)
val = hi;
else if (val < lo)
val = lo;
if (val < lo)
val = lo;
if (val > hi)
val = hi;
if (val < lo)
val = lo;
else if (val > hi)
val = hi;
Assuming lo > hi, and the first condition triggers, these snippets all
yield different results, all of which are undefined nonsense:
if (val > hi)
val = hi;
if (val < lo)
val = lo;
--> val is lo
if (val > hi)
val = hi;
else if (val < lo)
val = lo;
--> val is hi
if (val < lo)
val = lo;
if (val > hi)
val = hi;
--> val is hi
if (val < lo)
val = lo;
else if (val > hi)
val = hi;
--> val is lo
Jason
next prev parent reply other threads:[~2022-09-23 10:49 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-23 10:06 [PATCH] minmax: clamp more efficiently by avoiding extra comparison Jason A. Donenfeld
2022-09-23 10:35 ` Andy Shevchenko
2022-09-23 10:40 ` Jason A. Donenfeld
2022-09-23 10:48 ` Jason A. Donenfeld [this message]
2022-09-23 15:12 ` Andy Shevchenko
2022-09-23 15:13 ` Jason A. Donenfeld
2022-09-23 15:40 ` [PATCH v2] " Jason A. Donenfeld
2022-09-23 16:41 ` Kees Cook
2022-09-23 16:42 ` Jason A. Donenfeld
2022-09-23 16:53 ` Andy Shevchenko
2022-09-23 16:54 ` Jason A. Donenfeld
2022-09-23 19:47 ` Kees Cook
2022-09-23 22:54 ` Andrew Morton
2022-09-24 0:02 ` Kees Cook
2022-09-24 10:37 ` Jason A. Donenfeld
2022-09-25 16:29 ` Andrew Morton
2022-09-26 10:00 ` Andy Shevchenko
2022-09-26 12:23 ` Jason A. Donenfeld
2022-09-26 13:34 ` [PATCH v3 1/2] minmax: sanity check constant bounds when clamping Jason A. Donenfeld
2022-09-26 13:34 ` [PATCH v3 2/2] minmax: clamp more efficiently by avoiding extra comparison Jason A. Donenfeld
2022-09-26 18:30 ` Kees Cook
2022-09-26 21:33 ` Jason A. Donenfeld
2022-09-26 13:46 ` [PATCH v3 1/2] minmax: sanity check constant bounds when clamping Andy Shevchenko
2022-09-26 18:26 ` Kees Cook
2022-10-04 13:41 ` Jason A. Donenfeld
2022-10-04 14:39 ` Kees Cook
2022-10-04 15:01 ` Jason A. Donenfeld
2022-09-26 18:30 ` [PATCH v2] minmax: clamp more efficiently by avoiding extra comparison Kees Cook
2022-09-23 15:10 ` [PATCH] " Andy Shevchenko
2022-09-23 15:11 ` Jason A. Donenfeld
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='CAHmME9p+SKTHzniw=TsC=vnQsyCuBa08+LcK-CsFcjA58K+ifA@mail.gmail.com' \
--to=jason@zx2c4.com \
--cc=akpm@linux-foundation.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.org \
/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.
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 NNTP newsgroup(s).