Skip to content

Commit 1a89a69

Browse files
committed
Merge tag 'kernel-6.14-rc1.pid' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
Pull pid_max namespacing update from Christian Brauner: "The pid_max sysctl is a global value. For a long time the default value has been 65535 and during the pidfd dicussions Linus proposed to bump pid_max by default. Based on this discussion systemd started bumping pid_max to 2^22. So all new systems now run with a very high pid_max limit with some distros having also backported that change. The decision to bump pid_max is obviously correct. It just doesn't make a lot of sense nowadays to enforce such a low pid number. There's sufficient tooling to make selecting specific processes without typing really large pid numbers available. In any case, there are workloads that have expections about how large pid numbers they accept. Either for historical reasons or architectural reasons. One concreate example is the 32-bit version of Android's bionic libc which requires pid numbers less than 65536. There are workloads where it is run in a 32-bit container on a 64-bit kernel. If the host has a pid_max value greater than 65535 the libc will abort thread creation because of size assumptions of pthread_mutex_t. That's a fairly specific use-case however, in general specific workloads that are moved into containers running on a host with a new kernel and a new systemd can run into issues with large pid_max values. Obviously making assumptions about the size of the allocated pid is suboptimal but we have userspace that does it. Of course, giving containers the ability to restrict the number of processes in their respective pid namespace indepent of the global limit through pid_max is something desirable in itself and comes in handy in general. Independent of motivating use-cases the existence of pid namespaces makes this also a good semantical extension and there have been prior proposals pushing in a similar direction. The trick here is to minimize the risk of regressions which I think is doable. The fact that pid namespaces are hierarchical will help us here. What we mostly care about is that when the host sets a low pid_max limit, say (crazy number) 100 that no descendant pid namespace can allocate a higher pid number in its namespace. Since pid allocation is hierarchial this can be ensured by checking each pid allocation against the pid namespace's pid_max limit. This means if the allocation in the descendant pid namespace succeeds, the ancestor pid namespace can reject it. If the ancestor pid namespace has a higher limit than the descendant pid namespace the descendant pid namespace will reject the pid allocation. The ancestor pid namespace will obviously not care about this. All in all this means pid_max continues to enforce a system wide limit on the number of processes but allows pid namespaces sufficient leeway in handling workloads with assumptions about pid values and allows containers to restrict the number of processes in a pid namespace through the pid_max interface" * tag 'kernel-6.14-rc1.pid' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs: tests/pid_namespace: add pid_max tests pid: allow pid_max to be set per pid namespace
2 parents 37c12fc + c625aa2 commit 1a89a69

File tree

11 files changed

+521
-36
lines changed

11 files changed

+521
-36
lines changed

include/linux/pid.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,6 @@ extern void exchange_tids(struct task_struct *task, struct task_struct *old);
108108
extern void transfer_pid(struct task_struct *old, struct task_struct *new,
109109
enum pid_type);
110110

111-
extern int pid_max;
112-
extern int pid_max_min, pid_max_max;
113-
114111
/*
115112
* look up a PID in the hash table. Must be called with the tasklist_lock
116113
* or rcu_read_lock() held.

include/linux/pid_namespace.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct pid_namespace {
3030
struct task_struct *child_reaper;
3131
struct kmem_cache *pid_cachep;
3232
unsigned int level;
33+
int pid_max;
3334
struct pid_namespace *parent;
3435
#ifdef CONFIG_BSD_PROCESS_ACCT
3536
struct fs_pin *bacct;
@@ -38,9 +39,14 @@ struct pid_namespace {
3839
struct ucounts *ucounts;
3940
int reboot; /* group exit code if this pidns was rebooted */
4041
struct ns_common ns;
41-
#if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE)
42+
struct work_struct work;
43+
#ifdef CONFIG_SYSCTL
44+
struct ctl_table_set set;
45+
struct ctl_table_header *sysctls;
46+
#if defined(CONFIG_MEMFD_CREATE)
4247
int memfd_noexec_scope;
4348
#endif
49+
#endif
4450
} __randomize_layout;
4551

4652
extern struct pid_namespace init_pid_ns;
@@ -117,6 +123,8 @@ static inline int reboot_pid_ns(struct pid_namespace *pid_ns, int cmd)
117123
extern struct pid_namespace *task_active_pid_ns(struct task_struct *tsk);
118124
void pidhash_init(void);
119125
void pid_idr_init(void);
126+
int register_pidns_sysctls(struct pid_namespace *pidns);
127+
void unregister_pidns_sysctls(struct pid_namespace *pidns);
120128

