Home | History | Annotate | Line # | Download | only in kern
subr_pcu.c revision 1.10
      1 /*	$NetBSD: subr_pcu.c,v 1.10 2011/09/27 01:02:39 jym Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Mindaugas Rasiukevicius.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Per CPU Unit (PCU) - is an interface to manage synchronization of any
     34  * per CPU context (unit) tied with LWP context.  Typical use: FPU state.
     35  *
     36  * Concurrency notes:
     37  *
     38  *	PCU state may be loaded only by the current LWP, that is, curlwp.
     39  *	Therefore, only LWP itself can set a CPU for lwp_t::l_pcu_cpu[id].
     40  *
     41  *	Request for a PCU release can be from owner LWP (whether PCU state
     42  *	is on current CPU or remote CPU) or any other LWP running on that
     43  *	CPU (in such case, owner LWP is on a remote CPU or sleeping).
     44  *
     45  *	In any case, PCU state can only be changed from the running CPU.
     46  *	If said PCU state is on the remote CPU, a cross-call will be sent
     47  *	by the owner LWP.  Therefore struct cpu_info::ci_pcu_curlwp[id]
     48  *	may only be changed by current CPU, and lwp_t::l_pcu_cpu[id] may
     49  *	only be unset by the CPU which has PCU state loaded.
     50  *
     51  *	There is a race condition: LWP may have a PCU state on a remote CPU,
     52  *	which it requests to be released via cross-call.  At the same time,
     53  *	other LWP on remote CPU might release existing PCU state and load
     54  *	its own one.  Cross-call may arrive after this and release different
     55  *	PCU state than intended.  In such case, such LWP would re-load its
     56  *	PCU state again.
     57  */
     58 
     59 #include <sys/cdefs.h>
     60 __KERNEL_RCSID(0, "$NetBSD: subr_pcu.c,v 1.10 2011/09/27 01:02:39 jym Exp $");
     61 
     62 #include <sys/param.h>
     63 #include <sys/cpu.h>
     64 #include <sys/lwp.h>
     65 #include <sys/pcu.h>
     66 #include <sys/xcall.h>
     67 
     68 #if PCU_UNIT_COUNT > 0
     69 
     70 static void pcu_lwp_op(const pcu_ops_t *, lwp_t *, int);
     71 
     72 #define	PCU_SAVE		0x01	/* Save PCU state to the LWP. */
     73 #define	PCU_RELEASE		0x02	/* Release PCU state on the CPU. */
     74 
     75 /* XXX */
     76 extern const pcu_ops_t * const	pcu_ops_md_defs[];
     77 
     78 void
     79 pcu_switchpoint(lwp_t *l)
     80 {
     81 	const uint32_t pcu_inuse = l->l_pcu_used;
     82 	u_int id;
     83 	/* int s; */
     84 
     85 	KASSERT(l == curlwp);
     86 
     87 	if (__predict_true(pcu_inuse == 0)) {
     88 		/* PCUs are not in use. */
     89 		return;
     90 	}
     91 	/* s = splsoftclock(); */
     92 	for (id = 0; id < PCU_UNIT_COUNT; id++) {
     93 		if ((pcu_inuse & (1 << id)) == 0) {
     94 			continue;
     95 		}
     96 		struct cpu_info * const pcu_ci = l->l_pcu_cpu[id];
     97 		if (pcu_ci == NULL || pcu_ci == l->l_cpu) {
     98 			continue;
     99 		}
    100 		const pcu_ops_t * const pcu = pcu_ops_md_defs[id];
    101 		pcu->pcu_state_release(l);
    102 	}
    103 	/* splx(s); */
    104 }
    105 
    106 void
    107 pcu_discard_all(lwp_t *l)
    108 {
    109 	const uint32_t pcu_inuse = l->l_pcu_used;
    110 
    111 	KASSERT(l == curlwp || ((l->l_flag & LW_SYSTEM) && pcu_inuse == 0));
    112 
    113 	if (__predict_true(pcu_inuse == 0)) {
    114 		/* PCUs are not in use. */
    115 		return;
    116 	}
    117 	const int s = splsoftclock();
    118 	for (u_int id = 0; id < PCU_UNIT_COUNT; id++) {
    119 		if ((pcu_inuse & (1 << id)) == 0) {
    120 			continue;
    121 		}
    122 		if (__predict_true(l->l_pcu_cpu[id] == NULL)) {
    123 			continue;
    124 		}
    125 		const pcu_ops_t * const pcu = pcu_ops_md_defs[id];
    126 		/*
    127 		 * We aren't releasing since this LWP isn't giving up PCU,
    128 		 * just saving it.
    129 		 */
    130 		pcu_lwp_op(pcu, l, PCU_RELEASE);
    131 	}
    132 	l->l_pcu_used = 0;
    133 	splx(s);
    134 }
    135 
    136 void
    137 pcu_save_all(lwp_t *l)
    138 {
    139 	const uint32_t pcu_inuse = l->l_pcu_used;
    140 	const int flags = PCU_SAVE | (l->l_flag & LW_WCORE ? PCU_RELEASE : 0);
    141 
    142 	/*
    143 	 * Normally we save for the current LWP, but sometimes we get called
    144 	 * with a different LWP (forking a system LWP or doing a coredump of
    145 	 * a process with multiple threads) and we need to deal with that.
    146 	 */
    147 	KASSERT(l == curlwp
    148 	    || (((l->l_flag & LW_SYSTEM)
    149 		 || (curlwp->l_proc == l->l_proc && l->l_stat == LSSUSPENDED))
    150 	        && pcu_inuse == 0));
    151 
    152 	if (__predict_true(pcu_inuse == 0)) {
    153 		/* PCUs are not in use. */
    154 		return;
    155 	}
    156 	const int s = splsoftclock();
    157 	for (u_int id = 0; id < PCU_UNIT_COUNT; id++) {
    158 		if ((pcu_inuse & (1 << id)) == 0) {
    159 			continue;
    160 		}
    161 		if (__predict_true(l->l_pcu_cpu[id] == NULL)) {
    162 			continue;
    163 		}
    164 		const pcu_ops_t * const pcu = pcu_ops_md_defs[id];
    165 		/*
    166 		 * We aren't releasing since this LWP isn't giving up PCU,
    167 		 * just saving it.
    168 		 */
    169 		pcu_lwp_op(pcu, l, flags);
    170 	}
    171 	splx(s);
    172 }
    173 
    174 /*
    175  * pcu_do_op: save/release PCU state on the current CPU.
    176  *
    177  * => Must be called at IPL_SOFTCLOCK or from the soft-interrupt.
    178  */
    179 static inline void
    180 pcu_do_op(const pcu_ops_t *pcu, lwp_t * const l, const int flags)
    181 {
    182 	struct cpu_info * const ci = curcpu();
    183 	const u_int id = pcu->pcu_id;
    184 
    185 	KASSERT(l->l_pcu_cpu[id] == ci);
    186 
    187 	if (flags & PCU_SAVE) {
    188 		pcu->pcu_state_save(l);
    189 	}
    190 	if (flags & PCU_RELEASE) {
    191 		pcu->pcu_state_release(l);
    192 		ci->ci_pcu_curlwp[id] = NULL;
    193 		l->l_pcu_cpu[id] = NULL;
    194 	}
    195 }
    196 
    197 /*
    198  * pcu_cpu_op: helper routine to call pcu_do_op() via xcall(9) or
    199  * by pcu_load.
    200  */
    201 static void
    202 pcu_cpu_op(const pcu_ops_t *pcu, const int flags)
    203 {
    204 	const u_int id = pcu->pcu_id;
    205 	lwp_t * const l = curcpu()->ci_pcu_curlwp[id];
    206 
    207 	//KASSERT(cpu_softintr_p());
    208 
    209 	/* If no state - nothing to do. */
    210 	if (l == NULL) {
    211 		return;
    212 	}
    213 	pcu_do_op(pcu, l, flags);
    214 }
    215 
    216 /*
    217  * pcu_lwp_op: perform PCU state save, release or both operations on LWP.
    218  */
    219 static void
    220 pcu_lwp_op(const pcu_ops_t *pcu, lwp_t *l, int flags)
    221 {
    222 	const u_int id = pcu->pcu_id;
    223 	struct cpu_info *ci;
    224 	uint64_t where;
    225 	int s;
    226 
    227 	/*
    228 	 * Caller should have re-checked if there is any state to manage.
    229 	 * Block the interrupts and inspect again, since cross-call sent
    230 	 * by remote CPU could have changed the state.
    231 	 */
    232 	s = splsoftclock();
    233 	ci = l->l_pcu_cpu[id];
    234 	if (ci == curcpu()) {
    235 		/*
    236 		 * State is on the current CPU - just perform the operations.
    237 		 */
    238 		KASSERTMSG(ci->ci_pcu_curlwp[id] == l,
    239 		    "%s: cpu%u: pcu_curlwp[%u] (%p) != l (%p)",
    240 		     __func__, cpu_index(ci), id, ci->ci_pcu_curlwp[id], l);
    241 		pcu_do_op(pcu, l, flags);
    242 		splx(s);
    243 		return;
    244 	}
    245 	splx(s);
    246 
    247 	if (__predict_false(ci == NULL)) {
    248 		/* Cross-call has won the race - no state to manage. */
    249 		return;
    250 	}
    251 
    252 	/*
    253 	 * State is on the remote CPU - perform the operations there.
    254 	 * Note: there is a race condition; see description in the top.
    255 	 */
    256 	where = xc_unicast(XC_HIGHPRI, (xcfunc_t)pcu_cpu_op,
    257 	    __UNCONST(pcu), (void *)(uintptr_t)flags, ci);
    258 	xc_wait(where);
    259 
    260 	KASSERT((flags & PCU_RELEASE) == 0 || l->l_pcu_cpu[id] == NULL);
    261 }
    262 
    263 /*
    264  * pcu_load: load/initialize the PCU state of current LWP on current CPU.
    265  */
    266 void
    267 pcu_load(const pcu_ops_t *pcu)
    268 {
    269 	const u_int id = pcu->pcu_id;
    270 	struct cpu_info *ci, *curci;
    271 	lwp_t * const l = curlwp;
    272 	uint64_t where;
    273 	int s;
    274 
    275 	KASSERT(!cpu_intr_p() && !cpu_softintr_p());
    276 
    277 	s = splsoftclock();
    278 	curci = curcpu();
    279 	ci = l->l_pcu_cpu[id];
    280 
    281 	/* Does this CPU already have our PCU state loaded? */
    282 	if (ci == curci) {
    283 		KASSERT(curci->ci_pcu_curlwp[id] == l);
    284 		splx(s);
    285 		return;
    286 	}
    287 
    288 	/* If PCU state of this LWP is on the remote CPU - save it there. */
    289 	if (ci) {
    290 		splx(s);
    291 		/* Note: there is a race; see description in the top. */
    292 		where = xc_unicast(XC_HIGHPRI, (xcfunc_t)pcu_cpu_op,
    293 		    __UNCONST(pcu), (void *)(PCU_SAVE | PCU_RELEASE), ci);
    294 		xc_wait(where);
    295 
    296 		/* Enter IPL_SOFTCLOCK and re-fetch the current CPU. */
    297 		s = splsoftclock();
    298 		curci = curcpu();
    299 	}
    300 	KASSERT(l->l_pcu_cpu[id] == NULL);
    301 
    302 	/* Save the PCU state on the current CPU, if there is any. */
    303 	pcu_cpu_op(pcu, PCU_SAVE | PCU_RELEASE);
    304 	KASSERT(curci->ci_pcu_curlwp[id] == NULL);
    305 
    306 	/*
    307 	 * Finally, load the state for this LWP on this CPU.  Indicate to
    308 	 * load function whether PCU was used before.  Note the usage.
    309 	 */
    310 	pcu->pcu_state_load(l, ((1 << id) & l->l_pcu_used) != 0);
    311 	curci->ci_pcu_curlwp[id] = l;
    312 	l->l_pcu_cpu[id] = curci;
    313 	l->l_pcu_used |= (1 << id);
    314 	splx(s);
    315 }
    316 
    317 /*
    318  * pcu_discard: discard the PCU state of current LWP.
    319  */
    320 void
    321 pcu_discard(const pcu_ops_t *pcu)
    322 {
    323 	const u_int id = pcu->pcu_id;
    324 	lwp_t * const l = curlwp;
    325 
    326 	KASSERT(!cpu_intr_p() && !cpu_softintr_p());
    327 
    328 	if (__predict_true(l->l_pcu_cpu[id] == NULL)) {
    329 		return;
    330 	}
    331 	pcu_lwp_op(pcu, l, PCU_RELEASE);
    332 	l->l_pcu_used &= ~(1 << id);
    333 }
    334 
    335 /*
    336  * pcu_save_lwp: save PCU state to the given LWP.
    337  */
    338 void
    339 pcu_save(const pcu_ops_t *pcu)
    340 {
    341 	const u_int id = pcu->pcu_id;
    342 	lwp_t * const l = curlwp;
    343 
    344 	KASSERT(!cpu_intr_p() && !cpu_softintr_p());
    345 
    346 	if (__predict_true(l->l_pcu_cpu[id] == NULL)) {
    347 		return;
    348 	}
    349 	pcu_lwp_op(pcu, l, PCU_SAVE | PCU_RELEASE);
    350 }
    351 
    352 /*
    353  * pcu_used: return true if PCU was used (pcu_load() case) by the LWP.
    354  */
    355 bool
    356 pcu_used_p(const pcu_ops_t *pcu)
    357 {
    358 	const u_int id = pcu->pcu_id;
    359 	lwp_t * const l = curlwp;
    360 
    361 	return l->l_pcu_used & (1 << id);
    362 }
    363 
    364 #endif /* PCU_UNIT_COUNT > 0 */
    365