From: Yafang Shao <laoar.shao@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Roman Gushchin <guro@fb.com>,
Johannes Weiner <hannes@cmpxchg.org>,
Michal Hocko <mhocko@kernel.org>,
Vladimir Davydov <vdavydov.dev@gmail.com>,
Linux MM <linux-mm@kvack.org>
Subject: Re: [PATCH] mm, memcg: reduce size of struct mem_cgroup by using bit field
Date: Thu, 2 Jan 2020 13:43:05 +0800 [thread overview]
Message-ID: <CALOAHbDT_8rvie_72N7rTszi7bpofjOoPUzDVcC1-pfpj0Nzng@mail.gmail.com> (raw)
In-Reply-To: <20191231143139.20a912b3386548062343a5b2@linux-foundation.org>
On Wed, Jan 1, 2020 at 6:31 AM Andrew Morton <akpm@linux-foundation.org> wrote:
>
> On Fri, 27 Dec 2019 07:43:52 -0500 Yafang Shao <laoar.shao@gmail.com> wrote:
>
> > There are some members in struct mem_group can be either 0(false) or
> > 1(true), so we can define them using bit field to reduce size. With this
> > patch, the size of struct mem_cgroup can be reduced by 64 bytes in theory,
> > but as there're some MEMCG_PADDING()s, the real number may be different,
> > which is relate with the cacheline size. Anyway, this patch could reduce
> > the size of struct mem_cgroup more or less.
> >
> > ...
> >
> > --- a/include/linux/memcontrol.h
> > +++ b/include/linux/memcontrol.h
> > @@ -229,20 +229,26 @@ struct mem_cgroup {
> > /*
> > * Should the accounting and control be hierarchical, per subtree?
> > */
> > - bool use_hierarchy;
> > + unsigned int use_hierarchy : 1;
> > +
> > + /* Legacy tcp memory accounting */
> > + unsigned int tcpmem_active : 1;
> > + unsigned int tcpmem_pressure : 1;
>
> Kernel coding style for this is normally no-spaces:
>
> bool foo:1;
>
I always learn the kernel coding style from the kernel source code.
Before I tried to define them using bit field in the kernel, I checked
what the kernel did in the past.
I found there're some places defined with spaces[1], some places
defined without spaces[2].
Finally I selected the one with spaces, unfortunately that's the wrong one .
Anyway I know what the right thing is now, thanks.
[1]. https://elixir.bootlin.com/linux/v5.5-rc4/source/include/linux/tcp.h#L87
[2]. https://elixir.bootlin.com/linux/v5.5-rc4/source/include/linux/tcp.h#L213
>
>
> More significantly... Now that these fields share the same word of
> memory, what prevents races when two CPUs do read-modify-write
> operations on adjacent bitfields?
>
> This:
>
> struct foo {
> int a;
> int b;
> };
>
> doesn't need locking to prevent modifications of `a' from scribbling on
> `b'. But with this:
>
> struct foo {
> int a:1;
> int b:1;
> }
>
> a simple `a = 1' on CPU1 could race with a `b = 1' on CPU2.
>
> I think. Maybe the compiler can take care of this in some fashion, but
> it would require atomic bitops and I doubt if gcc does that for us?
>
>
GCC can guarantees that accesses to distinct structure members (which
aren't part of a bit-field) are independent, but it can't guarantee
that to bitfields.
I thought there are some synchronization mechanism to protect memcg
against concurrent access.
Thanks
Yafang
next prev parent reply other threads:[~2020-01-02 5:43 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-27 12:43 Yafang Shao
2019-12-27 12:43 ` [PATCH] mm, memcg: reset memcg's memory.{min, low} for reclaiming itself Yafang Shao
2019-12-27 23:49 ` Roman Gushchin
2019-12-28 1:45 ` Yafang Shao
2019-12-28 2:59 ` Roman Gushchin
2019-12-28 4:24 ` Yafang Shao
2019-12-27 23:55 ` [PATCH] mm, memcg: reduce size of struct mem_cgroup by using bit field Roman Gushchin
2019-12-28 4:22 ` Yafang Shao
2019-12-31 22:31 ` Andrew Morton
2020-01-02 5:43 ` Yafang Shao [this message]
2020-01-06 10:19 ` Michal Hocko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CALOAHbDT_8rvie_72N7rTszi7bpofjOoPUzDVcC1-pfpj0Nzng@mail.gmail.com \
--to=laoar.shao@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=guro@fb.com \
--cc=hannes@cmpxchg.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=vdavydov.dev@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox