diff mbox series

[v5,1/1] mm: enumerate all gfp flags

Message ID 20240224015800.2569851-1-surenb@google.com (mailing list archive)
State New
Headers show
Series [v5,1/1] mm: enumerate all gfp flags | expand

Commit Message

Suren Baghdasaryan Feb. 24, 2024, 1:58 a.m. UTC
Introduce GFP bits enumeration to let compiler track the number of used
bits (which depends on the config options) instead of hardcoding them.
That simplifies __GFP_BITS_SHIFT calculation.

Suggested-by: Petr Tesařík <petr@tesarici.cz>
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Pasha Tatashin <pasha.tatashin@soleen.com>
Acked-by: Michal Hocko <mhocko@suse.com>
---
Changes from v4 [1]:
- Split from the series [2] as a stand-alone patch, per Michal Hocko
- Added Reviewed-by, per Pasha Tatashin
- Added Acked-by, per Michal Hocko

[1] https://lore.kernel.org/all/20240221194052.927623-7-surenb@google.com/
[2] https://lore.kernel.org/all/20240221194052.927623-1-surenb@google.com/

 include/linux/gfp_types.h | 90 +++++++++++++++++++++++++++------------
 1 file changed, 62 insertions(+), 28 deletions(-)


base-commit: 603c04e27c3e9891ce7afa5cd6b496bfacff4206

Comments

Christophe JAILLET Feb. 24, 2024, 7:03 a.m. UTC | #1
Le 24/02/2024 à 02:58, Suren Baghdasaryan a écrit :
> Introduce GFP bits enumeration to let compiler track the number of used
> bits (which depends on the config options) instead of hardcoding them.
> That simplifies __GFP_BITS_SHIFT calculation.
> 
> Suggested-by: Petr Tesařík <petr@tesarici.cz>
> Signed-off-by: Suren Baghdasaryan <surenb@google.com>
> Reviewed-by: Kees Cook <keescook@chromium.org>
> Reviewed-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> Acked-by: Michal Hocko <mhocko@suse.com>
> ---
> Changes from v4 [1]:
> - Split from the series [2] as a stand-alone patch, per Michal Hocko
> - Added Reviewed-by, per Pasha Tatashin
> - Added Acked-by, per Michal Hocko
> 
> [1] https://lore.kernel.org/all/20240221194052.927623-7-surenb@google.com/
> [2] https://lore.kernel.org/all/20240221194052.927623-1-surenb@google.com/
> 
>   include/linux/gfp_types.h | 90 +++++++++++++++++++++++++++------------
>   1 file changed, 62 insertions(+), 28 deletions(-)
> 
> diff --git a/include/linux/gfp_types.h b/include/linux/gfp_types.h
> index 1b6053da8754..868c8fb1bbc1 100644
> --- a/include/linux/gfp_types.h
> +++ b/include/linux/gfp_types.h
> @@ -21,44 +21,78 @@ typedef unsigned int __bitwise gfp_t;
>    * include/trace/events/mmflags.h and tools/perf/builtin-kmem.c
>    */
>   
> +enum {
> +	___GFP_DMA_BIT,
> +	___GFP_HIGHMEM_BIT,
> +	___GFP_DMA32_BIT,
> +	___GFP_MOVABLE_BIT,
> +	___GFP_RECLAIMABLE_BIT,
> +	___GFP_HIGH_BIT,
> +	___GFP_IO_BIT,
> +	___GFP_FS_BIT,
> +	___GFP_ZERO_BIT,
> +	___GFP_UNUSED_BIT,	/* 0x200u unused */

Hi,

what is the need to have this ___GFP_UNUSED_BIT now?

> +	___GFP_DIRECT_RECLAIM_BIT,
> +	___GFP_KSWAPD_RECLAIM_BIT,
> +	___GFP_WRITE_BIT,
> +	___GFP_NOWARN_BIT,
> +	___GFP_RETRY_MAYFAIL_BIT,
> +	___GFP_NOFAIL_BIT,
> +	___GFP_NORETRY_BIT,
> +	___GFP_MEMALLOC_BIT,
> +	___GFP_COMP_BIT,
> +	___GFP_NOMEMALLOC_BIT,
> +	___GFP_HARDWALL_BIT,
> +	___GFP_THISNODE_BIT,
> +	___GFP_ACCOUNT_BIT,
> +	___GFP_ZEROTAGS_BIT,
> +#ifdef CONFIG_KASAN_HW_TAGS
> +	___GFP_SKIP_ZERO_BIT,
> +	___GFP_SKIP_KASAN_BIT,
> +#endif
> +#ifdef CONFIG_LOCKDEP
> +	___GFP_NOLOCKDEP_BIT,
> +#endif
> +	___GFP_LAST_BIT
> +};

