HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/core/cgroup.c

179,588 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 543d1ac8e3c43a5af0861e2f1a038485663554eb

Lines 1001–2000 of 4,696

 
        /* If this is a LUKS/DM device, recursively try to get the originating block device */
        (void) block_get_originating(*ret, ret, /* recursive= */ true);
 
        /* If this is a partition, try to get the originating block device */
        (void) block_get_whole_disk(*ret, ret);
        return 0;
}
 
static bool cgroup_context_has_cpu_weight(CGroupContext *c) {
        return c->cpu_weight != CGROUP_WEIGHT_INVALID ||
                c->startup_cpu_weight != CGROUP_WEIGHT_INVALID;
}
 
static bool cgroup_context_has_allowed_cpus(CGroupContext *c) {
        return c->cpuset_cpus.set || c->startup_cpuset_cpus.set;
}
 
static bool cgroup_context_has_allowed_mems(CGroupContext *c) {
        return c->cpuset_mems.set || c->startup_cpuset_mems.set;
}
 
uint64_t cgroup_context_cpu_weight(CGroupContext *c, ManagerState state) {
        assert(c);
 
        if (IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING, MANAGER_STOPPING) &&
            c->startup_cpu_weight != CGROUP_WEIGHT_INVALID)
                return c->startup_cpu_weight;
        else if (c->cpu_weight != CGROUP_WEIGHT_INVALID)
                return c->cpu_weight;
        else
                return CGROUP_WEIGHT_DEFAULT;
}
 
static CPUSet *cgroup_context_allowed_cpus(CGroupContext *c, ManagerState state) {
        if (IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING, MANAGER_STOPPING) &&
            c->startup_cpuset_cpus.set)
                return &c->startup_cpuset_cpus;
        else
                return &c->cpuset_cpus;
}
 
static CPUSet *cgroup_context_allowed_mems(CGroupContext *c, ManagerState state) {
        if (IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING, MANAGER_STOPPING) &&
            c->startup_cpuset_mems.set)
                return &c->startup_cpuset_mems;
        else
                return &c->cpuset_mems;
}
 
usec_t cgroup_cpu_adjust_period(usec_t period, usec_t quota, usec_t resolution, usec_t max_period) {
        /* kernel uses a minimum resolution of 1ms, so both period and (quota * period)
         * need to be higher than that boundary. quota is specified in USecPerSec.
         * Additionally, period must be at most max_period. */
        assert(quota > 0);
 
        return MIN(MAX3(period, resolution, resolution * USEC_PER_SEC / quota), max_period);
}
 
static usec_t cgroup_cpu_adjust_period_and_log(Unit *u, usec_t period, usec_t quota) {
        usec_t new_period;
 
        assert(u);
 
        CGroupRuntime *crt = unit_get_cgroup_runtime(u);
        if (!crt)
                return USEC_INFINITY;
 
        if (quota == USEC_INFINITY)
                /* Always use default period for infinity quota. */
                return CGROUP_CPU_QUOTA_DEFAULT_PERIOD_USEC;
 
        if (period == USEC_INFINITY)
                /* Default period was requested. */
                period = CGROUP_CPU_QUOTA_DEFAULT_PERIOD_USEC;
 
        /* Clamp to interval [1ms, 1s] */
        new_period = cgroup_cpu_adjust_period(period, quota, USEC_PER_MSEC, USEC_PER_SEC);
 
        if (new_period != period) {
                log_unit_full(u, crt->warned_clamping_cpu_quota_period ? LOG_DEBUG : LOG_WARNING,
                              "Clamping CPU interval for cpu.max: period is now %s",
                              FORMAT_TIMESPAN(new_period, 1));
                crt->warned_clamping_cpu_quota_period = true;
        }
 
        return new_period;
}
 
static void cgroup_apply_cpu_weight(Unit *u, uint64_t weight) {
        char buf[DECIMAL_STR_MAX(uint64_t) + 2];
 
        if (weight == CGROUP_WEIGHT_IDLE)
                return;
        xsprintf(buf, "%" PRIu64 "\n", weight);
        (void) set_attribute_and_warn(u, "cpu.weight", buf);
}
 
static void cgroup_apply_cpu_idle(Unit *u, uint64_t weight) {
        int r;
        bool is_idle;
        const char *idle_val;
 
        assert(u);
 
        CGroupRuntime *crt = unit_get_cgroup_runtime(u);
        if (!crt || !crt->cgroup_path)
                return;
 
        is_idle = weight == CGROUP_WEIGHT_IDLE;
        idle_val = one_zero(is_idle);
        r = cg_set_attribute(crt->cgroup_path, "cpu.idle", idle_val);
        if (r < 0 && (r != -ENOENT || is_idle))
                log_unit_full_errno(u, LOG_LEVEL_CGROUP_WRITE(r), r, "Failed to set '%s' attribute on '%s' to '%s': %m",
                                    "cpu.idle", empty_to_root(crt->cgroup_path), idle_val);
}
 
