From fe41d17ab2e31acf7e76ba2f70bf247e51c27d9d Mon Sep 17 00:00:00 2001 From: John Baldwin Date: Tue, 17 Aug 2010 21:34:19 +0000 Subject: [PATCH] Keep the process locked when calling ktrops() or ktrsetchildren() instead of dropping the lock only to immediately reacquire it. --- sys/kern/kern_ktrace.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/sys/kern/kern_ktrace.c b/sys/kern/kern_ktrace.c index d5eb4e1cb5a5..26584379f5e7 100644 --- a/sys/kern/kern_ktrace.c +++ b/sys/kern/kern_ktrace.c @@ -741,7 +741,6 @@ ktrace(td, uap) PROC_UNLOCK(p); continue; } - PROC_UNLOCK(p); nfound++; if (descend) ret |= ktrsetchildren(td, p, ops, facs, vp); @@ -758,18 +757,13 @@ ktrace(td, uap) * by pid */ p = pfind(uap->pid); - if (p == NULL) { - sx_sunlock(&proctree_lock); + if (p == NULL) error = ESRCH; - goto done; - } - error = p_cansee(td, p); - /* - * The slock of the proctree lock will keep this process - * from going away, so unlocking the proc here is ok. - */ - PROC_UNLOCK(p); + else + error = p_cansee(td, p); if (error) { + if (p != NULL) + PROC_UNLOCK(p); sx_sunlock(&proctree_lock); goto done; } @@ -841,11 +835,16 @@ ktrops(td, p, ops, facs, vp) struct vnode *tracevp = NULL; struct ucred *tracecred = NULL; - PROC_LOCK(p); + PROC_LOCK_ASSERT(p, MA_OWNED); if (!ktrcanset(td, p)) { PROC_UNLOCK(p); return (0); } + if (p->p_flag & P_WEXIT) { + /* If the process is exiting, just ignore it. */ + PROC_UNLOCK(p); + return (1); + } mtx_lock(&ktrace_mtx); if (ops == KTROP_SET) { if (p->p_tracevp != vp) { @@ -900,6 +899,7 @@ ktrsetchildren(td, top, ops, facs, vp) register int ret = 0; p = top; + PROC_LOCK_ASSERT(p, MA_OWNED); sx_assert(&proctree_lock, SX_LOCKED); for (;;) { ret |= ktrops(td, p, ops, facs, vp); @@ -919,6 +919,7 @@ ktrsetchildren(td, top, ops, facs, vp) } p = p->p_pptr; } + PROC_LOCK(p); } /*NOTREACHED*/ }