summaryrefslogtreecommitdiffstats
path: root/sys/kern
diff options
context:
space:
mode:
authorjhb <jhb@FreeBSD.org>2011-03-24 18:40:11 +0000
committerjhb <jhb@FreeBSD.org>2011-03-24 18:40:11 +0000
commitc7ac62aecd199084355eed0c34e006b1a886e37e (patch)
treeb8f82913909a64d0acd11bf6bd0728bcb4cc6c84 /sys/kern
parent7a8bd43974c3b65965d0a1661898bf7a3f1ff052 (diff)
downloadFreeBSD-src-c7ac62aecd199084355eed0c34e006b1a886e37e.zip
FreeBSD-src-c7ac62aecd199084355eed0c34e006b1a886e37e.tar.gz
Fix some locking nits with the p_state field of struct proc:
- Hold the proc lock while changing the state from PRS_NEW to PRS_NORMAL in fork to honor the locking requirements. While here, expand the scope of the PROC_LOCK() on the new process (p2) to avoid some LORs. Previously the code was locking the new child process (p2) after it had locked the parent process (p1). However, when locking two processes, the safe order is to lock the child first, then the parent. - Fix various places that were checking p_state against PRS_NEW without having the process locked to use PROC_LOCK(). Every place was already locking the process, just after the PRS_NEW check. - Remove or reduce the use of PROC_SLOCK() for places that were checking p_state against PRS_NEW. The PROC_LOCK() alone is sufficient for reading the current state. - Reorder fill_kinfo_proc() slightly so it only acquires PROC_SLOCK() once. MFC after: 1 week
Diffstat (limited to 'sys/kern')
-rw-r--r--sys/kern/kern_descrip.c6
-rw-r--r--sys/kern/kern_fork.c11
-rw-r--r--sys/kern/kern_proc.c16
-rw-r--r--sys/kern/kern_resource.c6
-rw-r--r--sys/kern/kern_thread.c4
5 files changed, 18 insertions, 25 deletions
diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c
index bca83b5..a979368 100644
--- a/sys/kern/kern_descrip.c
+++ b/sys/kern/kern_descrip.c
@@ -2634,9 +2634,11 @@ sysctl_kern_file(SYSCTL_HANDLER_ARGS)
xf.xf_size = sizeof(xf);
sx_slock(&allproc_lock);
FOREACH_PROC_IN_SYSTEM(p) {
- if (p->p_state == PRS_NEW)
- continue;
PROC_LOCK(p);
+ if (p->p_state == PRS_NEW) {
+ PROC_UNLOCK(p);
+ continue;
+ }
if (p_cansee(req->td, p) != 0) {
PROC_UNLOCK(p);
continue;
diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c
index a29dc17..ebd4e6d 100644
--- a/sys/kern/kern_fork.c
+++ b/sys/kern/kern_fork.c
@@ -630,12 +630,13 @@ do_fork(struct thread *td, int flags, struct proc *p2, struct thread *td2,
/*
* Set the child start time and mark the process as being complete.
*/
+ PROC_LOCK(p2);
+ PROC_LOCK(p1);
microuptime(&p2->p_stats->p_start);
PROC_SLOCK(p2);
p2->p_state = PRS_NORMAL;
PROC_SUNLOCK(p2);
- PROC_LOCK(p1);
#ifdef KDTRACE_HOOKS
/*
* Tell the DTrace fasttrap provider about the new process
@@ -643,11 +644,8 @@ do_fork(struct thread *td, int flags, struct proc *p2, struct thread *td2,
* p_state is PRS_NORMAL since the fasttrap module will use pfind()
* later on.
*/
- if (dtrace_fasttrap_fork) {
- PROC_LOCK(p2);
+ if (dtrace_fasttrap_fork)
dtrace_fasttrap_fork(p1, p2);
- PROC_UNLOCK(p2);
- }
#endif
if ((p1->p_flag & (P_TRACED | P_FOLLOWFORK)) == (P_TRACED |
P_FOLLOWFORK)) {
@@ -660,12 +658,11 @@ do_fork(struct thread *td, int flags, struct proc *p2, struct thread *td2,
*/
td->td_dbgflags |= TDB_FORK;
td->td_dbg_forked = p2->p_pid;
- PROC_LOCK(p2);
td2->td_dbgflags |= TDB_STOPATFORK;
_PHOLD(p2);
p2_held = 1;
- PROC_UNLOCK(p2);
}
+ PROC_UNLOCK(p2);
if ((flags & RFSTOPPED) == 0) {
/*
* If RFSTOPPED not requested, make child runnable and
diff --git a/sys/kern/kern_proc.c b/sys/kern/kern_proc.c
index e02b5b2..34cc570 100644
--- a/sys/kern/kern_proc.c
+++ b/sys/kern/kern_proc.c
@@ -291,11 +291,11 @@ pfind(pid)
sx_slock(&allproc_lock);
LIST_FOREACH(p, PIDHASH(pid), p_hash)
if (p->p_pid == pid) {
+ PROC_LOCK(p);
if (p->p_state == PRS_NEW) {
+ PROC_UNLOCK(p);
p = NULL;
- break;
}
- PROC_LOCK(p);
break;
}
sx_sunlock(&allproc_lock);
@@ -756,7 +756,6 @@ fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
kp->ki_sigcatch = ps->ps_sigcatch;
mtx_unlock(&ps->ps_mtx);
}
- PROC_SLOCK(p);
if (p->p_state != PRS_NEW &&
p->p_state != PRS_ZOMBIE &&
p->p_vmspace != NULL) {
@@ -782,12 +781,11 @@ fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
kp->ki_swtime = (ticks - p->p_swtick) / hz;
kp->ki_pid = p->p_pid;
kp->ki_nice = p->p_nice;
- rufetch(p, &kp->ki_rusage);
- kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
- PROC_SUNLOCK(p);
kp->ki_start = p->p_stats->p_start;
timevaladd(&kp->ki_start, &boottime);
PROC_SLOCK(p);
+ rufetch(p, &kp->ki_rusage);
+ kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
PROC_SUNLOCK(p);
calccru(p, &kp->ki_childutime, &kp->ki_childstime);
@@ -1213,13 +1211,11 @@ sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
/*
* Skip embryonic processes.
*/
- PROC_SLOCK(p);
+ PROC_LOCK(p);
if (p->p_state == PRS_NEW) {
- PROC_SUNLOCK(p);
+ PROC_UNLOCK(p);
continue;
}
- PROC_SUNLOCK(p);
- PROC_LOCK(p);
KASSERT(p->p_ucred != NULL,
("process credential is NULL for non-NEW proc"));
/*
diff --git a/sys/kern/kern_resource.c b/sys/kern/kern_resource.c
index 5bc3da5..66b6e2d 100644
--- a/sys/kern/kern_resource.c
+++ b/sys/kern/kern_resource.c
@@ -142,11 +142,9 @@ getpriority(td, uap)
uap->who = td->td_ucred->cr_uid;
sx_slock(&allproc_lock);
FOREACH_PROC_IN_SYSTEM(p) {
- /* Do not bother to check PRS_NEW processes */
- if (p->p_state == PRS_NEW)
- continue;
PROC_LOCK(p);
- if (p_cansee(td, p) == 0 &&
+ if (p->p_state == PRS_NORMAL &&
+ p_cansee(td, p) == 0 &&
p->p_ucred->cr_uid == uap->who) {
if (p->p_nice < low)
low = p->p_nice;
diff --git a/sys/kern/kern_thread.c b/sys/kern/kern_thread.c
index 5ef9864..29bdaa2 100644
--- a/sys/kern/kern_thread.c
+++ b/sys/kern/kern_thread.c
@@ -981,7 +981,9 @@ tdfind(lwpid_t tid, pid_t pid)
td = NULL;
break;
}
+ PROC_LOCK(td->td_proc);
if (td->td_proc->p_state == PRS_NEW) {
+ PROC_UNLOCK(td->td_proc);
td = NULL;
break;
}
@@ -990,12 +992,10 @@ tdfind(lwpid_t tid, pid_t pid)
LIST_REMOVE(td, td_hash);
LIST_INSERT_HEAD(TIDHASH(td->td_tid),
td, td_hash);
- PROC_LOCK(td->td_proc);
rw_wunlock(&tidhash_lock);
return (td);
}
}
- PROC_LOCK(td->td_proc);
break;
}
run++;
OpenPOWER on IntegriCloud