proc: Fix proc_init / proc_dtor ordering issues
Some checks are pending
Cross-build Kernel / amd64 ubuntu-22.04 (clang-15) (push) Waiting to run
Cross-build Kernel / aarch64 ubuntu-22.04 (clang-15) (push) Waiting to run
Cross-build Kernel / amd64 ubuntu-24.04 (clang-18) (push) Waiting to run
Cross-build Kernel / aarch64 ubuntu-24.04 (clang-18) (push) Waiting to run
Cross-build Kernel / amd64 macos-latest (clang-18) (push) Waiting to run
Cross-build Kernel / aarch64 macos-latest (clang-18) (push) Waiting to run

* Move the initialization of p_ktr into proc_init() and make the check
  in proc_dtor() unconditional.  Prior to this, it was possible to fail
  and invoke proc_dtor() after the first thread had been created (which
  was the condition for checking p_ktr in proc_dtor()) but before p_ktr
  had been initialized.

* Move the p_klist initialization in fork1() past the last possible
  failure point so we don't have to free it on failure.  We didn't,
  which meant we were leaking a knlist every time we failed to fork
  due to hitting the resource limit.

PR:		291470
MFC after:	1 week
Reviewed by:	kib
Differential Revision:	https://reviews.freebsd.org/D54215
This commit is contained in:
Dag-Erling Smørgrav 2025-12-14 14:16:22 +01:00
parent d4f25d0c79
commit 026d962ef1
2 changed files with 6 additions and 6 deletions

View file

@ -1065,8 +1065,6 @@ fork1(struct thread *td, struct fork_req *fr)
#ifdef MAC
mac_proc_init(newproc);
#endif
newproc->p_klist = knlist_alloc(&newproc->p_mtx);
STAILQ_INIT(&newproc->p_ktr);
/*
* Increment the count of procs running with this uid. Don't allow
@ -1079,6 +1077,8 @@ fork1(struct thread *td, struct fork_req *fr)
chgproccnt(cred->cr_ruidinfo, 1, 0);
}
newproc->p_klist = knlist_alloc(&newproc->p_mtx);
do_fork(td, fr, newproc, td2, vm2, fp_procdesc);
error = 0;
goto cleanup;

View file

@ -241,11 +241,9 @@ proc_dtor(void *mem, int size, void *arg)
p = (struct proc *)mem;
td = FIRST_THREAD_IN_PROC(p);
if (td != NULL) {
#ifdef INVARIANTS
KASSERT((p->p_numthreads == 1),
("bad number of threads in exiting process"));
KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
#endif
("too many threads in exiting process"));
/* Free all OSD associated to this thread. */
osd_thread_exit(td);
ast_kclear(td);
@ -253,6 +251,7 @@ proc_dtor(void *mem, int size, void *arg)
/* Make sure all thread destructors are executed */
EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td);
}
KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
EVENTHANDLER_DIRECT_INVOKE(process_dtor, p);
#ifdef KDTRACE_HOOKS
kdtrace_proc_dtor(p);
@ -281,6 +280,7 @@ proc_init(void *mem, int size, int flags)
p->p_stats = pstats_alloc();
p->p_pgrp = NULL;
TAILQ_INIT(&p->p_kqtim_stop);
STAILQ_INIT(&p->p_ktr);
return (0);
}