From d56b3978bbcd28246b3e3ce3f8c958ac95785dd7 Mon Sep 17 00:00:00 2001
From: fangxiuning <fangxiuning@huawei.com>
Date: Wed, 22 Apr 2020 11:55:18 +0800
Subject:
  After systemd 239 version, a new feature is added to cgroups.
The processes started by users default to the cgroup group belonging
to user.slice, and the processes started by the system default to
system.slice. This is the direction of github systemd evolution.
However, there are still a large number of operations downstream
that systemd does not perceive to modify the cgroup group,
such as directly echo the process number to system.slice.

For example:
1. sleep 1000 &
2. echo sleep pid > /sys/fs/cgroup/memory/system.slice/task
3. systemctl daemon-reload
4. cat /proc/sleep pid/cgroup
this kind of operation, systemd is not aware of it.
When systemctl disable service or systemctl daemon-reload operation
is executed, systemd will re-attach each process to its original
Under the group(user.slice).

---
 src/core/main.c           | 1 +
 src/core/manager.c        | 2 ++
 src/core/manager.h        | 1 +
 src/core/system.conf.in   | 1 +
 src/core/unit-serialize.c | 2 +-
 5 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/core/main.c b/src/core/main.c
index e9f56fa..964adb5 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -684,6 +684,7 @@ static int parse_config_file(void) {
                 { "Manager", "DefaultFreezerAccounting",     config_parse_bool,                  0,                        &arg_defaults.freezer_accounting   },
                 { "Manager", "DefaultTasksAccounting",       config_parse_bool,                  0,                        &arg_defaults.tasks_accounting    },
                 { "Manager", "DefaultTasksMax",              config_parse_tasks_max,             0,                        &arg_defaults.tasks_max           },
+                { "Manager", "DefaultInvalidateCgroup",      config_parse_bool,                  0,                        &arg_defaults.invalidate_cgroup    },
                 { "Manager", "DefaultMemoryPressureThresholdSec", config_parse_sec,              0,                        &arg_defaults.memory_pressure_threshold_usec },
                 { "Manager", "DefaultMemoryPressureWatch",   config_parse_memory_pressure_watch, 0,                        &arg_defaults.memory_pressure_watch },
                 { "Manager", "CtrlAltDelBurstAction",        config_parse_emergency_action,      arg_runtime_scope,        &arg_cad_burst_action             },
diff --git a/src/core/manager.c b/src/core/manager.c
index 59170af..57dd3d1 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -4200,6 +4200,7 @@ int manager_set_unit_defaults(Manager *m, const UnitDefaults *defaults) {
         m->defaults.ip_accounting = defaults->ip_accounting;
 
         m->defaults.tasks_max = defaults->tasks_max;
+        m->defaults.invalidate_cgroup = defaults->invalidate_cgroup;
         m->defaults.optional_log = defaults->optional_log;
         m->defaults.timer_accuracy_usec = defaults->timer_accuracy_usec;
 
@@ -4969,6 +4970,7 @@ void unit_defaults_init(UnitDefaults *defaults, RuntimeScope scope) {
                 .io_accounting = false,
                 .blockio_accounting = false,
                 .tasks_accounting = true,
+                .invalidate_cgroup = true,
                 .ip_accounting = false,
 
                 .tasks_max = DEFAULT_TASKS_MAX,
diff --git a/src/core/manager.h b/src/core/manager.h
index 3c954af..0c9a2ea 100644
--- a/src/core/manager.h
+++ b/src/core/manager.h
@@ -173,6 +173,7 @@ typedef struct UnitDefaults {
 
         CGroupTasksMax tasks_max;
         usec_t timer_accuracy_usec;
+        bool invalidate_cgroup;
 
         OOMPolicy oom_policy;
         int oom_score_adjust;
diff --git a/src/core/system.conf.in b/src/core/system.conf.in
index a55106c..f48452d 100644
--- a/src/core/system.conf.in
+++ b/src/core/system.conf.in
@@ -78,6 +78,7 @@ DefaultLimitMEMLOCK=64M
 #DefaultLimitNICE=
 #DefaultLimitRTPRIO=
 #DefaultLimitRTTIME=
+#DefaultInvalidateCgroup=yes
 #DefaultMemoryPressureThresholdSec=200ms
 #DefaultMemoryPressureWatch=auto
 #DefaultOOMPolicy=stop
diff --git a/src/core/unit-serialize.c b/src/core/unit-serialize.c
index fe4221c..091e7b6 100644
--- a/src/core/unit-serialize.c
+++ b/src/core/unit-serialize.c
@@ -574,7 +574,7 @@ int unit_deserialize_state(Unit *u, FILE *f, FDSet *fds) {
         /* Let's make sure that everything that is deserialized also gets any potential new cgroup settings
          * applied after we are done. For that we invalidate anything already realized, so that we can
          * realize it again. */
-        if (u->cgroup_realized) {
+        if (u->cgroup_realized && u->manager->defaults.invalidate_cgroup) {
                 unit_invalidate_cgroup(u, _CGROUP_MASK_ALL);
                 unit_invalidate_cgroup_bpf(u);
         }
-- 
2.33.0