Does it make sense to have something like:
   BUILD_BUG_ON(___GFP_LAST_BIT > BITS_PER_LONG, "blah");
(should we need to use BIT_ULL() one day)

> +
>   /* Plain integer GFP bitmasks. Do not use this directly. */
> -#define ___GFP_DMA		0x01u
> -#define ___GFP_HIGHMEM		0x02u
> -#define ___GFP_DMA32		0x04u
> -#define ___GFP_MOVABLE		0x08u
> -#define ___GFP_RECLAIMABLE	0x10u
> -#define ___GFP_HIGH		0x20u
> -#define ___GFP_IO		0x40u
> -#define ___GFP_FS		0x80u
> -#define ___GFP_ZERO		0x100u
> +#define ___GFP_DMA		BIT(___GFP_DMA_BIT)
> +#define ___GFP_HIGHMEM		BIT(___GFP_HIGHMEM_BIT)
> +#define ___GFP_DMA32		BIT(___GFP_DMA32_BIT)
> +#define ___GFP_MOVABLE		BIT(___GFP_MOVABLE_BIT)
> +#define ___GFP_RECLAIMABLE	BIT(___GFP_RECLAIMABLE_BIT)
> +#define ___GFP_HIGH		BIT(___GFP_HIGH_BIT)
> +#define ___GFP_IO		BIT(___GFP_IO_BIT)
> +#define ___GFP_FS		BIT(___GFP_FS_BIT)
> +#define ___GFP_ZERO		BIT(___GFP_ZERO_BIT)
>   /* 0x200u unused */

Keeping this comment here is now useless, IMHO.

CJ

