Index: sys/kern/kern_lwp.c
===================================================================
RCS file: /cvsroot/src/sys/kern/kern_lwp.c,v
retrieving revision 1.206
diff -p -u -r1.206 kern_lwp.c
--- sys/kern/kern_lwp.c	7 Nov 2019 19:45:18 -0000	1.206
+++ sys/kern/kern_lwp.c	10 Nov 2019 22:45:22 -0000
@@ -902,8 +902,6 @@ lwp_create(lwp_t *l1, proc_t *p2, vaddr_
 	if ((flags & LWP_PIDLID) != 0) {
 		lid = proc_alloc_pid(p2);
 		l2->l_pflag |= LP_PIDLID;
-	} else if (p2->p_nlwps == 0) {
-		lid = l1->l_lid;
 	} else {
 		lid = 0;
 	}
@@ -922,20 +920,36 @@ lwp_create(lwp_t *l1, proc_t *p2, vaddr_
 	sigemptyset(&l2->l_sigpend.sp_set);
 
 	if (__predict_true(lid == 0)) {
-		/*
-		 * XXX: l_lid are expected to be unique (for a process)
-		 * if LWP_PIDLID is sometimes set this won't be true.
-		 * Once 2^31 threads have been allocated we have to
-		 * scan to ensure we allocate a unique value.
-		 */
-		lid = ++p2->p_nlwpid;
-		if (__predict_false(lid & LID_SCAN)) {
-			lid = lwp_find_free_lid(lid, l2, p2);
+		if (p2->p_nlwps == 0 && l1->l_lid != 1) {
+			KASSERT(p2->p_nlwpid == 0);
+			/*
+			 * XXX: When forking (p_nlwps == 0) copy
+			 * the parent LID to keep the expectations
+			 * of our runtime linker.  Set LID_SCAN so
+			 * that subsequent calls will search the
+			 * LID space.
+			 * Avoid this when the next LID (p_nlwpid+1)
+			 * (== 1 after fork) is the same as the parent
+			 * LID.
+			 */
+			lid = l1->l_lid;
 			p2->p_nlwpid = lid | LID_SCAN;
-			/* l2 as been inserted into p_lwps in order */
-			goto skip_insert;
+		} else {
+			/*
+			 * XXX: l_lid are expected to be unique (for a process)
+			 * if LWP_PIDLID is sometimes set this won't be true.
+			 * Once 2^31 threads have been allocated we have to
+			 * scan to ensure we allocate a unique value.
+			 */
+			lid = ++p2->p_nlwpid;
+			if (__predict_false(lid & LID_SCAN)) {
+				lid = lwp_find_free_lid(lid, l2, p2);
+				p2->p_nlwpid = lid | LID_SCAN;
+				/* l2 has been inserted into p_lwps in order */
+				goto skip_insert;
+			}
+			p2->p_nlwpid = lid;
 		}
-		p2->p_nlwpid = lid;
 	}
 	LIST_INSERT_HEAD(&p2->p_lwps, l2, l_sibling);
     skip_insert:
