kern_module_hook.c revision 1.2 1 /* $NetBSD: kern_module_hook.c,v 1.2 2019/12/13 07:59:36 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Kernel module support.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: kern_module_hook.c,v 1.2 2019/12/13 07:59:36 skrll Exp $");
35
36 #include <sys/param.h>
37 #include <sys/atomic.h>
38 #include <sys/module_hook.h>
39 #include <sys/mutex.h>
40 #include <sys/condvar.h>
41 #include <sys/pserialize.h>
42
43 #include <uvm/uvm_extern.h>
44
45 /* Locking/synchronization stuff for module hooks */
46
47 static struct {
48 kmutex_t mtx;
49 kcondvar_t cv;
50 pserialize_t psz;
51 } module_hook __cacheline_aligned;
52
53 /*
54 * We use pserialize_perform() to issue a memory barrier on the current
55 * CPU and on all other CPUs so that all prior memory operations on the
56 * current CPU globally happen before all subsequent memory operations
57 * on the current CPU, as perceived by any other CPU.
58 *
59 * pserialize_perform() might be rather heavy-weight here, but it only
60 * happens during module loading, and it allows MODULE_HOOK_CALL() to
61 * work without any other memory barriers.
62 */
63
64 void
65 module_hook_set(bool *hooked, struct localcount *lc)
66 {
67
68 KASSERT(kernconfig_is_held());
69 KASSERT(!*hooked);
70
71 localcount_init(lc);
72
73 /* Wait until setup has been witnessed by all CPUs. */
74 pserialize_perform(module_hook.psz);
75
76 /* Let others use it */
77 atomic_store_relaxed(hooked, true);
78 }
79
80 void
81 module_hook_unset(bool *hooked, struct localcount *lc)
82 {
83
84 KASSERT(kernconfig_is_held());
85 KASSERT(*hooked);
86
87 /* Get exclusive with pserialize and localcount. */
88 mutex_enter(&module_hook.mtx);
89
90 /* Prevent new calls to module_hook_tryenter(). */
91 atomic_store_relaxed(hooked, false);
92
93 /* Wait for existing calls to module_hook_tryenter(). */
94 pserialize_perform(module_hook.psz);
95
96 /* Wait for module_hook_exit. */
97 localcount_drain(lc, &module_hook.cv, &module_hook.mtx);
98
99 /* All done! */
100 mutex_exit(&module_hook.mtx);
101 localcount_fini(lc);
102 }
103
104 bool
105 module_hook_tryenter(bool *hooked, struct localcount *lc)
106 {
107 bool call_hook;
108 int s;
109
110 s = pserialize_read_enter();
111 call_hook = atomic_load_relaxed(hooked);
112 if (call_hook)
113 localcount_acquire(lc);
114 pserialize_read_exit(s);
115
116 return call_hook;
117 }
118
119 void
120 module_hook_exit(struct localcount *lc)
121 {
122
123 localcount_release(lc, &module_hook.cv, &module_hook.mtx);
124 }
125
126 void
127 module_hook_init(void)
128 {
129
130 mutex_init(&module_hook.mtx, MUTEX_DEFAULT, IPL_NONE);
131 cv_init(&module_hook.cv, "mod_hook");
132 module_hook.psz = pserialize_create();
133 }
134