> -#define ___GFP_DIRECT_RECLAIM	0x400u
> -#define ___GFP_KSWAPD_RECLAIM	0x800u
> -#define ___GFP_WRITE		0x1000u
> -#define ___GFP_NOWARN		0x2000u
> -#define ___GFP_RETRY_MAYFAIL	0x4000u
> -#define ___GFP_NOFAIL		0x8000u
> -#define ___GFP_NORETRY		0x10000u
> -#define ___GFP_MEMALLOC		0x20000u
> -#define ___GFP_COMP		0x40000u
> -#define ___GFP_NOMEMALLOC	0x80000u
> -#define ___GFP_HARDWALL		0x100000u
> -#define ___GFP_THISNODE		0x200000u
> -#define ___GFP_ACCOUNT		0x400000u
> -#define ___GFP_ZEROTAGS		0x800000u
> +#define ___GFP_DIRECT_RECLAIM	BIT(___GFP_DIRECT_RECLAIM_BIT)
> +#define ___GFP_KSWAPD_RECLAIM	BIT(___GFP_KSWAPD_RECLAIM_BIT)
> +#define ___GFP_WRITE		BIT(___GFP_WRITE_BIT)
> +#define ___GFP_NOWARN		BIT(___GFP_NOWARN_BIT)
> +#define ___GFP_RETRY_MAYFAIL	BIT(___GFP_RETRY_MAYFAIL_BIT)
> +#define ___GFP_NOFAIL		BIT(___GFP_NOFAIL_BIT)
> +#define ___GFP_NORETRY		BIT(___GFP_NORETRY_BIT)
> +#define ___GFP_MEMALLOC		BIT(___GFP_MEMALLOC_BIT)
> +#define ___GFP_COMP		BIT(___GFP_COMP_BIT)
> +#define ___GFP_NOMEMALLOC	BIT(___GFP_NOMEMALLOC_BIT)
> +#define ___GFP_HARDWALL		BIT(___GFP_HARDWALL_BIT)
> +#define ___GFP_THISNODE		BIT(___GFP_THISNODE_BIT)
> +#define ___GFP_ACCOUNT		BIT(___GFP_ACCOUNT_BIT)
> +#define ___GFP_ZEROTAGS		BIT(___GFP_ZEROTAGS_BIT)
>   #ifdef CONFIG_KASAN_HW_TAGS
> -#define ___GFP_SKIP_ZERO	0x1000000u
> -#define ___GFP_SKIP_KASAN	0x2000000u
> +#define ___GFP_SKIP_ZERO	BIT(___GFP_SKIP_ZERO_BIT)
> +#define ___GFP_SKIP_KASAN	BIT(___GFP_SKIP_KASAN_BIT)
>   #else
>   #define ___GFP_SKIP_ZERO	0
>   #define ___GFP_SKIP_KASAN	0
>   #endif
>   #ifdef CONFIG_LOCKDEP
> -#define ___GFP_NOLOCKDEP	0x4000000u
> +#define ___GFP_NOLOCKDEP	BIT(___GFP_NOLOCKDEP_BIT)
>   #else
>   #define ___GFP_NOLOCKDEP	0
>   #endif
> -/* If the above are modified, __GFP_BITS_SHIFT may need updating */
>   
>   /*
>    * Physical address zone modifiers (see linux/mmzone.h - low four bits)
> @@ -249,7 +283,7 @@ typedef unsigned int __bitwise gfp_t;
>   #define __GFP_NOLOCKDEP ((__force gfp_t)___GFP_NOLOCKDEP)
>   
>   /* Room for N __GFP_FOO bits */
> -#define __GFP_BITS_SHIFT (26 + IS_ENABLED(CONFIG_LOCKDEP))
> +#define __GFP_BITS_SHIFT ___GFP_LAST_BIT
>   #define __GFP_BITS_MASK ((__force gfp_t)((1 << __GFP_BITS_SHIFT) - 1))
>   
>   /**
> 
> base-commit: 603c04e27c3e9891ce7afa5cd6b496bfacff4206
Suren Baghdasaryan Feb. 25, 2024, 1:12 a.m. UTC | #2
On Sat, Feb 24, 2024 at 7:03 AM Christophe JAILLET
<christophe.jaillet@wanadoo.fr> wrote:
>
> Le 24/02/2024 à 02:58, Suren Baghdasaryan a écrit :
> > Introduce GFP bits enumeration to let compiler track the number of used
> > bits (which depends on the config options) instead of hardcoding them.
> > That simplifies __GFP_BITS_SHIFT calculation.
> >
> > Suggested-by: Petr Tesařík <petr@tesarici.cz>
> > Signed-off-by: Suren Baghdasaryan <surenb@google.com>
> > Reviewed-by: Kees Cook <keescook@chromium.org>
> > Reviewed-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> > Acked-by: Michal Hocko <mhocko@suse.com>
> > ---
> > Changes from v4 [1]:
> > - Split from the series [2] as a stand-alone patch, per Michal Hocko
> > - Added Reviewed-by, per Pasha Tatashin
> > - Added Acked-by, per Michal Hocko
> >
> > [1] https://lore.kernel.org/all/20240221194052.927623-7-surenb@google.com/
> > [2] https://lore.kernel.org/all/20240221194052.927623-1-surenb@google.com/
> >
> >   include/linux/gfp_types.h | 90 +++++++++++++++++++++++++++------------
> >   1 file changed, 62 insertions(+), 28 deletions(-)
> >
> > diff --git a/include/linux/gfp_types.h b/include/linux/gfp_types.h
> > index 1b6053da8754..868c8fb1bbc1 100644
> > --- a/include/linux/gfp_types.h
> > +++ b/include/linux/gfp_types.h
> > @@ -21,44 +21,78 @@ typedef unsigned int __bitwise gfp_t;
> >    * include/trace/events/mmflags.h and tools/perf/builtin-kmem.c
> >    */
> >
> > +enum {
> > +     ___GFP_DMA_BIT,
> > +     ___GFP_HIGHMEM_BIT,
> > +     ___GFP_DMA32_BIT,
> > +     ___GFP_MOVABLE_BIT,
> > +     ___GFP_RECLAIMABLE_BIT,
> > +     ___GFP_HIGH_BIT,
> > +     ___GFP_IO_BIT,
> > +     ___GFP_FS_BIT,
> > +     ___GFP_ZERO_BIT,
> > +     ___GFP_UNUSED_BIT,      /* 0x200u unused */
>
> Hi,
>
> what is the need to have this ___GFP_UNUSED_BIT now?

Hi!
We can remove it but then all values will shift. That should be safe
to do now but I prefer one patch to do only one thing. We can add a
separate patch to do further cleanup of unused values.

>
> > +     ___GFP_DIRECT_RECLAIM_BIT,
> > +     ___GFP_KSWAPD_RECLAIM_BIT,
> > +     ___GFP_WRITE_BIT,
> > +     ___GFP_NOWARN_BIT,
> > +     ___GFP_RETRY_MAYFAIL_BIT,
> > +     ___GFP_NOFAIL_BIT,
> > +     ___GFP_NORETRY_BIT,
> > +     ___GFP_MEMALLOC_BIT,
> > +     ___GFP_COMP_BIT,
> > +     ___GFP_NOMEMALLOC_BIT,
> > +     ___GFP_HARDWALL_BIT,
> > +     ___GFP_THISNODE_BIT,
> > +     ___GFP_ACCOUNT_BIT,
> > +     ___GFP_ZEROTAGS_BIT,
> > +#ifdef CONFIG_KASAN_HW_TAGS
> > +     ___GFP_SKIP_ZERO_BIT,
> > +     ___GFP_SKIP_KASAN_BIT,
> > +#endif
> > +#ifdef CONFIG_LOCKDEP
> > +     ___GFP_NOLOCKDEP_BIT,
> > +#endif
> > +     ___GFP_LAST_BIT
> > +};
>
> Does it make sense to have something like:
>    BUILD_BUG_ON(___GFP_LAST_BIT > BITS_PER_LONG, "blah");

