Home | History | Annotate | Line # | Download | only in kern
subr_pserialize.c revision 1.5.6.2
      1  1.5.6.2  jdolecek /*	$NetBSD: subr_pserialize.c,v 1.5.6.2 2017/12/03 11:38:45 jdolecek Exp $	*/
      2      1.1  christos 
      3      1.1  christos /*-
      4      1.1  christos  * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
      5      1.1  christos  * All rights reserved.
      6      1.1  christos  *
      7      1.1  christos  * Redistribution and use in source and binary forms, with or without
      8      1.1  christos  * modification, are permitted provided that the following conditions
      9      1.1  christos  * are met:
     10      1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11      1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12      1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  christos  *    documentation and/or other materials provided with the distribution.
     15      1.1  christos  *
     16      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17      1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18      1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19      1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20      1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21      1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22      1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23      1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24      1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25      1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26      1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     27      1.1  christos  */
     28      1.1  christos 
     29      1.1  christos /*
     30      1.1  christos  * Passive serialization.
     31      1.1  christos  *
     32      1.1  christos  * Implementation accurately matches the lapsed US patent 4809168, therefore
     33      1.1  christos  * code is patent-free in the United States.  Your use of this code is at
     34      1.1  christos  * your own risk.
     35      1.1  christos  *
     36      1.1  christos  * Note for NetBSD developers: all changes to this source file must be
     37      1.1  christos  * approved by the <core>.
     38      1.1  christos  */
     39      1.1  christos 
     40      1.1  christos #include <sys/cdefs.h>
     41  1.5.6.2  jdolecek __KERNEL_RCSID(0, "$NetBSD: subr_pserialize.c,v 1.5.6.2 2017/12/03 11:38:45 jdolecek Exp $");
     42      1.1  christos 
     43      1.1  christos #include <sys/param.h>
     44      1.1  christos 
     45      1.1  christos #include <sys/condvar.h>
     46      1.1  christos #include <sys/cpu.h>
     47      1.2        he #include <sys/evcnt.h>
     48      1.1  christos #include <sys/kmem.h>
     49      1.1  christos #include <sys/mutex.h>
     50      1.1  christos #include <sys/pserialize.h>
     51  1.5.6.1       tls #include <sys/proc.h>
     52      1.1  christos #include <sys/queue.h>
     53      1.1  christos #include <sys/xcall.h>
     54      1.1  christos 
     55      1.1  christos struct pserialize {
     56      1.1  christos 	TAILQ_ENTRY(pserialize)	psz_chain;
     57      1.1  christos 	lwp_t *			psz_owner;
     58      1.1  christos 	kcpuset_t *		psz_target;
     59      1.1  christos 	kcpuset_t *		psz_pass;
     60      1.1  christos };
     61      1.1  christos 
     62      1.1  christos static u_int			psz_work_todo	__cacheline_aligned;
     63      1.1  christos static kmutex_t			psz_lock	__cacheline_aligned;
     64      1.1  christos static struct evcnt		psz_ev_excl	__cacheline_aligned;
     65      1.1  christos 
     66      1.1  christos /*
     67      1.1  christos  * As defined in "Method 1":
     68      1.1  christos  *	q0: "0 MP checkpoints have occured".
     69      1.1  christos  *	q1: "1 MP checkpoint has occured".
     70      1.1  christos  *	q2: "2 MP checkpoints have occured".
     71      1.1  christos  */
     72      1.1  christos static TAILQ_HEAD(, pserialize)	psz_queue0	__cacheline_aligned;
     73      1.1  christos static TAILQ_HEAD(, pserialize)	psz_queue1	__cacheline_aligned;
     74      1.1  christos static TAILQ_HEAD(, pserialize)	psz_queue2	__cacheline_aligned;
     75      1.1  christos 
     76  1.5.6.2  jdolecek #ifdef LOCKDEBUG
     77  1.5.6.2  jdolecek #include <sys/percpu.h>
     78  1.5.6.2  jdolecek 
     79  1.5.6.2  jdolecek static percpu_t		*psz_debug_nreads	__cacheline_aligned;
     80  1.5.6.2  jdolecek #endif
     81  1.5.6.2  jdolecek 
     82      1.1  christos /*
     83      1.1  christos  * pserialize_init:
     84      1.1  christos  *
     85      1.1  christos  *	Initialize passive serialization structures.
     86      1.1  christos  */
     87      1.1  christos void
     88      1.1  christos pserialize_init(void)
     89      1.1  christos {
     90      1.1  christos 
     91      1.1  christos 	psz_work_todo = 0;
     92      1.1  christos 	TAILQ_INIT(&psz_queue0);
     93      1.1  christos 	TAILQ_INIT(&psz_queue1);
     94      1.1  christos 	TAILQ_INIT(&psz_queue2);
     95      1.1  christos 	mutex_init(&psz_lock, MUTEX_DEFAULT, IPL_SCHED);
     96      1.1  christos 	evcnt_attach_dynamic(&psz_ev_excl, EVCNT_TYPE_MISC, NULL,
     97      1.1  christos 	    "pserialize", "exclusive access");
     98  1.5.6.2  jdolecek #ifdef LOCKDEBUG
     99  1.5.6.2  jdolecek 	psz_debug_nreads = percpu_alloc(sizeof(uint32_t));
    100  1.5.6.2  jdolecek #endif
    101      1.1  christos }
    102      1.1  christos 
    103      1.1  christos /*
    104      1.1  christos  * pserialize_create:
    105      1.1  christos  *
    106      1.1  christos  *	Create and initialize a passive serialization object.
    107      1.1  christos  */
    108      1.1  christos pserialize_t
    109      1.1  christos pserialize_create(void)
    110      1.1  christos {
    111      1.1  christos 	pserialize_t psz;
    112      1.1  christos 
    113      1.1  christos 	psz = kmem_zalloc(sizeof(struct pserialize), KM_SLEEP);
    114      1.4     rmind 	kcpuset_create(&psz->psz_target, true);
    115      1.4     rmind 	kcpuset_create(&psz->psz_pass, true);
    116      1.1  christos 	psz->psz_owner = NULL;
    117      1.1  christos 
    118      1.1  christos 	return psz;
    119      1.1  christos }
    120      1.1  christos 
    121      1.1  christos /*
    122      1.1  christos  * pserialize_destroy:
    123      1.1  christos  *
    124      1.1  christos  *	Destroy a passive serialization object.
    125      1.1  christos  */
    126      1.1  christos void
    127      1.1  christos pserialize_destroy(pserialize_t psz)
    128      1.1  christos {
    129      1.1  christos 
    130      1.1  christos 	KASSERT(psz->psz_owner == NULL);
    131      1.1  christos 
    132      1.1  christos 	kcpuset_destroy(psz->psz_target);
    133      1.1  christos 	kcpuset_destroy(psz->psz_pass);
    134      1.1  christos 	kmem_free(psz, sizeof(struct pserialize));
    135      1.1  christos }
    136      1.1  christos 
    137      1.1  christos /*
    138      1.1  christos  * pserialize_perform:
    139      1.1  christos  *
    140      1.1  christos  *	Perform the write side of passive serialization.  The calling
    141      1.1  christos  *	thread holds an exclusive lock on the data object(s) being updated.
    142      1.1  christos  *	We wait until every processor in the system has made at least two
    143  1.5.6.2  jdolecek  *	passes through cpu_switchto().  The wait is made with the caller's
    144      1.1  christos  *	update lock held, but is short term.
    145      1.1  christos  */
    146      1.1  christos void
    147      1.1  christos pserialize_perform(pserialize_t psz)
    148      1.1  christos {
    149  1.5.6.1       tls 	uint64_t xc;
    150      1.1  christos 
    151      1.1  christos 	KASSERT(!cpu_intr_p());
    152      1.1  christos 	KASSERT(!cpu_softintr_p());
    153      1.1  christos 
    154      1.1  christos 	if (__predict_false(panicstr != NULL)) {
    155      1.1  christos 		return;
    156      1.1  christos 	}
    157      1.1  christos 	KASSERT(psz->psz_owner == NULL);
    158      1.1  christos 	KASSERT(ncpu > 0);
    159      1.1  christos 
    160      1.1  christos 	/*
    161      1.1  christos 	 * Set up the object and put it onto the queue.  The lock
    162      1.1  christos 	 * activity here provides the necessary memory barrier to
    163      1.1  christos 	 * make the caller's data update completely visible to
    164      1.1  christos 	 * other processors.
    165      1.1  christos 	 */
    166      1.1  christos 	psz->psz_owner = curlwp;
    167      1.5     rmind 	kcpuset_copy(psz->psz_target, kcpuset_running);
    168      1.1  christos 	kcpuset_zero(psz->psz_pass);
    169      1.1  christos 
    170      1.1  christos 	mutex_spin_enter(&psz_lock);
    171      1.1  christos 	TAILQ_INSERT_TAIL(&psz_queue0, psz, psz_chain);
    172      1.1  christos 	psz_work_todo++;
    173      1.1  christos 
    174  1.5.6.1       tls 	do {
    175  1.5.6.1       tls 		mutex_spin_exit(&psz_lock);
    176  1.5.6.1       tls 
    177  1.5.6.1       tls 		/*
    178  1.5.6.1       tls 		 * Force some context switch activity on every CPU, as
    179  1.5.6.1       tls 		 * the system may not be busy.  Pause to not flood.
    180  1.5.6.1       tls 		 */
    181  1.5.6.1       tls 		xc = xc_broadcast(XC_HIGHPRI, (xcfunc_t)nullop, NULL, NULL);
    182  1.5.6.1       tls 		xc_wait(xc);
    183  1.5.6.1       tls 		kpause("psrlz", false, 1, NULL);
    184  1.5.6.1       tls 
    185  1.5.6.1       tls 		mutex_spin_enter(&psz_lock);
    186  1.5.6.1       tls 	} while (!kcpuset_iszero(psz->psz_target));
    187      1.1  christos 
    188      1.1  christos 	psz_ev_excl.ev_count++;
    189      1.1  christos 	mutex_spin_exit(&psz_lock);
    190      1.1  christos 
    191      1.1  christos 	psz->psz_owner = NULL;
    192      1.1  christos }
    193      1.1  christos 
    194      1.1  christos int
    195      1.1  christos pserialize_read_enter(void)
    196      1.1  christos {
    197  1.5.6.2  jdolecek 	int s;
    198      1.1  christos 
    199      1.1  christos 	KASSERT(!cpu_intr_p());
    200  1.5.6.2  jdolecek 	s = splsoftserial();
    201  1.5.6.2  jdolecek #ifdef LOCKDEBUG
    202  1.5.6.2  jdolecek 	{
    203  1.5.6.2  jdolecek 		uint32_t *nreads;
    204  1.5.6.2  jdolecek 		nreads = percpu_getref(psz_debug_nreads);
    205  1.5.6.2  jdolecek 		(*nreads)++;
    206  1.5.6.2  jdolecek 		if (*nreads == 0)
    207  1.5.6.2  jdolecek 			panic("nreads overflow");
    208  1.5.6.2  jdolecek 		percpu_putref(psz_debug_nreads);
    209  1.5.6.2  jdolecek 	}
    210  1.5.6.2  jdolecek #endif
    211  1.5.6.2  jdolecek 	return s;
    212      1.1  christos }
    213      1.1  christos 
    214      1.1  christos void
    215      1.1  christos pserialize_read_exit(int s)
    216      1.1  christos {
    217      1.1  christos 
    218  1.5.6.2  jdolecek #ifdef LOCKDEBUG
    219  1.5.6.2  jdolecek 	{
    220  1.5.6.2  jdolecek 		uint32_t *nreads;
    221  1.5.6.2  jdolecek 		nreads = percpu_getref(psz_debug_nreads);
    222  1.5.6.2  jdolecek 		(*nreads)--;
    223  1.5.6.2  jdolecek 		if (*nreads == UINT_MAX)
    224  1.5.6.2  jdolecek 			panic("nreads underflow");
    225  1.5.6.2  jdolecek 		percpu_putref(psz_debug_nreads);
    226  1.5.6.2  jdolecek 	}
    227  1.5.6.2  jdolecek #endif
    228      1.1  christos 	splx(s);
    229      1.1  christos }
    230      1.1  christos 
    231      1.1  christos /*
    232      1.1  christos  * pserialize_switchpoint:
    233      1.1  christos  *
    234      1.1  christos  *	Monitor system context switch activity.  Called from machine
    235      1.1  christos  *	independent code after mi_switch() returns.
    236      1.1  christos  */
    237      1.1  christos void
    238      1.1  christos pserialize_switchpoint(void)
    239      1.1  christos {
    240      1.1  christos 	pserialize_t psz, next;
    241      1.1  christos 	cpuid_t cid;
    242      1.1  christos 
    243  1.5.6.2  jdolecek 	/* We must to ensure not to come here from inside a read section. */
    244  1.5.6.2  jdolecek 	KASSERT(pserialize_not_in_read_section());
    245  1.5.6.2  jdolecek 
    246      1.1  christos 	/*
    247      1.1  christos 	 * If no updates pending, bail out.  No need to lock in order to
    248      1.1  christos 	 * test psz_work_todo; the only ill effect of missing an update
    249      1.1  christos 	 * would be to delay LWPs waiting in pserialize_perform().  That
    250      1.1  christos 	 * will not happen because updates are on the queue before an
    251      1.1  christos 	 * xcall is generated (serialization) to tickle every CPU.
    252      1.1  christos 	 */
    253      1.1  christos 	if (__predict_true(psz_work_todo == 0)) {
    254      1.1  christos 		return;
    255      1.1  christos 	}
    256      1.1  christos 	mutex_spin_enter(&psz_lock);
    257      1.1  christos 	cid = cpu_index(curcpu());
    258      1.1  christos 
    259      1.1  christos 	/*
    260      1.1  christos 	 * At first, scan through the second queue and update each request,
    261      1.1  christos 	 * if passed all processors, then transfer to the third queue.
    262      1.1  christos 	 */
    263      1.1  christos 	for (psz = TAILQ_FIRST(&psz_queue1); psz != NULL; psz = next) {
    264      1.1  christos 		next = TAILQ_NEXT(psz, psz_chain);
    265  1.5.6.1       tls 		kcpuset_set(psz->psz_pass, cid);
    266      1.1  christos 		if (!kcpuset_match(psz->psz_pass, psz->psz_target)) {
    267      1.1  christos 			continue;
    268      1.1  christos 		}
    269      1.1  christos 		kcpuset_zero(psz->psz_pass);
    270      1.1  christos 		TAILQ_REMOVE(&psz_queue1, psz, psz_chain);
    271      1.1  christos 		TAILQ_INSERT_TAIL(&psz_queue2, psz, psz_chain);
    272      1.1  christos 	}
    273      1.1  christos 	/*
    274      1.1  christos 	 * Scan through the first queue and update each request,
    275      1.1  christos 	 * if passed all processors, then move to the second queue.
    276      1.1  christos 	 */
    277      1.1  christos 	for (psz = TAILQ_FIRST(&psz_queue0); psz != NULL; psz = next) {
    278      1.1  christos 		next = TAILQ_NEXT(psz, psz_chain);
    279  1.5.6.1       tls 		kcpuset_set(psz->psz_pass, cid);
    280      1.1  christos 		if (!kcpuset_match(psz->psz_pass, psz->psz_target)) {
    281      1.1  christos 			continue;
    282      1.1  christos 		}
    283      1.1  christos 		kcpuset_zero(psz->psz_pass);
    284      1.1  christos 		TAILQ_REMOVE(&psz_queue0, psz, psz_chain);
    285      1.1  christos 		TAILQ_INSERT_TAIL(&psz_queue1, psz, psz_chain);
    286      1.1  christos 	}
    287      1.1  christos 	/*
    288      1.1  christos 	 * Process the third queue: entries have been seen twice on every
    289      1.1  christos 	 * processor, remove from the queue and notify the updating thread.
    290      1.1  christos 	 */
    291      1.1  christos 	while ((psz = TAILQ_FIRST(&psz_queue2)) != NULL) {
    292      1.1  christos 		TAILQ_REMOVE(&psz_queue2, psz, psz_chain);
    293      1.1  christos 		kcpuset_zero(psz->psz_target);
    294      1.1  christos 		psz_work_todo--;
    295      1.1  christos 	}
    296      1.1  christos 	mutex_spin_exit(&psz_lock);
    297      1.1  christos }
    298  1.5.6.2  jdolecek 
    299  1.5.6.2  jdolecek /*
    300  1.5.6.2  jdolecek  * pserialize_in_read_section:
    301  1.5.6.2  jdolecek  *
    302  1.5.6.2  jdolecek  *   True if the caller is in a pserialize read section.  To be used only
    303  1.5.6.2  jdolecek  *   for diagnostic assertions where we want to guarantee the condition like:
    304  1.5.6.2  jdolecek  *
    305  1.5.6.2  jdolecek  *     KASSERT(pserialize_in_read_section());
    306  1.5.6.2  jdolecek  */
    307  1.5.6.2  jdolecek bool
    308  1.5.6.2  jdolecek pserialize_in_read_section(void)
    309  1.5.6.2  jdolecek {
    310  1.5.6.2  jdolecek #ifdef LOCKDEBUG
    311  1.5.6.2  jdolecek 	uint32_t *nreads;
    312  1.5.6.2  jdolecek 	bool in;
    313  1.5.6.2  jdolecek 
    314  1.5.6.2  jdolecek 	/* Not initialized yet */
    315  1.5.6.2  jdolecek 	if (__predict_false(psz_debug_nreads == NULL))
    316  1.5.6.2  jdolecek 		return true;
    317  1.5.6.2  jdolecek 
    318  1.5.6.2  jdolecek 	nreads = percpu_getref(psz_debug_nreads);
    319  1.5.6.2  jdolecek 	in = *nreads != 0;
    320  1.5.6.2  jdolecek 	percpu_putref(psz_debug_nreads);
    321  1.5.6.2  jdolecek 
    322  1.5.6.2  jdolecek 	return in;
    323  1.5.6.2  jdolecek #else
    324  1.5.6.2  jdolecek 	return true;
    325  1.5.6.2  jdolecek #endif
    326  1.5.6.2  jdolecek }
    327  1.5.6.2  jdolecek 
    328  1.5.6.2  jdolecek /*
    329  1.5.6.2  jdolecek  * pserialize_not_in_read_section:
    330  1.5.6.2  jdolecek  *
    331  1.5.6.2  jdolecek  *   True if the caller is not in a pserialize read section.  To be used only
    332  1.5.6.2  jdolecek  *   for diagnostic assertions where we want to guarantee the condition like:
    333  1.5.6.2  jdolecek  *
    334  1.5.6.2  jdolecek  *     KASSERT(pserialize_not_in_read_section());
    335  1.5.6.2  jdolecek  */
    336  1.5.6.2  jdolecek bool
    337  1.5.6.2  jdolecek pserialize_not_in_read_section(void)
    338  1.5.6.2  jdolecek {
    339  1.5.6.2  jdolecek #ifdef LOCKDEBUG
    340  1.5.6.2  jdolecek 	uint32_t *nreads;
    341  1.5.6.2  jdolecek 	bool notin;
    342  1.5.6.2  jdolecek 
    343  1.5.6.2  jdolecek 	/* Not initialized yet */
    344  1.5.6.2  jdolecek 	if (__predict_false(psz_debug_nreads == NULL))
    345  1.5.6.2  jdolecek 		return true;
    346  1.5.6.2  jdolecek 
    347  1.5.6.2  jdolecek 	nreads = percpu_getref(psz_debug_nreads);
    348  1.5.6.2  jdolecek 	notin = *nreads == 0;
    349  1.5.6.2  jdolecek 	percpu_putref(psz_debug_nreads);
    350  1.5.6.2  jdolecek 
    351  1.5.6.2  jdolecek 	return notin;
    352  1.5.6.2  jdolecek #else
    353  1.5.6.2  jdolecek 	return true;
    354  1.5.6.2  jdolecek #endif
    355  1.5.6.2  jdolecek }
    356