From 2d79d4b5a10f3e117aa79e926f1f059309bd52b2 Mon Sep 17 00:00:00 2001 From: mvs Date: Tue, 20 Aug 2024 07:48:23 +0000 Subject: [PATCH] Unlock KERN_MAXPROC and KERN_MAXTHREAD from `kern_vars'. Both `maxprocess' and `maxthread' are atomically accessed integers. ok mpi --- sys/conf/param.c | 11 ++++++++--- sys/kern/kern_fork.c | 15 +++++++++------ sys/kern/kern_resource.c | 4 ++-- sys/kern/kern_sysctl.c | 4 +++- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/sys/conf/param.c b/sys/conf/param.c index ac994d92090..39bf00676d8 100644 --- a/sys/conf/param.c +++ b/sys/conf/param.c @@ -1,4 +1,4 @@ -/* $OpenBSD: param.c,v 1.50 2024/05/05 06:14:37 jsg Exp $ */ +/* $OpenBSD: param.c,v 1.51 2024/08/20 07:48:23 mvs Exp $ */ /* $NetBSD: param.c,v 1.16 1996/03/12 03:08:40 mrg Exp $ */ /* @@ -49,6 +49,11 @@ #include #endif +/* + * Locks used to protect data: + * a atomic + */ + /* * System parameter formulae. * @@ -67,8 +72,8 @@ int utc_offset = 0; #define NTEXT (80 + NPROCESS / 8) /* actually the object cache */ #define NVNODE (NPROCESS * 2 + NTEXT + 100) int initialvnodes = NVNODE; -int maxprocess = NPROCESS; -int maxthread = 2 * NPROCESS; +int maxprocess = NPROCESS; /* [a] */ +int maxthread = 2 * NPROCESS; /* [a] */ int maxfiles = 5 * (NPROCESS + MAXUSERS) + 80; long nmbclust = NMBCLUSTERS; diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c index d0d465babb6..2f97fabaf89 100644 --- a/sys/kern/kern_fork.c +++ b/sys/kern/kern_fork.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_fork.c,v 1.263 2024/08/16 16:19:03 mpi Exp $ */ +/* $OpenBSD: kern_fork.c,v 1.264 2024/08/20 07:48:23 mvs Exp $ */ /* $NetBSD: kern_fork.c,v 1.29 1996/02/09 18:59:34 christos Exp $ */ /* @@ -307,7 +307,7 @@ struct timeval fork_tfmrate = { 10, 0 }; int fork_check_maxthread(uid_t uid) { - int val; + int maxthread_local, val; /* * Although process entries are dynamically created, we still keep @@ -318,8 +318,10 @@ fork_check_maxthread(uid_t uid) * the variable nthreads is the current number of procs, maxthread is * the limit. */ + maxthread_local = atomic_load_int(&maxthread); val = atomic_inc_int_nv(&nthreads); - if ((val > maxthread - 5 && uid != 0) || val > maxthread) { + if ((val > maxthread_local - 5 && uid != 0) || + val > maxthread_local) { static struct timeval lasttfm; if (ratecheck(&lasttfm, &fork_tfmrate)) @@ -353,7 +355,7 @@ fork1(struct proc *curp, int flags, void (*func)(void *), void *arg, struct proc *p; uid_t uid = curp->p_ucred->cr_ruid; struct vmspace *vm; - int count; + int count, maxprocess_local; vaddr_t uaddr; int error; struct ptrace_state *newptstat = NULL; @@ -366,8 +368,9 @@ fork1(struct proc *curp, int flags, void (*func)(void *), void *arg, if ((error = fork_check_maxthread(uid))) return error; - if ((nprocesses >= maxprocess - 5 && uid != 0) || - nprocesses >= maxprocess) { + maxprocess_local = atomic_load_int(&maxprocess); + if ((nprocesses >= maxprocess_local - 5 && uid != 0) || + nprocesses >= maxprocess_local) { static struct timeval lasttfm; if (ratecheck(&lasttfm, &fork_tfmrate)) diff --git a/sys/kern/kern_resource.c b/sys/kern/kern_resource.c index c4fec1b4b6b..2afc6287f21 100644 --- a/sys/kern/kern_resource.c +++ b/sys/kern/kern_resource.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_resource.c,v 1.86 2024/07/09 15:20:15 claudio Exp $ */ +/* $OpenBSD: kern_resource.c,v 1.87 2024/08/20 07:48:23 mvs Exp $ */ /* $NetBSD: kern_resource.c,v 1.38 1996/10/23 07:19:38 matthias Exp $ */ /*- @@ -278,7 +278,7 @@ dosetrlimit(struct proc *p, u_int which, struct rlimit *limp) maxlim = maxfiles; break; case RLIMIT_NPROC: - maxlim = maxprocess; + maxlim = atomic_load_int(&maxprocess); break; default: maxlim = RLIM_INFINITY; diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c index 8b10fd14208..698548fbcf8 100644 --- a/sys/kern/kern_sysctl.c +++ b/sys/kern/kern_sysctl.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_sysctl.c,v 1.440 2024/08/20 07:44:36 mvs Exp $ */ +/* $OpenBSD: kern_sysctl.c,v 1.441 2024/08/20 07:48:23 mvs Exp $ */ /* $NetBSD: kern_sysctl.c,v 1.17 1996/05/20 17:49:05 mrg Exp $ */ /*- @@ -554,6 +554,7 @@ kern_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, return (sysctl_rdint(oldp, oldlenp, newp, mp->msg_bufs)); } case KERN_OSREV: + case KERN_MAXPROC: case KERN_NFILES: case KERN_TTYCOUNT: case KERN_ARGMAX: @@ -563,6 +564,7 @@ kern_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, case KERN_SAVED_IDS: case KERN_MAXPARTITIONS: case KERN_RAWPARTITION: + case KERN_MAXTHREAD: case KERN_NTHREADS: case KERN_SOMAXCONN: case KERN_SOMINCONN: -- 2.20.1