I suppose that would not hurt, except gfp_t is unsigned int, not long.
Something like this would work I think:

BUILD_BUG_ON_MSG(___GFP_LAST_BIT > BITS_PER_TYPE(gfp_t), "GFP bit overflow");

except I'm not sure where to put this check. One of the __init
functions in page_alloc.c would probably work but none seem to be
appropriate. mm_core_init() perhaps? Other ideas?

> (should we need to use BIT_ULL() one day)
>
> > +
> >   /* Plain integer GFP bitmasks. Do not use this directly. */
> > -#define ___GFP_DMA           0x01u
> > -#define ___GFP_HIGHMEM               0x02u
> > -#define ___GFP_DMA32         0x04u
> > -#define ___GFP_MOVABLE               0x08u
> > -#define ___GFP_RECLAIMABLE   0x10u
> > -#define ___GFP_HIGH          0x20u
> > -#define ___GFP_IO            0x40u
> > -#define ___GFP_FS            0x80u
> > -#define ___GFP_ZERO          0x100u
> > +#define ___GFP_DMA           BIT(___GFP_DMA_BIT)
> > +#define ___GFP_HIGHMEM               BIT(___GFP_HIGHMEM_BIT)
> > +#define ___GFP_DMA32         BIT(___GFP_DMA32_BIT)
> > +#define ___GFP_MOVABLE               BIT(___GFP_MOVABLE_BIT)
> > +#define ___GFP_RECLAIMABLE   BIT(___GFP_RECLAIMABLE_BIT)
> > +#define ___GFP_HIGH          BIT(___GFP_HIGH_BIT)
> > +#define ___GFP_IO            BIT(___GFP_IO_BIT)
> > +#define ___GFP_FS            BIT(___GFP_FS_BIT)
> > +#define ___GFP_ZERO          BIT(___GFP_ZERO_BIT)
> >   /* 0x200u unused */
>
> Keeping this comment here is now useless, IMHO.

Until the cleanup I would prefer to have it here to denote the gap.
Thanks,
Suren.

