Re: [PATCH] fix cast of gfp_t to ulong in __def_gfpflag_names

From: Al Viro
Date: Wed Dec 04 2019 - 21:08:30 EST


On Wed, Dec 04, 2019 at 06:54:25PM +0100, Luc Van Oostenryck wrote:
> 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.

Ugh...
> - {(unsigned long)GFP_TRANSHUGE, "GFP_TRANSHUGE"}, \
<plenty of such>
> + {(__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.