svn commit: r254457 - head/sys/kern

Bryan Venteicher bryanv at FreeBSD.org
Sat Aug 17 17:02:44 UTC 2013


Author: bryanv
Date: Sat Aug 17 17:02:43 2013
New Revision: 254457
URL: http://svnweb.freebsd.org/changeset/base/254457

Log:
  Do not use potentially stale thread in kthread_add()
  
  When an existing process is provided, the thread selected to use
  to initialize the new thread could have exited and be reaped.
  Acquire the proc lock earlier to ensure the thread remains valid.
  
  Reviewed by:	jhb, julian (previous version)
  MFC after:	3 days

Modified:
  head/sys/kern/kern_kthread.c

Modified: head/sys/kern/kern_kthread.c
==============================================================================
--- head/sys/kern/kern_kthread.c	Sat Aug 17 16:42:18 2013	(r254456)
+++ head/sys/kern/kern_kthread.c	Sat Aug 17 17:02:43 2013	(r254457)
@@ -257,18 +257,17 @@ kthread_add(void (*func)(void *), void *
 		panic("kthread_add called too soon");
 
 	/* If no process supplied, put it on proc0 */
-	if (p == NULL) {
+	if (p == NULL)
 		p = &proc0;
-		oldtd = &thread0;
-	} else {
-		oldtd = FIRST_THREAD_IN_PROC(p);
-	}
 
 	/* Initialize our new td  */
 	newtd = thread_alloc(pages);
 	if (newtd == NULL)
 		return (ENOMEM);
 
+	PROC_LOCK(p);
+	oldtd = FIRST_THREAD_IN_PROC(p);
+
 	bzero(&newtd->td_startzero,
 	    __rangeof(struct thread, td_startzero, td_endzero));
 	bcopy(&oldtd->td_startcopy, &newtd->td_startcopy,
@@ -292,7 +291,6 @@ kthread_add(void (*func)(void *), void *
 	newtd->td_ucred = crhold(p->p_ucred);
 
 	/* this code almost the same as create_thread() in kern_thr.c */
-	PROC_LOCK(p);
 	p->p_flag |= P_HADTHREADS;
 	thread_link(newtd, p);
 	thread_lock(oldtd);


More information about the svn-src-all mailing list