>
> CJ
>
> > -#define ___GFP_DIRECT_RECLAIM        0x400u
> > -#define ___GFP_KSWAPD_RECLAIM        0x800u
> > -#define ___GFP_WRITE         0x1000u
> > -#define ___GFP_NOWARN                0x2000u
> > -#define ___GFP_RETRY_MAYFAIL 0x4000u
> > -#define ___GFP_NOFAIL                0x8000u
> > -#define ___GFP_NORETRY               0x10000u
> > -#define ___GFP_MEMALLOC              0x20000u
> > -#define ___GFP_COMP          0x40000u
> > -#define ___GFP_NOMEMALLOC    0x80000u
> > -#define ___GFP_HARDWALL              0x100000u
> > -#define ___GFP_THISNODE              0x200000u
> > -#define ___GFP_ACCOUNT               0x400000u
> > -#define ___GFP_ZEROTAGS              0x800000u
> > +#define ___GFP_DIRECT_RECLAIM        BIT(___GFP_DIRECT_RECLAIM_BIT)
> > +#define ___GFP_KSWAPD_RECLAIM        BIT(___GFP_KSWAPD_RECLAIM_BIT)
> > +#define ___GFP_WRITE         BIT(___GFP_WRITE_BIT)
> > +#define ___GFP_NOWARN                BIT(___GFP_NOWARN_BIT)
> > +#define ___GFP_RETRY_MAYFAIL BIT(___GFP_RETRY_MAYFAIL_BIT)
> > +#define ___GFP_NOFAIL                BIT(___GFP_NOFAIL_BIT)
> > +#define ___GFP_NORETRY               BIT(___GFP_NORETRY_BIT)
> > +#define ___GFP_MEMALLOC              BIT(___GFP_MEMALLOC_BIT)
> > +#define ___GFP_COMP          BIT(___GFP_COMP_BIT)
> > +#define ___GFP_NOMEMALLOC    BIT(___GFP_NOMEMALLOC_BIT)
> > +#define ___GFP_HARDWALL              BIT(___GFP_HARDWALL_BIT)
> > +#define ___GFP_THISNODE              BIT(___GFP_THISNODE_BIT)
> > +#define ___GFP_ACCOUNT               BIT(___GFP_ACCOUNT_BIT)
> > +#define ___GFP_ZEROTAGS              BIT(___GFP_ZEROTAGS_BIT)
> >   #ifdef CONFIG_KASAN_HW_TAGS
> > -#define ___GFP_SKIP_ZERO     0x1000000u
> > -#define ___GFP_SKIP_KASAN    0x2000000u
> > +#define ___GFP_SKIP_ZERO     BIT(___GFP_SKIP_ZERO_BIT)
> > +#define ___GFP_SKIP_KASAN    BIT(___GFP_SKIP_KASAN_BIT)
> >   #else
> >   #define ___GFP_SKIP_ZERO    0
> >   #define ___GFP_SKIP_KASAN   0
> >   #endif
> >   #ifdef CONFIG_LOCKDEP
> > -#define ___GFP_NOLOCKDEP     0x4000000u
> > +#define ___GFP_NOLOCKDEP     BIT(___GFP_NOLOCKDEP_BIT)
> >   #else
> >   #define ___GFP_NOLOCKDEP    0
> >   #endif
> > -/* If the above are modified, __GFP_BITS_SHIFT may need updating */
> >
> >   /*
> >    * Physical address zone modifiers (see linux/mmzone.h - low four bits)
> > @@ -249,7 +283,7 @@ typedef unsigned int __bitwise gfp_t;
> >   #define __GFP_NOLOCKDEP ((__force gfp_t)___GFP_NOLOCKDEP)
> >
> >   /* Room for N __GFP_FOO bits */
> > -#define __GFP_BITS_SHIFT (26 + IS_ENABLED(CONFIG_LOCKDEP))
> > +#define __GFP_BITS_SHIFT ___GFP_LAST_BIT
> >   #define __GFP_BITS_MASK ((__force gfp_t)((1 << __GFP_BITS_SHIFT) - 1))
> >
> >   /**
> >
> > base-commit: 603c04e27c3e9891ce7afa5cd6b496bfacff4206
>
Michal Hocko Feb. 26, 2024, 8:43 a.m. UTC | #3
On Sun 25-02-24 01:12:46, Suren Baghdasaryan wrote:
> On Sat, Feb 24, 2024 at 7:03 AM Christophe JAILLET
> <christophe.jaillet@wanadoo.fr> wrote:
> >
> > Le 24/02/2024 à 02:58, Suren Baghdasaryan a écrit :
> > > Introduce GFP bits enumeration to let compiler track the number of used
> > > bits (which depends on the config options) instead of hardcoding them.
> > > That simplifies __GFP_BITS_SHIFT calculation.
> > >
> > > Suggested-by: Petr Tesařík <petr@tesarici.cz>
> > > Signed-off-by: Suren Baghdasaryan <surenb@google.com>
> > > Reviewed-by: Kees Cook <keescook@chromium.org>
> > > Reviewed-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> > > Acked-by: Michal Hocko <mhocko@suse.com>
> > > ---
> > > Changes from v4 [1]:
> > > - Split from the series [2] as a stand-alone patch, per Michal Hocko
> > > - Added Reviewed-by, per Pasha Tatashin
> > > - Added Acked-by, per Michal Hocko
> > >
> > > [1] https://lore.kernel.org/all/20240221194052.927623-7-surenb@google.com/
> > > [2] https://lore.kernel.org/all/20240221194052.927623-1-surenb@google.com/
> > >
> > >   include/linux/gfp_types.h | 90 +++++++++++++++++++++++++++------------
> > >   1 file changed, 62 insertions(+), 28 deletions(-)
> > >
> > > diff --git a/include/linux/gfp_types.h b/include/linux/gfp_types.h
> > > index 1b6053da8754..868c8fb1bbc1 100644
> > > --- a/include/linux/gfp_types.h
> > > +++ b/include/linux/gfp_types.h
> > > @@ -21,44 +21,78 @@ typedef unsigned int __bitwise gfp_t;
> > >    * include/trace/events/mmflags.h and tools/perf/builtin-kmem.c
> > >    */
> > >
> > > +enum {
> > > +     ___GFP_DMA_BIT,
> > > +     ___GFP_HIGHMEM_BIT,
> > > +     ___GFP_DMA32_BIT,
> > > +     ___GFP_MOVABLE_BIT,
> > > +     ___GFP_RECLAIMABLE_BIT,
> > > +     ___GFP_HIGH_BIT,
> > > +     ___GFP_IO_BIT,
> > > +     ___GFP_FS_BIT,
> > > +     ___GFP_ZERO_BIT,
> > > +     ___GFP_UNUSED_BIT,      /* 0x200u unused */
> >
> > Hi,
> >
> > what is the need to have this ___GFP_UNUSED_BIT now?
> 
> Hi!
> We can remove it but then all values will shift. That should be safe
> to do now but I prefer one patch to do only one thing. We can add a
> separate patch to do further cleanup of unused values.

Agreed!

> > > +     ___GFP_DIRECT_RECLAIM_BIT,
> > > +     ___GFP_KSWAPD_RECLAIM_BIT,
> > > +     ___GFP_WRITE_BIT,
> > > +     ___GFP_NOWARN_BIT,
> > > +     ___GFP_RETRY_MAYFAIL_BIT,
> > > +     ___GFP_NOFAIL_BIT,
> > > +     ___GFP_NORETRY_BIT,
> > > +     ___GFP_MEMALLOC_BIT,
> > > +     ___GFP_COMP_BIT,
> > > +     ___GFP_NOMEMALLOC_BIT,
> > > +     ___GFP_HARDWALL_BIT,
> > > +     ___GFP_THISNODE_BIT,
> > > +     ___GFP_ACCOUNT_BIT,
> > > +     ___GFP_ZEROTAGS_BIT,
> > > +#ifdef CONFIG_KASAN_HW_TAGS
> > > +     ___GFP_SKIP_ZERO_BIT,
> > > +     ___GFP_SKIP_KASAN_BIT,
> > > +#endif
> > > +#ifdef CONFIG_LOCKDEP
> > > +     ___GFP_NOLOCKDEP_BIT,
> > > +#endif
> > > +     ___GFP_LAST_BIT
> > > +};
> >
> > Does it make sense to have something like:
> >    BUILD_BUG_ON(___GFP_LAST_BIT > BITS_PER_LONG, "blah");
> 
> I suppose that would not hurt, except gfp_t is unsigned int, not long.
> Something like this would work I think:
> 
> BUILD_BUG_ON_MSG(___GFP_LAST_BIT > BITS_PER_TYPE(gfp_t), "GFP bit overflow");
> 
> except I'm not sure where to put this check. One of the __init
> functions in page_alloc.c would probably work but none seem to be
> appropriate. mm_core_init() perhaps? Other ideas?