121129
static inline bool task_is_in_init_pid_ns(struct task_struct *tsk)
122130
{

kernel/pid.c

Lines changed: 118 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,8 @@ struct pid init_struct_pid = {
6161
}, }
6262
};
6363

64-
int pid_max = PID_MAX_DEFAULT;
65-
66-
int pid_max_min = RESERVED_PIDS + 1;
67-
int pid_max_max = PID_MAX_LIMIT;
64+
static int pid_max_min = RESERVED_PIDS + 1;
65+
static int pid_max_max = PID_MAX_LIMIT;
6866

6967
/*
7068
* PID-map pages start out as NULL, they get allocated upon
@@ -83,6 +81,7 @@ struct pid_namespace init_pid_ns = {
8381
#ifdef CONFIG_PID_NS
8482
.ns.ops = &pidns_operations,
8583
#endif
84+
.pid_max = PID_MAX_DEFAULT,
8685
#if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE)
8786
.memfd_noexec_scope = MEMFD_NOEXEC_SCOPE_EXEC,
8887
#endif
@@ -191,6 +190,7 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid,
191190

192191
for (i = ns->level; i >= 0; i--) {
193192
int tid = 0;
193+
int pid_max = READ_ONCE(tmp->pid_max);
194194

195195
if (set_tid_size) {
196196
tid = set_tid[ns->level - i];
@@ -644,17 +644,118 @@ SYSCALL_DEFINE2(pidfd_open, pid_t, pid, unsigned int, flags)
644644
return fd;
645645
}
646646

647+
#ifdef CONFIG_SYSCTL
648+
static struct ctl_table_set *pid_table_root_lookup(struct ctl_table_root *root)
649+
{
650+
return &task_active_pid_ns(current)->set;
651+
}
652+
653+
static int set_is_seen(struct ctl_table_set *set)
654+
{
655+
return &task_active_pid_ns(current)->set == set;
656+
}
657+
658+
static int pid_table_root_permissions(struct ctl_table_header *head,
659+
const struct ctl_table *table)
660+
{
661+
struct pid_namespace *pidns =
662+
container_of(head->set, struct pid_namespace, set);
663+
int mode = table->mode;
664+
665+
if (ns_capable(pidns->user_ns, CAP_SYS_ADMIN) ||
666+
uid_eq(current_euid(), make_kuid(pidns->user_ns, 0)))
667+
mode = (mode & S_IRWXU) >> 6;
668+
else if (in_egroup_p(make_kgid(pidns->user_ns, 0)))
669+
mode = (mode & S_IRWXG) >> 3;
670+
else
671+
mode = mode & S_IROTH;
672+
return (mode << 6) | (mode << 3) | mode;
673+
}
674+
675+
static void pid_table_root_set_ownership(struct ctl_table_header *head,
676+
kuid_t *uid, kgid_t *gid)
677+
{
678+
struct pid_namespace *pidns =
679+
container_of(head->set, struct pid_namespace, set);
680+
kuid_t ns_root_uid;
681+
kgid_t ns_root_gid;
682+
683+
ns_root_uid = make_kuid(pidns->user_ns, 0);
684+
if (uid_valid(ns_root_uid))
685+
*uid = ns_root_uid;
686+
687+
ns_root_gid = make_kgid(pidns->user_ns, 0);
688+
if (gid_valid(ns_root_gid))
689+
*gid = ns_root_gid;
690+
}
691+
692+
static struct ctl_table_root pid_table_root = {
693+
.lookup = pid_table_root_lookup,
694+
.permissions = pid_table_root_permissions,
695+
.set_ownership = pid_table_root_set_ownership,
696+
};
697+
698+
static struct ctl_table pid_table[] = {
699+
{
700+
.procname = "pid_max",
701+
.data = &init_pid_ns.pid_max,
702+
.maxlen = sizeof(int),
703+
.mode = 0644,
704+
.proc_handler = proc_dointvec_minmax,
705+
.extra1 = &pid_max_min,
706+
.extra2 = &pid_max_max,
707+
},
708+
};
709+
#endif
710+
711+
int register_pidns_sysctls(struct pid_namespace *pidns)
712+
{
713+
#ifdef CONFIG_SYSCTL
714+
struct ctl_table *tbl;
715+
716+
setup_sysctl_set(&pidns->set, &pid_table_root, set_is_seen);
717+
718+
tbl = kmemdup(pid_table, sizeof(pid_table), GFP_KERNEL);
719+
if (!tbl)
720+
return -ENOMEM;
721+
tbl->data = &pidns->pid_max;
722+
pidns->pid_max = min(pid_max_max, max_t(int, pidns->pid_max,
723+
PIDS_PER_CPU_DEFAULT * num_possible_cpus()));
724+
725+
pidns->sysctls = __register_sysctl_table(&pidns->set, "kernel", tbl,
726+
ARRAY_SIZE(pid_table));
727+
if (!pidns->sysctls) {
728+
kfree(tbl);
729+
retire_sysctl_set(&pidns->set);
730+
return -ENOMEM;
731+
}
732+
#endif
733+
return 0;
734+
}
735+
736+
void unregister_pidns_sysctls(struct pid_namespace *pidns)
737+
{
738+
#ifdef CONFIG_SYSCTL
739+
const struct ctl_table *tbl;
740+
741+
tbl = pidns->sysctls->ctl_table_arg;
742+
unregister_sysctl_table(pidns->sysctls);
743+
retire_sysctl_set(&pidns->set);
744+
kfree(tbl);
745+
#endif
746+
}
747+
647748
void __init pid_idr_init(void)
648749
{
649750
/* Verify no one has done anything silly: */
650751
BUILD_BUG_ON(PID_MAX_LIMIT >= PIDNS_ADDING);
651752

