Home | History | Annotate | Line # | Download | only in kern
subr_localcount.c revision 1.1.6.3
      1 /*	$NetBSD: subr_localcount.c,v 1.1.6.3 2017/05/02 03:19:22 pgoyette Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2016 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      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  * CPU-local reference counts
     34  *
     35  *	localcount(9) is a reference-counting scheme that involves no
     36  *	interprocessor synchronization most of the time, at the cost of
     37  *	eight bytes of memory per CPU per object and at the cost of
     38  *	expensive interprocessor synchronization to drain references.
     39  *
     40  *	localcount(9) references may be held across sleeps, may be
     41  *	transferred from CPU to CPU or thread to thread: they behave
     42  *	semantically like typical reference counts, with different
     43  *	pragmatic performance characteristics.
     44  */
     45 
     46 #include <sys/cdefs.h>
     47 __KERNEL_RCSID(0, "$NetBSD: subr_localcount.c,v 1.1.6.3 2017/05/02 03:19:22 pgoyette Exp $");
     48 
     49 #include <sys/param.h>
     50 #include <sys/localcount.h>
     51 #include <sys/types.h>
     52 #include <sys/condvar.h>
     53 #include <sys/errno.h>
     54 #include <sys/mutex.h>
     55 #include <sys/percpu.h>
     56 #include <sys/xcall.h>
     57 
     58 /*
     59  * localcount_init(lc)
     60  *
     61  *	Initialize a localcount object.  Returns 0 on success, error
     62  *	code on failure.  May fail to allocate memory for percpu(9).
     63  *
     64  *	The caller must call localcount_drain and then localcount_fini
     65  *	when done with lc.
     66  */
     67 static void localcount_xc(void *, void *);
     68 
     69 int
     70 localcount_init(struct localcount *lc)
     71 {
     72 
     73 	lc->lc_totalp = NULL;
     74 	lc->lc_percpu = percpu_alloc(sizeof(int64_t));
     75 	if (lc->lc_percpu == NULL)
     76 		return ENOMEM;
     77 
     78 	return 0;
     79 }
     80 
     81 /*
     82  * localcount_drain(lc, cv, interlock)
     83  *
     84  *	Wait for all acquired references to lc to drain.  Caller must
     85  *	hold interlock; localcount_drain releases it during cross-calls
     86  *	and waits on cv.  The cv and interlock passed here must be the
     87  *	same as are passed to localcount_release for this lc.
     88  *
     89  *	Caller must guarantee that no new references can be acquired
     90  *	with localcount_acquire before calling localcount_drain.  For
     91  *	example, any object that may be found in a list and acquired
     92  *	must be removed from the list before localcount_drain.
     93  *
     94  *	The localcount object lc may be used only with localcount_fini
     95  *	after this, unless reinitialized after localcount_fini with
     96  *	localcount_init.
     97  */
     98 void
     99 localcount_drain(struct localcount *lc, kcondvar_t *cv, kmutex_t *interlock)
    100 {
    101 	int64_t total = 0;
    102 
    103 	KASSERT(mutex_owned(interlock));
    104 	KASSERT(lc->lc_totalp == NULL);
    105 
    106 	/* Mark it draining.  */
    107 	lc->lc_totalp = &total;
    108 
    109 	/*
    110 	 * Count up all references on all CPUs.
    111 	 *
    112 	 * This serves as a global memory barrier: after xc_wait, all
    113 	 * CPUs will have witnessed the nonnull value of lc->lc_totalp,
    114 	 * so that it is safe to wait on the cv for them.
    115 	 */
    116 	mutex_exit(interlock);
    117 	xc_wait(xc_broadcast(0, &localcount_xc, lc, interlock));
    118 	mutex_enter(interlock);
    119 
    120 	/* Wait for remaining references to drain.  */
    121 	while (total != 0) {
    122 		/*
    123 		 * At this point, now that we have added up all
    124 		 * references on all CPUs, the total had better be
    125 		 * nonnegative.
    126 		 */
    127 		KASSERTMSG((0 < total),
    128 		    "negatively referenced localcount: %p, %"PRId64,
    129 		    lc, total);
    130 		cv_wait(cv, interlock);
    131 	}
    132 
    133 	/* Paranoia: Cause any further use of lc->lc_totalp to crash.  */
    134 	lc->lc_totalp = (void *)(uintptr_t)1;
    135 }
    136 
    137 /*
    138  * localcount_fini(lc)
    139  *
    140  *	Finalize a localcount object, releasing any memory allocated
    141  *	for it.  Caller must have already called localcount_drain.
    142  */
    143 void
    144 localcount_fini(struct localcount *lc)
    145 {
    146 
    147 	KASSERT(lc->lc_totalp == (void *)(uintptr_t)1);
    148 	percpu_free(lc->lc_percpu, sizeof(uint64_t));
    149 }
    150 
    151 static void
    152 localcount_xc(void *cookie0, void *cookie1)
    153 {
    154 	struct localcount *lc = cookie0;
    155 	kmutex_t *interlock = cookie1;
    156 	int64_t *localp;
    157 
    158 	mutex_enter(interlock);
    159 	localp = percpu_getref(lc->lc_percpu);
    160 	*lc->lc_totalp += *localp;
    161 	percpu_putref(lc->lc_percpu);
    162 	mutex_exit(interlock);
    163 }
    164 
    165 static void
    166 localcount_adjust(struct localcount *lc, int delta)
    167 {
    168 	int64_t *localp;
    169 
    170 	localp = percpu_getref(lc->lc_percpu);
    171 	*localp += delta;
    172 	percpu_putref(lc->lc_percpu);
    173 }
    174 
    175 /*
    176  * localcount_acquire(lc)
    177  *
    178  *	Acquire a reference to lc.
    179  *
    180  *	The reference may be held across sleeps and may be migrated
    181  *	from CPU to CPU, or even thread to thread -- it is only
    182  *	counted, not associated with a particular concrete owner.
    183  *
    184  *	Involves no interprocessor synchronization.  May be used in any
    185  *	context: while a lock is held, within a pserialize(9) read
    186  *	section, in hard interrupt context (provided other users block
    187  *	hard interrupts), in soft interrupt context, in thread context,
    188  *	&c.
    189  *
    190  *	Caller must guarantee that there is no concurrent
    191  *	localcount_drain.  For example, any object that may be found in
    192  *	a list and acquired must be removed from the list before
    193  *	localcount_drain.
    194  */
    195 void
    196 localcount_acquire(struct localcount *lc)
    197 {
    198 
    199 	KASSERT(lc->lc_totalp == NULL);
    200 	localcount_adjust(lc, +1);
    201 }
    202 
    203 /*
    204  * localcount_release(lc, cv, interlock)
    205  *
    206  *	Release a reference to lc.  If there is a concurrent
    207  *	localcount_drain and this may be the last reference, notify
    208  *	localcount_drain by acquiring interlock, waking cv, and
    209  *	releasing interlock.  The cv and interlock passed here must be
    210  *	the same as are passed to localcount_drain for this lc.
    211  *
    212  *	Involves no interprocessor synchronization unless there is a
    213  *	concurrent localcount_drain in progress.
    214  */
    215 void
    216 localcount_release(struct localcount *lc, kcondvar_t *cv, kmutex_t *interlock)
    217 {
    218 	int s;
    219 
    220 	/*
    221 	 * Block xcall so that if someone begins draining after we see
    222 	 * lc->lc_totalp as null, then they won't start cv_wait until
    223 	 * after they have counted this CPU's contributions.
    224 	 *
    225 	 * Otherwise, localcount_drain may notice an extant reference
    226 	 * from this CPU and cv_wait for it, but having seen
    227 	 * lc->lc_totalp as null, this CPU will not wake
    228 	 * localcount_drain.
    229 	 */
    230 	s = splsoftserial();
    231 
    232 	KDASSERT(mutex_ownable(interlock));
    233 	if (__predict_false(lc->lc_totalp != NULL)) {
    234 		/*
    235 		 * Slow path -- wake localcount_drain in case this is
    236 		 * the last reference.
    237 		 */
    238 		mutex_enter(interlock);
    239 		localcount_adjust(lc, -1);
    240 		*lc->lc_totalp -= 1;
    241 		if (*lc->lc_totalp == 0)
    242 			cv_broadcast(cv);
    243 		mutex_exit(interlock);
    244 		goto out;
    245 	}
    246 
    247 	localcount_adjust(lc, -1);
    248 out:	splx(s);
    249 }
    250