2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010,2013 Lawrence Stewart <lstewart@freebsd.org>
5 * Copyright (c) 2010 The FreeBSD Foundation
8 * This software was developed by Lawrence Stewart while studying at the Centre
9 * for Advanced Internet Architectures, Swinburne University of Technology,
10 * made possible in part by grants from the FreeBSD Foundation and Cisco
11 * University Research Program Fund at Community Foundation Silicon Valley.
13 * Portions of this software were developed at the Centre for Advanced
14 * Internet Architectures, Swinburne University of Technology, Melbourne,
15 * Australia by Lawrence Stewart under sponsorship from the FreeBSD Foundation.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/hhook.h>
45 #include <sys/khelp.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/module_khelp.h>
50 #include <sys/queue.h>
51 #include <sys/refcount.h>
52 #include <sys/systm.h>
57 hhook_func_t hhk_func;
58 struct helper *hhk_helper;
60 STAILQ_ENTRY(hhook) hhk_next;
63 static MALLOC_DEFINE(M_HHOOK, "hhook", "Helper hooks are linked off hhook_head lists");
65 LIST_HEAD(hhookheadhead, hhook_head);
66 struct hhookheadhead hhook_head_list;
67 VNET_DEFINE(struct hhookheadhead, hhook_vhead_list);
68 #define V_hhook_vhead_list VNET(hhook_vhead_list)
70 static struct mtx hhook_head_list_lock;
71 MTX_SYSINIT(hhookheadlistlock, &hhook_head_list_lock, "hhook_head list lock",
74 /* Protected by hhook_head_list_lock. */
75 static uint32_t n_hhookheads;
77 /* Private function prototypes. */
78 static void hhook_head_destroy(struct hhook_head *hhh);
79 void khelp_new_hhook_registered(struct hhook_head *hhh, uint32_t flags);
81 #define HHHLIST_LOCK() mtx_lock(&hhook_head_list_lock)
82 #define HHHLIST_UNLOCK() mtx_unlock(&hhook_head_list_lock)
83 #define HHHLIST_LOCK_ASSERT() mtx_assert(&hhook_head_list_lock, MA_OWNED)
85 #define HHH_LOCK_INIT(hhh) rm_init(&(hhh)->hhh_lock, "hhook_head rm lock")
86 #define HHH_LOCK_DESTROY(hhh) rm_destroy(&(hhh)->hhh_lock)
87 #define HHH_WLOCK(hhh) rm_wlock(&(hhh)->hhh_lock)
88 #define HHH_WUNLOCK(hhh) rm_wunlock(&(hhh)->hhh_lock)
89 #define HHH_RLOCK(hhh, rmpt) rm_rlock(&(hhh)->hhh_lock, (rmpt))
90 #define HHH_RUNLOCK(hhh, rmpt) rm_runlock(&(hhh)->hhh_lock, (rmpt))
93 * Run all helper hook functions for a given hook point.
96 hhook_run_hooks(struct hhook_head *hhh, void *ctx_data, struct osd *hosd)
100 struct rm_priotracker rmpt;
102 KASSERT(hhh->hhh_refcount > 0, ("hhook_head %p refcount is 0", hhh));
104 HHH_RLOCK(hhh, &rmpt);
105 STAILQ_FOREACH(hhk, &hhh->hhh_hooks, hhk_next) {
106 if (hhk->hhk_helper != NULL &&
107 hhk->hhk_helper->h_flags & HELPER_NEEDS_OSD) {
108 hdata = osd_get(OSD_KHELP, hosd, hhk->hhk_helper->h_id);
115 * XXXLAS: We currently ignore the int returned by the hook,
116 * but will likely want to handle it in future to allow hhook to
117 * be used like pfil and effect changes at the hhook calling
118 * site e.g. we could define a new hook type of HHOOK_TYPE_PFIL
119 * and standardise what particular return values mean and set
120 * the context data to pass exactly the same information as pfil
121 * hooks currently receive, thus replicating pfil with hhook.
123 hhk->hhk_func(hhh->hhh_type, hhh->hhh_id, hhk->hhk_udata,
124 ctx_data, hdata, hosd);
126 HHH_RUNLOCK(hhh, &rmpt);
130 * Register a new helper hook function with a helper hook point.
133 hhook_add_hook(struct hhook_head *hhh, struct hookinfo *hki, uint32_t flags)
135 struct hhook *hhk, *tmp;
143 hhk = malloc(sizeof(struct hhook), M_HHOOK,
144 M_ZERO | ((flags & HHOOK_WAITOK) ? M_WAITOK : M_NOWAIT));
149 hhk->hhk_helper = hki->hook_helper;
150 hhk->hhk_func = hki->hook_func;
151 hhk->hhk_udata = hki->hook_udata;
154 STAILQ_FOREACH(tmp, &hhh->hhh_hooks, hhk_next) {
155 if (tmp->hhk_func == hki->hook_func &&
156 tmp->hhk_udata == hki->hook_udata) {
157 /* The helper hook function is already registered. */
164 STAILQ_INSERT_TAIL(&hhh->hhh_hooks, hhk, hhk_next);
175 * Register a helper hook function with a helper hook point (including all
176 * virtual instances of the hook point if it is virtualised).
178 * The logic is unfortunately far more complex than for
179 * hhook_remove_hook_lookup() because hhook_add_hook() can call malloc() with
180 * M_WAITOK and thus we cannot call hhook_add_hook() with the
181 * hhook_head_list_lock held.
183 * The logic assembles an array of hhook_head structs that correspond to the
184 * helper hook point being hooked and bumps the refcount on each (all done with
185 * the hhook_head_list_lock held). The hhook_head_list_lock is then dropped, and
186 * hhook_add_hook() is called and the refcount dropped for each hhook_head
187 * struct in the array.
190 hhook_add_hook_lookup(struct hookinfo *hki, uint32_t flags)
192 struct hhook_head **heads_to_hook, *hhh;
193 int error, i, n_heads_to_hook;
198 * Accessing n_hhookheads without hhook_head_list_lock held opens up a
199 * race with hhook_head_register() which we are unlikely to lose, but
200 * nonetheless have to cope with - hence the complex goto logic.
202 n_heads_to_hook = n_hhookheads;
203 heads_to_hook = malloc(n_heads_to_hook * sizeof(struct hhook_head *),
204 M_HHOOK, flags & HHOOK_WAITOK ? M_WAITOK : M_NOWAIT);
205 if (heads_to_hook == NULL)
209 LIST_FOREACH(hhh, &hhook_head_list, hhh_next) {
210 if (hhh->hhh_type == hki->hook_type &&
211 hhh->hhh_id == hki->hook_id) {
212 if (i < n_heads_to_hook) {
213 heads_to_hook[i] = hhh;
214 refcount_acquire(&heads_to_hook[i]->hhh_refcount);
218 * We raced with hhook_head_register() which
219 * inserted a hhook_head that we need to hook
220 * but did not malloc space for. Abort this run
223 for (i--; i >= 0; i--)
224 refcount_release(&heads_to_hook[i]->hhh_refcount);
225 free(heads_to_hook, M_HHOOK);
233 for (i--; i >= 0; i--) {
235 error = hhook_add_hook(heads_to_hook[i], hki, flags);
236 refcount_release(&heads_to_hook[i]->hhh_refcount);
239 free(heads_to_hook, M_HHOOK);
245 * Remove a helper hook function from a helper hook point.
248 hhook_remove_hook(struct hhook_head *hhh, struct hookinfo *hki)
256 STAILQ_FOREACH(tmp, &hhh->hhh_hooks, hhk_next) {
257 if (tmp->hhk_func == hki->hook_func &&
258 tmp->hhk_udata == hki->hook_udata) {
259 STAILQ_REMOVE(&hhh->hhh_hooks, tmp, hhook, hhk_next);
271 * Remove a helper hook function from a helper hook point (including all
272 * virtual instances of the hook point if it is virtualised).
275 hhook_remove_hook_lookup(struct hookinfo *hki)
277 struct hhook_head *hhh;
280 LIST_FOREACH(hhh, &hhook_head_list, hhh_next) {
281 if (hhh->hhh_type == hki->hook_type &&
282 hhh->hhh_id == hki->hook_id)
283 hhook_remove_hook(hhh, hki);
291 * Register a new helper hook point.
294 hhook_head_register(int32_t hhook_type, int32_t hhook_id, struct hhook_head **hhh,
297 struct hhook_head *tmphhh;
299 tmphhh = hhook_head_get(hhook_type, hhook_id);
301 if (tmphhh != NULL) {
302 /* Hook point previously registered. */
303 hhook_head_release(tmphhh);
307 tmphhh = malloc(sizeof(struct hhook_head), M_HHOOK,
308 M_ZERO | ((flags & HHOOK_WAITOK) ? M_WAITOK : M_NOWAIT));
313 tmphhh->hhh_type = hhook_type;
314 tmphhh->hhh_id = hhook_id;
315 tmphhh->hhh_nhooks = 0;
316 STAILQ_INIT(&tmphhh->hhh_hooks);
317 HHH_LOCK_INIT(tmphhh);
318 refcount_init(&tmphhh->hhh_refcount, 1);
321 if (flags & HHOOK_HEADISINVNET) {
322 tmphhh->hhh_flags |= HHH_ISINVNET;
324 KASSERT(curvnet != NULL, ("curvnet is NULL"));
325 tmphhh->hhh_vid = (uintptr_t)curvnet;
326 LIST_INSERT_HEAD(&V_hhook_vhead_list, tmphhh, hhh_vnext);
329 LIST_INSERT_HEAD(&hhook_head_list, tmphhh, hhh_next);
333 khelp_new_hhook_registered(tmphhh, flags);
338 refcount_release(&tmphhh->hhh_refcount);
344 hhook_head_destroy(struct hhook_head *hhh)
346 struct hhook *tmp, *tmp2;
348 HHHLIST_LOCK_ASSERT();
349 KASSERT(n_hhookheads > 0, ("n_hhookheads should be > 0"));
351 LIST_REMOVE(hhh, hhh_next);
353 if (hhook_head_is_virtualised(hhh) == HHOOK_HEADISINVNET)
354 LIST_REMOVE(hhh, hhh_vnext);
357 STAILQ_FOREACH_SAFE(tmp, &hhh->hhh_hooks, hhk_next, tmp2)
360 HHH_LOCK_DESTROY(hhh);
366 * Remove a helper hook point.
369 hhook_head_deregister(struct hhook_head *hhh)
378 else if (hhh->hhh_refcount > 1)
381 hhook_head_destroy(hhh);
388 * Remove a helper hook point via a hhook_head lookup.
391 hhook_head_deregister_lookup(int32_t hhook_type, int32_t hhook_id)
393 struct hhook_head *hhh;
396 hhh = hhook_head_get(hhook_type, hhook_id);
397 error = hhook_head_deregister(hhh);
400 hhook_head_release(hhh);
406 * Lookup and return the hhook_head struct associated with the specified type
407 * and id, or NULL if not found. If found, the hhook_head's refcount is bumped.
410 hhook_head_get(int32_t hhook_type, int32_t hhook_id)
412 struct hhook_head *hhh;
415 LIST_FOREACH(hhh, &hhook_head_list, hhh_next) {
416 if (hhh->hhh_type == hhook_type && hhh->hhh_id == hhook_id) {
418 if (hhook_head_is_virtualised(hhh) ==
419 HHOOK_HEADISINVNET) {
420 KASSERT(curvnet != NULL, ("curvnet is NULL"));
421 if (hhh->hhh_vid != (uintptr_t)curvnet)
425 refcount_acquire(&hhh->hhh_refcount);
435 hhook_head_release(struct hhook_head *hhh)
438 refcount_release(&hhh->hhh_refcount);
442 * Check the hhook_head private flags and return the appropriate public
443 * representation of the flag to the caller. The function is implemented in a
444 * way that allows us to cope with other subsystems becoming virtualised in the
448 hhook_head_is_virtualised(struct hhook_head *hhh)
455 if (hhh->hhh_flags & HHH_ISINVNET)
456 ret = HHOOK_HEADISINVNET;
463 hhook_head_is_virtualised_lookup(int32_t hook_type, int32_t hook_id)
465 struct hhook_head *hhh;
468 hhh = hhook_head_get(hook_type, hook_id);
473 ret = hhook_head_is_virtualised(hhh);
474 hhook_head_release(hhh);
480 * Vnet created and being initialised.
483 hhook_vnet_init(const void *unused __unused)
486 LIST_INIT(&V_hhook_vhead_list);
490 * Vnet being torn down and destroyed.
493 hhook_vnet_uninit(const void *unused __unused)
495 struct hhook_head *hhh, *tmphhh;
498 * If subsystems which export helper hook points use the hhook KPI
499 * correctly, the loop below should have no work to do because the
500 * subsystem should have already called hhook_head_deregister().
503 LIST_FOREACH_SAFE(hhh, &V_hhook_vhead_list, hhh_vnext, tmphhh) {
504 printf("%s: hhook_head type=%d, id=%d cleanup required\n",
505 __func__, hhh->hhh_type, hhh->hhh_id);
506 hhook_head_destroy(hhh);
512 * When a vnet is created and being initialised, init the V_hhook_vhead_list.
514 VNET_SYSINIT(hhook_vnet_init, SI_SUB_INIT_IF, SI_ORDER_FIRST,
515 hhook_vnet_init, NULL);
518 * The hhook KPI provides a mechanism for subsystems which export helper hook
519 * points to clean up on vnet tear down, but in case the KPI is misused,
520 * provide a function to clean up and free memory for a vnet being destroyed.
522 VNET_SYSUNINIT(hhook_vnet_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
523 hhook_vnet_uninit, NULL);