1 1.24 ad /* $NetBSD: subr_pserialize.c,v 1.24 2023/10/04 20:28:06 ad Exp $ */ 2 1.1 christos 3 1.1 christos /*- 4 1.24 ad * Copyright (c) 2010, 2011, 2023 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.24 ad __KERNEL_RCSID(0, "$NetBSD: subr_pserialize.c,v 1.24 2023/10/04 20:28:06 ad Exp $"); 35 1.1 christos 36 1.1 christos #include <sys/param.h> 37 1.20 riastrad 38 1.14 riastrad #include <sys/atomic.h> 39 1.1 christos #include <sys/cpu.h> 40 1.20 riastrad #include <sys/evcnt.h> 41 1.19 macallan #include <sys/kernel.h> 42 1.1 christos #include <sys/kmem.h> 43 1.20 riastrad #include <sys/lwp.h> 44 1.16 riastrad #include <sys/mutex.h> 45 1.1 christos #include <sys/pserialize.h> 46 1.1 christos #include <sys/xcall.h> 47 1.1 christos 48 1.1 christos struct pserialize { 49 1.18 riastrad char psz_dummy; 50 1.1 christos }; 51 1.1 christos 52 1.16 riastrad static kmutex_t psz_lock __cacheline_aligned; 53 1.17 riastrad static struct evcnt psz_ev_excl __cacheline_aligned = 54 1.17 riastrad EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pserialize", "exclusive access"); 55 1.17 riastrad EVCNT_ATTACH_STATIC(psz_ev_excl); 56 1.1 christos 57 1.1 christos /* 58 1.1 christos * pserialize_init: 59 1.1 christos * 60 1.1 christos * Initialize passive serialization structures. 61 1.1 christos */ 62 1.1 christos void 63 1.1 christos pserialize_init(void) 64 1.1 christos { 65 1.1 christos 66 1.16 riastrad mutex_init(&psz_lock, MUTEX_DEFAULT, IPL_NONE); 67 1.1 christos } 68 1.1 christos 69 1.1 christos /* 70 1.1 christos * pserialize_create: 71 1.1 christos * 72 1.1 christos * Create and initialize a passive serialization object. 73 1.1 christos */ 74 1.1 christos pserialize_t 75 1.1 christos pserialize_create(void) 76 1.1 christos { 77 1.1 christos pserialize_t psz; 78 1.1 christos 79 1.14 riastrad psz = kmem_zalloc(sizeof(*psz), KM_SLEEP); 80 1.1 christos return psz; 81 1.1 christos } 82 1.1 christos 83 1.1 christos /* 84 1.1 christos * pserialize_destroy: 85 1.1 christos * 86 1.1 christos * Destroy a passive serialization object. 87 1.1 christos */ 88 1.1 christos void 89 1.1 christos pserialize_destroy(pserialize_t psz) 90 1.1 christos { 91 1.1 christos 92 1.14 riastrad kmem_free(psz, sizeof(*psz)); 93 1.1 christos } 94 1.1 christos 95 1.1 christos /* 96 1.1 christos * pserialize_perform: 97 1.1 christos * 98 1.18 riastrad * Perform the write side of passive serialization. 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 111 1.10 msaitoh if (__predict_false(mp_online == false)) { 112 1.10 msaitoh psz_ev_excl.ev_count++; 113 1.10 msaitoh return; 114 1.10 msaitoh } 115 1.10 msaitoh 116 1.1 christos /* 117 1.14 riastrad * Broadcast a NOP to all CPUs and wait until all of them complete. 118 1.1 christos */ 119 1.14 riastrad xc_barrier(XC_HIGHPRI); 120 1.1 christos 121 1.16 riastrad mutex_enter(&psz_lock); 122 1.16 riastrad psz_ev_excl.ev_count++; 123 1.16 riastrad mutex_exit(&psz_lock); 124 1.1 christos } 125 1.1 christos 126 1.1 christos int 127 1.1 christos pserialize_read_enter(void) 128 1.1 christos { 129 1.9 ozaki int s; 130 1.1 christos 131 1.9 ozaki s = splsoftserial(); 132 1.14 riastrad curcpu()->ci_psz_read_depth++; 133 1.14 riastrad __insn_barrier(); 134 1.9 ozaki return s; 135 1.1 christos } 136 1.1 christos 137 1.1 christos void 138 1.1 christos pserialize_read_exit(int s) 139 1.1 christos { 140 1.1 christos 141 1.21 riastrad KASSERT(__predict_false(cold) || kpreempt_disabled()); 142 1.14 riastrad 143 1.14 riastrad __insn_barrier(); 144 1.14 riastrad if (__predict_false(curcpu()->ci_psz_read_depth-- == 0)) 145 1.14 riastrad panic("mismatching pserialize_read_exit()"); 146 1.1 christos splx(s); 147 1.1 christos } 148 1.1 christos 149 1.1 christos /* 150 1.9 ozaki * pserialize_in_read_section: 151 1.9 ozaki * 152 1.14 riastrad * True if the caller is in a pserialize read section. To be used 153 1.14 riastrad * only for diagnostic assertions where we want to guarantee the 154 1.14 riastrad * condition like: 155 1.9 ozaki * 156 1.14 riastrad * KASSERT(pserialize_in_read_section()); 157 1.9 ozaki */ 158 1.9 ozaki bool 159 1.9 ozaki pserialize_in_read_section(void) 160 1.9 ozaki { 161 1.14 riastrad 162 1.14 riastrad return kpreempt_disabled() && curcpu()->ci_psz_read_depth > 0; 163 1.9 ozaki } 164 1.9 ozaki 165 1.9 ozaki /* 166 1.9 ozaki * pserialize_not_in_read_section: 167 1.9 ozaki * 168 1.14 riastrad * True if the caller is not in a pserialize read section. To be 169 1.14 riastrad * used only for diagnostic assertions where we want to guarantee 170 1.14 riastrad * the condition like: 171 1.9 ozaki * 172 1.14 riastrad * KASSERT(pserialize_not_in_read_section()); 173 1.9 ozaki */ 174 1.9 ozaki bool 175 1.9 ozaki pserialize_not_in_read_section(void) 176 1.9 ozaki { 177 1.9 ozaki bool notin; 178 1.24 ad long pctr; 179 1.9 ozaki 180 1.24 ad pctr = lwp_pctr(); 181 1.22 riastrad notin = __predict_true(curcpu()->ci_psz_read_depth == 0); 182 1.20 riastrad 183 1.20 riastrad /* 184 1.20 riastrad * If we had a context switch, we're definitely not in a 185 1.20 riastrad * pserialize read section because pserialize read sections 186 1.20 riastrad * block preemption. 187 1.20 riastrad */ 188 1.24 ad if (__predict_false(pctr != lwp_pctr())) 189 1.20 riastrad notin = true; 190 1.9 ozaki 191 1.9 ozaki return notin; 192 1.9 ozaki } 193