Home | History | Annotate | Line # | Download | only in kern
subr_xcall.c revision 1.21
      1 /*	$NetBSD: subr_xcall.c,v 1.21 2018/02/01 03:15:29 ozaki-r Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2007-2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran and 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  * Cross call support
     34  *
     35  * Background
     36  *
     37  *	Sometimes it is necessary to modify hardware state that is tied
     38  *	directly to individual CPUs (such as a CPU's local timer), and
     39  *	these updates can not be done remotely by another CPU.  The LWP
     40  *	requesting the update may be unable to guarantee that it will be
     41  *	running on the CPU where the update must occur, when the update
     42  *	occurs.
     43  *
     44  *	Additionally, it's sometimes necessary to modify per-CPU software
     45  *	state from a remote CPU.  Where these update operations are so
     46  *	rare or the access to the per-CPU data so frequent that the cost
     47  *	of using locking or atomic operations to provide coherency is
     48  *	prohibitive, another way must be found.
     49  *
     50  *	Cross calls help to solve these types of problem by allowing
     51  *	any CPU in the system to request that an arbitrary function be
     52  *	executed on any other CPU.
     53  *
     54  * Implementation
     55  *
     56  *	A slow mechanism for making 'low priority' cross calls is
     57  *	provided.  The function to be executed runs on the remote CPU
     58  *	within a bound kthread.  No queueing is provided, and the
     59  *	implementation uses global state.  The function being called may
     60  *	block briefly on locks, but in doing so must be careful to not
     61  *	interfere with other cross calls in the system.  The function is
     62  *	called with thread context and not from a soft interrupt, so it
     63  *	can ensure that it is not interrupting other code running on the
     64  *	CPU, and so has exclusive access to the CPU.  Since this facility
     65  *	is heavyweight, it's expected that it will not be used often.
     66  *
     67  *	Cross calls must not allocate memory, as the pagedaemon uses
     68  *	them (and memory allocation may need to wait on the pagedaemon).
     69  *
     70  *	A low-overhead mechanism for high priority calls (XC_HIGHPRI) is
     71  *	also provided.  The function to be executed runs on a software
     72  *	interrupt context, at IPL_SOFTSERIAL level, and is expected to
     73  *	be very lightweight, e.g. avoid blocking.
     74  */
     75 
     76 #include <sys/cdefs.h>
     77 __KERNEL_RCSID(0, "$NetBSD: subr_xcall.c,v 1.21 2018/02/01 03:15:29 ozaki-r Exp $");
     78 
     79 #include <sys/types.h>
     80 #include <sys/param.h>
     81 #include <sys/xcall.h>
     82 #include <sys/mutex.h>
     83 #include <sys/condvar.h>
     84 #include <sys/evcnt.h>
     85 #include <sys/kthread.h>
     86 #include <sys/cpu.h>
     87 
     88 #ifdef _RUMPKERNEL
     89 #include "rump_private.h"
     90 #endif
     91 
     92 /* Cross-call state box. */
     93 typedef struct {
     94 	kmutex_t	xc_lock;
     95 	kcondvar_t	xc_busy;
     96 	xcfunc_t	xc_func;
     97 	void *		xc_arg1;
     98 	void *		xc_arg2;
     99 	uint64_t	xc_headp;
    100 	uint64_t	xc_donep;
    101 	unsigned int	xc_ipl;
    102 } xc_state_t;
    103 
    104 /* Bit indicating high (1) or low (0) priority. */
    105 #define	XC_PRI_BIT	(1ULL << 63)
    106 
    107 /* Low priority xcall structures. */
    108 static xc_state_t	xc_low_pri	__cacheline_aligned;
    109 
    110 /* High priority xcall structures. */
    111 static xc_state_t	xc_high_pri	__cacheline_aligned;
    112 static void *		xc_sihs[4]	__cacheline_aligned;
    113 
    114 /* Event counters. */
    115 static struct evcnt	xc_unicast_ev	__cacheline_aligned;
    116 static struct evcnt	xc_broadcast_ev	__cacheline_aligned;
    117 
    118 static void		xc_init(void);
    119 static void		xc_thread(void *);
    120 
    121 static inline uint64_t	xc_highpri(xcfunc_t, void *, void *, struct cpu_info *,
    122 			    unsigned int);
    123 static inline uint64_t	xc_lowpri(xcfunc_t, void *, void *, struct cpu_info *);
    124 
    125 /* The internal form of IPL */
    126 #define XC_IPL_MASK		0xff00
    127 /*
    128  * Assign 0 to XC_IPL_SOFTSERIAL to treat IPL_SOFTSERIAL as the default value
    129  * (just XC_HIGHPRI).
    130  */
    131 #define XC_IPL_SOFTSERIAL	0
    132 #define XC_IPL_SOFTBIO		1
    133 #define XC_IPL_SOFTCLOCK	2
    134 #define XC_IPL_SOFTNET		3
    135 #define XC_IPL_MAX		XC_IPL_SOFTNET
    136 
    137 CTASSERT(XC_IPL_MAX <= __arraycount(xc_sihs));
    138 
    139 /*
    140  * xc_init:
    141  *
    142  *	Initialize low and high priority cross-call structures.
    143  */
    144 static void
    145 xc_init(void)
    146 {
    147 	xc_state_t *xclo = &xc_low_pri, *xchi = &xc_high_pri;
    148 
    149 	memset(xclo, 0, sizeof(xc_state_t));
    150 	mutex_init(&xclo->xc_lock, MUTEX_DEFAULT, IPL_NONE);
    151 	cv_init(&xclo->xc_busy, "xclocv");
    152 
    153 	memset(xchi, 0, sizeof(xc_state_t));
    154 	mutex_init(&xchi->xc_lock, MUTEX_DEFAULT, IPL_SOFTSERIAL);
    155 	cv_init(&xchi->xc_busy, "xchicv");
    156 
    157 #define SETUP_SOFTINT(xipl, sipl) do {					\
    158 		xc_sihs[(xipl)] = softint_establish( (sipl) | SOFTINT_MPSAFE,\
    159 		    xc__highpri_intr, NULL);				\
    160 		KASSERT(xc_sihs[(xipl)] != NULL);			\
    161 	} while (0)
    162 
    163 	SETUP_SOFTINT(XC_IPL_SOFTSERIAL, SOFTINT_SERIAL);
    164 	SETUP_SOFTINT(XC_IPL_SOFTBIO, SOFTINT_BIO);
    165 	SETUP_SOFTINT(XC_IPL_SOFTCLOCK, SOFTINT_CLOCK);
    166 	SETUP_SOFTINT(XC_IPL_SOFTNET, SOFTINT_NET);
    167 
    168 #undef SETUP_SOFTINT
    169 
    170 	evcnt_attach_dynamic(&xc_unicast_ev, EVCNT_TYPE_MISC, NULL,
    171 	   "crosscall", "unicast");
    172 	evcnt_attach_dynamic(&xc_broadcast_ev, EVCNT_TYPE_MISC, NULL,
    173 	   "crosscall", "broadcast");
    174 }
    175 
    176 /*
    177  * Encode an IPL to a form that can be embedded into flags of xc_broadcast
    178  * or xc_unicast.
    179  */
    180 unsigned int
    181 xc_encode_ipl(int ipl)
    182 {
    183 
    184 	switch (ipl) {
    185 	case IPL_SOFTSERIAL:
    186 		return __SHIFTIN(XC_IPL_SOFTSERIAL, XC_IPL_MASK);
    187 	case IPL_SOFTBIO:
    188 		return __SHIFTIN(XC_IPL_SOFTBIO, XC_IPL_MASK);
    189 	case IPL_SOFTCLOCK:
    190 		return __SHIFTIN(XC_IPL_SOFTCLOCK, XC_IPL_MASK);
    191 	case IPL_SOFTNET:
    192 		return __SHIFTIN(XC_IPL_SOFTNET, XC_IPL_MASK);
    193 	}
    194 
    195 	panic("Invalid IPL: %d", ipl);
    196 }
    197 
    198 /*
    199  * Extract an XC_IPL from flags of xc_broadcast or xc_unicast.
    200  */
    201 static inline unsigned int
    202 xc_extract_ipl(unsigned int flags)
    203 {
    204 
    205 	return __SHIFTOUT(flags, XC_IPL_MASK);
    206 }
    207 
    208 /*
    209  * xc_init_cpu:
    210  *
    211  *	Initialize the cross-call subsystem.  Called once for each CPU
    212  *	in the system as they are attached.
    213  */
    214 void
    215 xc_init_cpu(struct cpu_info *ci)
    216 {
    217 	static bool again = false;
    218 	int error __diagused;
    219 
    220 	if (!again) {
    221 		/* Autoconfiguration will prevent re-entry. */
    222 		xc_init();
    223 		again = true;
    224 	}
    225 	cv_init(&ci->ci_data.cpu_xcall, "xcall");
    226 	error = kthread_create(PRI_XCALL, KTHREAD_MPSAFE, ci, xc_thread,
    227 	    NULL, NULL, "xcall/%u", ci->ci_index);
    228 	KASSERT(error == 0);
    229 }
    230 
    231 /*
    232  * xc_broadcast:
    233  *
    234  *	Trigger a call on all CPUs in the system.
    235  */
    236 uint64_t
    237 xc_broadcast(unsigned int flags, xcfunc_t func, void *arg1, void *arg2)
    238 {
    239 
    240 	KASSERT(!cpu_intr_p() && !cpu_softintr_p());
    241 
    242 	if ((flags & XC_HIGHPRI) != 0) {
    243 		int ipl = xc_extract_ipl(flags);
    244 		return xc_highpri(func, arg1, arg2, NULL, ipl);
    245 	} else {
    246 		return xc_lowpri(func, arg1, arg2, NULL);
    247 	}
    248 }
    249 
    250 /*
    251  * xc_unicast:
    252  *
    253  *	Trigger a call on one CPU.
    254  */
    255 uint64_t
    256 xc_unicast(unsigned int flags, xcfunc_t func, void *arg1, void *arg2,
    257 	   struct cpu_info *ci)
    258 {
    259 
    260 	KASSERT(ci != NULL);
    261 	KASSERT(!cpu_intr_p() && !cpu_softintr_p());
    262 
    263 	if ((flags & XC_HIGHPRI) != 0) {
    264 		int ipl = xc_extract_ipl(flags);
    265 		return xc_highpri(func, arg1, arg2, ci, ipl);
    266 	} else {
    267 		return xc_lowpri(func, arg1, arg2, ci);
    268 	}
    269 }
    270 
    271 /*
    272  * xc_wait:
    273  *
    274  *	Wait for a cross call to complete.
    275  */
    276 void
    277 xc_wait(uint64_t where)
    278 {
    279 	xc_state_t *xc;
    280 
    281 	KASSERT(!cpu_intr_p() && !cpu_softintr_p());
    282 
    283 	/* Determine whether it is high or low priority cross-call. */
    284 	if ((where & XC_PRI_BIT) != 0) {
    285 		xc = &xc_high_pri;
    286 		where &= ~XC_PRI_BIT;
    287 	} else {
    288 		xc = &xc_low_pri;
    289 	}
    290 
    291 	/* Fast path, if already done. */
    292 	if (xc->xc_donep >= where) {
    293 		return;
    294 	}
    295 
    296 	/* Slow path: block until awoken. */
    297 	mutex_enter(&xc->xc_lock);
    298 	while (xc->xc_donep < where) {
    299 		cv_wait(&xc->xc_busy, &xc->xc_lock);
    300 	}
    301 	mutex_exit(&xc->xc_lock);
    302 }
    303 
    304 /*
    305  * xc_lowpri:
    306  *
    307  *	Trigger a low priority call on one or more CPUs.
    308  */
    309 static inline uint64_t
    310 xc_lowpri(xcfunc_t func, void *arg1, void *arg2, struct cpu_info *ci)
    311 {
    312 	xc_state_t *xc = &xc_low_pri;
    313 	CPU_INFO_ITERATOR cii;
    314 	uint64_t where;
    315 
    316 	mutex_enter(&xc->xc_lock);
    317 	while (xc->xc_headp != xc->xc_donep) {
    318 		cv_wait(&xc->xc_busy, &xc->xc_lock);
    319 	}
    320 	xc->xc_arg1 = arg1;
    321 	xc->xc_arg2 = arg2;
    322 	xc->xc_func = func;
    323 	if (ci == NULL) {
    324 		xc_broadcast_ev.ev_count++;
    325 		for (CPU_INFO_FOREACH(cii, ci)) {
    326 			if ((ci->ci_schedstate.spc_flags & SPCF_RUNNING) == 0)
    327 				continue;
    328 			xc->xc_headp += 1;
    329 			ci->ci_data.cpu_xcall_pending = true;
    330 			cv_signal(&ci->ci_data.cpu_xcall);
    331 		}
    332 	} else {
    333 		xc_unicast_ev.ev_count++;
    334 		xc->xc_headp += 1;
    335 		ci->ci_data.cpu_xcall_pending = true;
    336 		cv_signal(&ci->ci_data.cpu_xcall);
    337 	}
    338 	KASSERT(xc->xc_donep < xc->xc_headp);
    339 	where = xc->xc_headp;
    340 	mutex_exit(&xc->xc_lock);
    341 
    342 	/* Return a low priority ticket. */
    343 	KASSERT((where & XC_PRI_BIT) == 0);
    344 	return where;
    345 }
    346 
    347 /*
    348  * xc_thread:
    349  *
    350  *	One thread per-CPU to dispatch low priority calls.
    351  */
    352 static void
    353 xc_thread(void *cookie)
    354 {
    355 	struct cpu_info *ci = curcpu();
    356 	xc_state_t *xc = &xc_low_pri;
    357 	void *arg1, *arg2;
    358 	xcfunc_t func;
    359 
    360 	mutex_enter(&xc->xc_lock);
    361 	for (;;) {
    362 		while (!ci->ci_data.cpu_xcall_pending) {
    363 			if (xc->xc_headp == xc->xc_donep) {
    364 				cv_broadcast(&xc->xc_busy);
    365 			}
    366 			cv_wait(&ci->ci_data.cpu_xcall, &xc->xc_lock);
    367 			KASSERT(ci == curcpu());
    368 		}
    369 		ci->ci_data.cpu_xcall_pending = false;
    370 		func = xc->xc_func;
    371 		arg1 = xc->xc_arg1;
    372 		arg2 = xc->xc_arg2;
    373 		mutex_exit(&xc->xc_lock);
    374 
    375 		KASSERT(func != NULL);
    376 		(*func)(arg1, arg2);
    377 
    378 		mutex_enter(&xc->xc_lock);
    379 		xc->xc_donep++;
    380 	}
    381 	/* NOTREACHED */
    382 }
    383 
    384 /*
    385  * xc_ipi_handler:
    386  *
    387  *	Handler of cross-call IPI.
    388  */
    389 void
    390 xc_ipi_handler(void)
    391 {
    392 	xc_state_t *xc = & xc_high_pri;
    393 
    394 	KASSERT(xc->xc_ipl < __arraycount(xc_sihs));
    395 
    396 	/* Executes xc__highpri_intr() via software interrupt. */
    397 	softint_schedule(xc_sihs[xc->xc_ipl]);
    398 }
    399 
    400 /*
    401  * xc__highpri_intr:
    402  *
    403  *	A software interrupt handler for high priority calls.
    404  */
    405 void
    406 xc__highpri_intr(void *dummy)
    407 {
    408 	xc_state_t *xc = &xc_high_pri;
    409 	void *arg1, *arg2;
    410 	xcfunc_t func;
    411 
    412 	KASSERTMSG(!cpu_intr_p(), "high priority xcall for function %p",
    413 	    xc->xc_func);
    414 	/*
    415 	 * Lock-less fetch of function and its arguments.
    416 	 * Safe since it cannot change at this point.
    417 	 */
    418 	KASSERT(xc->xc_donep < xc->xc_headp);
    419 	func = xc->xc_func;
    420 	arg1 = xc->xc_arg1;
    421 	arg2 = xc->xc_arg2;
    422 
    423 	KASSERT(func != NULL);
    424 	(*func)(arg1, arg2);
    425 
    426 	/*
    427 	 * Note the request as done, and if we have reached the head,
    428 	 * cross-call has been processed - notify waiters, if any.
    429 	 */
    430 	mutex_enter(&xc->xc_lock);
    431 	if (++xc->xc_donep == xc->xc_headp) {
    432 		cv_broadcast(&xc->xc_busy);
    433 	}
    434 	mutex_exit(&xc->xc_lock);
    435 }
    436 
    437 /*
    438  * xc_highpri:
    439  *
    440  *	Trigger a high priority call on one or more CPUs.
    441  */
    442 static inline uint64_t
    443 xc_highpri(xcfunc_t func, void *arg1, void *arg2, struct cpu_info *ci,
    444     unsigned int ipl)
    445 {
    446 	xc_state_t *xc = &xc_high_pri;
    447 	uint64_t where;
    448 
    449 	mutex_enter(&xc->xc_lock);
    450 	while (xc->xc_headp != xc->xc_donep) {
    451 		cv_wait(&xc->xc_busy, &xc->xc_lock);
    452 	}
    453 	xc->xc_func = func;
    454 	xc->xc_arg1 = arg1;
    455 	xc->xc_arg2 = arg2;
    456 	xc->xc_headp += (ci ? 1 : ncpu);
    457 	xc->xc_ipl = ipl;
    458 	where = xc->xc_headp;
    459 	mutex_exit(&xc->xc_lock);
    460 
    461 	/*
    462 	 * Send the IPI once lock is released.
    463 	 * Note: it will handle the local CPU case.
    464 	 */
    465 
    466 #ifdef _RUMPKERNEL
    467 	rump_xc_highpri(ci);
    468 #else
    469 #ifdef MULTIPROCESSOR
    470 	kpreempt_disable();
    471 	if (curcpu() == ci) {
    472 		/* Unicast: local CPU. */
    473 		xc_ipi_handler();
    474 	} else if (ci) {
    475 		/* Unicast: remote CPU. */
    476 		xc_send_ipi(ci);
    477 	} else {
    478 		/* Broadcast: all, including local. */
    479 		xc_send_ipi(NULL);
    480 		xc_ipi_handler();
    481 	}
    482 	kpreempt_enable();
    483 #else
    484 	KASSERT(ci == NULL || curcpu() == ci);
    485 	xc_ipi_handler();
    486 #endif
    487 #endif
    488 
    489 	/* Indicate a high priority ticket. */
    490 	return (where | XC_PRI_BIT);
    491 }
    492