From ca96903881c334af03c01c3bdb0daee5f3f9bcb3 Mon Sep 17 00:00:00 2001 From: Philip Elcan Date: Fri, 7 Jun 2019 15:12:51 -0400 Subject: [PATCH] gpu: nvgpu: posix: make BITS_TO_LONGS CERT-C friendly Fix CERT-C INT30 violations caused by BIT_TO_LONGS() macro in bitops.h. JIRA NVGPU-3587 Change-Id: Idadd0c719160fe4bc54c80c0e26890d3f1256c94 Signed-off-by: Philip Elcan Reviewed-on: https://git-master.nvidia.com/r/2132540 GVS: Gerrit_Virtual_Submit Reviewed-by: Alex Waterman Reviewed-by: mobile promotions Tested-by: mobile promotions --- drivers/gpu/nvgpu/include/nvgpu/posix/bitops.h | 13 ++++++++++--- drivers/gpu/nvgpu/include/nvgpu/utils.h | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/drivers/gpu/nvgpu/include/nvgpu/posix/bitops.h b/drivers/gpu/nvgpu/include/nvgpu/posix/bitops.h index 18b945929..a184ea039 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/posix/bitops.h +++ b/drivers/gpu/nvgpu/include/nvgpu/posix/bitops.h @@ -31,7 +31,7 @@ #define BITS_PER_BYTE 8UL #define BITS_PER_LONG ((unsigned long)__SIZEOF_LONG__ * BITS_PER_BYTE) #define BITS_TO_LONGS(bits) \ - (((bits) + BITS_PER_LONG - 1UL) / BITS_PER_LONG) + (nvgpu_safe_add_u64(bits, BITS_PER_LONG - 1UL) / BITS_PER_LONG) /* * Deprecated; use the explicit BITxx() macros instead. @@ -42,8 +42,15 @@ (((~0UL) - (1UL << (lo)) + 1UL) & \ (~0UL >> (BITS_PER_LONG - 1UL - (unsigned long)(hi)))) -#define DECLARE_BITMAP(bmap, bits) \ - unsigned long bmap[BITS_TO_LONGS(bits)] +/* + * Can't use BITS_TO_LONGS to declare arrays where we can't use BUG(), so if the + * range is invalid, use -1 for the size which will generate a compiler error. + */ +#define DECLARE_BITMAP(bmap, bits) \ + unsigned long bmap[(((LONG_MAX - (bits)) < (BITS_PER_LONG - 1UL)) ? \ + -1 : \ + (long int)(((bits) + BITS_PER_LONG - 1UL) / \ + BITS_PER_LONG))] #define for_each_set_bit(bit, addr, size) \ for ((bit) = find_first_bit((addr), (size)); \ diff --git a/drivers/gpu/nvgpu/include/nvgpu/utils.h b/drivers/gpu/nvgpu/include/nvgpu/utils.h index 8962337ee..99618cb3e 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/utils.h +++ b/drivers/gpu/nvgpu/include/nvgpu/utils.h @@ -24,6 +24,7 @@ #define NVGPU_UTILS_H #include +#include #ifdef __KERNEL__ #include