Would that check add much? We currently cannot use the full width of the
gfp_t because radix tree code needs to fit also its own tag into the
same word (see radix_tree_init). If the radix tree constrain is lifted
then we should add something like the above.
Suren Baghdasaryan Feb. 26, 2024, 4:13 p.m. UTC | #4
On Mon, Feb 26, 2024 at 12:43 AM Michal Hocko <mhocko@suse.com> wrote:
>
> On Sun 25-02-24 01:12:46, Suren Baghdasaryan wrote:
> > On Sat, Feb 24, 2024 at 7:03 AM Christophe JAILLET
> > <christophe.jaillet@wanadoo.fr> wrote:
> > >
> > > Le 24/02/2024 à 02:58, Suren Baghdasaryan a écrit :
> > > > Introduce GFP bits enumeration to let compiler track the number of used
> > > > bits (which depends on the config options) instead of hardcoding them.
> > > > That simplifies __GFP_BITS_SHIFT calculation.
> > > >
> > > > Suggested-by: Petr Tesařík <petr@tesarici.cz>
> > > > Signed-off-by: Suren Baghdasaryan <surenb@google.com>
> > > > Reviewed-by: Kees Cook <keescook@chromium.org>
> > > > Reviewed-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> > > > Acked-by: Michal Hocko <mhocko@suse.com>
> > > > ---
> > > > Changes from v4 [1]:
> > > > - Split from the series [2] as a stand-alone patch, per Michal Hocko
> > > > - Added Reviewed-by, per Pasha Tatashin
> > > > - Added Acked-by, per Michal Hocko
> > > >
> > > > [1] https://lore.kernel.org/all/20240221194052.927623-7-surenb@google.com/
> > > > [2] https://lore.kernel.org/all/20240221194052.927623-1-surenb@google.com/
> > > >
> > > >   include/linux/gfp_types.h | 90 +++++++++++++++++++++++++++------------
> > > >   1 file changed, 62 insertions(+), 28 deletions(-)
> > > >
> > > > diff --git a/include/linux/gfp_types.h b/include/linux/gfp_types.h
> > > > index 1b6053da8754..868c8fb1bbc1 100644
> > > > --- a/include/linux/gfp_types.h
> > > > +++ b/include/linux/gfp_types.h
> > > > @@ -21,44 +21,78 @@ typedef unsigned int __bitwise gfp_t;
> > > >    * include/trace/events/mmflags.h and tools/perf/builtin-kmem.c
> > > >    */
> > > >
> > > > +enum {
> > > > +     ___GFP_DMA_BIT,
> > > > +     ___GFP_HIGHMEM_BIT,
> > > > +     ___GFP_DMA32_BIT,
> > > > +     ___GFP_MOVABLE_BIT,
> > > > +     ___GFP_RECLAIMABLE_BIT,
> > > > +     ___GFP_HIGH_BIT,
> > > > +     ___GFP_IO_BIT,
> > > > +     ___GFP_FS_BIT,
> > > > +     ___GFP_ZERO_BIT,
> > > > +     ___GFP_UNUSED_BIT,      /* 0x200u unused */
> > >
> > > Hi,
> > >
> > > what is the need to have this ___GFP_UNUSED_BIT now?
> >
> > Hi!
> > We can remove it but then all values will shift. That should be safe
> > to do now but I prefer one patch to do only one thing. We can add a
> > separate patch to do further cleanup of unused values.
>
> Agreed!
>
> > > > +     ___GFP_DIRECT_RECLAIM_BIT,
> > > > +     ___GFP_KSWAPD_RECLAIM_BIT,
> > > > +     ___GFP_WRITE_BIT,
> > > > +     ___GFP_NOWARN_BIT,
> > > > +     ___GFP_RETRY_MAYFAIL_BIT,
> > > > +     ___GFP_NOFAIL_BIT,
> > > > +     ___GFP_NORETRY_BIT,
> > > > +     ___GFP_MEMALLOC_BIT,
> > > > +     ___GFP_COMP_BIT,
> > > > +     ___GFP_NOMEMALLOC_BIT,
> > > > +     ___GFP_HARDWALL_BIT,
> > > > +     ___GFP_THISNODE_BIT,
> > > > +     ___GFP_ACCOUNT_BIT,
> > > > +     ___GFP_ZEROTAGS_BIT,
> > > > +#ifdef CONFIG_KASAN_HW_TAGS
> > > > +     ___GFP_SKIP_ZERO_BIT,
> > > > +     ___GFP_SKIP_KASAN_BIT,
> > > > +#endif
> > > > +#ifdef CONFIG_LOCKDEP
> > > > +     ___GFP_NOLOCKDEP_BIT,
> > > > +#endif
> > > > +     ___GFP_LAST_BIT
> > > > +};
> > >
> > > Does it make sense to have something like:
> > >    BUILD_BUG_ON(___GFP_LAST_BIT > BITS_PER_LONG, "blah");
> >
> > I suppose that would not hurt, except gfp_t is unsigned int, not long.
> > Something like this would work I think:
> >
> > BUILD_BUG_ON_MSG(___GFP_LAST_BIT > BITS_PER_TYPE(gfp_t), "GFP bit overflow");
> >
> > except I'm not sure where to put this check. One of the __init
> > functions in page_alloc.c would probably work but none seem to be
> > appropriate. mm_core_init() perhaps? Other ideas?
>
> Would that check add much? We currently cannot use the full width of the
> gfp_t because radix tree code needs to fit also its own tag into the
> same word (see radix_tree_init). If the radix tree constrain is lifted
> then we should add something like the above.

