dri-devel.lists.freedesktop.org archive mirror
 help / color / mirror / Atom feed
From: Gwan-gyeong Mun <gwan-gyeong.mun@intel.com>
To: intel-gfx@lists.freedesktop.org
Cc: thomas.hellstrom@linux.intel.com, mauro.chehab@linux.intel.com,
	andi.shyti@linux.intel.com, keescook@chromium.org,
	jani.nikula@intel.com, linux-kernel@vger.kernel.org,
	dri-devel@lists.freedesktop.org, chris@chris-wilson.co.uk,
	airlied@linux.ie, andrzej.hajda@intel.com,
	matthew.auld@intel.com, intel-gfx-trybot@lists.freedesktop.org,
	mchehab@kernel.org, nirmoy.das@intel.com
Subject: [PATCH v8 2/8] util_macros: Add exact_type macro to catch type mis-match while compiling
Date: Tue, 23 Aug 2022 19:17:23 +0900	[thread overview]
Message-ID: <20220823101729.2098841-3-gwan-gyeong.mun@intel.com> (raw)
In-Reply-To: <20220823101729.2098841-1-gwan-gyeong.mun@intel.com>

It adds exact_type and exactly_pgoff_t macro to catch type mis-match while
compiling. The existing typecheck() macro outputs build warnings, but the
newly added exact_type() macro uses the BUILD_BUG_ON() macro to generate
a build break when the types are different and can be used to detect
explicit build errors.

v6: Move macro addition location so that it can be used by other than drm
    subsystem (Jani, Mauro, Andi)

Signed-off-by: Gwan-gyeong Mun <gwan-gyeong.mun@intel.com>
Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Cc: Matthew Auld <matthew.auld@intel.com>
Cc: Nirmoy Das <nirmoy.das@intel.com>
Cc: Jani Nikula <jani.nikula@intel.com>
Cc: Andi Shyti <andi.shyti@linux.intel.com>
Cc: Mauro Carvalho Chehab <mchehab@kernel.org>
---
 include/linux/util_macros.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/include/linux/util_macros.h b/include/linux/util_macros.h
index 72299f261b25..b6624b275257 100644
--- a/include/linux/util_macros.h
+++ b/include/linux/util_macros.h
@@ -2,6 +2,9 @@
 #ifndef _LINUX_HELPER_MACROS_H_
 #define _LINUX_HELPER_MACROS_H_
 
+#include <linux/types.h>
+#include <linux/bug.h>
+
 #define __find_closest(x, a, as, op)					\
 ({									\
 	typeof(as) __fc_i, __fc_as = (as) - 1;				\
@@ -38,4 +41,26 @@
  */
 #define find_closest_descending(x, a, as) __find_closest(x, a, as, >=)
 
+/**
+ * exact_type - break compile if source type and destination value's type are
+ * not the same
+ * @T: Source type
+ * @n: Destination value
+ *
+ * It is a helper macro for a poor man's -Wconversion: only allow variables of
+ * an exact type. It determines whether the source type and destination value's
+ * type are the same while compiling, and it breaks compile if two types are
+ * not the same
+ */
+#define exact_type(T, n) \
+	BUILD_BUG_ON(!__builtin_constant_p(n) && !__builtin_types_compatible_p(T, typeof(n)))
+
+/**
+ * exactly_pgoff_t - helper to check if the type of a value is pgoff_t
+ * @n: value to compare pgoff_t type
+ *
+ * It breaks compile if the argument value's type is not pgoff_t type.
+ */
+#define exactly_pgoff_t(n) exact_type(pgoff_t, n)
+
 #endif
-- 
2.37.1


  parent reply	other threads:[~2022-08-23 10:20 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-23 10:17 [PATCH v8 0/8] Fixes integer overflow or integer truncation issues in page lookups, ttm place configuration and scatterlist creation Gwan-gyeong Mun
2022-08-23 10:17 ` [PATCH v8 1/8] overflow: Move and add few utility macros into overflow Gwan-gyeong Mun
2022-08-23 12:35   ` [Intel-gfx] " Andrzej Hajda
2022-08-24  9:31     ` Gwan-gyeong Mun
2022-08-23 10:17 ` Gwan-gyeong Mun [this message]
2022-08-23 10:17 ` [PATCH v8 3/8] drm/i915/gem: Typecheck page lookups Gwan-gyeong Mun
2022-08-23 10:17 ` [PATCH v8 4/8] drm/i915: Check for integer truncation on scatterlist creation Gwan-gyeong Mun
2022-08-23 10:17 ` [PATCH v8 5/8] drm/i915: Check for integer truncation on the configuration of ttm place Gwan-gyeong Mun
2022-08-23 10:17 ` [PATCH v8 6/8] drm/i915: Check if the size is too big while creating shmem file Gwan-gyeong Mun
2022-08-23 10:17 ` [PATCH v8 7/8] drm/i915: Use error code as -E2BIG when the size of gem ttm object is too large Gwan-gyeong Mun
2022-08-23 10:17 ` [PATCH v8 8/8] drm/i915: Remove truncation warning for large objects Gwan-gyeong Mun

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=20220823101729.2098841-3-gwan-gyeong.mun@intel.com \
    --to=gwan-gyeong.mun@intel.com \
    --cc=airlied@linux.ie \
    --cc=andi.shyti@linux.intel.com \
    --cc=andrzej.hajda@intel.com \
    --cc=chris@chris-wilson.co.uk \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-gfx-trybot@lists.freedesktop.org \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=jani.nikula@intel.com \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=matthew.auld@intel.com \
    --cc=mauro.chehab@linux.intel.com \
    --cc=mchehab@kernel.org \
    --cc=nirmoy.das@intel.com \
    --cc=thomas.hellstrom@linux.intel.com \
    /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).