kern_module_hook.c revision 1.1 1 /* $NetBSD: kern_module_hook.c,v 1.1 2019/12/12 22:55:20 pgoyette 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.1 2019/12/12 22:55:20 pgoyette Exp $");
35
36 #include <sys/param.h>
37 #include <sys/module_hook.h>
38 #include <sys/mutex.h>
39 #include <sys/condvar.h>
40 #include <sys/pserialize.h>
41
42 #include <uvm/uvm_extern.h>
43
44 /* Locking/synchronization stuff for module hooks */
45
46 static struct {
47 kmutex_t mtx;
48 kcondvar_t cv;
49 pserialize_t psz;
50 } module_hook __cacheline_aligned;
51
52 /*
53 * We use pserialize_perform() to issue a memory barrier on the current
54 * CPU and on all other CPUs so that all prior memory operations on the
55 * current CPU globally happen before all subsequent memory operations
56 * on the current CPU, as perceived by any other CPU.
57 *
58 * pserialize_perform() might be rather heavy-weight here, but it only
59 * happens during module loading, and it allows MODULE_HOOK_CALL() to
60 * work without any other memory barriers.
61 */
62
63 void
64 module_hook_set(bool *hooked, struct localcount *lc)
65 {
66
67 KASSERT(kernconfig_is_held());
68 KASSERT(!*hooked);
69
70 localcount_init(lc);
71
72 /* Wait until setup has been witnessed by all CPUs. */
73 pserialize_perform(module_hook.psz);
74
75 /* Let others use it */
76 atomic_store_relaxed(hooked, true);
77 }
78
79 void
80 module_hook_unset(bool *hooked, struct localcount *lc)
81 {
82
83 KASSERT(kernconfig_is_held());
84 KASSERT(*hooked);
85
86 /* Get exclusive with pserialize and localcount. */
87 mutex_enter(&module_hook.mtx);
88
89 /* Prevent new calls to module_hook_tryenter(). */
90 atomic_store_relaxed(hooked, false);
91
92 /* Wait for existing calls to module_hook_tryenter(). */
93 pserialize_perform(module_hook.psz);
94
95 /* Wait for module_hook_exit. */
96 localcount_drain(lc, &module_hook.cv, &module_hook.mtx);
97
98 /* All done! */
99 mutex_exit(&module_hook.mtx);
100 localcount_fini(lc);
101 }
102
103 bool
104 module_hook_tryenter(bool *hooked, struct localcount *lc)
105 {
106 bool call_hook;
107 int s;
108
109 s = pserialize_read_enter();
110 call_hook = atomic_load_relaxed(hooked);
111 if (call_hook)
112 localcount_acquire(lc);
113 pserialize_read_exit(s);
114
115 return call_hook;
116 }
117
118 void
119 module_hook_exit(struct localcount *lc)
120 {
121
122 localcount_release(lc, &module_hook.cv, &module_hook.mtx);
123 }
124
125 void
126 module_hook_init(void)
127 {
128
129 mutex_init(&module_hook.mtx, MUTEX_DEFAULT, IPL_NONE);
130 cv_init(&module_hook.cv, "mod_hook");
131 module_hook.psz = pserialize_create();
132 }
133