On Sun, Mar 14, 2021 at 07:02:01PM -0700, Andi Kleen wrote:
> Yu Zhao <yuz...@google.com> writes:

Hi Andi!

Recovering the context a bit:

                err = -EBUSY;

> > +
> > +#ifdef CONFIG_MEMCG
> > +           if (memcg && atomic_read(&memcg->moving_account))
> > +                   goto contended;
> > +#endif
> > +           if (!mmap_read_trylock(mm))
> > +                   goto contended;
> 
> These are essentially spinloops. Surely you need a cpu_relax() somewhere?

contended:
                ...
                cond_resched();
        } while (err == -EAGAIN && !mm_is_oom_victim(mm) && 
!mm_has_migrated(mm, memcg));

So if it's contended, we break the loop.

> In general for all of spinloop like constructs it would be useful to
> consider how to teach lockdep about them.
> 
> > +   do {
> > +           old_flags = READ_ONCE(page->flags);
> > +           new_gen = ((old_flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
> > +           VM_BUG_ON_PAGE(new_gen < 0, page);
> > +           if (new_gen >= 0 && new_gen != old_gen)
> > +                   goto sort;
> > +
> > +           new_gen = (old_gen + 1) % MAX_NR_GENS;
> > +           new_flags = (old_flags & ~LRU_GEN_MASK) | ((new_gen + 1UL) << 
> > LRU_GEN_PGOFF);
> > +           /* mark page for reclaim if pending writeback */
> > +           if (front)
> > +                   new_flags |= BIT(PG_reclaim);
> > +   } while (cmpxchg(&page->flags, old_flags, new_flags) !=
> > old_flags);
> 
> I see this cmpxchg flags pattern a lot. Could there be some common code
> factoring?

Thanks for noticing this. A shorthand macro would be nice. Hmm... let
me investigate. I don't know how to do this off the top of my head:

A macro can be used like

  cmpxchg_macro() {
     func();
  }

without backslashes and it'll be expanded to

  do {
     func();
   } while ();

Reply via email to