xref: /netbsd/sys/kern/kern_cfglock.c (revision 4a743ad4)
1 /*	$NetBSD: kern_cfglock.c,v 1.1 2010/08/21 13:17:31 pgoyette Exp $ */
2 
3 /*-
4  * Copyright (c) 2002, 2006, 2007, 2008, 2009 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center, and by Andrew Doran.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
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
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
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
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
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: kern_cfglock.c,v 1.1 2010/08/21 13:17:31 pgoyette Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/cpu.h>
38 #include <sys/mutex.h>
39 #include <sys/lwp.h>
40 #include <sys/systm.h>
41 
42 static kmutex_t kernconfig_mutex;
43 static lwp_t *kernconfig_lwp;
44 static int kernconfig_recurse;
45 
46 /*
47  * Functions for manipulating the kernel configuration lock.  This
48  * recursive lock should be used to protect all additions and removals
49  * of kernel functionality, such as device configuration and loading
50  * of modular kernel components.
51  */
52 
53 void
kernconfig_lock_init(void)54 kernconfig_lock_init(void)
55 {
56 
57 	mutex_init(&kernconfig_mutex, MUTEX_DEFAULT, IPL_NONE);
58 	kernconfig_lwp = NULL;
59 	kernconfig_recurse = 0;
60 }
61 
62 void
kernconfig_lock(void)63 kernconfig_lock(void)
64 {
65 	lwp_t	*my_lwp;
66 
67 	/*
68 	 * It's OK to check this unlocked, since it could only be set to
69 	 * curlwp by the current thread itself, and not by an interrupt
70 	 * or any other LWP.
71 	 */
72 	KASSERT(!cpu_intr_p());
73 	my_lwp = curlwp;
74 	if (kernconfig_lwp == my_lwp) {
75 		kernconfig_recurse++;
76 		KASSERT(kernconfig_recurse > 1);
77 	} else {
78 		mutex_enter(&kernconfig_mutex);
79 		kernconfig_lwp = my_lwp;
80 		kernconfig_recurse = 1;
81 	}
82 }
83 
84 void
kernconfig_unlock(void)85 kernconfig_unlock(void)
86 {
87 
88 	KASSERT(kernconfig_is_held());
89 	KASSERT(kernconfig_recurse != 0);
90 	if (--kernconfig_recurse == 0) {
91 		kernconfig_lwp = NULL;
92 		mutex_exit(&kernconfig_mutex);
93 	}
94 }
95 
96 bool
kernconfig_is_held(void)97 kernconfig_is_held(void)
98 {
99 
100 	return mutex_owned(&kernconfig_mutex);
101 }
102