Re: [PATCH] mm: introduce PAGEFLAGS_MASK to replace ((1UL << NR_PAGEFLAGS) - 1)

From: Muchun Song
Date: Thu Aug 19 2021 - 11:06:27 EST


On Thu, Aug 19, 2021 at 7:16 PM Johannes Weiner <hannes@xxxxxxxxxxx> wrote:
>
> On Thu, Aug 19, 2021 at 02:33:37PM +0800, Muchun Song wrote:
> > On Wed, Aug 18, 2021 at 3:39 PM Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> wrote:
> > >
> > > On Tue, 17 Aug 2021 21:44:36 -0700 Roman Gushchin <guro@xxxxxx> wrote:
> > >
> > > > On Wed, Aug 18, 2021 at 12:35:08PM +0800, Muchun Song wrote:
> > > > > On Wed, Aug 18, 2021 at 10:16 AM Roman Gushchin <guro@xxxxxx> wrote:
> > > > > >
> > > > > > On Tue, Aug 17, 2021 at 11:30:32AM +0800, Muchun Song wrote:
> > > > > > > Instead of hard-coding ((1UL << NR_PAGEFLAGS) - 1) everywhere, introducing
> > > > > > > PAGEFLAGS_MASK to make the code clear to get the page flags.
> > > > > > >
> > > > > > > Signed-off-by: Muchun Song <songmuchun@xxxxxxxxxxxxx>
> > > > > > > ---
> > > > > > > include/linux/page-flags.h | 4 +++-
> > > > > > > include/trace/events/page_ref.h | 4 ++--
> > > > > > > lib/test_printf.c | 2 +-
> > > > > > > lib/vsprintf.c | 2 +-
> > > > > > > 4 files changed, 7 insertions(+), 5 deletions(-)
> > > > > > >
> > > > > > > diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h
> > > > > > > index 54c4af35c628..1f951ac24a5e 100644
> > > > > > > --- a/include/linux/page-flags.h
> > > > > > > +++ b/include/linux/page-flags.h
> > > > > > > @@ -180,6 +180,8 @@ enum pageflags {
> > > > > > > PG_reported = PG_uptodate,
> > > > > > > };
> > > > > > >
> > > > > > > +#define PAGEFLAGS_MASK (~((1UL << NR_PAGEFLAGS) - 1))
> > > > > >
> > > > > > Hm, isn't it better to invert it? Like
> > > > > > #define PAGEFLAGS_MASK ((1UL << NR_PAGEFLAGS) - 1)
> > > > > >
> > > > > > It feels more usual and will simplify the rest of the patch.
> > > > >
> > > > > Actually, I learned from PAGE_MASK. So I thought the macro
> > > > > like xxx_MASK should be the format of 0xff...ff00...00. I don't
> > > > > know if it is an unwritten rule. I can invert PAGEFLAGS_MASK
> > > > > if it's not a rule.
> > > >
> > > > There are many examples of both approached in the kernel tree,
> > > > however I'd say the more common is without "~" (out of my head).
> > > >
> > > > It's definitely OK to define it like
> > > > #define PAGEFLAGS_MASK ((1UL << NR_PAGEFLAGS) - 1)
> > > >
> > >
> > > PAGE_MASK has always seemed weird to me but I figured that emulating it
> > > would be the approach of least surprise. Might be wrong about that...
> >
> > IIUC, you seem to agree with the current approach. Right?
>
> We do use both schemes. PAGE_MASK, as you pointed out. PMD_MASK,
> PUD_MASK etc. also *hide* rather than extract the named bits.
>
> However, we already have a series of masks for page->flags in mm.h
> that follow the more common scheme of *extracting* the named bits:
>
> #define ZONES_MASK ((1UL << ZONES_WIDTH) - 1)
> #define NODES_MASK ((1UL << NODES_WIDTH) - 1)
> #define SECTIONS_MASK ((1UL << SECTIONS_WIDTH) - 1)
> #define LAST_CPUPID_MASK ((1UL << LAST_CPUPID_SHIFT) - 1)
> #define KASAN_TAG_MASK ((1UL << KASAN_TAG_WIDTH) - 1)
> #define ZONEID_MASK ((1UL << ZONEID_SHIFT) - 1)
>
> Because of that, I would agree with Roman here and say it probably
> makes sense to put the PAGEFLAGS_MASK definition next to those and
> follow the "mask everything but" semantics.

Thanks for your suggestions. I'll send a v2 patch.