[Intel-gfx] [PATCH v2 1/7] drm: Move and add a few utility macros into drm util header

Mauro Carvalho Chehab mauro.chehab at linux.intel.com
Wed Jul 6 17:05:13 UTC 2022


On Wed, 6 Jul 2022 18:04:20 +0300
Gwan-gyeong Mun <gwan-gyeong.mun at intel.com> wrote:

> On 7/5/22 5:23 PM, Mauro Carvalho Chehab wrote:
> > On Tue,  5 Jul 2022 15:24:49 +0300
> > Gwan-gyeong Mun <gwan-gyeong.mun at intel.com> wrote:
> >   
> >> It moves overflows_type utility macro into drm util header from i915_utils
> >> header. The overflows_type can be used to catch the truncation between data
> >> types. And it adds safe_conversion() macro which performs a type conversion
> >> (cast) of an source value into a new variable, checking that the
> >> destination is large enough to hold the source value.
> >> And it adds exact_type and exactly_pgoff_t macro to catch type mis-match
> >> while compiling.
> >>
> >> Signed-off-by: Gwan-gyeong Mun <gwan-gyeong.mun at intel.com>
> >> Cc: Thomas Hellström <thomas.hellstrom at linux.intel.com>
> >> Cc: Matthew Auld <matthew.auld at intel.com>
> >> Cc: Nirmoy Das <nirmoy.das at intel.com>
> >> Cc: Jani Nikula <jani.nikula at intel.com>
> >> ---
> >>   drivers/gpu/drm/i915/i915_utils.h |  5 +--
> >>   include/drm/drm_util.h            | 54 +++++++++++++++++++++++++++++++
> >>   2 files changed, 55 insertions(+), 4 deletions(-)
> >>
> >> diff --git a/drivers/gpu/drm/i915/i915_utils.h b/drivers/gpu/drm/i915/i915_utils.h
> >> index c10d68cdc3ca..345e5b2dc1cd 100644
> >> --- a/drivers/gpu/drm/i915/i915_utils.h
> >> +++ b/drivers/gpu/drm/i915/i915_utils.h
> >> @@ -32,6 +32,7 @@
> >>   #include <linux/types.h>
> >>   #include <linux/workqueue.h>
> >>   #include <linux/sched/clock.h>
> >> +#include <drm/drm_util.h>
> >>   
> >>   #ifdef CONFIG_X86
> >>   #include <asm/hypervisor.h>
> >> @@ -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))
> >>   
> >> -/* 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 = (unsigned long)(ptr);			\
> >>   	(typeof(ptr))(__v & -BIT(n));					\
> >> diff --git a/include/drm/drm_util.h b/include/drm/drm_util.h
> >> index 79952d8c4bba..c56230e39e37 100644
> >> --- a/include/drm/drm_util.h
> >> +++ b/include/drm/drm_util.h
> >> @@ -62,6 +62,60 @@
> >>    */
> >>   #define for_each_if(condition) if (!(condition)) {} else
> >>   
> >> +/**
> >> + * overflows_type - helper for checking the truncation between data types
> >> + * @x: Source for overflow type comparison
> >> + * @T: Destination for overflow type comparison
> >> + *
> >> + * It compares the values and size of each data type between the first and
> >> + * second argument to check whether truncation can occur when assigning the
> >> + * first argument to the variable of the second argument.
> >> + * It does't consider signbits.
> >> + *
> >> + * Returns:
> >> + * True if truncation can occur, false otherwise.
> >> + */
> >> +#define overflows_type(x, T) \
> >> +	(sizeof(x) > sizeof(T) && (x) >> BITS_PER_TYPE(T)) 
> > 
> > As pointed on its description, this macro only works if both types
> > are either signed or unsigned. However, the macro itself doesn't check
> > it.
> > 
> > It probably worth adding something there to ensure that both types are
> > either signed or unsigned. I would add this ancillary macro probably on
> > on a generic kernel header - as this can be useful outside drm:
> > 
> > 	#define sign_matches(x, y) \
> > 	        (!((typeof(x))-1 >= 0) ^ ((typeof(y))-1 >= 0))
> > 
> > And then include use it at overflows_type:
> > 
> > 	BUILD_BUG_ON(!sign_matches(x, T))
> >   
> Hi Mauro, thanks for checking it.
> 
> What you commented here (sign_matches macro) is to check whether the 
> sign bits of two types are the same,  but the purpose of the 
> overflows_type() macro checks overflows while assigning a variable with 
> a large data size (BITS_PER_TYPE is large) to a variable with a small 
> data size (BITS_PER_TYPE is small).

True, but the problem is that such macro just assumes that either both
are signed or unsigned without actually checking it.

Basically, if one tries to store for instance a s32 value on an u64 var,
the value won't be stored correctly, due to an underflow. As the hole
idea of this macro is to exactly detect if the "container" variable
is big enough to properly represent the measure, it sounds incomplete
to not handle the integer signal.

Btw, after reviewing all patches, using BUILD_BUG_ON() is not needed
here, as such macro (or similar) is already used at the callers code.