Ah, good point. That check in radix_tree_init() is already more strict
than this one. Looks like we are covered.
Thanks,
Suren.

> --
> Michal Hocko
> SUSE Labs
diff mbox series

Patch

diff --git a/include/linux/gfp_types.h b/include/linux/gfp_types.h
index 1b6053da8754..868c8fb1bbc1 100644
--- a/include/linux/gfp_types.h
+++ b/include/linux/gfp_types.h
@@ -21,44 +21,78 @@  typedef unsigned int __bitwise gfp_t;
  * include/trace/events/mmflags.h and tools/perf/builtin-kmem.c
  */
 
+enum {
+	___GFP_DMA_BIT,
+	___GFP_HIGHMEM_BIT,
+	___GFP_DMA32_BIT,
+	___GFP_MOVABLE_BIT,
+	___GFP_RECLAIMABLE_BIT,
+	___GFP_HIGH_BIT,
+	___GFP_IO_BIT,
+	___GFP_FS_BIT,
+	___GFP_ZERO_BIT,
+	___GFP_UNUSED_BIT,	/* 0x200u unused */
+	___GFP_DIRECT_RECLAIM_BIT,
+	___GFP_KSWAPD_RECLAIM_BIT,
+	___GFP_WRITE_BIT,
+	___GFP_NOWARN_BIT,
+	___GFP_RETRY_MAYFAIL_BIT,
+	___GFP_NOFAIL_BIT,
+	___GFP_NORETRY_BIT,
+	___GFP_MEMALLOC_BIT,
+	___GFP_COMP_BIT,
+	___GFP_NOMEMALLOC_BIT,
+	___GFP_HARDWALL_BIT,
+	___GFP_THISNODE_BIT,
+	___GFP_ACCOUNT_BIT,
+	___GFP_ZEROTAGS_BIT,
+#ifdef CONFIG_KASAN_HW_TAGS
+	___GFP_SKIP_ZERO_BIT,
+	___GFP_SKIP_KASAN_BIT,
+#endif
+#ifdef CONFIG_LOCKDEP
+	___GFP_NOLOCKDEP_BIT,
+#endif
+	___GFP_LAST_BIT
+};
+
 /* Plain integer GFP bitmasks. Do not use this directly. */
