Home | History | Annotate | Line # | Download | only in kern
subr_pserialize.c revision 1.18
      1  1.18  riastrad /*	$NetBSD: subr_pserialize.c,v 1.18 2021/10/10 11:20:46 riastradh 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.18  riastrad __KERNEL_RCSID(0, "$NetBSD: subr_pserialize.c,v 1.18 2021/10/10 11:20:46 riastradh Exp $");
     35   1.1  christos 
     36   1.1  christos #include <sys/param.h>
     37  1.14  riastrad #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.16  riastrad #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.18  riastrad 	char			psz_dummy;
     47   1.1  christos };
     48   1.1  christos 
     49  1.16  riastrad static kmutex_t			psz_lock	__cacheline_aligned;
     50  1.17  riastrad static struct evcnt		psz_ev_excl	__cacheline_aligned =
     51  1.17  riastrad     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pserialize", "exclusive access");
     52  1.17  riastrad EVCNT_ATTACH_STATIC(psz_ev_excl);
     53   1.1  christos 
     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.16  riastrad 	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.14  riastrad 	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.14  riastrad 	kmem_free(psz, sizeof(*psz));
     90   1.1  christos }
     91   1.1  christos 
     92   1.1  christos /*
     93   1.1  christos  * pserialize_perform:
     94   1.1  christos  *
     95  1.18  riastrad  *	Perform the write side of passive serialization.
     96   1.1  christos  */
     97   1.1  christos void
     98   1.1  christos pserialize_perform(pserialize_t psz)
     99   1.1  christos {
    100   1.1  christos 
    101   1.1  christos 	KASSERT(!cpu_intr_p());
    102   1.1  christos 	KASSERT(!cpu_softintr_p());
    103   1.1  christos 
    104   1.1  christos 	if (__predict_false(panicstr != NULL)) {
    105   1.1  christos 		return;
    106   1.1  christos 	}
    107   1.1  christos 
    108  1.10   msaitoh 	if (__predict_false(mp_online == false)) {
    109  1.10   msaitoh 		psz_ev_excl.ev_count++;
    110  1.10   msaitoh 		return;
    111  1.10   msaitoh 	}
    112  1.10   msaitoh 
    113   1.1  christos 	/*
    114  1.14  riastrad 	 * Broadcast a NOP to all CPUs and wait until all of them complete.
    115   1.1  christos 	 */
    116  1.14  riastrad 	xc_barrier(XC_HIGHPRI);
    117   1.1  christos 
    118  1.16  riastrad 	mutex_enter(&psz_lock);
    119  1.16  riastrad 	psz_ev_excl.ev_count++;
    120  1.16  riastrad 	mutex_exit(&psz_lock);
    121   1.1  christos }
    122   1.1  christos 
    123   1.1  christos int
    124   1.1  christos pserialize_read_enter(void)
    125   1.1  christos {
    126   1.9     ozaki 	int s;
    127   1.1  christos 
    128   1.9     ozaki 	s = splsoftserial();
    129  1.14  riastrad 	curcpu()->ci_psz_read_depth++;
    130  1.14  riastrad 	__insn_barrier();
    131   1.9     ozaki 	return s;
    132   1.1  christos }
    133   1.1  christos 
    134   1.1  christos void
    135   1.1  christos pserialize_read_exit(int s)
    136   1.1  christos {
    137   1.1  christos 
    138  1.14  riastrad 	KASSERT(kpreempt_disabled());
    139  1.14  riastrad 
    140  1.14  riastrad 	__insn_barrier();
    141  1.14  riastrad 	if (__predict_false(curcpu()->ci_psz_read_depth-- == 0))
    142  1.14  riastrad 		panic("mismatching pserialize_read_exit()");
    143   1.1  christos 	splx(s);
    144   1.1  christos }
    145   1.1  christos 
    146   1.1  christos /*
    147   1.9     ozaki  * pserialize_in_read_section:
    148   1.9     ozaki  *
    149  1.14  riastrad  *	True if the caller is in a pserialize read section.  To be used
    150  1.14  riastrad  *	only for diagnostic assertions where we want to guarantee the
    151  1.14  riastrad  *	condition like:
    152   1.9     ozaki  *
    153  1.14  riastrad  *		KASSERT(pserialize_in_read_section());
    154   1.9     ozaki  */
    155   1.9     ozaki bool
    156   1.9     ozaki pserialize_in_read_section(void)
    157   1.9     ozaki {
    158  1.14  riastrad 
    159  1.14  riastrad 	return kpreempt_disabled() && curcpu()->ci_psz_read_depth > 0;
    160   1.9     ozaki }
    161   1.9     ozaki 
    162   1.9     ozaki /*
    163   1.9     ozaki  * pserialize_not_in_read_section:
    164   1.9     ozaki  *
    165  1.14  riastrad  *	True if the caller is not in a pserialize read section.  To be
    166  1.14  riastrad  *	used only for diagnostic assertions where we want to guarantee
    167  1.14  riastrad  *	the condition like:
    168   1.9     ozaki  *
    169  1.14  riastrad  *		KASSERT(pserialize_not_in_read_section());
    170   1.9     ozaki  */
    171   1.9     ozaki bool
    172   1.9     ozaki pserialize_not_in_read_section(void)
    173   1.9     ozaki {
    174   1.9     ozaki 	bool notin;
    175   1.9     ozaki 
    176  1.14  riastrad 	kpreempt_disable();
    177  1.14  riastrad 	notin = (curcpu()->ci_psz_read_depth == 0);
    178  1.14  riastrad 	kpreempt_enable();
    179   1.9     ozaki 
    180   1.9     ozaki 	return notin;
    181   1.9     ozaki }
    182