Date: Mon, 29 Mar 2021 22:12:02 GMT From: John Baldwin <jhb@FreeBSD.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org Subject: git: 053eb17635e1 - stable/13 - Set TDP_KTHREAD before calling cpu_fork() and cpu_copy_thread(). Message-ID: <202103292212.12TMC2CD064649@gitrepo.freebsd.org>
next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=053eb17635e19c5cfe8140d0d33dd9330bf6bc22 commit 053eb17635e19c5cfe8140d0d33dd9330bf6bc22 Author: John Baldwin <jhb@FreeBSD.org> AuthorDate: 2021-03-12 17:48:20 +0000 Commit: John Baldwin <jhb@FreeBSD.org> CommitDate: 2021-03-29 18:10:54 +0000 Set TDP_KTHREAD before calling cpu_fork() and cpu_copy_thread(). This permits these routines to use special logic for initializing MD kthread state. For the kproc case, this required moving the logic to set these flags from kproc_create() into do_fork(). Sponsored by: Netflix (cherry picked from commit 640d54045bdbf894ae3c75cd9818c29fc2f6e5e7) --- sys/kern/kern_fork.c | 12 ++++++++++-- sys/kern/kern_kthread.c | 17 +++++------------ sys/sys/proc.h | 1 + 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c index 870ae494de5d..3da8205d8ab0 100644 --- a/sys/kern/kern_fork.c +++ b/sys/kern/kern_fork.c @@ -497,9 +497,12 @@ do_fork(struct thread *td, struct fork_req *fr, struct proc *p2, struct thread * } else { sigacts_copy(newsigacts, p1->p_sigacts); p2->p_sigacts = newsigacts; - if ((fr->fr_flags2 & FR2_DROPSIG_CAUGHT) != 0) { + if ((fr->fr_flags2 & (FR2_DROPSIG_CAUGHT | FR2_KPROC)) != 0) { mtx_lock(&p2->p_sigacts->ps_mtx); - sig_drop_caught(p2); + if ((fr->fr_flags2 & FR2_DROPSIG_CAUGHT) != 0) + sig_drop_caught(p2); + if ((fr->fr_flags2 & FR2_KPROC) != 0) + p2->p_sigacts->ps_flag |= PS_NOCLDWAIT; mtx_unlock(&p2->p_sigacts->ps_mtx); } } @@ -511,6 +514,11 @@ do_fork(struct thread *td, struct fork_req *fr, struct proc *p2, struct thread * else p2->p_sigparent = SIGCHLD; + if ((fr->fr_flags2 & FR2_KPROC) != 0) { + p2->p_flag |= P_SYSTEM | P_KPROC; + td2->td_pflags |= TDP_KTHREAD; + } + p2->p_textvp = p1->p_textvp; p2->p_fd = fd; p2->p_fdtol = fdtol; diff --git a/sys/kern/kern_kthread.c b/sys/kern/kern_kthread.c index d049a099847c..32832bde2f53 100644 --- a/sys/kern/kern_kthread.c +++ b/sys/kern/kern_kthread.c @@ -95,6 +95,7 @@ kproc_create(void (*func)(void *), void *arg, bzero(&fr, sizeof(fr)); fr.fr_flags = RFMEM | RFFDG | RFPROC | RFSTOPPED | flags; + fr.fr_flags2 = FR2_KPROC; fr.fr_pages = pages; fr.fr_procp = &p2; error = fork1(&thread0, &fr); @@ -105,21 +106,11 @@ kproc_create(void (*func)(void *), void *arg, if (newpp != NULL) *newpp = p2; - /* this is a non-swapped system process */ - PROC_LOCK(p2); - td = FIRST_THREAD_IN_PROC(p2); - p2->p_flag |= P_SYSTEM | P_KPROC; - td->td_pflags |= TDP_KTHREAD; - mtx_lock(&p2->p_sigacts->ps_mtx); - p2->p_sigacts->ps_flag |= PS_NOCLDWAIT; - mtx_unlock(&p2->p_sigacts->ps_mtx); - PROC_UNLOCK(p2); - /* set up arg0 for 'ps', et al */ va_start(ap, fmt); vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap); va_end(ap); - /* set up arg0 for 'ps', et al */ + td = FIRST_THREAD_IN_PROC(p2); va_start(ap, fmt); vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap); va_end(ap); @@ -295,12 +286,14 @@ kthread_add(void (*func)(void *), void *arg, struct proc *p, TSTHREAD(newtd, newtd->td_name); newtd->td_proc = p; /* needed for cpu_copy_thread */ + newtd->td_pflags |= TDP_KTHREAD; + /* might be further optimized for kthread */ cpu_copy_thread(newtd, oldtd); + /* put the designated function(arg) as the resume context */ cpu_fork_kthread_handler(newtd, func, arg); - newtd->td_pflags |= TDP_KTHREAD; thread_cow_get_proc(newtd, p); /* this code almost the same as create_thread() in kern_thr.c */ diff --git a/sys/sys/proc.h b/sys/sys/proc.h index 99257878c2e0..783c3896298c 100644 --- a/sys/sys/proc.h +++ b/sys/sys/proc.h @@ -1043,6 +1043,7 @@ struct fork_req { int fr_flags2; #define FR2_DROPSIG_CAUGHT 0x00000001 /* Drop caught non-DFL signals */ #define FR2_SHARE_PATHS 0x00000002 /* Invert sense of RFFDG for paths */ +#define FR2_KPROC 0x00000004 /* Create a kernel process */ }; /*
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202103292212.12TMC2CD064649>