static void cgroup_apply_cpu_quota(Unit *u, usec_t quota, usec_t period) {
        char buf[(DECIMAL_STR_MAX(usec_t) + 1) * 2 + 1];
 
        assert(u);
 
        period = cgroup_cpu_adjust_period_and_log(u, period, quota);
        if (quota != USEC_INFINITY)
                xsprintf(buf, USEC_FMT " " USEC_FMT "\n",
                         MAX(quota * period / USEC_PER_SEC, USEC_PER_MSEC), period);
        else
                xsprintf(buf, "max " USEC_FMT "\n", period);
        (void) set_attribute_and_warn(u, "cpu.max", buf);
}
 
static void cgroup_apply_cpuset(Unit *u, const char *name, const CPUSet *cpus) {
        _cleanup_free_ char *buf = NULL;
 
        buf = cpu_set_to_range_string(cpus);
        if (!buf) {
                log_oom();
                return;
        }
 
        (void) set_attribute_and_warn(u, name, buf);
}
 
static int cgroup_cpuset_partition_invalid(const char *partition) {
        _cleanup_free_ char *part_str = NULL, *invalid = NULL;
        int r;
 
        assert(partition);
 
        /* An invalid line looks like <partition> invalid (<reason>) */
        r = extract_many_words(&partition, /* separators= */ NULL, /* flags= */ 0, &part_str, &invalid);
        if (r < 0)
                return r;
        if (r < 2)
                return false;
 
        return streq_ptr(invalid, "invalid");
}
 
static void cgroup_apply_cpuset_partition(Unit *u, const char *name, const char *partition) {
        _cleanup_free_ char *buf = NULL;
        CGroupRuntime *crt;
        int r;
 
        assert(u);
        assert(name);
        assert(partition);
 
        if (set_attribute_and_warn(u, name, partition) < 0)
                return;
 
        /* We are writing and then reading back, crt is already checked while writing */
        crt = ASSERT_PTR(unit_get_cgroup_runtime(u));
 
        r = cg_get_attribute(crt->cgroup_path, name, &buf);
        if (r < 0) {
                log_unit_full_errno(u, LOG_LEVEL_CGROUP_WRITE(r), r, "Failed to read back '%s' attribute on '%s' as '%.*s': %m",
                                    name, empty_to_root(crt->cgroup_path), (int) strcspn(partition, NEWLINE), partition);
                return;
        }
 
        r = cgroup_cpuset_partition_invalid(buf);
        if (r < 0)
                log_unit_full_errno(u, LOG_LEVEL_CGROUP_WRITE(r), r, "Failed to read back '%s' attribute on '%s' as '%.*s': %m",
                                    name, empty_to_root(crt->cgroup_path), (int) strcspn(partition, NEWLINE), partition);
        else if (r)
                log_unit_warning(u, "Failed to set '%s' attribute on '%s' to '%.*s': %s",
                                 name, empty_to_root(crt->cgroup_path), (int) strcspn(partition, NEWLINE), partition, buf);
}
 
static bool cgroup_context_has_io_config(CGroupContext *c) {
        assert(c);
 
        return c->io_weight != CGROUP_WEIGHT_INVALID ||
                c->startup_io_weight != CGROUP_WEIGHT_INVALID ||
                c->io_device_weights ||
                c->io_device_latencies ||
                c->io_device_limits;
}
 
static uint64_t cgroup_context_io_weight(CGroupContext *c, ManagerState state) {
        if (IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING, MANAGER_STOPPING) &&
            c->startup_io_weight != CGROUP_WEIGHT_INVALID)
                return c->startup_io_weight;
        if (c->io_weight != CGROUP_WEIGHT_INVALID)
                return c->io_weight;
        return CGROUP_WEIGHT_DEFAULT;
}
 
static int set_bfq_weight(Unit *u, dev_t dev, uint64_t io_weight) {
        char buf[DECIMAL_STR_MAX(dev_t)*2+2+DECIMAL_STR_MAX(uint64_t)+STRLEN("\n")];
        uint64_t bfq_weight;
        int r;
 
        assert(u);
 
        CGroupRuntime *crt = unit_get_cgroup_runtime(u);
        if (!crt || !crt->cgroup_path)
                return -EOWNERDEAD;
 
        /* FIXME: drop this function when distro kernels properly support BFQ through "io.weight"
         * See also: https://github.com/systemd/systemd/pull/13335 and
         * https://github.com/torvalds/linux/commit/65752aef0a407e1ef17ec78a7fc31ba4e0b360f9. */
        /* Adjust to kernel range is 1..1000, the default is 100. */
        bfq_weight = BFQ_WEIGHT(io_weight);
 
        if (major(dev) > 0)
                xsprintf(buf, DEVNUM_FORMAT_STR " %" PRIu64 "\n", DEVNUM_FORMAT_VAL(dev), bfq_weight);
        else
                xsprintf(buf, "%" PRIu64 "\n", bfq_weight);
 
        r = cg_set_attribute(crt->cgroup_path, "io.bfq.weight", buf);
        if (r >= 0 && io_weight != bfq_weight)
                log_unit_debug(u, "%s=%" PRIu64 " scaled to io.bfq.weight=%" PRIu64,
                               major(dev) > 0 ? "IODeviceWeight" : "IOWeight",
                               io_weight, bfq_weight);
        return r;
}
 
