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