Re: Memory reclaim protection and cgroup nesting (desktop use)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 2020-03-04 at 11:30 -0500, Tejun Heo wrote:
> Hello,
> 
> (cc'ing Johannes and quoting whole msg)
> 
> On Wed, Mar 04, 2020 at 10:44:44AM +0100, Benjamin Berg wrote:
> > Hi,
> > 
> > TL;DR: I seem to need memory.min/memory.max to be set on each child
> > cgroup and not just the parents. Is this expected?
> 
> Yes, currently. However, v5.7+ will have a cgroup2 mount option to
> propagate protection automatically.
> 
>   https://lore.kernel.org/linux-mm/20191219200718.15696-4-hannes@xxxxxxxxxxx/

Oh, that is an interesting discussion/development, thanks for the
pointer!

I think that mount option is great. It is also interesting to see that
I could achieve a similar effect by disabling the memory controller.
That makes sense, but it had not occurred to me.


A major discussion point seemed to be that cgroups should be grouped by
their resource management needs rather than a logical hierarchy. I
think that the resource management needs actually map well enough to
the logical hierarchy in our case. The hierarchy looks like:

                         root
                       /     \
           system.slice       user.slice
          /    |              |         \
      cron  journal    user-1000.slice   user-1001.slice
                              |                      \
                      user@1000.service            [SAME]
                        |          |
                   apps.slice   session.slice
                       |             |
                  unprotected    protected

Where the tree underneath user@1000.service is managed separately from
the rest. Moving parts of this tree to other locations would require
interactions between two systemd processes.

But, all layers can actually have a purpose:
 * user.slice:
   - Admin/distribution defines resource guarantees/constraints that
     apply to all users as a group.
     (Assuming a single user system, the same guarantees as below.)
   - i.e. it defines the separation of system services vs. user.
 * user-.slice:
   - Admin defines resource guarantees/constrains for a single user
   - A separate policy dynamically changes this depending on whether
     the user is currently active (on seat0). Ensuring only the
     active user is benefiting.
 * user@1000.service:
   - Nothing needed here, just the point where resource management
     is delegated to the users control.
 * session.slice:
   - User/session manages resources to ensure UI responsiveness

I think this actually makes sense. Both from an hierarchical point of
view and also for configuring resources. In particular the user-.slice
layer is important, because this grouping allows us to dynamically
adjust resource management. The obvious thing we can do there is to
prioritise the currently active user while also lowering resource
allocations for inactive users (e.g. graphical greeter still running in
the background).

Note, that from my point of view the scenario that most concerns me is
a resource competition between session.slice and its siblings. This
makes the hierarchy above even less important; we just need to give the
user enough control to do resource allocations within their own
subtree.

So, it seems to me that the suggested mount option should work well in
our scenario.

Benjamin


> > I have been experimenting with using cgroups to protect a GNOME
> > session. The intention is that the GNOME Shell itself and important
> > other services remain responsive, even if the application workload is
> > thrashing. The long term goal here is to bridge the time until an OOM
> > killer like oomd would get the system back into normal conditions using
> > memory pressure information.
> > 
> > Note that I have done these tests without any swap and with huge
> > memory.min/memory.low values. I consider this scenario pathological,
> > however, it seems like a reasonable way to really exercise the cgroup
> > reclaim protection logic.
> 
> It's incomplete and more brittle in that the kernel has to treat a
> large portion of memory usage as essentially memlocked.
> 
> > The resulting cgroup hierarchy looked something like:
> > 
> > -.slice
> > ├─user.slice
> > │ └─user-1000.slice
> > │   ├─user@1000.service
> > │   │ ├─session.slice
> > │   │ │ ├─gsd-*.service
> > │   │ │ │ └─208803 /usr/libexec/gsd-rfkill
> > │   │ │ ├─gnome-shell-wayland.service
> > │   │ │ │ ├─208493 /usr/bin/gnome-shell
> > │   │ │ │ ├─208549 /usr/bin/Xwayland :0 -rootless -noreset -accessx
> > -core -auth /run/user/1000/.mutter-Xwayla>
> > │   │ │ │ └─ …
> > │   │ └─apps.slice
> > │   │   ├─gnome-launched-tracker-miner-fs.desktop-208880.scope
> > │   │   │ └─208880 /usr/libexec/tracker-miner-fs
> > │   │   ├─dbus-:1.2-org.gnome.OnlineAccounts@0.service
> > │   │   │ └─208668 /usr/libexec/goa-daemon
> > │   │   ├─flatpak-org.gnome.Fractal-210350.scope
> > │   │   ├─gnome-terminal-server.service
> > │   │   │ ├─209261 /usr/libexec/gnome-terminal-server
> > │   │   │ ├─209434 bash
> > │   │   │ └─ … including the test load i.e. "make -j32" of a C++
> > code
> > 
> > 
> > I also enabled the CPU and IO controllers in my tests, but I don't
> > think that is as relevant. The main thing is that I set
> 
> CPU control isn't but IO is. Without working IO isolation, it's
> relatively easy to drive the system into the ground given enough
> stress ouside the protected area.
> 
> >   memory.min: 2GiB
> >   memory.low: 4GiB
> > 
> > using systemd on all of
> > 
> >  * user.slice,
> >  * user-1000.slice,
> >  * user@1000.slice,
> >  * session.slice and
> >  * everything inside session.slice
> >    (i.e. gnome-shell-wayland.service, gsd-*.service, …)
> > 
> > excluding apps.slice from protection.
> > 
> > (In a realistic scenario I expect to have swap and then reserving maybe
> > a few hundred MiB; DAMON might help with finding good values.)
> 
> What's DAMON?
> 
> > At that point, the protection started working pretty much flawlessly.
> > i.e. my gnome-shell would continue to run without major page faulting
> > even though everything in apps.slice was thrashing heavily. The
> > mouse/keyboard remained completely responsive, and interacting with
> > applications ended up working much better thanks to knowing where input
> > was going. Even if the applications themselves took seconds to react.
> > 
> > So far, so good. What surprises me is that I needed to set the
> > protection on the child cgroups (i.e. gnome-shell-wayland.service).
> > Without this, it would not work (reliably) and my gnome-shell would
> > still have a lot of re-faults to load libraries and other mmap'ed data
> > back into memory (I used "perf --no-syscalls -F" to trace this and
> > observed these to be repeatedly for the same pages loading e.g.
> > functions for execution).
> > 
> > Due to accounting effects, I would expect re-faults to happen up to one
> > time in this scenario. At that point the page in question will be
> > accounted against the shell's cgroup and reclaim protection could kick
> > in. Unfortunately, that did not seem to happen unless the shell's
> > cgroup itself had protections and not just all of its parents.
> > 
> > Is it expected that I need to set limits on each child?
> 
> Yes, right now, memory.low needs to be configured all the way down to
> the leaf to be effective, which can be rather cumbersome. As written
> above, future kernels will be easier to work with in this respect.
> 
> Thanks.
> 

Attachment: signature.asc
Description: This is a digitally signed message part


[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux OMAP]     [Linux MIPS]     [eCos]     [Asterisk Internet PBX]     [Linux API]     [Monitors]

  Powered by Linux