static void cgroup_apply_io_device_weight(Unit *u, const char *dev_path, uint64_t io_weight) {
        char buf[DECIMAL_STR_MAX(dev_t)*2+2+DECIMAL_STR_MAX(uint64_t)+1];
        dev_t dev;
        int r, r1, r2;
 
        assert(u);
 
        CGroupRuntime *crt = unit_get_cgroup_runtime(u);
        if (!crt || !crt->cgroup_path)
                return;
 
        if (lookup_block_device(dev_path, &dev) < 0)
                return;
 
        r1 = set_bfq_weight(u, dev, io_weight);
 
        xsprintf(buf, DEVNUM_FORMAT_STR " %" PRIu64 "\n", DEVNUM_FORMAT_VAL(dev), io_weight);
        r2 = cg_set_attribute(crt->cgroup_path, "io.weight", buf);
 
        /* Look at the configured device, when both fail, prefer io.weight errno. */
        r = r2 == -EOPNOTSUPP ? r1 : r2;
 
        if (r < 0)
                log_unit_full_errno(u, LOG_LEVEL_CGROUP_WRITE(r),
                                    r, "Failed to set 'io[.bfq].weight' attribute on '%s' to '%.*s': %m",
                                    empty_to_root(crt->cgroup_path), (int) strcspn(buf, NEWLINE), buf);
}
 
static void cgroup_apply_io_device_latency(Unit *u, const char *dev_path, usec_t target) {
        char buf[DECIMAL_STR_MAX(dev_t)*2+2+7+DECIMAL_STR_MAX(uint64_t)+1];
        dev_t dev;
        int r;
 
        r = lookup_block_device(dev_path, &dev);
        if (r < 0)
                return;
 
        if (target != USEC_INFINITY)
                xsprintf(buf, DEVNUM_FORMAT_STR " target=%" PRIu64 "\n", DEVNUM_FORMAT_VAL(dev), target);
        else
                xsprintf(buf, DEVNUM_FORMAT_STR " target=max\n", DEVNUM_FORMAT_VAL(dev));
 
        (void) set_attribute_and_warn(u, "io.latency", buf);
}
 
static void cgroup_apply_io_device_limit(Unit *u, const char *dev_path, uint64_t *limits) {
        char limit_bufs[_CGROUP_IO_LIMIT_TYPE_MAX][DECIMAL_STR_MAX(uint64_t)],
             buf[DECIMAL_STR_MAX(dev_t)*2+2+(6+DECIMAL_STR_MAX(uint64_t)+1)*4];
        dev_t dev;
 
        if (lookup_block_device(dev_path, &dev) < 0)
                return;
 
        for (CGroupIOLimitType type = 0; type < _CGROUP_IO_LIMIT_TYPE_MAX; type++)
                if (limits[type] != cgroup_io_limit_defaults[type])
                        xsprintf(limit_bufs[type], "%" PRIu64, limits[type]);
                else
                        xsprintf(limit_bufs[type], "%s", limits[type] == CGROUP_LIMIT_MAX ? "max" : "0");
 
        xsprintf(buf, DEVNUM_FORMAT_STR " rbps=%s wbps=%s riops=%s wiops=%s\n", DEVNUM_FORMAT_VAL(dev),
                 limit_bufs[CGROUP_IO_RBPS_MAX], limit_bufs[CGROUP_IO_WBPS_MAX],
                 limit_bufs[CGROUP_IO_RIOPS_MAX], limit_bufs[CGROUP_IO_WIOPS_MAX]);
        (void) set_attribute_and_warn(u, "io.max", buf);
}
 
static bool cgroup_context_has_memory_config(CGroupContext *c) {
        assert(c);
 
        return c->memory_min > 0 ||
               c->memory_low > 0 || c->startup_memory_low_set ||
               c->memory_high != CGROUP_LIMIT_MAX || c->startup_memory_high_set ||
               c->memory_max != CGROUP_LIMIT_MAX || c->startup_memory_max_set ||
               c->memory_swap_max != CGROUP_LIMIT_MAX || c->startup_memory_swap_max_set ||
               c->memory_zswap_max != CGROUP_LIMIT_MAX || c->startup_memory_zswap_max_set;
}
 
static void cgroup_apply_memory_limit(Unit *u, const char *file, uint64_t v) {
        char buf[DECIMAL_STR_MAX(uint64_t) + 1] = "max\n";
 
        if (v != CGROUP_LIMIT_MAX)
                xsprintf(buf, "%" PRIu64 "\n", v);
 
        (void) set_attribute_and_warn(u, file, buf);
}
 