652753
/* bump default and minimum pid_max based on number of cpus */
653-
pid_max = min(pid_max_max, max_t(int, pid_max,
654-
PIDS_PER_CPU_DEFAULT * num_possible_cpus()));
754+
init_pid_ns.pid_max = min(pid_max_max, max_t(int, init_pid_ns.pid_max,
755+
PIDS_PER_CPU_DEFAULT * num_possible_cpus()));
655756
pid_max_min = max_t(int, pid_max_min,
656757
PIDS_PER_CPU_MIN * num_possible_cpus());
657-
pr_info("pid_max: default: %u minimum: %u\n", pid_max, pid_max_min);
758+
pr_info("pid_max: default: %u minimum: %u\n", init_pid_ns.pid_max, pid_max_min);
658759

659760
idr_init(&init_pid_ns.idr);
660761

@@ -665,6 +766,16 @@ void __init pid_idr_init(void)
665766
NULL);
666767
}
667768

769+
static __init int pid_namespace_sysctl_init(void)
770+
{
771+
#ifdef CONFIG_SYSCTL
772+
/* "kernel" directory will have already been initialized. */
773+
BUG_ON(register_pidns_sysctls(&init_pid_ns));
774+
#endif
775+
return 0;
776+
}
777+
subsys_initcall(pid_namespace_sysctl_init);
778+
668779
static struct file *__pidfd_fget(struct task_struct *task, int fd)
669780
{
670781
struct file *file;

kernel/pid_namespace.c

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ static void dec_pid_namespaces(struct ucounts *ucounts)
7070
dec_ucount(ucounts, UCOUNT_PID_NAMESPACES);
7171
}
7272

