subr_pserialize.c revision 1.13
1/* $NetBSD: subr_pserialize.c,v 1.13 2019/10/06 15:11:17 uwe Exp $ */ 2 3/*- 4 * Copyright (c) 2010, 2011 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 * Passive serialization. 31 * 32 * Implementation accurately matches the lapsed US patent 4809168, therefore 33 * code is patent-free in the United States. Your use of this code is at 34 * your own risk. 35 * 36 * Note for NetBSD developers: all changes to this source file must be 37 * approved by the <core>. 38 */ 39 40#include <sys/cdefs.h> 41__KERNEL_RCSID(0, "$NetBSD: subr_pserialize.c,v 1.13 2019/10/06 15:11:17 uwe Exp $"); 42 43#include <sys/param.h> 44 45#include <sys/condvar.h> 46#include <sys/cpu.h> 47#include <sys/evcnt.h> 48#include <sys/kmem.h> 49#include <sys/mutex.h> 50#include <sys/pserialize.h> 51#include <sys/proc.h> 52#include <sys/queue.h> 53#include <sys/xcall.h> 54 55struct pserialize { 56 TAILQ_ENTRY(pserialize) psz_chain; 57 lwp_t * psz_owner; 58 kcpuset_t * psz_target; 59 kcpuset_t * psz_pass; 60}; 61 62static u_int psz_work_todo __cacheline_aligned; 63static kmutex_t psz_lock __cacheline_aligned; 64static struct evcnt psz_ev_excl __cacheline_aligned; 65 66/* 67 * As defined in "Method 1": 68 * q0: "0 MP checkpoints have occured". 69 * q1: "1 MP checkpoint has occured". 70 * q2: "2 MP checkpoints have occured". 71 */ 72static TAILQ_HEAD(, pserialize) psz_queue0 __cacheline_aligned; 73static TAILQ_HEAD(, pserialize) psz_queue1 __cacheline_aligned; 74static TAILQ_HEAD(, pserialize) psz_queue2 __cacheline_aligned; 75 76#ifdef LOCKDEBUG 77#include <sys/percpu.h> 78 79static percpu_t *psz_debug_nreads __cacheline_aligned; 80#endif 81 82/* 83 * pserialize_init: 84 * 85 * Initialize passive serialization structures. 86 */ 87void 88pserialize_init(void) 89{ 90 91 psz_work_todo = 0; 92 TAILQ_INIT(&psz_queue0); 93 TAILQ_INIT(&psz_queue1); 94 TAILQ_INIT(&psz_queue2); 95 mutex_init(&psz_lock, MUTEX_DEFAULT, IPL_SCHED); 96 evcnt_attach_dynamic(&psz_ev_excl, EVCNT_TYPE_MISC, NULL, 97 "pserialize", "exclusive access"); 98#ifdef LOCKDEBUG 99 psz_debug_nreads = percpu_alloc(sizeof(uint32_t)); 100#endif 101} 102 103/* 104 * pserialize_create: 105 * 106 * Create and initialize a passive serialization object. 107 */ 108pserialize_t 109pserialize_create(void) 110{ 111 pserialize_t psz; 112 113 psz = kmem_zalloc(sizeof(struct pserialize), KM_SLEEP); 114 kcpuset_create(&psz->psz_target, true); 115 kcpuset_create(&psz->psz_pass, true); 116 psz->psz_owner = NULL; 117 118 return psz; 119} 120 121/* 122 * pserialize_destroy: 123 * 124 * Destroy a passive serialization object. 125 */ 126void 127pserialize_destroy(pserialize_t psz) 128{ 129 130 KASSERT(psz->psz_owner == NULL); 131 132 kcpuset_destroy(psz->psz_target); 133 kcpuset_destroy(psz->psz_pass); 134 kmem_free(psz, sizeof(struct pserialize)); 135} 136 137/* 138 * pserialize_perform: 139 * 140 * Perform the write side of passive serialization. The calling 141 * thread holds an exclusive lock on the data object(s) being updated. 142 * We wait until every processor in the system has made at least two 143 * passes through cpu_switchto(). The wait is made with the caller's 144 * update lock held, but is short term. 145 */ 146void 147pserialize_perform(pserialize_t psz) 148{ 149 int n; 150 151 KASSERT(!cpu_intr_p()); 152 KASSERT(!cpu_softintr_p()); 153 154 if (__predict_false(panicstr != NULL)) { 155 return; 156 } 157 KASSERT(psz->psz_owner == NULL); 158 KASSERT(ncpu > 0); 159 160 if (__predict_false(mp_online == false)) { 161 psz_ev_excl.ev_count++; 162 return; 163 } 164 165 /* 166 * Set up the object and put it onto the queue. The lock 167 * activity here provides the necessary memory barrier to 168 * make the caller's data update completely visible to 169 * other processors. 170 */ 171 psz->psz_owner = curlwp; 172 kcpuset_copy(psz->psz_target, kcpuset_running); 173 kcpuset_zero(psz->psz_pass); 174 175 mutex_spin_enter(&psz_lock); 176 TAILQ_INSERT_TAIL(&psz_queue0, psz, psz_chain); 177 psz_work_todo++; 178 179 n = 0; 180 do { 181 mutex_spin_exit(&psz_lock); 182 183 /* 184 * Force some context switch activity on every CPU, as 185 * the system may not be busy. Pause to not flood. 186 */ 187 if (n++ > 1) 188 kpause("psrlz", false, 1, NULL); 189 xc_barrier(XC_HIGHPRI); 190 191 mutex_spin_enter(&psz_lock); 192 } while (!kcpuset_iszero(psz->psz_target)); 193 194 psz_ev_excl.ev_count++; 195 mutex_spin_exit(&psz_lock); 196 197 psz->psz_owner = NULL; 198} 199 200int 201pserialize_read_enter(void) 202{ 203 int s; 204 205 KASSERT(!cpu_intr_p()); 206 s = splsoftserial(); 207#ifdef LOCKDEBUG 208 { 209 uint32_t *nreads; 210 nreads = percpu_getref(psz_debug_nreads); 211 (*nreads)++; 212 if (*nreads == 0) 213 panic("nreads overflow"); 214 percpu_putref(psz_debug_nreads); 215 } 216#endif 217 return s; 218} 219 220void 221pserialize_read_exit(int s) 222{ 223 224#ifdef LOCKDEBUG 225 { 226 uint32_t *nreads; 227 nreads = percpu_getref(psz_debug_nreads); 228 (*nreads)--; 229 if (*nreads == UINT_MAX) 230 panic("nreads underflow"); 231 percpu_putref(psz_debug_nreads); 232 } 233#endif 234 splx(s); 235} 236 237/* 238 * pserialize_switchpoint: 239 * 240 * Monitor system context switch activity. Called from machine 241 * independent code after mi_switch() returns. 242 */ 243void 244pserialize_switchpoint(void) 245{ 246 pserialize_t psz, next; 247 cpuid_t cid; 248 249 /* 250 * If no updates pending, bail out. No need to lock in order to 251 * test psz_work_todo; the only ill effect of missing an update 252 * would be to delay LWPs waiting in pserialize_perform(). That 253 * will not happen because updates are on the queue before an 254 * xcall is generated (serialization) to tickle every CPU. 255 */ 256 if (__predict_true(psz_work_todo == 0)) { 257 return; 258 } 259 mutex_spin_enter(&psz_lock); 260 cid = cpu_index(curcpu()); 261 262 /* 263 * At first, scan through the second queue and update each request, 264 * if passed all processors, then transfer to the third queue. 265 */ 266 for (psz = TAILQ_FIRST(&psz_queue1); psz != NULL; psz = next) { 267 next = TAILQ_NEXT(psz, psz_chain); 268 kcpuset_set(psz->psz_pass, cid); 269 if (!kcpuset_match(psz->psz_pass, psz->psz_target)) { 270 continue; 271 } 272 kcpuset_zero(psz->psz_pass); 273 TAILQ_REMOVE(&psz_queue1, psz, psz_chain); 274 TAILQ_INSERT_TAIL(&psz_queue2, psz, psz_chain); 275 } 276 /* 277 * Scan through the first queue and update each request, 278 * if passed all processors, then move to the second queue. 279 */ 280 for (psz = TAILQ_FIRST(&psz_queue0); psz != NULL; psz = next) { 281 next = TAILQ_NEXT(psz, psz_chain); 282 kcpuset_set(psz->psz_pass, cid); 283 if (!kcpuset_match(psz->psz_pass, psz->psz_target)) { 284 continue; 285 } 286 kcpuset_zero(psz->psz_pass); 287 TAILQ_REMOVE(&psz_queue0, psz, psz_chain); 288 TAILQ_INSERT_TAIL(&psz_queue1, psz, psz_chain); 289 } 290 /* 291 * Process the third queue: entries have been seen twice on every 292 * processor, remove from the queue and notify the updating thread. 293 */ 294 while ((psz = TAILQ_FIRST(&psz_queue2)) != NULL) { 295 TAILQ_REMOVE(&psz_queue2, psz, psz_chain); 296 kcpuset_zero(psz->psz_target); 297 psz_work_todo--; 298 } 299 mutex_spin_exit(&psz_lock); 300} 301 302/* 303 * pserialize_in_read_section: 304 * 305 * True if the caller is in a pserialize read section. To be used only 306 * for diagnostic assertions where we want to guarantee the condition like: 307 * 308 * KASSERT(pserialize_in_read_section()); 309 */ 310bool 311pserialize_in_read_section(void) 312{ 313#ifdef LOCKDEBUG 314 uint32_t *nreads; 315 bool in; 316 317 /* Not initialized yet */ 318 if (__predict_false(psz_debug_nreads == NULL)) 319 return true; 320 321 nreads = percpu_getref(psz_debug_nreads); 322 in = *nreads != 0; 323 percpu_putref(psz_debug_nreads); 324 325 return in; 326#else 327 return true; 328#endif 329} 330 331/* 332 * pserialize_not_in_read_section: 333 * 334 * True if the caller is not in a pserialize read section. To be used only 335 * for diagnostic assertions where we want to guarantee the condition like: 336 * 337 * KASSERT(pserialize_not_in_read_section()); 338 */ 339bool 340pserialize_not_in_read_section(void) 341{ 342#ifdef LOCKDEBUG 343 uint32_t *nreads; 344 bool notin; 345 346 /* Not initialized yet */ 347 if (__predict_false(psz_debug_nreads == NULL)) 348 return true; 349 350 nreads = percpu_getref(psz_debug_nreads); 351 notin = *nreads == 0; 352 percpu_putref(psz_debug_nreads); 353 354 return notin; 355#else 356 return true; 357#endif 358} 359