static void cgroup_apply_firewall(Unit *u) {
        assert(u);
 
        /* Best-effort: let's apply IP firewalling and/or accounting if that's enabled */
 
        if (bpf_firewall_compile(u) < 0)
                return;
 
        (void) bpf_firewall_load_custom(u);
        (void) bpf_firewall_install(u);
}
 
static void unit_modify_nft_set(Unit *u, bool add) {
        int r;
 
        assert(u);
 
        if (!MANAGER_IS_SYSTEM(u->manager))
                return;
 
        if (!UNIT_HAS_CGROUP_CONTEXT(u))
                return;
 
        CGroupRuntime *crt = unit_get_cgroup_runtime(u);
        if (!crt || crt->cgroup_id == 0)
                return;
 
        CGroupContext *c = ASSERT_PTR(unit_get_cgroup_context(u));
 
        FOREACH_ARRAY(nft_set, c->nft_set_context.sets, c->nft_set_context.n_sets) {
                if (nft_set->source != NFT_SET_SOURCE_CGROUP)
                        continue;
 
                if (!u->manager->nfnl) {
                        r = sd_nfnl_socket_open(&u->manager->nfnl);
                        if (r < 0)
                                return (void) log_once_errno(LOG_WARNING, r, "Failed to open NETLINK_NETFILTER socket, ignoring: %m");
                }
 
                uint64_t element = crt->cgroup_id;
 
                r = nft_set_element_modify_any(u->manager->nfnl, add, nft_set->nfproto, nft_set->table, nft_set->set, &element, sizeof(element));
                if (r < 0)
                        log_warning_errno(r, "Failed to %s NFT set entry: family %s, table %s, set %s, cgroup %" PRIu64 ", ignoring: %m",
                                          add? "add" : "delete", nfproto_to_string(nft_set->nfproto), nft_set->table, nft_set->set, crt->cgroup_id);
                else
                        log_debug("%s NFT set entry: family %s, table %s, set %s, cgroup %" PRIu64,
                                  add? "Added" : "Deleted", nfproto_to_string(nft_set->nfproto), nft_set->table, nft_set->set, crt->cgroup_id);
        }
}
 
static void cgroup_apply_socket_bind(Unit *u) {
        assert(u);
 
        (void) bpf_socket_bind_install(u);
}
 
static void cgroup_apply_restrict_network_interfaces(Unit *u) {
        assert(u);
 
        (void) bpf_restrict_ifaces_install(u);
}
 
static void cgroup_apply_bind_network_interface(Unit *u) {
        assert(u);
 
        (void) bpf_bind_network_interface_install(u);
}
 
static int cgroup_apply_devices(Unit *u) {
        _cleanup_(bpf_program_freep) BPFProgram *prog = NULL;
        CGroupContext *c;
        CGroupDevicePolicy policy;
        int r;
 
        assert_se(c = unit_get_cgroup_context(u));
 
        CGroupRuntime *crt = unit_get_cgroup_runtime(u);
        if (!crt || !crt->cgroup_path)
                return -EOWNERDEAD;
 
        policy = c->device_policy;
 
        r = bpf_devices_cgroup_init(&prog, policy, c->device_allow);
        if (r < 0)
                return log_unit_warning_errno(u, r, "Failed to initialize device control bpf program: %m");
 
        bool allow_list_static = policy == CGROUP_DEVICE_POLICY_CLOSED ||
                (policy == CGROUP_DEVICE_POLICY_AUTO && c->device_allow);
 
        bool any = false;
        if (allow_list_static) {
                r = bpf_devices_allow_list_static(prog, crt->cgroup_path);
                if (r > 0)
                        any = true;
        }
 
        LIST_FOREACH(device_allow, a, c->device_allow) {
                const char *val;
 
                if (a->permissions == 0)
                        continue;
 
                if (path_startswith(a->path, "/dev/"))
                        r = bpf_devices_allow_list_device(prog, crt->cgroup_path, a->path, a->permissions);
                else if ((val = startswith(a->path, "block-")))
                        r = bpf_devices_allow_list_major(prog, crt->cgroup_path, val, 'b', a->permissions);
                else if ((val = startswith(a->path, "char-")))
                        r = bpf_devices_allow_list_major(prog, crt->cgroup_path, val, 'c', a->permissions);
                else {
                        log_unit_debug(u, "Ignoring device '%s' while writing cgroup attribute.", a->path);
                        continue;
                }
 
                if (r > 0)
                        any = true;
        }
 
        if (prog && !any) {
                log_unit_warning(u, "No devices matched by device filter.");
 
                /* The kernel verifier would reject a program we would build with the normal intro and outro
                   but no allow-listing rules (outro would contain an unreachable instruction for successful
                   return). */
                policy = CGROUP_DEVICE_POLICY_STRICT;
        }
 
        r = bpf_devices_apply_policy(&prog, policy, any, crt->cgroup_path, &crt->bpf_device_control_installed);
        if (r < 0) {
                static bool warned = false;
 
                log_full_errno(warned ? LOG_DEBUG : LOG_WARNING, r,
                               "Unit %s configures device ACL, but the local system doesn't seem to support the BPF-based device controller.\n"
                               "Proceeding WITHOUT applying ACL (all devices will be accessible)!\n"
                               "(This warning is only shown for the first loaded unit using device ACL.)", u->id);
 
                warned = true;
        }
        return r;
}
 
