Re: [patch] mm: memcontrol: do not iterate uninitialized memcgs

From: Johannes Weiner
Date: Thu Sep 25 2014 - 09:44:05 EST


On Wed, Sep 24, 2014 at 10:57:58PM -0400, Tejun Heo wrote:
> Hello,
>
> On Wed, Sep 24, 2014 at 10:31:18PM -0400, Johannes Weiner wrote:
> ..
> > not meet the ordering requirements for memcg, and so we still may see
> > partially initialized memcgs from the iterators.
>
> It's mainly the other way around - a fully initialized css may not
> show up in an iteration, but given that there's no memory ordering or
> synchronization around the flag, anything can happen.

Oh sure, I'm just more worried about leaking invalid memcgs rather
than temporarily skipping over a fully initialized one. But I updated
the changelog to mention both possibilities.

> > + if (next_css == &root->css ||
> > + css_tryget_online(next_css)) {
> > + struct mem_cgroup *memcg;
> > +
> > + memcg = mem_cgroup_from_css(next_css);
> > + if (memcg->initialized) {
> > + /*
> > + * Make sure the caller's accesses to
> > + * the memcg members are issued after
> > + * we see this flag set.
>
> I usually prefer if the comment points to the exact location that the
> matching memory barriers live. Sometimes it's difficult to locate the
> partner barrier even w/ the functional explanation.

That makes sense, updated.

> > + */
> > + smp_rmb();
> > + return memcg;
>
> In an unlikely event this rmb becomes an issue, a self-pointing
> pointer which is set/read using smp_store_release() and
> smp_load_acquire() respectively can do with plain barrier() on the
> reader side on archs which don't need data dependency barrier
> (basically everything except alpha). Not sure whether that'd be more
> or less readable than this tho.

So as far as I understand memory-barriers.txt we do not even need a
data dependency here to use store_release and load_acquire:

mem_cgroup_css_online():
<initialize memcg>
smp_store_release(&memcg->initialized, 1);

mem_cgroup_iter():
<look up maybe-initialized memcg>
if (smp_load_acquire(&memcg->initialized))
return memcg;

So while I doubt that the smp_rmb() will become a problem in this
path, it would be neat to annotate the state flag around which we
synchronize like this, rather than have an anonymous barrier.

Peter, would you know if this is correct, or whether these primitives
actually do require a data dependency?

Thanks!

Updated patch:

---