From: cheloha Date: Sat, 23 Jul 2022 22:10:58 +0000 (+0000) Subject: kernel: remove global "randompid" toggle X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=608889ebe255266b621bd65a999b88dc9ad36dc0;p=openbsd kernel: remove global "randompid" toggle Apparently, we used to created several kthreads before the kernel random number generator was up and running. A toggle, "randompid", was needed to tell allocpid() whether it made sense to attempt to allocate random PIDs. However, these days we get e.g. arc4random(9) into a working state before any kthreads are spawned, so the toggle is no longer needed. Thread: https://marc.info/?l=openbsd-tech&m=165541052614453&w=2 Very nice historical context provided by miod@. probably ok miod@ deraadt@ --- diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c index bfc324c3ae5..a7413dc59ab 100644 --- a/sys/kern/init_main.c +++ b/sys/kern/init_main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: init_main.c,v 1.315 2022/02/22 01:15:01 guenther Exp $ */ +/* $OpenBSD: init_main.c,v 1.316 2022/07/23 22:10:58 cheloha Exp $ */ /* $NetBSD: init_main.c,v 1.84.4.1 1996/06/02 09:08:06 mrg Exp $ */ /* @@ -431,8 +431,6 @@ main(void *framep) initprocess = initproc->p_p; } - randompid = 1; - /* * Create any kernel threads whose creation was deferred because * initprocess had not yet been created. diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c index 8b4b4206058..e5bdcc7e9bc 100644 --- a/sys/kern/kern_fork.c +++ b/sys/kern/kern_fork.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_fork.c,v 1.240 2022/05/13 15:32:00 claudio Exp $ */ +/* $OpenBSD: kern_fork.c,v 1.241 2022/07/23 22:10:58 cheloha Exp $ */ /* $NetBSD: kern_fork.c,v 1.29 1996/02/09 18:59:34 christos Exp $ */ /* @@ -67,7 +67,6 @@ int nprocesses = 1; /* process 0 */ int nthreads = 1; /* proc 0 */ -int randompid; /* when set to 1, pid's go random */ struct forkstat forkstat; void fork_return(void *); @@ -638,20 +637,22 @@ ispidtaken(pid_t pid) pid_t allocpid(void) { - static pid_t lastpid; + static int first = 1; pid_t pid; - if (!randompid) { - /* only used early on for system processes */ - pid = ++lastpid; - } else { - /* Find an unused pid satisfying lastpid < pid <= PID_MAX */ - do { - pid = arc4random_uniform(PID_MAX - lastpid) + 1 + - lastpid; - } while (ispidtaken(pid)); + /* The first PID allocated is always 1. */ + if (first) { + first = 0; + return 1; } + /* + * All subsequent PIDs are chosen randomly. We need to + * find an unused PID in the range [2, PID_MAX]. + */ + do { + pid = 2 + arc4random_uniform(PID_MAX - 1); + } while (ispidtaken(pid)); return pid; } diff --git a/sys/sys/proc.h b/sys/sys/proc.h index ec2fd803476..01fa10baf70 100644 --- a/sys/sys/proc.h +++ b/sys/sys/proc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: proc.h,v 1.333 2022/07/05 15:06:16 visa Exp $ */ +/* $OpenBSD: proc.h,v 1.334 2022/07/23 22:10:59 cheloha Exp $ */ /* $NetBSD: proc.h,v 1.44 1996/04/22 01:23:21 christos Exp $ */ /*- @@ -498,7 +498,6 @@ extern struct proc proc0; /* Process slot for swapper. */ extern struct process process0; /* Process slot for kernel threads. */ extern int nprocesses, maxprocess; /* Cur and max number of processes. */ extern int nthreads, maxthread; /* Cur and max number of threads. */ -extern int randompid; /* fork() should create random pid's */ LIST_HEAD(proclist, proc); LIST_HEAD(processlist, process);