static void set_io_weight(Unit *u, uint64_t weight) {
        char buf[STRLEN("default \n")+DECIMAL_STR_MAX(uint64_t)];
 
        assert(u);
 
        (void) set_bfq_weight(u, makedev(0, 0), weight);
 
        xsprintf(buf, "default %" PRIu64 "\n", weight);
        (void) set_attribute_and_warn(u, "io.weight", buf);
}
 
static void cgroup_context_apply(
                Unit *u,
                CGroupMask apply_mask,
                ManagerState state) {
 
        bool is_host_root, is_local_root;
        CGroupContext *c;
        int r;
 
        assert(u);
 
        /* Nothing to do? Exit early! */
        if (apply_mask == 0)
                return;
 
        /* Some cgroup attributes are not supported on the host root cgroup, hence silently ignore them here. And other
         * attributes should only be managed for cgroups further down the tree. */
        is_local_root = unit_has_name(u, SPECIAL_ROOT_SLICE);
        is_host_root = unit_has_host_root_cgroup(u);
 
        assert_se(c = unit_get_cgroup_context(u));
 
        CGroupRuntime *crt = unit_get_cgroup_runtime(u);
        if (!crt || !crt->cgroup_path)
                return;
 
        /* We generally ignore errors caused by read-only mounted cgroup trees (assuming we are running in a container
         * then), and missing cgroups, i.e. EROFS and ENOENT. */
 
        /* These attributes don't exist on the host cgroup root. */
        if ((apply_mask & CGROUP_MASK_CPU) && !is_local_root) {
                uint64_t weight;
 
                if (cgroup_context_has_cpu_weight(c))
                        weight = cgroup_context_cpu_weight(c, state);
                else
                        weight = CGROUP_WEIGHT_DEFAULT;
 
                cgroup_apply_cpu_idle(u, weight);
                cgroup_apply_cpu_weight(u, weight);
                cgroup_apply_cpu_quota(u, c->cpu_quota_per_sec_usec, c->cpu_quota_period_usec);
        }
 
        if ((apply_mask & CGROUP_MASK_CPUSET) && !is_local_root) {
                cgroup_apply_cpuset(u, "cpuset.cpus", cgroup_context_allowed_cpus(c, state));
                cgroup_apply_cpuset(u, "cpuset.mems", cgroup_context_allowed_mems(c, state));
 
                if (c->cpuset_partition >= 0)
                        cgroup_apply_cpuset_partition(u, "cpuset.cpus.partition", cpuset_partition_to_string(c->cpuset_partition));
        }
 
        /* The 'io' controller attributes are not exported on the host's root cgroup (being a pure cgroup v2
         * controller), and in case of containers we want to leave control of these attributes to the container manager
         * (and we couldn't access that stuff anyway, even if we tried if proper delegation is used). */
        if ((apply_mask & CGROUP_MASK_IO) && !is_local_root) {
                bool has_io;
                uint64_t weight;
 
                has_io = cgroup_context_has_io_config(c);
 
                if (has_io)
                        weight = cgroup_context_io_weight(c, state);
                else
                        weight = CGROUP_WEIGHT_DEFAULT;
 
                set_io_weight(u, weight);
 
                if (has_io) {
                        LIST_FOREACH(device_weights, w, c->io_device_weights)
                                cgroup_apply_io_device_weight(u, w->path, w->weight);
 
                        LIST_FOREACH(device_limits, limit, c->io_device_limits)
                                cgroup_apply_io_device_limit(u, limit->path, limit->limits);
 
                        LIST_FOREACH(device_latencies, latency, c->io_device_latencies)
                                cgroup_apply_io_device_latency(u, latency->path, latency->target_usec);
                }
        }
 
        /* 'memory' attributes do not exist on the root cgroup. */
        if ((apply_mask & CGROUP_MASK_MEMORY) && !is_local_root) {
                uint64_t low = CGROUP_LIMIT_MIN, max = CGROUP_LIMIT_MAX, swap_max = CGROUP_LIMIT_MAX, zswap_max = CGROUP_LIMIT_MAX, high = CGROUP_LIMIT_MAX;
 
                if (cgroup_context_has_memory_config(c)) {
                        bool startup = IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING, MANAGER_STOPPING);
 
                        low = startup && c->startup_memory_low_set ? c->startup_memory_low : c->memory_low;
                        high = startup && c->startup_memory_high_set ? c->startup_memory_high : c->memory_high;
                        max = startup && c->startup_memory_max_set ? c->startup_memory_max : c->memory_max;
                        swap_max = startup && c->startup_memory_swap_max_set ? c->startup_memory_swap_max : c->memory_swap_max;
                        zswap_max = startup && c->startup_memory_zswap_max_set ? c->startup_memory_zswap_max : c->memory_zswap_max;
                }
 
                cgroup_apply_memory_limit(u, "memory.min", c->memory_min);
                cgroup_apply_memory_limit(u, "memory.low", low);
                cgroup_apply_memory_limit(u, "memory.high", high);
                cgroup_apply_memory_limit(u, "memory.max", max);
                cgroup_apply_memory_limit(u, "memory.swap.max", swap_max);
                cgroup_apply_memory_limit(u, "memory.zswap.max", zswap_max);
 
                (void) set_attribute_and_warn(u, "memory.oom.group", one_zero(c->memory_oom_group));
                (void) set_attribute_and_warn(u, "memory.zswap.writeback", one_zero(c->memory_zswap_writeback));
        }
 
        if (apply_mask & CGROUP_MASK_PIDS) {
 
                if (is_host_root) {
                        /* So, the "pids" controller does not expose anything on the root cgroup, in order not to
                         * replicate knobs exposed elsewhere needlessly. We abstract this away here however, and when
                         * the knobs of the root cgroup are modified propagate this to the relevant sysctls. There's a
                         * non-obvious asymmetry however: unlike the cgroup properties we don't really want to take
                         * exclusive ownership of the sysctls, but we still want to honour things if the user sets
                         * limits. Hence we employ sort of a one-way strategy: when the user sets a bounded limit
                         * through us it counts. When the user afterwards unsets it again (i.e. sets it to unbounded)
                         * it also counts. But if the user never set a limit through us (i.e. we are the default of
                         * "unbounded") we leave things unmodified. For this we manage a global boolean that we turn on
                         * the first time we set a limit. Note that this boolean is flushed out on manager reload,
                         * which is desirable so that there's an official way to release control of the sysctl from
                         * systemd: set the limit to unbounded and reload. */
 
                        if (cgroup_tasks_max_isset(&c->tasks_max)) {
                                u->manager->sysctl_pid_max_changed = true;
                                r = procfs_tasks_set_limit(cgroup_tasks_max_resolve(&c->tasks_max));
                        } else if (u->manager->sysctl_pid_max_changed)
                                r = procfs_tasks_set_limit(TASKS_MAX);
                        else
                                r = 0;
                        if (r < 0)
                                log_unit_full_errno(u, LOG_LEVEL_CGROUP_WRITE(r), r,
                                                    "Failed to write to tasks limit sysctls: %m");
                }
 
                /* The attribute itself is not available on the host root cgroup, and in the container case we want to
                 * leave it for the container manager. */
                if (!is_local_root) {
                        if (cgroup_tasks_max_isset(&c->tasks_max)) {
                                char buf[DECIMAL_STR_MAX(uint64_t) + 1];
 
                                xsprintf(buf, "%" PRIu64 "\n", cgroup_tasks_max_resolve(&c->tasks_max));
                                (void) set_attribute_and_warn(u, "pids.max", buf);
                        } else
                                (void) set_attribute_and_warn(u, "pids.max", "max\n");
                }
        }
 
        /* On cgroup v2 we can apply BPF everywhere. */
        if (apply_mask & CGROUP_MASK_BPF_DEVICES)
                (void) cgroup_apply_devices(u);
 
        if (apply_mask & CGROUP_MASK_BPF_FIREWALL)
                cgroup_apply_firewall(u);
 
        if (apply_mask & CGROUP_MASK_BPF_FOREIGN)
                (void) bpf_foreign_install(u);
 
        if (apply_mask & CGROUP_MASK_BPF_SOCKET_BIND)
                cgroup_apply_socket_bind(u);
 
        if (apply_mask & CGROUP_MASK_BPF_RESTRICT_NETWORK_INTERFACES)
                cgroup_apply_restrict_network_interfaces(u);
 
        if (apply_mask & CGROUP_MASK_BPF_BIND_NETWORK_INTERFACE)
                cgroup_apply_bind_network_interface(u);
 
        unit_modify_nft_set(u, /* add= */ true);
}
 
