Skip to contentSkip to conversation

fix cast of gfp_t to ulong in __def_gfpflag_names

PATCH In review Luc Van Oostenryck 2 participants 2 messages Dec 4, 2019 – Dec 5, 2019 Tracing

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fix cast of gfp_t to ulong in __def_gfpflag_names

The macro '__def_gfpflag_names' is used to define arrays of struct trace_print_flags. This structure is defined as being a pair of 'unsigned long' - 'const char *'.

However, the macro __def_gfpflag_names is used to for GFP flags and thus take entries of type gfp_t (plus their name) which is a bitwise type, non-convertible to usual integers. These entries are casted to 'unsigned long' but this doesn't prevent Sparse to rughtfully complain:
warning: cast from restricted gfp_t

The correct way to cast a bitwise type to a normal integer (which is OK here) is to use '__force'.

So, fix the cast by adding the '__force' required for such casts.

---
 include/trace/events/mmflags.h | 70 +++++++++++++++++-----------------
 1 file changed, 35 insertions(+), 35 deletions(-)
include/trace/events/mmflags.h +35 -35
index a1675d43777e..3b85bbf79115 100644
@@ -14,41 +14,41 @@
  */
 
 #define __def_gfpflag_names						\
-	{(unsigned long)GFP_TRANSHUGE,		"GFP_TRANSHUGE"},	\
-	{(unsigned long)GFP_TRANSHUGE_LIGHT,	"GFP_TRANSHUGE_LIGHT"}, \
-	{(unsigned long)GFP_HIGHUSER_MOVABLE,	"GFP_HIGHUSER_MOVABLE"},\
-	{(unsigned long)GFP_HIGHUSER,		"GFP_HIGHUSER"},	\
-	{(unsigned long)GFP_USER,		"GFP_USER"},		\
-	{(unsigned long)GFP_KERNEL_ACCOUNT,	"GFP_KERNEL_ACCOUNT"},	\
-	{(unsigned long)GFP_KERNEL,		"GFP_KERNEL"},		\
-	{(unsigned long)GFP_NOFS,		"GFP_NOFS"},		\
-	{(unsigned long)GFP_ATOMIC,		"GFP_ATOMIC"},		\
-	{(unsigned long)GFP_NOIO,		"GFP_NOIO"},		\
-	{(unsigned long)GFP_NOWAIT,		"GFP_NOWAIT"},		\
-	{(unsigned long)GFP_DMA,		"GFP_DMA"},		\
-	{(unsigned long)__GFP_HIGHMEM,		"__GFP_HIGHMEM"},	\
-	{(unsigned long)GFP_DMA32,		"GFP_DMA32"},		\
-	{(unsigned long)__GFP_HIGH,		"__GFP_HIGH"},		\
-	{(unsigned long)__GFP_ATOMIC,		"__GFP_ATOMIC"},	\
-	{(unsigned long)__GFP_IO,		"__GFP_IO"},		\
-	{(unsigned long)__GFP_FS,		"__GFP_FS"},		\
-	{(unsigned long)__GFP_NOWARN,		"__GFP_NOWARN"},	\
-	{(unsigned long)__GFP_RETRY_MAYFAIL,	"__GFP_RETRY_MAYFAIL"},	\
-	{(unsigned long)__GFP_NOFAIL,		"__GFP_NOFAIL"},	\
-	{(unsigned long)__GFP_NORETRY,		"__GFP_NORETRY"},	\
-	{(unsigned long)__GFP_COMP,		"__GFP_COMP"},		\
-	{(unsigned long)__GFP_ZERO,		"__GFP_ZERO"},		\
-	{(unsigned long)__GFP_NOMEMALLOC,	"__GFP_NOMEMALLOC"},	\
-	{(unsigned long)__GFP_MEMALLOC,		"__GFP_MEMALLOC"},	\
-	{(unsigned long)__GFP_HARDWALL,		"__GFP_HARDWALL"},	\
-	{(unsigned long)__GFP_THISNODE,		"__GFP_THISNODE"},	\
-	{(unsigned long)__GFP_RECLAIMABLE,	"__GFP_RECLAIMABLE"},	\
-	{(unsigned long)__GFP_MOVABLE,		"__GFP_MOVABLE"},	\
-	{(unsigned long)__GFP_ACCOUNT,		"__GFP_ACCOUNT"},	\
-	{(unsigned long)__GFP_WRITE,		"__GFP_WRITE"},		\
-	{(unsigned long)__GFP_RECLAIM,		"__GFP_RECLAIM"},	\
-	{(unsigned long)__GFP_DIRECT_RECLAIM,	"__GFP_DIRECT_RECLAIM"},\
-	{(unsigned long)__GFP_KSWAPD_RECLAIM,	"__GFP_KSWAPD_RECLAIM"}\
+	{(__force ulong)GFP_TRANSHUGE,		"GFP_TRANSHUGE"},	\
+	{(__force ulong)GFP_TRANSHUGE_LIGHT,	"GFP_TRANSHUGE_LIGHT"}, \
+	{(__force ulong)GFP_HIGHUSER_MOVABLE,	"GFP_HIGHUSER_MOVABLE"},\
+	{(__force ulong)GFP_HIGHUSER,		"GFP_HIGHUSER"},	\
+	{(__force ulong)GFP_USER,		"GFP_USER"},		\
+	{(__force ulong)GFP_KERNEL_ACCOUNT,	"GFP_KERNEL_ACCOUNT"},	\
+	{(__force ulong)GFP_KERNEL,		"GFP_KERNEL"},		\
+	{(__force ulong)GFP_NOFS,		"GFP_NOFS"},		\
+	{(__force ulong)GFP_ATOMIC,		"GFP_ATOMIC"},		\
+	{(__force ulong)GFP_NOIO,		"GFP_NOIO"},		\
+	{(__force ulong)GFP_NOWAIT,		"GFP_NOWAIT"},		\
+	{(__force ulong)GFP_DMA,		"GFP_DMA"},		\
+	{(__force ulong)__GFP_HIGHMEM,		"__GFP_HIGHMEM"},	\
+	{(__force ulong)GFP_DMA32,		"GFP_DMA32"},		\
+	{(__force ulong)__GFP_HIGH,		"__GFP_HIGH"},		\
+	{(__force ulong)__GFP_ATOMIC,		"__GFP_ATOMIC"},	\
+	{(__force ulong)__GFP_IO,		"__GFP_IO"},		\
+	{(__force ulong)__GFP_FS,		"__GFP_FS"},		\
+	{(__force ulong)__GFP_NOWARN,		"__GFP_NOWARN"},	\
+	{(__force ulong)__GFP_RETRY_MAYFAIL,	"__GFP_RETRY_MAYFAIL"},	\
+	{(__force ulong)__GFP_NOFAIL,		"__GFP_NOFAIL"},	\
+	{(__force ulong)__GFP_NORETRY,		"__GFP_NORETRY"},	\
+	{(__force ulong)__GFP_COMP,		"__GFP_COMP"},		\
+	{(__force ulong)__GFP_ZERO,		"__GFP_ZERO"},		\
+	{(__force ulong)__GFP_NOMEMALLOC,	"__GFP_NOMEMALLOC"},	\
+	{(__force ulong)__GFP_MEMALLOC,		"__GFP_MEMALLOC"},	\
+	{(__force ulong)__GFP_HARDWALL,		"__GFP_HARDWALL"},	\
+	{(__force ulong)__GFP_THISNODE,		"__GFP_THISNODE"},	\
+	{(__force ulong)__GFP_RECLAIMABLE,	"__GFP_RECLAIMABLE"},	\
+	{(__force ulong)__GFP_MOVABLE,		"__GFP_MOVABLE"},	\
+	{(__force ulong)__GFP_ACCOUNT,		"__GFP_ACCOUNT"},	\
+	{(__force ulong)__GFP_WRITE,		"__GFP_WRITE"},		\
+	{(__force ulong)__GFP_RECLAIM,		"__GFP_RECLAIM"},	\
+	{(__force ulong)__GFP_DIRECT_RECLAIM,	"__GFP_DIRECT_RECLAIM"},\
+	{(__force ulong)__GFP_KSWAPD_RECLAIM,	"__GFP_KSWAPD_RECLAIM"}\
 
 #define show_gfp_flags(flags)						\
 	(flags) ? __print_flags(flags, "|",				\
Signature · 1 line
2.24.0
#1 lore raw
Quoted 11 lines · Luc Van Oostenryck

The macro '__def_gfpflag_names' is used to define arrays of struct trace_print_flags. This structure is defined as being a pair of 'unsigned long' - 'const char *'.

However, the macro __def_gfpflag_names is used to for GFP flags and thus take entries of type gfp_t (plus their name) which is a bitwise type, non-convertible to usual integers. These entries are casted to 'unsigned long' but this doesn't prevent Sparse to rughtfully complain:
warning: cast from restricted gfp_t

Quoted 4 lines

The correct way to cast a bitwise type to a normal integer (which is OK here) is to use '__force'.

So, fix the cast by adding the '__force' required for such casts.

Ugh...

include/trace/events/mmflags.h line 17

Quoted 1 line
include/trace/events/mmflags.h +0 -1
-	{(unsigned long)GFP_TRANSHUGE,		"GFP_TRANSHUGE"},	\

<plenty of such>

include/trace/events/mmflags.h line 17

Quoted 1 line
include/trace/events/mmflags.h +1 -0
+	{(__force ulong)GFP_TRANSHUGE,		"GFP_TRANSHUGE"},	\
# operator is there for purpose; as in
#define FOO(name) {(__force unsigned long)name, #name}
with those becoming
#define __def_gfpflag_names \
	FOO(GFP_TRANSHUGE), \
	FOO(GFP_TRANSHUGE_LIGHT), \
etc.