73+
static void destroy_pid_namespace_work(struct work_struct *work);
74+
7375
static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns,
7476
struct pid_namespace *parent_pid_ns)
7577
{
@@ -105,17 +107,27 @@ static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns
105107
goto out_free_idr;
106108
ns->ns.ops = &pidns_operations;
107109

110+
ns->pid_max = parent_pid_ns->pid_max;
111+
err = register_pidns_sysctls(ns);
112+
if (err)
113+
goto out_free_inum;
114+
108115
refcount_set(&ns->ns.count, 1);
109116
ns->level = level;
110117
ns->parent = get_pid_ns(parent_pid_ns);
111118
ns->user_ns = get_user_ns(user_ns);
112119
ns->ucounts = ucounts;
113120
ns->pid_allocated = PIDNS_ADDING;
121+
INIT_WORK(&ns->work, destroy_pid_namespace_work);
122+
114123
#if defined(CONFIG_SYSCTL) && defined(CONFIG_MEMFD_CREATE)
115124
ns->memfd_noexec_scope = pidns_memfd_noexec_scope(parent_pid_ns);
116125
#endif
126+
117127
return ns;
118128

129+
out_free_inum:
130+
ns_free_inum(&ns->ns);
119131
out_free_idr:
120132
idr_destroy(&ns->idr);
121133
kmem_cache_free(pid_ns_cachep, ns);
@@ -137,12 +149,28 @@ static void delayed_free_pidns(struct rcu_head *p)
137149

138150
static void destroy_pid_namespace(struct pid_namespace *ns)
139151
{
152+
unregister_pidns_sysctls(ns);
153+
140154
ns_free_inum(&ns->ns);
141155

142156
idr_destroy(&ns->idr);
143157
call_rcu(&ns->rcu, delayed_free_pidns);
144158
}
145159

160+
static void destroy_pid_namespace_work(struct work_struct *work)
161+
{
162+
struct pid_namespace *ns =
163+
container_of(work, struct pid_namespace, work);
164+
165+
do {
166+
struct pid_namespace *parent;
167+
168+
parent = ns->parent;
169+
destroy_pid_namespace(ns);
170+
ns = parent;
171+
} while (ns != &init_pid_ns && refcount_dec_and_test(&ns->ns.count));
172+
}
173+
146174
struct pid_namespace *copy_pid_ns(unsigned long flags,
147175
struct user_namespace *user_ns, struct pid_namespace *old_ns)
148176
{
@@ -155,15 +183,8 @@ struct pid_namespace *copy_pid_ns(unsigned long flags,
155183

156184
void put_pid_ns(struct pid_namespace *ns)
157185
{
158-
struct pid_namespace *parent;
159-
160-
while (ns != &init_pid_ns) {
161-
parent = ns->parent;
162-
if (!refcount_dec_and_test(&ns->ns.count))
163-
break;
164-
destroy_pid_namespace(ns);
165-
ns = parent;
166-
}
186+
if (ns && ns != &init_pid_ns && refcount_dec_and_test(&ns->ns.count))
187+
schedule_work(&ns->work);
167188
}
168189
EXPORT_SYMBOL_GPL(put_pid_ns);
169190

@@ -274,22 +295,22 @@ static int pid_ns_ctl_handler(const struct ctl_table *table, int write,
274295
next = idr_get_cursor(&pid_ns->idr) - 1;
275296

276297
tmp.data = &next;
298+
tmp.extra2 = &pid_ns->pid_max;
277299
ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
278300
if (!ret && write)
279301
idr_set_cursor(&pid_ns->idr, next + 1);
280302

281303
return ret;
282304
}
283305

284-
extern int pid_max;
285306
static struct ctl_table pid_ns_ctl_table[] = {
286307
{
287308
.procname = "ns_last_pid",
288309
.maxlen = sizeof(int),
289310
.mode = 0666, /* permissions are checked in the handler */
290311
.proc_handler = pid_ns_ctl_handler,
291312
.extra1 = SYSCTL_ZERO,
292-
.extra2 = &pid_max,
313+
.extra2 = &init_pid_ns.pid_max,
293314
},
294315
};
295316
#endif /* CONFIG_CHECKPOINT_RESTORE */

kernel/sysctl.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,15 +1803,6 @@ static struct ctl_table kern_table[] = {
18031803
.proc_handler = proc_dointvec,
18041804
},
18051805
#endif
1806-
{
1807-
.procname = "pid_max",
1808-
.data = &pid_max,
1809-
.maxlen = sizeof (int),
1810-
.mode = 0644,
1811-
.proc_handler = proc_dointvec_minmax,
1812-
.extra1 = &pid_max_min,
1813-
.extra2 = &pid_max_max,
1814-
},
18151806
{
18161807
.procname = "panic_on_oops",
18171808
.data = &panic_on_oops,

kernel/trace/pid_list.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ struct trace_pid_list *trace_pid_list_alloc(void)
414414
int i;
415415

416416
/* According to linux/thread.h, pids can be no bigger that 30 bits */
417-
WARN_ON_ONCE(pid_max > (1 << 30));
417+
WARN_ON_ONCE(init_pid_ns.pid_max > (1 << 30));
418418

419419
pid_list = kzalloc(sizeof(*pid_list), GFP_KERNEL);
420420
if (!pid_list)

kernel/trace/trace.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -717,8 +717,6 @@ extern unsigned long tracing_thresh;
717717

718718
/* PID filtering */
719719

720-
extern int pid_max;
721-
722720
bool trace_find_filtered_pid(struct trace_pid_list *filtered_pids,
723721
pid_t search_pid);
724722
bool trace_ignore_this_task(struct trace_pid_list *filtered_pids,

kernel/trace/trace_sched_switch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ int trace_alloc_tgid_map(void)
442442
if (tgid_map)
443443
return 0;
444444

445-
tgid_map_max = pid_max;
445+
tgid_map_max = init_pid_ns.pid_max;
446446
map = kvcalloc(tgid_map_max + 1, sizeof(*tgid_map),
447447
GFP_KERNEL);
448448
if (!map)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
pid_max
12
regression_enomem

tools/testing/selftests/pid_namespace/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SPDX-License-Identifier: GPL-2.0
22
CFLAGS += -g $(KHDR_INCLUDES)
33

4-
TEST_GEN_PROGS = regression_enomem
4+
TEST_GEN_PROGS = regression_enomem pid_max
55

66
LOCAL_HDRS += $(selfdir)/pidfd/pidfd.h
77

0 commit comments

Comments
 (0)