static bool unit_get_needs_bpf_firewall(Unit *u) {
        CGroupContext *c;
        assert(u);
 
        c = unit_get_cgroup_context(u);
        if (!c)
                return false;
 
        if (c->ip_accounting ||
            !set_isempty(c->ip_address_allow) ||
            !set_isempty(c->ip_address_deny) ||
            c->ip_filters_ingress ||
            c->ip_filters_egress)
                return true;
 
        /* If any parent slice has an IP access list defined, it applies too */
        for (Unit *p = UNIT_GET_SLICE(u); p; p = UNIT_GET_SLICE(p)) {
                c = unit_get_cgroup_context(p);
                if (!c)
                        return false;
 
                if (!set_isempty(c->ip_address_allow) ||
                    !set_isempty(c->ip_address_deny))
                        return true;
        }
 
        return false;
}
 
static bool unit_get_needs_bpf_foreign_program(Unit *u) {
        CGroupContext *c;
        assert(u);
 
        c = unit_get_cgroup_context(u);
        if (!c)
                return false;
 
        return !!c->bpf_foreign_programs;
}
 
static bool unit_get_needs_socket_bind(Unit *u) {
        CGroupContext *c;
        assert(u);
 
        c = unit_get_cgroup_context(u);
        if (!c)
                return false;
 
        return c->socket_bind_allow || c->socket_bind_deny;
}
 
