From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: "Jason A. Donenfeld" <Jason@zx2c4.com>
Cc: linux-kernel@vger.kernel.org,
Andrew Morton <akpm@linux-foundation.org>,
Kees Cook <keescook@chromium.org>
Subject: Re: [PATCH v3 1/2] minmax: sanity check constant bounds when clamping
Date: Mon, 26 Sep 2022 16:46:42 +0300 [thread overview]
Message-ID: <YzGtQnlG560rXQv9@smile.fi.intel.com> (raw)
In-Reply-To: <20220926133435.1333846-1-Jason@zx2c4.com>
On Mon, Sep 26, 2022 at 03:34:34PM +0200, Jason A. Donenfeld wrote:
> The clamp family of functions only makes sense if hi>=lo. If hi and lo
> are compile-time constants, then raise a build error. Doing so has
> already caught buggy code. This also introduces the infrastructure to
> improve the clamping function in subsequent commits.
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Thanks!
> Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Kees Cook <keescook@chromium.org>
> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
> ---
> include/linux/minmax.h | 26 ++++++++++++++++++++++++--
> 1 file changed, 24 insertions(+), 2 deletions(-)
>
> diff --git a/include/linux/minmax.h b/include/linux/minmax.h
> index 5433c08fcc68..293a66ad7830 100644
> --- a/include/linux/minmax.h
> +++ b/include/linux/minmax.h
> @@ -37,6 +37,28 @@
> __cmp(x, y, op), \
> __cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op))
>
> +#define __clamp(val, lo, hi) \
> + __cmp(__cmp(val, lo, >), hi, <)
> +
> +#define __clamp_once(val, lo, hi, unique_val, unique_lo, unique_hi) ({ \
> + typeof(val) unique_val = (val); \
> + typeof(lo) unique_lo = (lo); \
> + typeof(hi) unique_hi = (hi); \
> + __clamp(unique_val, unique_lo, unique_hi); })
> +
> +#define __clamp_input_check(lo, hi) \
> + (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \
> + __is_constexpr((lo) > (hi)), (lo) > (hi), false)))
> +
> +#define __careful_clamp(val, lo, hi) ({ \
> + __clamp_input_check(lo, hi) + \
> + __builtin_choose_expr(__typecheck(val, lo) && __typecheck(val, hi) && \
> + __typecheck(hi, lo) && __is_constexpr(val) && \
> + __is_constexpr(lo) && __is_constexpr(hi), \
> + __clamp(val, lo, hi), \
> + __clamp_once(val, lo, hi, __UNIQUE_ID(__val), \
> + __UNIQUE_ID(__lo), __UNIQUE_ID(__hi))); })
> +
> /**
> * min - return minimum of two values of the same or compatible types
> * @x: first value
> @@ -86,7 +108,7 @@
> * This macro does strict typechecking of @lo/@hi to make sure they are of the
> * same type as @val. See the unnecessary pointer comparisons.
> */
> -#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
> +#define clamp(val, lo, hi) __careful_clamp(val, lo, hi)
>
> /*
> * ..and if you can't take the strict
> @@ -121,7 +143,7 @@
> * This macro does no typechecking and uses temporary variables of type
> * @type to make all the comparisons.
> */
> -#define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
> +#define clamp_t(type, val, lo, hi) __careful_clamp((type)(val), (type)(lo), (type)(hi))
>
> /**
> * clamp_val - return a value clamped to a given range using val's type
> --
> 2.37.3
>
--
With Best Regards,
Andy Shevchenko
next prev parent reply other threads:[~2022-09-26 15:11 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
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 ` Andy Shevchenko [this message]
2022-09-26 18:26 ` [PATCH v3 1/2] minmax: sanity check constant bounds when clamping 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=YzGtQnlG560rXQv9@smile.fi.intel.com \
--to=andriy.shevchenko@linux.intel.com \
--cc=Jason@zx2c4.com \
--cc=akpm@linux-foundation.org \
--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).