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