Home | History | Annotate | Line # | Download | only in kern
subr_pserialize.c revision 1.11.2.2
      1  1.11.2.2    martin /*	$NetBSD: subr_pserialize.c,v 1.11.2.2 2020/04/13 08:05:04 martin Exp $	*/
      2       1.1  christos 
      3       1.1  christos /*-
      4       1.1  christos  * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
      5       1.1  christos  * All rights reserved.
      6       1.1  christos  *
      7       1.1  christos  * Redistribution and use in source and binary forms, with or without
      8       1.1  christos  * modification, are permitted provided that the following conditions
      9       1.1  christos  * are met:
     10       1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11       1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12       1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14       1.1  christos  *    documentation and/or other materials provided with the distribution.
     15       1.1  christos  *
     16       1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17       1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18       1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19       1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20       1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21       1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22       1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23       1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24       1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25       1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26       1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     27       1.1  christos  */
     28       1.1  christos 
     29       1.1  christos /*
     30       1.1  christos  * Passive serialization.
     31       1.1  christos  */
     32       1.1  christos 
     33       1.1  christos #include <sys/cdefs.h>
     34  1.11.2.2    martin __KERNEL_RCSID(0, "$NetBSD: subr_pserialize.c,v 1.11.2.2 2020/04/13 08:05:04 martin Exp $");
     35       1.1  christos 
     36       1.1  christos #include <sys/param.h>
     37  1.11.2.2    martin #include <sys/atomic.h>
     38       1.1  christos #include <sys/cpu.h>
     39       1.2        he #include <sys/evcnt.h>
     40       1.1  christos #include <sys/kmem.h>
     41       1.1  christos #include <sys/mutex.h>
     42       1.1  christos #include <sys/pserialize.h>
     43       1.1  christos #include <sys/xcall.h>
     44       1.1  christos 
     45       1.1  christos struct pserialize {
     46       1.1  christos 	lwp_t *			psz_owner;
     47       1.1  christos };
     48       1.1  christos 
     49       1.1  christos static kmutex_t			psz_lock	__cacheline_aligned;
     50  1.11.2.2    martin static struct evcnt		psz_ev_excl	__cacheline_aligned =
     51  1.11.2.2    martin     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pserialize", "exclusive access");
     52  1.11.2.2    martin EVCNT_ATTACH_STATIC(psz_ev_excl);
     53       1.9     ozaki 
     54       1.1  christos /*
     55       1.1  christos  * pserialize_init:
     56       1.1  christos  *
     57       1.1  christos  *	Initialize passive serialization structures.
     58       1.1  christos  */
     59       1.1  christos void
     60       1.1  christos pserialize_init(void)
     61       1.1  christos {
     62       1.1  christos 
     63  1.11.2.2    martin 	mutex_init(&psz_lock, MUTEX_DEFAULT, IPL_NONE);
     64       1.1  christos }
     65       1.1  christos 
     66       1.1  christos /*
     67       1.1  christos  * pserialize_create:
     68       1.1  christos  *
     69       1.1  christos  *	Create and initialize a passive serialization object.
     70       1.1  christos  */
     71       1.1  christos pserialize_t
     72       1.1  christos pserialize_create(void)
     73       1.1  christos {
     74       1.1  christos 	pserialize_t psz;
     75       1.1  christos 
     76  1.11.2.2    martin 	psz = kmem_zalloc(sizeof(*psz), KM_SLEEP);
     77       1.1  christos 	return psz;
     78       1.1  christos }
     79       1.1  christos 
     80       1.1  christos /*
     81       1.1  christos  * pserialize_destroy:
     82       1.1  christos  *
     83       1.1  christos  *	Destroy a passive serialization object.
     84       1.1  christos  */
     85       1.1  christos void
     86       1.1  christos pserialize_destroy(pserialize_t psz)
     87       1.1  christos {
     88       1.1  christos 
     89       1.1  christos 	KASSERT(psz->psz_owner == NULL);
     90  1.11.2.2    martin 	kmem_free(psz, sizeof(*psz));
     91       1.1  christos }
     92       1.1  christos 
     93       1.1  christos /*
     94       1.1  christos  * pserialize_perform:
     95       1.1  christos  *
     96  1.11.2.2    martin  *	Perform the write side of passive serialization.  This operation
     97  1.11.2.2    martin  *	MUST be serialized at a caller level (e.g. with a mutex or by a
     98  1.11.2.2    martin  *	single-threaded use).
     99       1.1  christos  */
    100       1.1  christos void
    101       1.1  christos pserialize_perform(pserialize_t psz)
    102       1.1  christos {
    103       1.1  christos 
    104       1.1  christos 	KASSERT(!cpu_intr_p());
    105       1.1  christos 	KASSERT(!cpu_softintr_p());
    106       1.1  christos 
    107       1.1  christos 	if (__predict_false(panicstr != NULL)) {
    108       1.1  christos 		return;
    109       1.1  christos 	}
    110       1.1  christos 	KASSERT(psz->psz_owner == NULL);
    111       1.1  christos 
    112      1.10   msaitoh 	if (__predict_false(mp_online == false)) {
    113      1.10   msaitoh 		psz_ev_excl.ev_count++;
    114      1.10   msaitoh 		return;
    115      1.10   msaitoh 	}
    116      1.10   msaitoh 
    117       1.1  christos 	psz->psz_owner = curlwp;
    118       1.1  christos 
    119  1.11.2.2    martin 	/*
    120  1.11.2.2    martin 	 * Broadcast a NOP to all CPUs and wait until all of them complete.
    121  1.11.2.2    martin 	 */
    122  1.11.2.2    martin 	xc_barrier(XC_HIGHPRI);
    123       1.1  christos 
    124  1.11.2.2    martin 	KASSERT(psz->psz_owner == curlwp);
    125       1.1  christos 	psz->psz_owner = NULL;
    126  1.11.2.2    martin 	mutex_enter(&psz_lock);
    127  1.11.2.2    martin 	psz_ev_excl.ev_count++;
    128  1.11.2.2    martin 	mutex_exit(&psz_lock);
    129       1.1  christos }
    130       1.1  christos 
    131       1.1  christos int
    132       1.1  christos pserialize_read_enter(void)
    133       1.1  christos {
    134       1.9     ozaki 	int s;
    135       1.1  christos 
    136       1.9     ozaki 	s = splsoftserial();
    137  1.11.2.2    martin 	curcpu()->ci_psz_read_depth++;
    138  1.11.2.2    martin 	__insn_barrier();
    139       1.9     ozaki 	return s;
    140       1.1  christos }
    141       1.1  christos 
    142       1.1  christos void
    143       1.1  christos pserialize_read_exit(int s)
    144       1.1  christos {
    145       1.1  christos 
    146  1.11.2.2    martin 	KASSERT(kpreempt_disabled());
    147       1.1  christos 
    148  1.11.2.2    martin 	__insn_barrier();
    149  1.11.2.2    martin 	if (__predict_false(curcpu()->ci_psz_read_depth-- == 0))
    150  1.11.2.2    martin 		panic("mismatching pserialize_read_exit()");
    151  1.11.2.2    martin 	splx(s);
    152       1.1  christos }
    153       1.9     ozaki 
    154       1.9     ozaki /*
    155       1.9     ozaki  * pserialize_in_read_section:
    156       1.9     ozaki  *
    157  1.11.2.2    martin  *	True if the caller is in a pserialize read section.  To be used
    158  1.11.2.2    martin  *	only for diagnostic assertions where we want to guarantee the
    159  1.11.2.2    martin  *	condition like:
    160       1.9     ozaki  *
    161  1.11.2.2    martin  *		KASSERT(pserialize_in_read_section());
    162       1.9     ozaki  */
    163       1.9     ozaki bool
    164       1.9     ozaki pserialize_in_read_section(void)
    165       1.9     ozaki {
    166  1.11.2.2    martin 
    167  1.11.2.2    martin 	return kpreempt_disabled() && curcpu()->ci_psz_read_depth > 0;
    168       1.9     ozaki }
    169       1.9     ozaki 
    170       1.9     ozaki /*
    171       1.9     ozaki  * pserialize_not_in_read_section:
    172       1.9     ozaki  *
    173  1.11.2.2    martin  *	True if the caller is not in a pserialize read section.  To be
    174  1.11.2.2    martin  *	used only for diagnostic assertions where we want to guarantee
    175  1.11.2.2    martin  *	the condition like:
    176       1.9     ozaki  *
    177  1.11.2.2    martin  *		KASSERT(pserialize_not_in_read_section());
    178       1.9     ozaki  */
    179       1.9     ozaki bool
    180       1.9     ozaki pserialize_not_in_read_section(void)
    181       1.9     ozaki {
    182       1.9     ozaki 	bool notin;
    183       1.9     ozaki 
    184  1.11.2.2    martin 	kpreempt_disable();
    185  1.11.2.2    martin 	notin = (curcpu()->ci_psz_read_depth == 0);
    186  1.11.2.2    martin 	kpreempt_enable();
    187       1.9     ozaki 
    188       1.9     ozaki 	return notin;
    189       1.9     ozaki }
    190