Module Name: src Committed By: rmind Date: Thu Feb 17 18:32:29 UTC 2011
Modified Files: src/sys/conf: files src/sys/sys: cpu_data.h lwp.h Added Files: src/sys/kern: subr_pcu.c src/sys/sys: pcu.h Log Message: Add PCU (Per-CPU Unit) - an interface to manage synchronization of any per CPU context tied with an LWP. Main use - lazy FPU handling on SMP. Requested by matt@, will be used on mips64. Note: implementation will be improved to use IPIs before adopting on x86. OK ad@. To generate a diff of this commit: cvs rdiff -u -r1.998 -r1.999 src/sys/conf/files cvs rdiff -u -r0 -r1.1 src/sys/kern/subr_pcu.c cvs rdiff -u -r1.32 -r1.33 src/sys/sys/cpu_data.h cvs rdiff -u -r1.142 -r1.143 src/sys/sys/lwp.h cvs rdiff -u -r0 -r1.1 src/sys/sys/pcu.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/conf/files diff -u src/sys/conf/files:1.998 src/sys/conf/files:1.999 --- src/sys/conf/files:1.998 Wed Feb 9 21:21:32 2011 +++ src/sys/conf/files Thu Feb 17 18:32:29 2011 @@ -1,4 +1,4 @@ -# $NetBSD: files,v 1.998 2011/02/09 21:21:32 macallan Exp $ +# $NetBSD: files,v 1.999 2011/02/17 18:32:29 rmind Exp $ # @(#)files.newconf 7.5 (Berkeley) 5/10/93 version 20100430 @@ -1533,6 +1533,7 @@ file kern/subr_lwp_specificdata.c file kern/subr_once.c file kern/subr_optstr.c +file kern/subr_pcu.c file kern/subr_percpu.c file kern/subr_pool.c file kern/subr_prf.c Index: src/sys/sys/cpu_data.h diff -u src/sys/sys/cpu_data.h:1.32 src/sys/sys/cpu_data.h:1.33 --- src/sys/sys/cpu_data.h:1.32 Mon Dec 20 00:25:47 2010 +++ src/sys/sys/cpu_data.h Thu Feb 17 18:32:29 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: cpu_data.h,v 1.32 2010/12/20 00:25:47 matt Exp $ */ +/* $NetBSD: cpu_data.h,v 1.33 2011/02/17 18:32:29 rmind Exp $ */ /*- * Copyright (c) 2004, 2006, 2007, 2008 The NetBSD Foundation, Inc. @@ -39,6 +39,7 @@ #include <sys/sched.h> /* for schedstate_percpu */ #include <sys/condvar.h> +#include <sys/pcu.h> #include <sys/percpu_types.h> #include <sys/queue.h> @@ -75,6 +76,8 @@ cpuid_t cpu_core_id; cpuid_t cpu_smt_id; + struct lwp * volatile cpu_pcu_curlwp[PCU_UNIT_COUNT]; + /* * This section is mostly CPU-private. */ @@ -116,6 +119,7 @@ #define ci_lockstat ci_data.cpu_lockstat #define ci_spin_locks2 ci_data.cpu_spin_locks2 #define ci_lkdebug_recurse ci_data.cpu_lkdebug_recurse +#define ci_pcu_curlwp ci_data.cpu_pcu_curlwp #define ci_package_id ci_data.cpu_package_id #define ci_core_id ci_data.cpu_core_id Index: src/sys/sys/lwp.h diff -u src/sys/sys/lwp.h:1.142 src/sys/sys/lwp.h:1.143 --- src/sys/sys/lwp.h:1.142 Fri Jan 28 16:58:27 2011 +++ src/sys/sys/lwp.h Thu Feb 17 18:32:29 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: lwp.h,v 1.142 2011/01/28 16:58:27 pooka Exp $ */ +/* $NetBSD: lwp.h,v 1.143 2011/02/17 18:32:29 rmind Exp $ */ /*- * Copyright (c) 2001, 2006, 2007, 2008, 2009, 2010 @@ -39,6 +39,7 @@ #include <sys/callout.h> #include <sys/mutex.h> #include <sys/condvar.h> +#include <sys/pcu.h> #include <sys/signalvar.h> #include <sys/sched.h> #include <sys/specificdata.h> @@ -123,6 +124,9 @@ callout_t l_timeout_ch; /* !: callout for tsleep */ u_int l_emap_gen; /* !: emap generation number */ + struct cpu_info * volatile l_pcu_cpu[PCU_UNIT_COUNT]; + uint32_t l_pcu_used; + /* Process level and global state, misc. */ LIST_ENTRY(lwp) l_list; /* a: entry on list of all LWPs */ void *l_ctxlink; /* p: uc_link {get,set}context */ Added files: Index: src/sys/kern/subr_pcu.c diff -u /dev/null src/sys/kern/subr_pcu.c:1.1 --- /dev/null Thu Feb 17 18:32:29 2011 +++ src/sys/kern/subr_pcu.c Thu Feb 17 18:32:29 2011 @@ -0,0 +1,258 @@ +/* $NetBSD: subr_pcu.c,v 1.1 2011/02/17 18:32:29 rmind Exp $ */ + +/*- + * Copyright (c) 2011 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Mindaugas Rasiukevicius. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Per CPU Unit (PCU) - is an interface to manage synchronization of any + * per CPU context (unit) tied with LWP context. Typical use: FPU state. + * + * Concurrency notes: + * + * PCU state may be loaded only by the current LWP, that is, curlwp. + * Therefore, only LWP itself can set a CPU for lwp_t::l_pcu_cpu[id]. + * + * Request for a PCU release can be from owner LWP (whether PCU state + * is on current CPU or remote CPU) or any other LWP running on that + * CPU (in such case, owner LWP is on a remote CPU or sleeping). + * + * In any case, PCU state can only be changed from the running CPU. + * If said PCU state is on the remote CPU, a cross-call will be sent + * by the owner LWP. Therefore struct cpu_info::ci_pcu_curlwp[id] + * may only be changed by current CPU, and lwp_t::l_pcu_cpu[id] may + * only be unset by the CPU which has PCU state loaded. + * + * There is a race condition: LWP may have a PCU state on a remote CPU, + * which it requests to be released via cross-call. At the same time, + * other LWP on remote CPU might release existing PCU state and load + * its own one. Cross-call may arrive after this and release different + * PCU state than intended. In such case, such LWP would re-load its + * PCU state again. + */ + +#include <sys/cdefs.h> +__KERNEL_RCSID(0, "$NetBSD: subr_pcu.c,v 1.1 2011/02/17 18:32:29 rmind Exp $"); + +#include <sys/param.h> +#include <sys/cpu.h> +#include <sys/lwp.h> +#include <sys/pcu.h> +#include <sys/xcall.h> + +#define PCU_SAVE 0x01 /* Save PCU state to the LWP. */ +#define PCU_RELEASE 0x02 /* Release PCU state on the CPU. */ + +#if 0 +/* + * pcu_init_lwp: initialize PCU structures for LWP. + */ +void +pcu_init_lwp(lwp_t *l) +{ + + memset(l->l_pcu_cpu, 0, sizeof(uint32_t) * PCU_UNIT_COUNT); + l->l_pcu_used = 0; +} +#endif + +/* + * pcu_cpu_op: save/release PCU state on the current CPU. + * + * => Must be called at IPL_SOFTCLOCK or from the soft-interrupt. + */ +static void +pcu_cpu_op(const pcu_ops_t *pcu, const int flags) +{ + const u_int id = pcu->pcu_id; + struct cpu_info *ci = curcpu(); + lwp_t *l = ci->ci_pcu_curlwp[id]; + + /* If no state - nothing to do. */ + if (l == NULL) { + return; + } + if (flags & PCU_SAVE) { + pcu->pcu_state_save(l); + } + if (flags & PCU_RELEASE) { + ci->ci_pcu_curlwp[id] = NULL; + l->l_pcu_cpu[id] = NULL; + } +} + +/* + * pcu_lwp_op: perform PCU state save, release or both operations on LWP. + */ +static void +pcu_lwp_op(const pcu_ops_t *pcu, lwp_t *l, int flags) +{ + const u_int id = pcu->pcu_id; + struct cpu_info *ci; + uint64_t where; + int s; + + /* + * Caller should have re-checked if there is any state to manage. + * Block the interrupts and inspect again, since cross-call sent + * by remote CPU could have changed the state. + */ + s = splsoftclock(); + ci = l->l_pcu_cpu[id]; + if (ci == curcpu()) { + /* + * State is on the current CPU - just perform the operations. + */ + KASSERT(ci->ci_pcu_curlwp[id] == l); + + if (flags & PCU_SAVE) { + pcu->pcu_state_save(l); + } + if (flags & PCU_RELEASE) { + ci->ci_pcu_curlwp[id] = NULL; + l->l_pcu_cpu[id] = NULL; + } + splx(s); + return; + } + splx(s); + + if (__predict_false(ci == NULL)) { + /* Cross-call has won the race - no state to manage. */ + return; + } + + /* + * State is on the remote CPU - perform the operations there. + * Note: there is a race condition; see description in the top. + */ + where = xc_unicast(XC_HIGHPRI, (xcfunc_t)pcu_cpu_op, + __UNCONST(pcu), (void *)(uintptr_t)flags, ci); + xc_wait(where); + + KASSERT((flags & PCU_RELEASE) == 0 || l->l_pcu_cpu[id] == NULL); +} + +/* + * pcu_load: load/initialize the PCU state of current LWP on current CPU. + */ +void +pcu_load(const pcu_ops_t *pcu) +{ + const u_int id = pcu->pcu_id; + struct cpu_info *ci, *curci; + lwp_t *l = curlwp; + uint64_t where; + int s; + + KASSERT(!cpu_intr_p() && !cpu_softintr_p()); + + s = splsoftclock(); + curci = curcpu(); + ci = l->l_pcu_cpu[id]; + + /* Does this CPU already have our PCU state loaded? */ + if (ci == curci) { + KASSERT(curci->ci_pcu_curlwp[id] == l); + splx(s); + return; + } + + /* If PCU state of this LWP is on the remote CPU - save it there. */ + if (ci) { + splx(s); + /* Note: there is a race; see description in the top. */ + where = xc_unicast(XC_HIGHPRI, (xcfunc_t)pcu_cpu_op, + __UNCONST(pcu), (void *)(PCU_SAVE | PCU_RELEASE), ci); + xc_wait(where); + + /* Enter IPL_SOFTCLOCK and re-fetch the current CPU. */ + s = splsoftclock(); + curci = curcpu(); + } + KASSERT(l->l_pcu_cpu[id] == NULL); + + /* Save the PCU state on the current CPU, if there is any. */ + pcu_cpu_op(pcu, PCU_SAVE | PCU_RELEASE); + KASSERT(curci->ci_pcu_curlwp[id] == NULL); + + /* + * Finally, load the state for this LWP on this CPU. Indicate to + * load function whether PCU was used before. Note the usage. + */ + pcu->pcu_state_load(l, ((1 << id) & l->l_pcu_used) != 0); + curci->ci_pcu_curlwp[id] = l; + l->l_pcu_cpu[id] = curci; + l->l_pcu_used |= (1 << id); + splx(s); +} + +/* + * pcu_discard: discard the PCU state of current LWP. + */ +void +pcu_discard(const pcu_ops_t *pcu) +{ + const u_int id = pcu->pcu_id; + lwp_t *l = curlwp; + + KASSERT(!cpu_intr_p() && !cpu_softintr_p()); + + if (__predict_true(l->l_pcu_cpu[id] == NULL)) { + return; + } + pcu_lwp_op(pcu, l, PCU_RELEASE); + l->l_pcu_used &= ~(1 << id); +} + +/* + * pcu_save_lwp: save PCU state to the given LWP. + */ +void +pcu_save_lwp(const pcu_ops_t *pcu, lwp_t *l) +{ + const u_int id = pcu->pcu_id; + + KASSERT(!cpu_intr_p() && !cpu_softintr_p()); + + if (__predict_true(l->l_pcu_cpu[id] == NULL)) { + return; + } + pcu_lwp_op(pcu, l, PCU_SAVE | PCU_RELEASE); +} + +/* + * pcu_used: return true if PCU was used (pcu_load() case) by the LWP. + */ +bool +pcu_used(const pcu_ops_t *pcu, lwp_t *l) +{ + const u_int id = pcu->pcu_id; + + return l->l_pcu_used & (1 << id); +} Index: src/sys/sys/pcu.h diff -u /dev/null src/sys/sys/pcu.h:1.1 --- /dev/null Thu Feb 17 18:32:29 2011 +++ src/sys/sys/pcu.h Thu Feb 17 18:32:29 2011 @@ -0,0 +1,57 @@ +/* $NetBSD: pcu.h,v 1.1 2011/02/17 18:32:29 rmind Exp $ */ + +/*- + * Copyright (c) 2011 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Mindaugas Rasiukevicius. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _SYS_PCU_H_ +#define _SYS_PCU_H_ + +#if !defined(_KERNEL) && !defined(_KMEMUSER) +#error "Kernel-only header" +#endif + +/* + * Default: one PCU for MD. + */ +#ifndef PCU_UNIT_COUNT +#define PCU_UNIT_COUNT 1 +#endif + +typedef struct { + u_int pcu_id; + void (*pcu_state_save)(lwp_t *); + void (*pcu_state_load)(lwp_t *, bool); +} pcu_ops_t; + +void pcu_load(const pcu_ops_t *); +void pcu_save_lwp(const pcu_ops_t *, lwp_t *); +void pcu_discard(const pcu_ops_t *); +bool pcu_used(const pcu_ops_t *, lwp_t *); + +#endif