-#define ___GFP_DMA		0x01u
-#define ___GFP_HIGHMEM		0x02u
-#define ___GFP_DMA32		0x04u
-#define ___GFP_MOVABLE		0x08u
-#define ___GFP_RECLAIMABLE	0x10u
-#define ___GFP_HIGH		0x20u
-#define ___GFP_IO		0x40u
-#define ___GFP_FS		0x80u
-#define ___GFP_ZERO		0x100u
+#define ___GFP_DMA		BIT(___GFP_DMA_BIT)
+#define ___GFP_HIGHMEM		BIT(___GFP_HIGHMEM_BIT)
+#define ___GFP_DMA32		BIT(___GFP_DMA32_BIT)
+#define ___GFP_MOVABLE		BIT(___GFP_MOVABLE_BIT)
+#define ___GFP_RECLAIMABLE	BIT(___GFP_RECLAIMABLE_BIT)
+#define ___GFP_HIGH		BIT(___GFP_HIGH_BIT)
+#define ___GFP_IO		BIT(___GFP_IO_BIT)
+#define ___GFP_FS		BIT(___GFP_FS_BIT)
+#define ___GFP_ZERO		BIT(___GFP_ZERO_BIT)
 /* 0x200u unused */
-#define ___GFP_DIRECT_RECLAIM	0x400u
-#define ___GFP_KSWAPD_RECLAIM	0x800u
-#define ___GFP_WRITE		0x1000u
-#define ___GFP_NOWARN		0x2000u
-#define ___GFP_RETRY_MAYFAIL	0x4000u
-#define ___GFP_NOFAIL		0x8000u
-#define ___GFP_NORETRY		0x10000u
-#define ___GFP_MEMALLOC		0x20000u
-#define ___GFP_COMP		0x40000u
-#define ___GFP_NOMEMALLOC	0x80000u
-#define ___GFP_HARDWALL		0x100000u
-#define ___GFP_THISNODE		0x200000u
-#define ___GFP_ACCOUNT		0x400000u
-#define ___GFP_ZEROTAGS		0x800000u
+#define ___GFP_DIRECT_RECLAIM	BIT(___GFP_DIRECT_RECLAIM_BIT)
+#define ___GFP_KSWAPD_RECLAIM	BIT(___GFP_KSWAPD_RECLAIM_BIT)
+#define ___GFP_WRITE		BIT(___GFP_WRITE_BIT)
+#define ___GFP_NOWARN		BIT(___GFP_NOWARN_BIT)
+#define ___GFP_RETRY_MAYFAIL	BIT(___GFP_RETRY_MAYFAIL_BIT)
+#define ___GFP_NOFAIL		BIT(___GFP_NOFAIL_BIT)
+#define ___GFP_NORETRY		BIT(___GFP_NORETRY_BIT)
+#define ___GFP_MEMALLOC		BIT(___GFP_MEMALLOC_BIT)
+#define ___GFP_COMP		BIT(___GFP_COMP_BIT)
+#define ___GFP_NOMEMALLOC	BIT(___GFP_NOMEMALLOC_BIT)
+#define ___GFP_HARDWALL		BIT(___GFP_HARDWALL_BIT)
+#define ___GFP_THISNODE		BIT(___GFP_THISNODE_BIT)
+#define ___GFP_ACCOUNT		BIT(___GFP_ACCOUNT_BIT)
+#define ___GFP_ZEROTAGS		BIT(___GFP_ZEROTAGS_BIT)
 #ifdef CONFIG_KASAN_HW_TAGS
-#define ___GFP_SKIP_ZERO	0x1000000u
-#define ___GFP_SKIP_KASAN	0x2000000u
+#define ___GFP_SKIP_ZERO	BIT(___GFP_SKIP_ZERO_BIT)
+#define ___GFP_SKIP_KASAN	BIT(___GFP_SKIP_KASAN_BIT)
 #else
 #define ___GFP_SKIP_ZERO	0
 #define ___GFP_SKIP_KASAN	0
 #endif
 #ifdef CONFIG_LOCKDEP
-#define ___GFP_NOLOCKDEP	0x4000000u
+#define ___GFP_NOLOCKDEP	BIT(___GFP_NOLOCKDEP_BIT)
 #else
 #define ___GFP_NOLOCKDEP	0
 #endif
-/* If the above are modified, __GFP_BITS_SHIFT may need updating */
 
 /*
  * Physical address zone modifiers (see linux/mmzone.h - low four bits)
@@ -249,7 +283,7 @@  typedef unsigned int __bitwise gfp_t;
 #define __GFP_NOLOCKDEP ((__force gfp_t)___GFP_NOLOCKDEP)
 
 /* Room for N __GFP_FOO bits */
-#define __GFP_BITS_SHIFT (26 + IS_ENABLED(CONFIG_LOCKDEP))
+#define __GFP_BITS_SHIFT ___GFP_LAST_BIT
 #define __GFP_BITS_MASK ((__force gfp_t)((1 << __GFP_BITS_SHIFT) - 1))
 
 /**