[BACK]Return to kern_module_hook.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / kern

File: [cvs.NetBSD.org] / src / sys / kern / kern_module_hook.c (download)

Revision 1.4, Fri Dec 13 08:02:53 2019 UTC (4 years, 4 months ago) by skrll
Branch: MAIN
CVS Tags: thorpej-ifq-base, thorpej-ifq, thorpej-i2c-spi-conf2-base, thorpej-i2c-spi-conf2, thorpej-i2c-spi-conf-base, thorpej-i2c-spi-conf, thorpej-futex2-base, thorpej-futex2, thorpej-futex-base, thorpej-futex, thorpej-cfargs2-base, thorpej-cfargs2, thorpej-cfargs-base, thorpej-cfargs, thorpej-altq-separation-base, thorpej-altq-separation, phil-wifi-20200421, phil-wifi-20200411, phil-wifi-20200406, netbsd-10-base, netbsd-10-0-RELEASE, netbsd-10-0-RC6, netbsd-10-0-RC5, netbsd-10-0-RC4, netbsd-10-0-RC3, netbsd-10-0-RC2, netbsd-10-0-RC1, netbsd-10, is-mlppp-base, is-mlppp, cjep_sun2x-base1, cjep_sun2x-base, cjep_sun2x, cjep_staticlib_x-base1, cjep_staticlib_x-base, cjep_staticlib_x, bouyer-xenpvh-base2, bouyer-xenpvh-base1, bouyer-xenpvh-base, bouyer-xenpvh, bouyer-sunxi-drm-base, bouyer-sunxi-drm, ad-namecache-base3, ad-namecache-base2, ad-namecache-base1, ad-namecache-base, ad-namecache, HEAD
Branch point for: phil-wifi
Changes since 1.3: +6 -6 lines

Trailing whitespace

/*	$NetBSD: kern_module_hook.c,v 1.4 2019/12/13 08:02:53 skrll Exp $ */

/*-
 * Copyright (c) 2019 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Kernel module support.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_module_hook.c,v 1.4 2019/12/13 08:02:53 skrll Exp $");

#include <sys/param.h>

#include <sys/atomic.h>
#include <sys/condvar.h>
#include <sys/module_hook.h>
#include <sys/mutex.h>
#include <sys/pserialize.h>

#include <uvm/uvm_extern.h>

/* Locking/synchronization stuff for module hooks */

static struct {
	kmutex_t	mtx;
	kcondvar_t	cv;
	pserialize_t	psz;
} module_hook __cacheline_aligned;

/*
 * We use pserialize_perform() to issue a memory barrier on the current
 * CPU and on all other CPUs so that all prior memory operations on the
 * current CPU globally happen before all subsequent memory operations
 * on the current CPU, as perceived by any other CPU.
 *
 * pserialize_perform() might be rather heavy-weight here, but it only
 * happens during module loading, and it allows MODULE_HOOK_CALL() to
 * work without any other memory barriers.
 */

void
module_hook_set(bool *hooked, struct localcount *lc)
{

	KASSERT(kernconfig_is_held());
	KASSERT(!*hooked);

	localcount_init(lc);

	/* Wait until setup has been witnessed by all CPUs.  */
	pserialize_perform(module_hook.psz);

	/* Let others use it */
	atomic_store_relaxed(hooked, true);
}

void
module_hook_unset(bool *hooked, struct localcount *lc)
{

	KASSERT(kernconfig_is_held());
	KASSERT(*hooked);

	/* Get exclusive with pserialize and localcount. */
	mutex_enter(&module_hook.mtx);

	/* Prevent new calls to module_hook_tryenter(). */
	atomic_store_relaxed(hooked, false);

	/* Wait for existing calls to module_hook_tryenter(). */
	pserialize_perform(module_hook.psz);

	/* Wait for module_hook_exit. */
	localcount_drain(lc, &module_hook.cv, &module_hook.mtx);

	/* All done! */
	mutex_exit(&module_hook.mtx);
	localcount_fini(lc);
}

bool
module_hook_tryenter(bool *hooked, struct localcount *lc)
{
	bool call_hook;
	int s;

	s = pserialize_read_enter();
	call_hook = atomic_load_relaxed(hooked);
	if (call_hook)
		localcount_acquire(lc);
	pserialize_read_exit(s);

	return call_hook;
}

void
module_hook_exit(struct localcount *lc)
{

	localcount_release(lc, &module_hook.cv, &module_hook.mtx);
}

void
module_hook_init(void)
{

	mutex_init(&module_hook.mtx, MUTEX_DEFAULT, IPL_NONE);
	cv_init(&module_hook.cv, "mod_hook");
	module_hook.psz = pserialize_create();
}