core: Avoid spurious realization of unit cgroups
authorMichal Koutný <mkoutny@suse.com>
Thu, 10 Jun 2021 13:58:43 +0000 (15:58 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 20 Jul 2021 16:12:00 +0000 (18:12 +0200)
commit9dcdb1aa09c57c0b182b67e85a6fbc0cfba40da3
treeb96486e2b3dd87cbb3b82bb9dee95851cc51cc4e
parent53d186048fd1cc545623e8ff3b376f90dbe26062
core: Avoid spurious realization of unit cgroups

Cgroups may be unnecessarily realized when they are not needed. This
happens, e.g. for mount units parsed from /proc/$PID/mountinfo, check

        touch /run/ns_mount
        unshare -n sh -c "mount --bind /proc/self/ns/net /run/ns_mount"
        # no cgroup exists
        file /sys/fs/cgroup/system.slice/run-ns_mount.mount
        systemctl daemon-reload
        # the vain cgroup exists
        file /sys/fs/cgroup/system.slice/run-ns_mount.mount

. (Such cgroups can account to a large number with many similar mounts.)

The code already accounts for "lazy" realization (see various checks for
Unit.cgroup_realized) but the unit_deserialize() in the reload/reexec
path performs unconditional realization.

Invalidate (and queue) the units for realization only if we know that
they were already realized in the past. This is a safe thing to do even
in the case the reload brings some new cgroup setting (controllers, BPF)
because units that aren't realized will use the updated setting when the
time for their realization comes. (It's not even needed to add a code
comment because the current formulation suggests the changed behavior.)

(cherry picked from commit cc815b7fea0ade5331e8dd22ef6b5183edb77608)
(cherry picked from commit 94f501805db8b272fd2d8d2c4eab0c5291db50e0)
(cherry picked from commit 8712fc5a181d1453840014d765c20c45dbf83b5f)
src/core/unit.c