static bool unit_get_needs_restrict_network_interfaces(Unit *u) {
        CGroupContext *c;
        assert(u);
 
        c = unit_get_cgroup_context(u);
        if (!c)
                return false;
 
        return !set_isempty(c->restrict_network_interfaces);
}
 
static bool unit_get_needs_bind_network_interface(Unit *u) {
        CGroupContext *c;
        assert(u);
 
        c = unit_get_cgroup_context(u);
        if (!c)
                return false;
 
        return c->bind_network_interface;
}
 
static CGroupMask unit_get_cgroup_mask(Unit *u) {
        CGroupMask mask = 0;
        CGroupContext *c;
 
        assert(u);
 
        assert_se(c = unit_get_cgroup_context(u));
 
        /* Figure out which controllers we need, based on the cgroup context object */
 
        if (cgroup_context_has_cpu_weight(c) ||
            c->cpu_quota_per_sec_usec != USEC_INFINITY)
                mask |= CGROUP_MASK_CPU;
 
        if (cgroup_context_has_allowed_cpus(c) || cgroup_context_has_allowed_mems(c))
                mask |= CGROUP_MASK_CPUSET;
 
        if (c->io_accounting ||
            cgroup_context_has_io_config(c))
                mask |= CGROUP_MASK_IO;
 
        if (c->memory_accounting ||
            cgroup_context_has_memory_config(c))
                mask |= CGROUP_MASK_MEMORY;
 
        if (cgroup_context_has_device_policy(c))
                mask |= CGROUP_MASK_BPF_DEVICES;
 
        if (c->tasks_accounting ||
            cgroup_tasks_max_isset(&c->tasks_max))
                mask |= CGROUP_MASK_PIDS;
 
        return mask;
}
 
static CGroupMask unit_get_bpf_mask(Unit *u) {
        CGroupMask mask = 0;
 
        /* Figure out which controllers we need, based on the cgroup context, possibly taking into account children
         * too. */
 
        if (unit_get_needs_bpf_firewall(u))
                mask |= CGROUP_MASK_BPF_FIREWALL;
 
        if (unit_get_needs_bpf_foreign_program(u))
                mask |= CGROUP_MASK_BPF_FOREIGN;
 
        if (unit_get_needs_socket_bind(u))
                mask |= CGROUP_MASK_BPF_SOCKET_BIND;
 
        if (unit_get_needs_restrict_network_interfaces(u))
                mask |= CGROUP_MASK_BPF_RESTRICT_NETWORK_INTERFACES;
 
        if (unit_get_needs_bind_network_interface(u))
                mask |= CGROUP_MASK_BPF_BIND_NETWORK_INTERFACE;
 
        return mask;
}
 
CGroupMask unit_get_own_mask(Unit *u) {
        CGroupContext *c;
 
        /* Returns the mask of controllers the unit needs for itself. If a unit is not properly loaded, return an empty
         * mask, as we shouldn't reflect it in the cgroup hierarchy then. */
 
        if (u->load_state != UNIT_LOADED)
                return 0;
 
        c = unit_get_cgroup_context(u);
        if (!c)
                return 0;
 
        return unit_get_cgroup_mask(u) | unit_get_bpf_mask(u) | unit_get_delegate_mask(u);
}
 
CGroupMask unit_get_delegate_mask(Unit *u) {
        CGroupContext *c;
 
        /* If delegation is turned on, then turn on selected controllers.
         *
         * Note that on the unified hierarchy it is safe to delegate controllers to unprivileged services. */
 
        if (!unit_cgroup_delegate(u))
                return 0;
 
        assert_se(c = unit_get_cgroup_context(u));
        return c->delegate_controllers;
}
 
static CGroupMask unit_get_subtree_mask(Unit *u) {
 
        /* Returns the mask of this subtree, meaning of the group
         * itself and its children. */
 
        return unit_get_own_mask(u) | unit_get_members_mask(u);
}
 
CGroupMask unit_get_members_mask(Unit *u) {
        assert(u);
 
        /* Returns the mask of controllers all of the unit's children require, merged */
 
        CGroupRuntime *crt = unit_get_cgroup_runtime(u);
        if (crt && crt->cgroup_members_mask_valid)
                return crt->cgroup_members_mask; /* Use cached value if possible */
 
        CGroupMask m = 0;
        if (u->type == UNIT_SLICE) {
                Unit *member;
 
                UNIT_FOREACH_DEPENDENCY(member, u, UNIT_ATOM_SLICE_OF)
                        m |= unit_get_subtree_mask(member); /* note that this calls ourselves again, for the children */
        }
 
        if (crt) {
                crt->cgroup_members_mask = m;
                crt->cgroup_members_mask_valid = true;
        }
 
        return m;
}
 