So, I would just return false if the signals are incompatible, e. g. 
if the type of the source value is signed and the type of the 
destination value is unsigned.

So:

#define overflows_type(x, T) \
	(!signal_matches(x,T) || (sizeof(x) > sizeof(T)))

Should do the trick[1]. 

[1] using BITS_PER_TYPE() macro is not really needed, as this is
    defined as:

	#define BITS_PER_BYTE              8
	#define BITS_PER_TYPE(type)      (sizeof(type) * BITS_PER_BYTE)

    So, checking if sizeof(x) > sizeof(T) is enough. Btw, the check
    there seems to be inverted, making the macro to always return zero!

Yet, strictly speaking, it is possible to store an unsigned value on a 
signed type, if the signed type is bigger than the size of unsigned
(so, a s64 int can store u32, but a s32 can't store u32).

Adding a check that would consider this should be like:

	#define is_type_unsigned(x) ((typeof(x))-1 >= 0) 
	#define is_type_signed(x) (!is_type_unsigned(x)) 
	#define overflows_type(x, T)							 \
	(										 \
		(is_type_signed(x) && is_type_unsigned(T)) ||				 \
		(is_type_unsigned(x) && is_type_signed(T) && (sizeof(x) == sizeof(T))) ||\
		(sizeof(x) > sizeof(T))							 \
	) 	

This should be generic enough to be used anywhere.

> we can check the additional sign bit by adding sign_matches() to the 
> overflows_type() macro, but in the current scenario, it is used only 
> when the sign bit is the same.

Yeah, but "current scenarios" can easily be extended to something
else, quickly going sideways specially on a subsystem-wide macro. 
Also, getting this right is particularly tricky when comparing typedef
integers. So, I would be more comfortable if the logic will also
check the signal at the destination variable.

> Should the macro be extended even for cases where the sign bit is 
> different in the current state? (If yes, I'll updated it as v3)
> 
> In addition, the place where this macro is currently used is only in the 
> i915 driver, so it has been moved to the header of the drm subsystem.
> IMHO, moving the macro location so that it can be used by multiple 
> subsystems of linux would be a good idea when there is a use case for 
> this macro. What do you think?

Good point. Yeah, it can stay there while not needed outside drm.

Btw, in order to get it right, I suggest double-checking in userspace
how each macros are evaluated, like using the code below.

It helps to check if the logic is doing what's expected or not.

----

#include <stdio.h>
#include <stdint.h>

// Kernel definitions from bits.h and bitops.h
#define BITS_PER_BYTE              8
#define BITS_PER_TYPE(type)      (sizeof(type) * BITS_PER_BYTE)

#define sign_matches(x, y) \
	(!((typeof(x))-1 >= 0) ^ ((typeof(y))-1 >= 0))

#define is_type_unsigned(x) ((typeof(x))-1 >= 0)
#define is_type_signed(x) (!is_type_unsigned(x))

#define overflows_type(x, T)								 \
	(										 \
		(is_type_signed(x) && is_type_unsigned(T)) ||				 \
		(is_type_unsigned(x) && is_type_signed(T) && (sizeof(x) == sizeof(T))) ||\
		(sizeof(x) > sizeof(T))							 \
	) ? "OVERFLOW" : "don't overflow"

int main(void)
{
	uint32_t	u32_1 = 0, u32_2 = 0;
	int32_t		s32_1 = 0, s32_2 = 0;
	uint64_t	u64_1 = 0, u64_2 = 0;
	int64_t		s64_1 = 0, s64_2 = 0;

	printf("u32 stored into u32: %s\n", overflows_type(u32_1, u32_2));
	printf("u64 stored into u32: %s\n", overflows_type(u64_1, u32_2));
	printf("s32 stored into u32: %s\n", overflows_type(s32_1, u32_2));
	printf("s64 stored into u32: %s\n", overflows_type(s64_1, u32_2));

	printf("u32 stored into s32: %s\n", overflows_type(u32_1, s32_2));
	printf("u64 stored into s32: %s\n", overflows_type(u64_1, s32_2));
	printf("s32 stored into s32: %s\n", overflows_type(s32_1, s32_2));
	printf("s64 stored into s32: %s\n", overflows_type(s64_1, s32_2));

	printf("u32 stored into u64: %s\n", overflows_type(u32_1, u64_2));
	printf("u64 stored into u64: %s\n", overflows_type(u64_1, u64_2));
	printf("s32 stored into u64: %s\n", overflows_type(s32_1, u64_2));
	printf("s64 stored into u64: %s\n", overflows_type(s64_1, u64_2));

	printf("u32 stored into s64: %s\n", overflows_type(u32_1, s64_2));
	printf("u64 stored into s64: %s\n", overflows_type(u64_1, s64_2));
	printf("s32 stored into u64: %s\n", overflows_type(s32_1, u64_2));
	printf("s64 stored into u64: %s\n", overflows_type(s64_1, u64_2));

	// Shutup warnings
	s64_1 = u32_1 + u32_2 + s64_2 + s32_1 + s32_2 + u64_1 + u64_2;

	return 0;
}


More information about the Intel-gfx mailing list