^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Process number limiting controller for cgroups.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Used to allow a cgroup hierarchy to stop any new processes from fork()ing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * after a certain limit is reached.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Since it is trivial to hit the task limit without hitting any kmemcg limits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * in place, PIDs are a fundamental resource. As such, PID exhaustion must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * preventable in the scope of a cgroup hierarchy by allowing resource limiting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * of the number of tasks in a cgroup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * In order to use the `pids` controller, set the maximum number of tasks in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * pids.max (this is not available in the root cgroup for obvious reasons). The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * number of processes currently in the cgroup is given by pids.current.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * Organisational operations are not blocked by cgroup policies, so it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * possible to have pids.current > pids.max. However, it is not possible to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * violate a cgroup policy through fork(). fork() will return -EAGAIN if forking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * would cause a cgroup policy to be violated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * To set a cgroup to have no limit, set pids.max to "max". This is the default
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * for all new cgroups (N.B. that PID limits are hierarchical, so the most
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * stringent limit in the hierarchy is followed).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * pids.current tracks all child cgroup hierarchies, so parent/pids.current is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * a superset of parent/child/pids.current.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * Copyright (C) 2015 Aleksa Sarai <cyphar@cyphar.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/threads.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/cgroup.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <linux/sched/task.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define PIDS_MAX (PID_MAX_LIMIT + 1ULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define PIDS_MAX_STR "max"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct pids_cgroup {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct cgroup_subsys_state css;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * Use 64-bit types so that we can safely represent "max" as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * %PIDS_MAX = (%PID_MAX_LIMIT + 1).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) atomic64_t counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) atomic64_t limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /* Handle for "pids.events" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct cgroup_file events_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /* Number of times fork failed because limit was hit. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) atomic64_t events_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static struct pids_cgroup *css_pids(struct cgroup_subsys_state *css)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return container_of(css, struct pids_cgroup, css);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static struct pids_cgroup *parent_pids(struct pids_cgroup *pids)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return css_pids(pids->css.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static struct cgroup_subsys_state *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) pids_css_alloc(struct cgroup_subsys_state *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct pids_cgroup *pids;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) pids = kzalloc(sizeof(struct pids_cgroup), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (!pids)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) atomic64_set(&pids->counter, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) atomic64_set(&pids->limit, PIDS_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) atomic64_set(&pids->events_limit, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return &pids->css;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static void pids_css_free(struct cgroup_subsys_state *css)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) kfree(css_pids(css));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * pids_cancel - uncharge the local pid count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * @pids: the pid cgroup state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * @num: the number of pids to cancel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * This function will WARN if the pid count goes under 0, because such a case is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * a bug in the pids controller proper.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static void pids_cancel(struct pids_cgroup *pids, int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * A negative count (or overflow for that matter) is invalid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * and indicates a bug in the `pids` controller proper.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) WARN_ON_ONCE(atomic64_add_negative(-num, &pids->counter));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * pids_uncharge - hierarchically uncharge the pid count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * @pids: the pid cgroup state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * @num: the number of pids to uncharge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static void pids_uncharge(struct pids_cgroup *pids, int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct pids_cgroup *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) for (p = pids; parent_pids(p); p = parent_pids(p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) pids_cancel(p, num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * pids_charge - hierarchically charge the pid count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * @pids: the pid cgroup state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * @num: the number of pids to charge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * This function does *not* follow the pid limit set. It cannot fail and the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * pid count may exceed the limit. This is only used for reverting failed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * attaches, where there is no other way out than violating the limit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static void pids_charge(struct pids_cgroup *pids, int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct pids_cgroup *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) for (p = pids; parent_pids(p); p = parent_pids(p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) atomic64_add(num, &p->counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * pids_try_charge - hierarchically try to charge the pid count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * @pids: the pid cgroup state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * @num: the number of pids to charge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * This function follows the set limit. It will fail if the charge would cause
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * the new value to exceed the hierarchical limit. Returns 0 if the charge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * succeeded, otherwise -EAGAIN.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static int pids_try_charge(struct pids_cgroup *pids, int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct pids_cgroup *p, *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) for (p = pids; parent_pids(p); p = parent_pids(p)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) int64_t new = atomic64_add_return(num, &p->counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) int64_t limit = atomic64_read(&p->limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * Since new is capped to the maximum number of pid_t, if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * p->limit is %PIDS_MAX then we know that this test will never
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (new > limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) goto revert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) revert:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) for (q = pids; q != p; q = parent_pids(q))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) pids_cancel(q, num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) pids_cancel(p, num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static int pids_can_attach(struct cgroup_taskset *tset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct task_struct *task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct cgroup_subsys_state *dst_css;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) cgroup_taskset_for_each(task, dst_css, tset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct pids_cgroup *pids = css_pids(dst_css);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct cgroup_subsys_state *old_css;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct pids_cgroup *old_pids;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * No need to pin @old_css between here and cancel_attach()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * because cgroup core protects it from being freed before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * the migration completes or fails.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) old_css = task_css(task, pids_cgrp_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) old_pids = css_pids(old_css);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) pids_charge(pids, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) pids_uncharge(old_pids, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static void pids_cancel_attach(struct cgroup_taskset *tset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) struct task_struct *task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) struct cgroup_subsys_state *dst_css;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) cgroup_taskset_for_each(task, dst_css, tset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct pids_cgroup *pids = css_pids(dst_css);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) struct cgroup_subsys_state *old_css;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) struct pids_cgroup *old_pids;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) old_css = task_css(task, pids_cgrp_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) old_pids = css_pids(old_css);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) pids_charge(old_pids, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) pids_uncharge(pids, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * task_css_check(true) in pids_can_fork() and pids_cancel_fork() relies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * on cgroup_threadgroup_change_begin() held by the copy_process().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static int pids_can_fork(struct task_struct *task, struct css_set *cset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) struct cgroup_subsys_state *css;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct pids_cgroup *pids;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (cset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) css = cset->subsys[pids_cgrp_id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) css = task_css_check(current, pids_cgrp_id, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) pids = css_pids(css);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) err = pids_try_charge(pids, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) /* Only log the first time events_limit is incremented. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (atomic64_inc_return(&pids->events_limit) == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) pr_info("cgroup: fork rejected by pids controller in ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) pr_cont_cgroup_path(css->cgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) pr_cont("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) cgroup_file_notify(&pids->events_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static void pids_cancel_fork(struct task_struct *task, struct css_set *cset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) struct cgroup_subsys_state *css;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct pids_cgroup *pids;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (cset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) css = cset->subsys[pids_cgrp_id];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) css = task_css_check(current, pids_cgrp_id, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) pids = css_pids(css);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) pids_uncharge(pids, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static void pids_release(struct task_struct *task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) struct pids_cgroup *pids = css_pids(task_css(task, pids_cgrp_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) pids_uncharge(pids, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) static ssize_t pids_max_write(struct kernfs_open_file *of, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) size_t nbytes, loff_t off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct cgroup_subsys_state *css = of_css(of);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct pids_cgroup *pids = css_pids(css);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) int64_t limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) buf = strstrip(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (!strcmp(buf, PIDS_MAX_STR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) limit = PIDS_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) goto set_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) err = kstrtoll(buf, 0, &limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (limit < 0 || limit >= PIDS_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) set_limit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * Limit updates don't need to be mutex'd, since it isn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * critical that any racing fork()s follow the new limit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) atomic64_set(&pids->limit, limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static int pids_max_show(struct seq_file *sf, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) struct cgroup_subsys_state *css = seq_css(sf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) struct pids_cgroup *pids = css_pids(css);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) int64_t limit = atomic64_read(&pids->limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (limit >= PIDS_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) seq_printf(sf, "%s\n", PIDS_MAX_STR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) seq_printf(sf, "%lld\n", limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) static s64 pids_current_read(struct cgroup_subsys_state *css,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) struct cftype *cft)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) struct pids_cgroup *pids = css_pids(css);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return atomic64_read(&pids->counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) static int pids_events_show(struct seq_file *sf, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) struct pids_cgroup *pids = css_pids(seq_css(sf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) seq_printf(sf, "max %lld\n", (s64)atomic64_read(&pids->events_limit));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) static struct cftype pids_files[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) .name = "max",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) .write = pids_max_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) .seq_show = pids_max_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) .flags = CFTYPE_NOT_ON_ROOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) .name = "current",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) .read_s64 = pids_current_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) .flags = CFTYPE_NOT_ON_ROOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) .name = "events",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) .seq_show = pids_events_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) .file_offset = offsetof(struct pids_cgroup, events_file),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) .flags = CFTYPE_NOT_ON_ROOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) { } /* terminate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) struct cgroup_subsys pids_cgrp_subsys = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) .css_alloc = pids_css_alloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) .css_free = pids_css_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) .can_attach = pids_can_attach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) .cancel_attach = pids_cancel_attach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) .can_fork = pids_can_fork,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) .cancel_fork = pids_cancel_fork,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) .release = pids_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) .legacy_cftypes = pids_files,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) .dfl_cftypes = pids_files,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) .threaded = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) };