From nobody Fri Apr 10 15:40:56 2026 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 3568DC00140 for ; Wed, 24 Aug 2022 08:47:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235972AbiHXIra (ORCPT ); Wed, 24 Aug 2022 04:47:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235960AbiHXIpd (ORCPT ); Wed, 24 Aug 2022 04:45:33 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 133975FF6E for ; Wed, 24 Aug 2022 01:45:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661330732; x=1692866732; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=+XjYRediJD7IwQVGFW2QA3pPp1WRUo9zWyg1rmydIJA=; b=HSus0EYjDS5FXmqHuxNxky23J1O0q3Aur7lOGDZFe8AWIIlb657hgsWw /l13+anc3XD2yTDKcBOpfm+UPlR5vlBbO3FvHMx+bKZhZo25B6EYjmjpD ios7AE+1hUw2ehLc+imqnvvJoo6Il2SKc1MzaXKOp22fJOzqdccnnwn/W KlHy+rc7mvr0xKR+3/SdxN+AepuoQpWHvoQeAqtj97DeK4h7MBywU2t9Q UeeH8ucRC3ggvno4MBigYPekprBYAWNkHXVxDmv5MfmjAe4YfxfVI6bX1 PFwyfL6Z5df51Vppv9RlZjB2DMXbTCgv7nBWczl5V5BuMSVK6Uz7mhm2f Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10448"; a="357882451" X-IronPort-AV: E=Sophos;i="5.93,260,1654585200"; d="scan'208";a="357882451" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Aug 2022 01:45:31 -0700 X-IronPort-AV: E=Sophos;i="5.93,260,1654585200"; d="scan'208";a="785553962" Received: from yuhsuanc-mobl.gar.corp.intel.com (HELO paris.amr.corp.intel.com) ([10.254.38.238]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Aug 2022 01:45:28 -0700 From: Gwan-gyeong Mun To: intel-gfx@lists.freedesktop.org Cc: linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, mchehab@kernel.org, chris@chris-wilson.co.uk, matthew.auld@intel.com, thomas.hellstrom@linux.intel.com, jani.nikula@intel.com, nirmoy.das@intel.com, airlied@linux.ie, daniel@ffwll.ch, andi.shyti@linux.intel.com, andrzej.hajda@intel.com, keescook@chromium.org, mauro.chehab@linux.intel.com, intel-gfx-trybot@lists.freedesktop.org Subject: [PATCH v9 1/8] overflow: Move and add few utility macros into overflow Date: Wed, 24 Aug 2022 17:45:07 +0900 Message-Id: <20220824084514.2261614-2-gwan-gyeong.mun@intel.com> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220824084514.2261614-1-gwan-gyeong.mun@intel.com> References: <20220824084514.2261614-1-gwan-gyeong.mun@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org It moves overflows_type utility macro into overflow header from i915_utils header. The overflows_type can be used to catch the truncaion (overflow) between different data types. And it adds check_assign() macro which performs an assigning source value into destination ptr along with an overflow check. overflow_type macro has been improved to handle the signbit by gcc's built-in overflow check function. And it adds overflows_ptr() helper macro for checking the overflows between a value and a pointer type/value. v3: Add is_type_unsigned() macro (Mauro) Modify overflows_type() macro to consider signed data types (Mauro) Fix the problem that safe_conversion() macro always returns true v4: Fix kernel-doc markups v6: Move macro addition location so that it can be used by other than drm subsystem (Jani, Mauro, Andi) Change is_type_unsigned to is_unsigned_type to have the same name form as is_signed_type macro v8: Add check_assign() and remove safe_conversion() (Kees) Fix overflows_type() to use gcc's built-in overflow function (Andrzej) Add overflows_ptr() to allow overflow checking when assigning a value into a pointer variable (G.G.) v9: Fix overflows_type() to use __builtin_add_overflow() instead of __builtin_add_overflow_p() (Andrzej) Fix overflows_ptr() to use overflows_type() with the unsigned long type (Andrzej) Signed-off-by: Gwan-gyeong Mun Cc: Thomas Hellstr=C3=B6m Cc: Matthew Auld Cc: Nirmoy Das Cc: Jani Nikula Cc: Andi Shyti Cc: Andrzej Hajda Cc: Mauro Carvalho Chehab Cc: Kees Cook Reviewed-by: Mauro Carvalho Chehab (v5) Reviewed-by: Andrzej Hajda --- drivers/gpu/drm/i915/i915_user_extensions.c | 3 +- drivers/gpu/drm/i915/i915_utils.h | 5 +- include/linux/overflow.h | 62 +++++++++++++++++++++ 3 files changed, 64 insertions(+), 6 deletions(-) diff --git a/drivers/gpu/drm/i915/i915_user_extensions.c b/drivers/gpu/drm/= i915/i915_user_extensions.c index c822d0aafd2d..6f6b5b910968 100644 --- a/drivers/gpu/drm/i915/i915_user_extensions.c +++ b/drivers/gpu/drm/i915/i915_user_extensions.c @@ -50,8 +50,7 @@ int i915_user_extensions(struct i915_user_extension __use= r *ext, if (err) return err; =20 - if (get_user(next, &ext->next_extension) || - overflows_type(next, ext)) + if (get_user(next, &ext->next_extension) || overflows_ptr(next)) return -EFAULT; =20 ext =3D u64_to_user_ptr(next); diff --git a/drivers/gpu/drm/i915/i915_utils.h b/drivers/gpu/drm/i915/i915_= utils.h index c10d68cdc3ca..eb0ded23fa9c 100644 --- a/drivers/gpu/drm/i915/i915_utils.h +++ b/drivers/gpu/drm/i915/i915_utils.h @@ -32,6 +32,7 @@ #include #include #include +#include =20 #ifdef CONFIG_X86 #include @@ -111,10 +112,6 @@ bool i915_error_injected(void); #define range_overflows_end_t(type, start, size, max) \ range_overflows_end((type)(start), (type)(size), (type)(max)) =20 -/* Note we don't consider signbits :| */ -#define overflows_type(x, T) \ - (sizeof(x) > sizeof(T) && (x) >> BITS_PER_TYPE(T)) - #define ptr_mask_bits(ptr, n) ({ \ unsigned long __v =3D (unsigned long)(ptr); \ (typeof(ptr))(__v & -BIT(n)); \ diff --git a/include/linux/overflow.h b/include/linux/overflow.h index f1221d11f8e5..6af9df1d67a1 100644 --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -52,6 +52,68 @@ static inline bool __must_check __must_check_overflow(bo= ol overflow) return unlikely(overflow); } =20 +/** + * overflows_type - helper for checking the overflows between data types or + * values + * + * @x: Source value or data type for overflow check + * @T: Destination value or data type for overflow check + * + * It compares the values or data type between the first and second argume= nt to + * check whether overflow can occur when assigning the first argument to t= he + * variable of the second argument. Source and Destination can be singned = or + * unsigned data types. Source and Destination can be different data types. + * + * Returns: + * True if overflow can occur, false otherwise. + */ +#define overflows_type(x, T) __must_check_overflow(({ \ + typeof(T) v =3D 0; \ + __builtin_add_overflow((x), v, &v); \ +})) + +/** + * overflows_ptr - helper for checking the occurrence of overflows when a = value + * assigns to the pointer data type + * + * @x: Source value for overflow check + * + * gcc's built-in overflow check functions don't support checking between = the + * pointer type and non-pointer type. And ILP32 and LP64 have the same bit= size + * between long and pointer. Therefore it internally compares the source v= alue + * and unsigned long data type for checking overflow. + * + * Returns: + * True if overflow can occur, false otherwise. + */ +#define overflows_ptr(x) __must_check_overflow(overflows_type(x, unsigned = long)) + +/** + * check_assign - perform an assigning source value into destination ptr a= long + * with an overflow check. + * + * @value: Source value + * @ptr: Destination pointer address, If the pointer type is not used, + * a warning message is output during build. + * + * It checks internally the ptr is a pointer type. And it uses gcc's built= -in + * overflow check function. + * It does not use the check_*() wrapper functions, but directly uses gcc's + * built-in overflow check function so that it can be used even when + * the type of value and the type pointed to by ptr are different without = build + * warning messages. + * + * Returns: + * If the value would overflow the destination, it returns true. If not re= turn + * false. When overflow does not occur, the assigning into ptr from value + * succeeds. It follows the return policy as other check_*_overflow() func= tions + * return non-zero as a failure. + */ +#define check_assign(value, ptr) __must_check_overflow(({ \ + typecheck_pointer(ptr); \ + __builtin_add_overflow(0, value, ptr); \ +})) + /* * For simplicity and code hygiene, the fallback code below insists on * a, b and *d having the same type (similar to the min() and max() --=20 2.37.1