CGroupMask unit_get_siblings_mask(Unit *u) {
        Unit *slice;
        assert(u);
 
        /* Returns the mask of controllers all of the unit's siblings
         * require, i.e. the members mask of the unit's parent slice
         * if there is one. */
 
        slice = UNIT_GET_SLICE(u);
        if (slice)
                return unit_get_members_mask(slice);
 
        return unit_get_subtree_mask(u); /* we are the top-level slice */
}
 
static CGroupMask unit_get_disable_mask(Unit *u) {
        CGroupContext *c;
 
        c = unit_get_cgroup_context(u);
        if (!c)
                return 0;
 
        return c->disable_controllers;
}
 
static CGroupMask unit_get_ancestor_disable_mask(Unit *u) {
        CGroupMask mask;
        Unit *slice;
 
        assert(u);
        mask = unit_get_disable_mask(u);
 
        /* Returns the mask of controllers which are marked as forcibly
         * disabled in any ancestor unit or the unit in question. */
 
        slice = UNIT_GET_SLICE(u);
        if (slice)
                mask |= unit_get_ancestor_disable_mask(slice);
 
        return mask;
}
 
CGroupMask unit_get_target_mask(Unit *u) {
        CGroupMask own_mask, mask;
 
        /* This returns the cgroup mask of all controllers to enable for a specific cgroup, i.e. everything
         * it needs itself, plus all that its children need, plus all that its siblings need. This is
         * primarily useful on the legacy cgroup hierarchy, where we need to duplicate each cgroup in each
         * hierarchy that shall be enabled for it. */
 
        own_mask = unit_get_own_mask(u);
 
        if (own_mask & CGROUP_MASK_BPF_FIREWALL & ~u->manager->cgroup_supported)
                emit_bpf_firewall_warning(u);
 
        mask = own_mask | unit_get_members_mask(u) | unit_get_siblings_mask(u);
 
        mask &= u->manager->cgroup_supported;
        mask &= ~unit_get_ancestor_disable_mask(u);
 
        return mask;
}
 
CGroupMask unit_get_enable_mask(Unit *u) {
        CGroupMask mask;
 
        /* This returns the cgroup mask of all controllers to enable
         * for the children of a specific cgroup. This is primarily
         * useful for the unified cgroup hierarchy, where each cgroup
         * controls which controllers are enabled for its children. */
 
        mask = unit_get_members_mask(u);
        mask &= u->manager->cgroup_supported;
        mask &= ~unit_get_ancestor_disable_mask(u);
 
        return mask;
}
 
void unit_invalidate_cgroup_members_masks(Unit *u) {
        Unit *slice;
 
        assert(u);
 
        CGroupRuntime *crt = unit_get_cgroup_runtime(u);
        if (!crt)
                return;
 
        /* Recurse invalidate the member masks cache all the way up the tree */
        crt->cgroup_members_mask_valid = false;
 
        slice = UNIT_GET_SLICE(u);
        if (slice)
                unit_invalidate_cgroup_members_masks(slice);
}
 
static int unit_default_cgroup_path(const Unit *u, char **ret) {
        _cleanup_free_ char *p = NULL;
        int r;
 
        assert(u);
        assert(ret);
 
        if (unit_has_name(u, SPECIAL_ROOT_SLICE))
                p = strdup(u->manager->cgroup_root);
        else {
                _cleanup_free_ char *escaped = NULL, *slice_path = NULL;
                Unit *slice;
 
                slice = UNIT_GET_SLICE(u);
                if (slice && !unit_has_name(slice, SPECIAL_ROOT_SLICE)) {
                        r = cg_slice_to_path(slice->id, &slice_path);
                        if (r < 0)
                                return r;
                }
 
                r = cg_escape(u->id, &escaped);
                if (r < 0)
                        return r;
 
                p = path_join(empty_to_root(u->manager->cgroup_root), slice_path, escaped);
        }
        if (!p)
                return -ENOMEM;
 
        *ret = TAKE_PTR(p);
        return 0;
}
 
static int unit_set_cgroup_path(Unit *u, const char *path) {
        _cleanup_free_ char *p = NULL;
        CGroupRuntime *crt;
        int r;
 
        assert(u);
 
        crt = unit_get_cgroup_runtime(u);
        if (crt && streq_ptr(crt->cgroup_path, path))
                return 0;
 
        unit_release_cgroup(u, /* drop_cgroup_runtime= */ true);
 
        crt = unit_setup_cgroup_runtime(u);
        if (!crt)
                return -ENOMEM;
 
        if (path) {
                p = strdup(path);
                if (!p)
                        return -ENOMEM;
 
                r = hashmap_put(u->manager->cgroup_unit, p, u);
                if (r < 0)
                        return r;
        }
 
        assert(!crt->cgroup_path);
        crt->cgroup_path = TAKE_PTR(p);
 
        return 1;
}
 
int unit_get_cgroup_path_with_fallback(const Unit *u, char **ret) {