subr_pserialize.c revision 1.2
1/* $NetBSD: subr_pserialize.c,v 1.2 2011/08/01 15:26:31 he 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.2 2011/08/01 15:26:31 he 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/queue.h> 52#include <sys/xcall.h> 53 54struct pserialize { 55 TAILQ_ENTRY(pserialize) psz_chain; 56 lwp_t * psz_owner; 57 kcondvar_t psz_notifier; 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/* 77 * pserialize_init: 78 * 79 * Initialize passive serialization structures. 80 */ 81void 82pserialize_init(void) 83{ 84 85 psz_work_todo = 0; 86 TAILQ_INIT(&psz_queue0); 87 TAILQ_INIT(&psz_queue1); 88 TAILQ_INIT(&psz_queue2); 89 mutex_init(&psz_lock, MUTEX_DEFAULT, IPL_SCHED); 90 evcnt_attach_dynamic(&psz_ev_excl, EVCNT_TYPE_MISC, NULL, 91 "pserialize", "exclusive access"); 92} 93 94/* 95 * pserialize_create: 96 * 97 * Create and initialize a passive serialization object. 98 */ 99pserialize_t 100pserialize_create(void) 101{ 102 pserialize_t psz; 103 104 psz = kmem_zalloc(sizeof(struct pserialize), KM_SLEEP); 105 cv_init(&psz->psz_notifier, "psrlz"); 106 psz->psz_target = kcpuset_create(); 107 psz->psz_pass = kcpuset_create(); 108 psz->psz_owner = NULL; 109 110 return psz; 111} 112 113/* 114 * pserialize_destroy: 115 * 116 * Destroy a passive serialization object. 117 */ 118void 119pserialize_destroy(pserialize_t psz) 120{ 121 122 KASSERT(psz->psz_owner == NULL); 123 124 cv_destroy(&psz->psz_notifier); 125 kcpuset_destroy(psz->psz_target); 126 kcpuset_destroy(psz->psz_pass); 127 kmem_free(psz, sizeof(struct pserialize)); 128} 129 130/* 131 * pserialize_perform: 132 * 133 * Perform the write side of passive serialization. The calling 134 * thread holds an exclusive lock on the data object(s) being updated. 135 * We wait until every processor in the system has made at least two 136 * passes through cpu_swichto(). The wait is made with the caller's 137 * update lock held, but is short term. 138 */ 139void 140pserialize_perform(pserialize_t psz) 141{ 142 143 KASSERT(!cpu_intr_p()); 144 KASSERT(!cpu_softintr_p()); 145 146 if (__predict_false(panicstr != NULL)) { 147 return; 148 } 149 KASSERT(psz->psz_owner == NULL); 150 KASSERT(kcpuset_iszero(psz->psz_target)); 151 KASSERT(ncpu > 0); 152 153 /* 154 * Set up the object and put it onto the queue. The lock 155 * activity here provides the necessary memory barrier to 156 * make the caller's data update completely visible to 157 * other processors. 158 */ 159 psz->psz_owner = curlwp; 160 kcpuset_fill(psz->psz_target); 161 kcpuset_zero(psz->psz_pass); 162 163 mutex_spin_enter(&psz_lock); 164 TAILQ_INSERT_TAIL(&psz_queue0, psz, psz_chain); 165 psz_work_todo++; 166 mutex_spin_exit(&psz_lock); 167 168 /* 169 * Force some context switch activity on every CPU, as the system 170 * may not be busy. Note: should pass the point twice. 171 */ 172 xc_broadcast(XC_HIGHPRI, (xcfunc_t)nullop, NULL, NULL); 173 xc_broadcast(XC_HIGHPRI, (xcfunc_t)nullop, NULL, NULL); 174 175 /* 176 * Wait for all CPUs to cycle through mi_switch() twice. 177 * The last one through will remove our update from the 178 * queue and awaken us. 179 */ 180 mutex_spin_enter(&psz_lock); 181 while (!kcpuset_iszero(psz->psz_target)) { 182 cv_wait(&psz->psz_notifier, &psz_lock); 183 } 184 psz_ev_excl.ev_count++; 185 mutex_spin_exit(&psz_lock); 186 187 psz->psz_owner = NULL; 188} 189 190int 191pserialize_read_enter(void) 192{ 193 194 KASSERT(!cpu_intr_p()); 195 return splsoftclock(); 196} 197 198void 199pserialize_read_exit(int s) 200{ 201 202 splx(s); 203} 204 205/* 206 * pserialize_switchpoint: 207 * 208 * Monitor system context switch activity. Called from machine 209 * independent code after mi_switch() returns. 210 */ 211void 212pserialize_switchpoint(void) 213{ 214 pserialize_t psz, next; 215 cpuid_t cid; 216 217 /* 218 * If no updates pending, bail out. No need to lock in order to 219 * test psz_work_todo; the only ill effect of missing an update 220 * would be to delay LWPs waiting in pserialize_perform(). That 221 * will not happen because updates are on the queue before an 222 * xcall is generated (serialization) to tickle every CPU. 223 */ 224 if (__predict_true(psz_work_todo == 0)) { 225 return; 226 } 227 mutex_spin_enter(&psz_lock); 228 cid = cpu_index(curcpu()); 229 230 /* 231 * At first, scan through the second queue and update each request, 232 * if passed all processors, then transfer to the third queue. 233 */ 234 for (psz = TAILQ_FIRST(&psz_queue1); psz != NULL; psz = next) { 235 next = TAILQ_NEXT(psz, psz_chain); 236 if (!kcpuset_match(psz->psz_pass, psz->psz_target)) { 237 kcpuset_set(cid, psz->psz_pass); 238 continue; 239 } 240 kcpuset_zero(psz->psz_pass); 241 TAILQ_REMOVE(&psz_queue1, psz, psz_chain); 242 TAILQ_INSERT_TAIL(&psz_queue2, psz, psz_chain); 243 } 244 /* 245 * Scan through the first queue and update each request, 246 * if passed all processors, then move to the second queue. 247 */ 248 for (psz = TAILQ_FIRST(&psz_queue0); psz != NULL; psz = next) { 249 next = TAILQ_NEXT(psz, psz_chain); 250 if (!kcpuset_match(psz->psz_pass, psz->psz_target)) { 251 kcpuset_set(cid, psz->psz_pass); 252 continue; 253 } 254 kcpuset_zero(psz->psz_pass); 255 TAILQ_REMOVE(&psz_queue0, psz, psz_chain); 256 TAILQ_INSERT_TAIL(&psz_queue1, psz, psz_chain); 257 } 258 /* 259 * Process the third queue: entries have been seen twice on every 260 * processor, remove from the queue and notify the updating thread. 261 */ 262 while ((psz = TAILQ_FIRST(&psz_queue2)) != NULL) { 263 TAILQ_REMOVE(&psz_queue2, psz, psz_chain); 264 kcpuset_zero(psz->psz_target); 265 cv_signal(&psz->psz_notifier); 266 psz_work_todo--; 267 } 268 mutex_spin_exit(&psz_lock); 269} 270