Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 00B1AC433FE for ; Tue, 15 Nov 2022 22:32:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231947AbiKOWcg (ORCPT ); Tue, 15 Nov 2022 17:32:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238585AbiKOWcM (ORCPT ); Tue, 15 Nov 2022 17:32:12 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E4926D8A for ; Tue, 15 Nov 2022 14:32:11 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 9F0BFB81B87 for ; Tue, 15 Nov 2022 22:32:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 444DCC433D6; Tue, 15 Nov 2022 22:32:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1668551529; bh=D15fsUVVHYtP4+53eZtVVnNU5WUuOCtsO5OwrkMgd6U=; h=Date:To:From:Subject:From; b=qfjIs7SjUSbZKNF3v/r4pD1PejOoS3wzCiJCbMkK/nVdfKlBXObNaA0CF5N6/LZC2 FwH59L2NSOI/8bmCF/Mk7SxFuYte7dVMn54J+9rq6VosD56vvpV1rTPFZtNkUC+53v iiWXZwHT1LiOShJMNjAIFdtYcZds9DXBGmoYodWY= Date: Tue, 15 Nov 2022 14:32:08 -0800 To: mm-commits@vger.kernel.org, keescook@chromium.org, andriy.shevchenko@linux.intel.com, Jason@zx2c4.com, akpm@linux-foundation.org From: Andrew Morton Subject: [merged mm-nonmm-stable] minmax-sanity-check-constant-bounds-when-clamping.patch removed from -mm tree Message-Id: <20221115223209.444DCC433D6@smtp.kernel.org> Precedence: bulk Reply-To: linux-kernel@vger.kernel.org List-ID: X-Mailing-List: mm-commits@vger.kernel.org The quilt patch titled Subject: minmax: sanity check constant bounds when clamping has been removed from the -mm tree. Its filename was minmax-sanity-check-constant-bounds-when-clamping.patch This patch was dropped because it was merged into the mm-nonmm-stable branch of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm ------------------------------------------------------ From: "Jason A. Donenfeld" Subject: minmax: sanity check constant bounds when clamping Date: Mon, 26 Sep 2022 15:34:34 +0200 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. [akpm@linux-foundation.org: coding-style cleanups] [akpm@linux-foundation.org: s@&&\@&& \@] Link: https://lkml.kernel.org/r/20220926133435.1333846-1-Jason@zx2c4.com Signed-off-by: Jason A. Donenfeld Reviewed-by: Andy Shevchenko Cc: Kees Cook Signed-off-by: Andrew Morton --- include/linux/minmax.h | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) --- a/include/linux/minmax.h~minmax-sanity-check-constant-bounds-when-clamping +++ a/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 _ Patches currently in -mm which might be from Jason@zx2c4.com are wifi-rt2x00-use-explicitly-signed-or-unsigned-types.patch