Re: [PATCH] mm: optimize once judgment with clang

From: David Laight

Date: Sat Mar 07 2026 - 05:50:19 EST


On Sat, 7 Mar 2026 10:12:18 +0200
Mike Rapoport <rppt@xxxxxxxxxx> wrote:

> On Sat, Mar 07, 2026 at 02:54:04PM +0800, Xie Yuanbin wrote:
> > commit 242b872239f6a7deacbc ("include/linux/once_lite.h: fix judgment in
> > WARN_ONCE with clang") helps optimize performance and size under the
> > clang compiler, but the modification is not complete.
>
> How much does it actually optimize for size?
> Note that performance is really not critical here because we are already
> dealing with slow path of debug code.

I suspect that unlikely(a && b) is really horrid - the compiler could
easily generate x = a && b; unlikely(x).
Probably enough to change to unlikely(a) && b, but if it is a slow path
perhaps just remove the unlikely().

David

>
> > Port the modification to WARN_ON_ONCE_GFP(), VM_WARN_ON_ONCE_PAGE(),
> > VM_WARN_ON_ONCE_FOLIO(), VM_WARN_ON_ONCE_MM() and VM_WARN_ON_ONCE_VMA().
> >
> > Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
> > Signed-off-by: Xie Yuanbin <qq570070308@xxxxxxxxx>
> > ---
> > include/linux/mmdebug.h | 8 ++++----
> > mm/internal.h | 3 ++-
> > 2 files changed, 6 insertions(+), 5 deletions(-)
> >
> > diff --git a/include/linux/mmdebug.h b/include/linux/mmdebug.h
> > index ab60ffba08f5..a167c5aa525e 100644
> > --- a/include/linux/mmdebug.h
> > +++ b/include/linux/mmdebug.h
> > @@ -60,7 +60,7 @@ void vma_iter_dump_tree(const struct vma_iterator *vmi);
> > static bool __section(".data..once") __warned; \
> > int __ret_warn_once = !!(cond); \
> > \
> > - if (unlikely(__ret_warn_once && !__warned)) { \
> > + if (unlikely(__ret_warn_once) && unlikely(!__warned)) { \
> > dump_page(page, "VM_WARN_ON_ONCE_PAGE(" __stringify(cond)")");\
> > __warned = true; \
> > WARN_ON(1); \
> > @@ -80,7 +80,7 @@ void vma_iter_dump_tree(const struct vma_iterator *vmi);
> > static bool __section(".data..once") __warned; \
> > int __ret_warn_once = !!(cond); \
> > \
> > - if (unlikely(__ret_warn_once && !__warned)) { \
> > + if (unlikely(__ret_warn_once) && unlikely(!__warned)) { \
> > dump_page(&folio->page, "VM_WARN_ON_ONCE_FOLIO(" __stringify(cond)")");\
> > __warned = true; \
> > WARN_ON(1); \
> > @@ -91,7 +91,7 @@ void vma_iter_dump_tree(const struct vma_iterator *vmi);
> > static bool __section(".data..once") __warned; \
> > int __ret_warn_once = !!(cond); \
> > \
> > - if (unlikely(__ret_warn_once && !__warned)) { \
> > + if (unlikely(__ret_warn_once) && unlikely(!__warned)) { \
> > dump_mm(mm); \
> > __warned = true; \
> > WARN_ON(1); \
> > @@ -102,7 +102,7 @@ void vma_iter_dump_tree(const struct vma_iterator *vmi);
> > static bool __section(".data..once") __warned; \
> > int __ret_warn_once = !!(cond); \
> > \
> > - if (unlikely(__ret_warn_once && !__warned)) { \
> > + if (unlikely(__ret_warn_once) && unlikely(!__warned)) { \
> > dump_vma(vma); \
> > __warned = true; \
> > WARN_ON(1); \
> > diff --git a/mm/internal.h b/mm/internal.h
> > index 6e1162e13289..52367f52d623 100644
> > --- a/mm/internal.h
> > +++ b/mm/internal.h
> > @@ -93,7 +93,8 @@ struct pagetable_move_control {
> > static bool __section(".data..once") __warned; \
> > int __ret_warn_once = !!(cond); \
> > \
> > - if (unlikely(!(gfp & __GFP_NOWARN) && __ret_warn_once && !__warned)) { \
> > + if (unlikely(__ret_warn_once) && !(gfp & __GFP_NOWARN) && \
> > + unlikely(!__warned)) { \
> > __warned = true; \
> > WARN_ON(1); \
> > } \
> > --
> > 2.51.0
> >
>