Tue Apr 26 17:40:38 2011 UTC ()
Hold the current lwp's mutex, instead of the lwp we want to wait for
in kthread_join(). This fixes panics with DIAGNOSTIC and possibly some
rare race conditons.


(ahoka)
diff -r1.31 -r1.32 src/sys/kern/kern_kthread.c

cvs diff -r1.31 -r1.32 src/sys/kern/kern_kthread.c (expand / switch to unified diff)

--- src/sys/kern/kern_kthread.c 2011/02/17 19:27:13 1.31
+++ src/sys/kern/kern_kthread.c 2011/04/26 17:40:38 1.32
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: kern_kthread.c,v 1.31 2011/02/17 19:27:13 matt Exp $ */ 1/* $NetBSD: kern_kthread.c,v 1.32 2011/04/26 17:40:38 ahoka Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1998, 1999, 2007, 2009 The NetBSD Foundation, Inc. 4 * Copyright (c) 1998, 1999, 2007, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, and by Andrew Doran. 9 * NASA Ames Research Center, and by Andrew Doran.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
@@ -21,27 +21,27 @@ @@ -21,27 +21,27 @@
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE. 30 * POSSIBILITY OF SUCH DAMAGE.
31 */ 31 */
32 32
33#include <sys/cdefs.h> 33#include <sys/cdefs.h>
34__KERNEL_RCSID(0, "$NetBSD: kern_kthread.c,v 1.31 2011/02/17 19:27:13 matt Exp $"); 34__KERNEL_RCSID(0, "$NetBSD: kern_kthread.c,v 1.32 2011/04/26 17:40:38 ahoka Exp $");
35 35
36#include <sys/param.h> 36#include <sys/param.h>
37#include <sys/systm.h> 37#include <sys/systm.h>
38#include <sys/kernel.h> 38#include <sys/kernel.h>
39#include <sys/kthread.h> 39#include <sys/kthread.h>
40#include <sys/proc.h> 40#include <sys/proc.h>
41#include <sys/sched.h> 41#include <sys/sched.h>
42#include <sys/kmem.h> 42#include <sys/kmem.h>
43 43
44#include <uvm/uvm_extern.h> 44#include <uvm/uvm_extern.h>
45 45
46/* 46/*
47 * note that stdarg.h and the ansi style va_start macro is used for both 47 * note that stdarg.h and the ansi style va_start macro is used for both
@@ -211,21 +211,21 @@ kthread_destroy(lwp_t *l) @@ -211,21 +211,21 @@ kthread_destroy(lwp_t *l)
211/* 211/*
212 * Wait for a kthread to exit, as pthread_join(). 212 * Wait for a kthread to exit, as pthread_join().
213 */ 213 */
214int 214int
215kthread_join(lwp_t *l) 215kthread_join(lwp_t *l)
216{ 216{
217 lwpid_t departed; 217 lwpid_t departed;
218 proc_t *p; 218 proc_t *p;
219 int error; 219 int error;
220 220
221 KASSERT((l->l_flag & LW_SYSTEM) != 0); 221 KASSERT((l->l_flag & LW_SYSTEM) != 0);
222 KASSERT((l->l_prflag & LPR_DETACHED) == 0); 222 KASSERT((l->l_prflag & LPR_DETACHED) == 0);
223  223
224 p = l->l_proc; 224 p = curlwp->l_proc;
225 225
226 mutex_enter(p->p_lock); 226 mutex_enter(p->p_lock);
227 error = lwp_wait1(curlwp, l->l_lid, &departed, LWPWAIT_EXITCONTROL); 227 error = lwp_wait1(curlwp, l->l_lid, &departed, LWPWAIT_EXITCONTROL);
228 mutex_exit(p->p_lock); 228 mutex_exit(p->p_lock);
229 229
230 return error; 230 return error;
231} 231}