Home | History | Annotate | Line # | Download | only in kern
subr_psref.c revision 1.14
      1  1.14  riastrad /*	$NetBSD: subr_psref.c,v 1.14 2021/06/02 09:23:32 riastradh Exp $	*/
      2   1.1  riastrad 
      3   1.1  riastrad /*-
      4   1.1  riastrad  * Copyright (c) 2016 The NetBSD Foundation, Inc.
      5   1.1  riastrad  * All rights reserved.
      6   1.1  riastrad  *
      7   1.1  riastrad  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  riastrad  * by Taylor R. Campbell.
      9   1.1  riastrad  *
     10   1.1  riastrad  * Redistribution and use in source and binary forms, with or without
     11   1.1  riastrad  * modification, are permitted provided that the following conditions
     12   1.1  riastrad  * are met:
     13   1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     14   1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     15   1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     18   1.1  riastrad  *
     19   1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  riastrad  */
     31   1.1  riastrad 
     32   1.1  riastrad /*
     33   1.1  riastrad  * Passive references
     34   1.1  riastrad  *
     35   1.1  riastrad  *	Passive references are references to objects that guarantee the
     36   1.1  riastrad  *	object will not be destroyed until the reference is released.
     37   1.1  riastrad  *
     38   1.1  riastrad  *	Passive references require no interprocessor synchronization to
     39   1.1  riastrad  *	acquire or release.  However, destroying the target of passive
     40   1.1  riastrad  *	references requires expensive interprocessor synchronization --
     41   1.1  riastrad  *	xcalls to determine on which CPUs the object is still in use.
     42   1.1  riastrad  *
     43   1.1  riastrad  *	Passive references may be held only on a single CPU and by a
     44   1.1  riastrad  *	single LWP.  They require the caller to allocate a little stack
     45   1.1  riastrad  *	space, a struct psref object.  Sleeping while a passive
     46   1.1  riastrad  *	reference is held is allowed, provided that the owner's LWP is
     47   1.1  riastrad  *	bound to a CPU -- e.g., the owner is a softint or a bound
     48   1.1  riastrad  *	kthread.  However, sleeping should be kept to a short duration,
     49   1.1  riastrad  *	e.g. sleeping on an adaptive lock.
     50   1.1  riastrad  *
     51   1.1  riastrad  *	Passive references serve as an intermediate stage between
     52   1.1  riastrad  *	reference counting and passive serialization (pserialize(9)):
     53   1.1  riastrad  *
     54   1.1  riastrad  *	- If you need references to transfer from CPU to CPU or LWP to
     55   1.1  riastrad  *	  LWP, or if you need long-term references, you must use
     56   1.1  riastrad  *	  reference counting, e.g. with atomic operations or locks,
     57   1.1  riastrad  *	  which incurs interprocessor synchronization for every use --
     58   1.1  riastrad  *	  cheaper than an xcall, but not scalable.
     59   1.1  riastrad  *
     60   1.1  riastrad  *	- If all users *guarantee* that they will not sleep, then it is
     61   1.1  riastrad  *	  not necessary to use passive references: you may as well just
     62   1.1  riastrad  *	  use the even cheaper pserialize(9), because you have
     63   1.1  riastrad  *	  satisfied the requirements of a pserialize read section.
     64   1.1  riastrad  */
     65   1.1  riastrad 
     66   1.1  riastrad #include <sys/cdefs.h>
     67  1.14  riastrad __KERNEL_RCSID(0, "$NetBSD: subr_psref.c,v 1.14 2021/06/02 09:23:32 riastradh Exp $");
     68   1.1  riastrad 
     69   1.1  riastrad #include <sys/types.h>
     70   1.1  riastrad #include <sys/condvar.h>
     71   1.1  riastrad #include <sys/cpu.h>
     72   1.1  riastrad #include <sys/intr.h>
     73   1.1  riastrad #include <sys/kmem.h>
     74   1.1  riastrad #include <sys/lwp.h>
     75   1.1  riastrad #include <sys/mutex.h>
     76   1.1  riastrad #include <sys/percpu.h>
     77   1.1  riastrad #include <sys/psref.h>
     78   1.1  riastrad #include <sys/queue.h>
     79   1.1  riastrad #include <sys/xcall.h>
     80  1.13     ozaki #include <sys/lwp.h>
     81   1.1  riastrad 
     82   1.8  knakahar SLIST_HEAD(psref_head, psref);
     83   1.1  riastrad 
     84   1.4  riastrad static bool	_psref_held(const struct psref_target *, struct psref_class *,
     85   1.4  riastrad 		    bool);
     86   1.4  riastrad 
     87   1.1  riastrad /*
     88   1.1  riastrad  * struct psref_class
     89   1.1  riastrad  *
     90   1.1  riastrad  *	Private global state for a class of passive reference targets.
     91   1.1  riastrad  *	Opaque to callers.
     92   1.1  riastrad  */
     93   1.1  riastrad struct psref_class {
     94   1.1  riastrad 	kmutex_t		prc_lock;
     95   1.1  riastrad 	kcondvar_t		prc_cv;
     96   1.1  riastrad 	struct percpu		*prc_percpu; /* struct psref_cpu */
     97   1.1  riastrad 	ipl_cookie_t		prc_iplcookie;
     98  1.11     ozaki 	unsigned int		prc_xc_flags;
     99   1.1  riastrad };
    100   1.1  riastrad 
    101   1.1  riastrad /*
    102   1.1  riastrad  * struct psref_cpu
    103   1.1  riastrad  *
    104   1.1  riastrad  *	Private per-CPU state for a class of passive reference targets.
    105   1.1  riastrad  *	Not exposed by the API.
    106   1.1  riastrad  */
    107   1.1  riastrad struct psref_cpu {
    108   1.1  riastrad 	struct psref_head	pcpu_head;
    109   1.1  riastrad };
    110   1.1  riastrad 
    111   1.1  riastrad /*
    112  1.13     ozaki  * Data structures and functions for debugging.
    113  1.13     ozaki  */
    114  1.13     ozaki #ifndef PSREF_DEBUG_NITEMS
    115  1.13     ozaki #define PSREF_DEBUG_NITEMS 16
    116  1.13     ozaki #endif
    117  1.13     ozaki 
    118  1.13     ozaki struct psref_debug_item {
    119  1.13     ozaki 	void			*prdi_caller;
    120  1.13     ozaki 	struct psref		*prdi_psref;
    121  1.13     ozaki };
    122  1.13     ozaki 
    123  1.13     ozaki struct psref_debug {
    124  1.13     ozaki 	int			prd_refs_peek;
    125  1.13     ozaki 	struct psref_debug_item prd_items[PSREF_DEBUG_NITEMS];
    126  1.13     ozaki };
    127  1.13     ozaki 
    128  1.13     ozaki #ifdef PSREF_DEBUG
    129  1.13     ozaki static void psref_debug_acquire(struct psref *);
    130  1.13     ozaki static void psref_debug_release(struct psref *);
    131  1.13     ozaki 
    132  1.13     ozaki static void psref_debug_lwp_free(void *);
    133  1.13     ozaki 
    134  1.13     ozaki static specificdata_key_t psref_debug_lwp_key;
    135  1.13     ozaki #endif
    136  1.13     ozaki 
    137  1.13     ozaki /*
    138  1.13     ozaki  * psref_init()
    139  1.13     ozaki  */
    140  1.13     ozaki void
    141  1.13     ozaki psref_init(void)
    142  1.13     ozaki {
    143  1.13     ozaki 
    144  1.13     ozaki #ifdef PSREF_DEBUG
    145  1.13     ozaki 	lwp_specific_key_create(&psref_debug_lwp_key, psref_debug_lwp_free);
    146  1.13     ozaki #endif
    147  1.13     ozaki }
    148  1.13     ozaki 
    149  1.13     ozaki /*
    150   1.1  riastrad  * psref_class_create(name, ipl)
    151   1.1  riastrad  *
    152   1.1  riastrad  *	Create a new passive reference class, with the given wchan name
    153   1.1  riastrad  *	and ipl.
    154   1.1  riastrad  */
    155   1.1  riastrad struct psref_class *
    156   1.1  riastrad psref_class_create(const char *name, int ipl)
    157   1.1  riastrad {
    158   1.1  riastrad 	struct psref_class *class;
    159   1.1  riastrad 
    160   1.1  riastrad 	ASSERT_SLEEPABLE();
    161   1.1  riastrad 
    162   1.1  riastrad 	class = kmem_alloc(sizeof(*class), KM_SLEEP);
    163   1.1  riastrad 	class->prc_percpu = percpu_alloc(sizeof(struct psref_cpu));
    164   1.1  riastrad 	mutex_init(&class->prc_lock, MUTEX_DEFAULT, ipl);
    165   1.1  riastrad 	cv_init(&class->prc_cv, name);
    166   1.1  riastrad 	class->prc_iplcookie = makeiplcookie(ipl);
    167  1.11     ozaki 	class->prc_xc_flags = XC_HIGHPRI_IPL(ipl);
    168   1.1  riastrad 
    169   1.1  riastrad 	return class;
    170   1.1  riastrad }
    171   1.1  riastrad 
    172   1.1  riastrad #ifdef DIAGNOSTIC
    173   1.1  riastrad static void
    174   1.1  riastrad psref_cpu_drained_p(void *p, void *cookie, struct cpu_info *ci __unused)
    175   1.1  riastrad {
    176   1.1  riastrad 	const struct psref_cpu *pcpu = p;
    177   1.1  riastrad 	bool *retp = cookie;
    178   1.1  riastrad 
    179   1.8  knakahar 	if (!SLIST_EMPTY(&pcpu->pcpu_head))
    180   1.1  riastrad 		*retp = false;
    181   1.1  riastrad }
    182   1.1  riastrad 
    183   1.1  riastrad static bool
    184   1.1  riastrad psref_class_drained_p(const struct psref_class *prc)
    185   1.1  riastrad {
    186   1.1  riastrad 	bool ret = true;
    187   1.1  riastrad 
    188   1.1  riastrad 	percpu_foreach(prc->prc_percpu, &psref_cpu_drained_p, &ret);
    189   1.1  riastrad 
    190   1.1  riastrad 	return ret;
    191   1.1  riastrad }
    192   1.1  riastrad #endif	/* DIAGNOSTIC */
    193   1.1  riastrad 
    194   1.1  riastrad /*
    195   1.1  riastrad  * psref_class_destroy(class)
    196   1.1  riastrad  *
    197   1.1  riastrad  *	Destroy a passive reference class and free memory associated
    198   1.1  riastrad  *	with it.  All targets in this class must have been drained and
    199   1.1  riastrad  *	destroyed already.
    200   1.1  riastrad  */
    201   1.1  riastrad void
    202   1.1  riastrad psref_class_destroy(struct psref_class *class)
    203   1.1  riastrad {
    204   1.1  riastrad 
    205   1.1  riastrad 	KASSERT(psref_class_drained_p(class));
    206   1.1  riastrad 
    207   1.1  riastrad 	cv_destroy(&class->prc_cv);
    208   1.1  riastrad 	mutex_destroy(&class->prc_lock);
    209   1.1  riastrad 	percpu_free(class->prc_percpu, sizeof(struct psref_cpu));
    210   1.1  riastrad 	kmem_free(class, sizeof(*class));
    211   1.1  riastrad }
    212   1.1  riastrad 
    213   1.1  riastrad /*
    214   1.1  riastrad  * psref_target_init(target, class)
    215   1.1  riastrad  *
    216   1.1  riastrad  *	Initialize a passive reference target in the specified class.
    217   1.1  riastrad  *	The caller is responsible for issuing a membar_producer after
    218   1.1  riastrad  *	psref_target_init and before exposing a pointer to the target
    219   1.1  riastrad  *	to other CPUs.
    220   1.1  riastrad  */
    221   1.1  riastrad void
    222   1.1  riastrad psref_target_init(struct psref_target *target,
    223   1.1  riastrad     struct psref_class *class)
    224   1.1  riastrad {
    225   1.1  riastrad 
    226   1.1  riastrad 	target->prt_class = class;
    227   1.1  riastrad 	target->prt_draining = false;
    228   1.1  riastrad }
    229   1.1  riastrad 
    230   1.6     ozaki #ifdef DEBUG
    231   1.9     ozaki static bool
    232   1.9     ozaki psref_exist(struct psref_cpu *pcpu, struct psref *psref)
    233   1.9     ozaki {
    234   1.9     ozaki 	struct psref *_psref;
    235   1.9     ozaki 
    236   1.9     ozaki 	SLIST_FOREACH(_psref, &pcpu->pcpu_head, psref_entry) {
    237   1.9     ozaki 		if (_psref == psref)
    238   1.9     ozaki 			return true;
    239   1.9     ozaki 	}
    240   1.9     ozaki 	return false;
    241   1.9     ozaki }
    242   1.9     ozaki 
    243   1.6     ozaki static void
    244   1.6     ozaki psref_check_duplication(struct psref_cpu *pcpu, struct psref *psref,
    245   1.6     ozaki     const struct psref_target *target)
    246   1.6     ozaki {
    247   1.6     ozaki 	bool found = false;
    248   1.6     ozaki 
    249   1.9     ozaki 	found = psref_exist(pcpu, psref);
    250   1.9     ozaki 	if (found) {
    251   1.9     ozaki 		panic("The psref is already in the list (acquiring twice?): "
    252   1.9     ozaki 		    "psref=%p target=%p", psref, target);
    253   1.6     ozaki 	}
    254   1.9     ozaki }
    255   1.9     ozaki 
    256   1.9     ozaki static void
    257   1.9     ozaki psref_check_existence(struct psref_cpu *pcpu, struct psref *psref,
    258   1.9     ozaki     const struct psref_target *target)
    259   1.9     ozaki {
    260   1.9     ozaki 	bool found = false;
    261   1.9     ozaki 
    262   1.9     ozaki 	found = psref_exist(pcpu, psref);
    263   1.9     ozaki 	if (!found) {
    264   1.9     ozaki 		panic("The psref isn't in the list (releasing unused psref?): "
    265   1.6     ozaki 		    "psref=%p target=%p", psref, target);
    266   1.6     ozaki 	}
    267   1.6     ozaki }
    268   1.6     ozaki #endif /* DEBUG */
    269   1.6     ozaki 
    270   1.1  riastrad /*
    271   1.1  riastrad  * psref_acquire(psref, target, class)
    272   1.1  riastrad  *
    273   1.1  riastrad  *	Acquire a passive reference to the specified target, which must
    274   1.1  riastrad  *	be in the specified class.
    275   1.1  riastrad  *
    276   1.1  riastrad  *	The caller must guarantee that the target will not be destroyed
    277   1.1  riastrad  *	before psref_acquire returns.
    278   1.1  riastrad  *
    279   1.1  riastrad  *	The caller must additionally guarantee that it will not switch
    280   1.1  riastrad  *	CPUs before releasing the passive reference, either by
    281   1.1  riastrad  *	disabling kpreemption and avoiding sleeps, or by being in a
    282   1.1  riastrad  *	softint or in an LWP bound to a CPU.
    283   1.1  riastrad  */
    284   1.1  riastrad void
    285   1.1  riastrad psref_acquire(struct psref *psref, const struct psref_target *target,
    286   1.1  riastrad     struct psref_class *class)
    287   1.1  riastrad {
    288   1.1  riastrad 	struct psref_cpu *pcpu;
    289   1.1  riastrad 	int s;
    290   1.1  riastrad 
    291   1.1  riastrad 	KASSERTMSG((kpreempt_disabled() || cpu_softintr_p() ||
    292   1.1  riastrad 		ISSET(curlwp->l_pflag, LP_BOUND)),
    293   1.1  riastrad 	    "passive references are CPU-local,"
    294   1.1  riastrad 	    " but preemption is enabled and the caller is not"
    295   1.1  riastrad 	    " in a softint or CPU-bound LWP");
    296  1.14  riastrad 	KASSERTMSG(!target->prt_draining, "psref target already destroyed: %p",
    297  1.14  riastrad 	    target);
    298   1.1  riastrad 	KASSERTMSG((target->prt_class == class),
    299   1.1  riastrad 	    "mismatched psref target class: %p (ref) != %p (expected)",
    300   1.1  riastrad 	    target->prt_class, class);
    301   1.1  riastrad 
    302   1.1  riastrad 	/* Block interrupts and acquire the current CPU's reference list.  */
    303   1.1  riastrad 	s = splraiseipl(class->prc_iplcookie);
    304   1.1  riastrad 	pcpu = percpu_getref(class->prc_percpu);
    305   1.1  riastrad 
    306   1.6     ozaki #ifdef DEBUG
    307   1.6     ozaki 	/* Sanity-check if the target is already acquired with the same psref.  */
    308   1.6     ozaki 	psref_check_duplication(pcpu, psref, target);
    309   1.6     ozaki #endif
    310   1.6     ozaki 
    311   1.1  riastrad 	/* Record our reference.  */
    312   1.8  knakahar 	SLIST_INSERT_HEAD(&pcpu->pcpu_head, psref, psref_entry);
    313   1.1  riastrad 	psref->psref_target = target;
    314   1.1  riastrad 	psref->psref_lwp = curlwp;
    315   1.1  riastrad 	psref->psref_cpu = curcpu();
    316   1.1  riastrad 
    317   1.1  riastrad 	/* Release the CPU list and restore interrupts.  */
    318   1.1  riastrad 	percpu_putref(class->prc_percpu);
    319   1.1  riastrad 	splx(s);
    320  1.12     ozaki 
    321  1.13     ozaki #if defined(DIAGNOSTIC) || defined(PSREF_DEBUG)
    322  1.12     ozaki 	curlwp->l_psrefs++;
    323  1.12     ozaki #endif
    324  1.13     ozaki #ifdef PSREF_DEBUG
    325  1.13     ozaki 	psref_debug_acquire(psref);
    326  1.13     ozaki #endif
    327   1.1  riastrad }
    328   1.1  riastrad 
    329   1.1  riastrad /*
    330   1.1  riastrad  * psref_release(psref, target, class)
    331   1.1  riastrad  *
    332   1.1  riastrad  *	Release a passive reference to the specified target, which must
    333   1.1  riastrad  *	be in the specified class.
    334   1.1  riastrad  *
    335   1.1  riastrad  *	The caller must not have switched CPUs or LWPs since acquiring
    336   1.1  riastrad  *	the passive reference.
    337   1.1  riastrad  */
    338   1.1  riastrad void
    339   1.1  riastrad psref_release(struct psref *psref, const struct psref_target *target,
    340   1.1  riastrad     struct psref_class *class)
    341   1.1  riastrad {
    342   1.8  knakahar 	struct psref_cpu *pcpu;
    343   1.1  riastrad 	int s;
    344   1.1  riastrad 
    345   1.1  riastrad 	KASSERTMSG((kpreempt_disabled() || cpu_softintr_p() ||
    346   1.1  riastrad 		ISSET(curlwp->l_pflag, LP_BOUND)),
    347   1.1  riastrad 	    "passive references are CPU-local,"
    348   1.1  riastrad 	    " but preemption is enabled and the caller is not"
    349   1.1  riastrad 	    " in a softint or CPU-bound LWP");
    350   1.1  riastrad 	KASSERTMSG((target->prt_class == class),
    351   1.1  riastrad 	    "mismatched psref target class: %p (ref) != %p (expected)",
    352   1.1  riastrad 	    target->prt_class, class);
    353   1.1  riastrad 
    354   1.1  riastrad 	/* Make sure the psref looks sensible.  */
    355   1.1  riastrad 	KASSERTMSG((psref->psref_target == target),
    356   1.1  riastrad 	    "passive reference target mismatch: %p (ref) != %p (expected)",
    357   1.1  riastrad 	    psref->psref_target, target);
    358   1.1  riastrad 	KASSERTMSG((psref->psref_lwp == curlwp),
    359   1.1  riastrad 	    "passive reference transferred from lwp %p to lwp %p",
    360   1.1  riastrad 	    psref->psref_lwp, curlwp);
    361   1.1  riastrad 	KASSERTMSG((psref->psref_cpu == curcpu()),
    362   1.1  riastrad 	    "passive reference transferred from CPU %u to CPU %u",
    363   1.1  riastrad 	    cpu_index(psref->psref_cpu), cpu_index(curcpu()));
    364   1.1  riastrad 
    365   1.1  riastrad 	/*
    366   1.1  riastrad 	 * Block interrupts and remove the psref from the current CPU's
    367   1.1  riastrad 	 * list.  No need to percpu_getref or get the head of the list,
    368   1.1  riastrad 	 * and the caller guarantees that we are bound to a CPU anyway
    369   1.1  riastrad 	 * (as does blocking interrupts).
    370   1.1  riastrad 	 */
    371   1.1  riastrad 	s = splraiseipl(class->prc_iplcookie);
    372   1.8  knakahar 	pcpu = percpu_getref(class->prc_percpu);
    373   1.9     ozaki #ifdef DEBUG
    374   1.9     ozaki 	/* Sanity-check if the target is surely acquired before.  */
    375   1.9     ozaki 	psref_check_existence(pcpu, psref, target);
    376   1.9     ozaki #endif
    377   1.8  knakahar 	SLIST_REMOVE(&pcpu->pcpu_head, psref, psref, psref_entry);
    378   1.8  knakahar 	percpu_putref(class->prc_percpu);
    379   1.1  riastrad 	splx(s);
    380   1.1  riastrad 
    381  1.13     ozaki #if defined(DIAGNOSTIC) || defined(PSREF_DEBUG)
    382  1.12     ozaki 	KASSERT(curlwp->l_psrefs > 0);
    383  1.12     ozaki 	curlwp->l_psrefs--;
    384  1.12     ozaki #endif
    385  1.13     ozaki #ifdef PSREF_DEBUG
    386  1.13     ozaki 	psref_debug_release(psref);
    387  1.13     ozaki #endif
    388  1.12     ozaki 
    389   1.1  riastrad 	/* If someone is waiting for users to drain, notify 'em.  */
    390   1.1  riastrad 	if (__predict_false(target->prt_draining))
    391   1.1  riastrad 		cv_broadcast(&class->prc_cv);
    392   1.1  riastrad }
    393   1.1  riastrad 
    394   1.1  riastrad /*
    395   1.1  riastrad  * psref_copy(pto, pfrom, class)
    396   1.1  riastrad  *
    397   1.1  riastrad  *	Copy a passive reference from pfrom, which must be in the
    398   1.1  riastrad  *	specified class, to pto.  Both pfrom and pto must later be
    399   1.1  riastrad  *	released with psref_release.
    400   1.1  riastrad  *
    401   1.1  riastrad  *	The caller must not have switched CPUs or LWPs since acquiring
    402   1.1  riastrad  *	pfrom, and must not switch CPUs or LWPs before releasing both
    403   1.1  riastrad  *	pfrom and pto.
    404   1.1  riastrad  */
    405   1.1  riastrad void
    406   1.1  riastrad psref_copy(struct psref *pto, const struct psref *pfrom,
    407   1.1  riastrad     struct psref_class *class)
    408   1.1  riastrad {
    409   1.1  riastrad 	struct psref_cpu *pcpu;
    410   1.1  riastrad 	int s;
    411   1.1  riastrad 
    412   1.1  riastrad 	KASSERTMSG((kpreempt_disabled() || cpu_softintr_p() ||
    413   1.1  riastrad 		ISSET(curlwp->l_pflag, LP_BOUND)),
    414   1.1  riastrad 	    "passive references are CPU-local,"
    415   1.1  riastrad 	    " but preemption is enabled and the caller is not"
    416   1.1  riastrad 	    " in a softint or CPU-bound LWP");
    417   1.1  riastrad 	KASSERTMSG((pto != pfrom),
    418   1.1  riastrad 	    "can't copy passive reference to itself: %p",
    419   1.1  riastrad 	    pto);
    420   1.1  riastrad 
    421   1.1  riastrad 	/* Make sure the pfrom reference looks sensible.  */
    422   1.1  riastrad 	KASSERTMSG((pfrom->psref_lwp == curlwp),
    423   1.1  riastrad 	    "passive reference transferred from lwp %p to lwp %p",
    424   1.1  riastrad 	    pfrom->psref_lwp, curlwp);
    425   1.1  riastrad 	KASSERTMSG((pfrom->psref_cpu == curcpu()),
    426   1.1  riastrad 	    "passive reference transferred from CPU %u to CPU %u",
    427   1.1  riastrad 	    cpu_index(pfrom->psref_cpu), cpu_index(curcpu()));
    428   1.1  riastrad 	KASSERTMSG((pfrom->psref_target->prt_class == class),
    429   1.1  riastrad 	    "mismatched psref target class: %p (ref) != %p (expected)",
    430   1.1  riastrad 	    pfrom->psref_target->prt_class, class);
    431   1.1  riastrad 
    432   1.1  riastrad 	/* Block interrupts and acquire the current CPU's reference list.  */
    433   1.1  riastrad 	s = splraiseipl(class->prc_iplcookie);
    434   1.1  riastrad 	pcpu = percpu_getref(class->prc_percpu);
    435   1.1  riastrad 
    436   1.1  riastrad 	/* Record the new reference.  */
    437   1.8  knakahar 	SLIST_INSERT_HEAD(&pcpu->pcpu_head, pto, psref_entry);
    438   1.1  riastrad 	pto->psref_target = pfrom->psref_target;
    439   1.1  riastrad 	pto->psref_lwp = curlwp;
    440   1.1  riastrad 	pto->psref_cpu = curcpu();
    441   1.1  riastrad 
    442   1.1  riastrad 	/* Release the CPU list and restore interrupts.  */
    443   1.1  riastrad 	percpu_putref(class->prc_percpu);
    444   1.1  riastrad 	splx(s);
    445  1.12     ozaki 
    446  1.13     ozaki #if defined(DIAGNOSTIC) || defined(PSREF_DEBUG)
    447  1.12     ozaki 	curlwp->l_psrefs++;
    448  1.12     ozaki #endif
    449   1.1  riastrad }
    450   1.1  riastrad 
    451   1.1  riastrad /*
    452   1.1  riastrad  * struct psreffed
    453   1.1  riastrad  *
    454   1.1  riastrad  *	Global state for draining a psref target.
    455   1.1  riastrad  */
    456   1.1  riastrad struct psreffed {
    457   1.1  riastrad 	struct psref_class	*class;
    458   1.1  riastrad 	struct psref_target	*target;
    459   1.1  riastrad 	bool			ret;
    460   1.1  riastrad };
    461   1.1  riastrad 
    462   1.1  riastrad static void
    463   1.1  riastrad psreffed_p_xc(void *cookie0, void *cookie1 __unused)
    464   1.1  riastrad {
    465   1.1  riastrad 	struct psreffed *P = cookie0;
    466   1.1  riastrad 
    467   1.1  riastrad 	/*
    468   1.1  riastrad 	 * If we hold a psref to the target, then answer true.
    469   1.1  riastrad 	 *
    470   1.1  riastrad 	 * This is the only dynamic decision that may be made with
    471   1.1  riastrad 	 * psref_held.
    472   1.1  riastrad 	 *
    473   1.1  riastrad 	 * No need to lock anything here: every write transitions from
    474   1.1  riastrad 	 * false to true, so there can be no conflicting writes.  No
    475   1.1  riastrad 	 * need for a memory barrier here because P->ret is read only
    476   1.1  riastrad 	 * after xc_wait, which has already issued any necessary memory
    477   1.1  riastrad 	 * barriers.
    478   1.1  riastrad 	 */
    479   1.4  riastrad 	if (_psref_held(P->target, P->class, true))
    480   1.1  riastrad 		P->ret = true;
    481   1.1  riastrad }
    482   1.1  riastrad 
    483   1.1  riastrad static bool
    484   1.1  riastrad psreffed_p(struct psref_target *target, struct psref_class *class)
    485   1.1  riastrad {
    486   1.1  riastrad 	struct psreffed P = {
    487   1.1  riastrad 		.class = class,
    488   1.1  riastrad 		.target = target,
    489   1.1  riastrad 		.ret = false,
    490   1.1  riastrad 	};
    491   1.1  riastrad 
    492  1.10   msaitoh 	if (__predict_true(mp_online)) {
    493  1.10   msaitoh 		/*
    494  1.10   msaitoh 		 * Ask all CPUs to say whether they hold a psref to the
    495  1.10   msaitoh 		 * target.
    496  1.10   msaitoh 		 */
    497  1.11     ozaki 		xc_wait(xc_broadcast(class->prc_xc_flags, &psreffed_p_xc, &P,
    498  1.11     ozaki 		                     NULL));
    499  1.10   msaitoh 	} else
    500  1.10   msaitoh 		psreffed_p_xc(&P, NULL);
    501   1.1  riastrad 
    502   1.1  riastrad 	return P.ret;
    503   1.1  riastrad }
    504   1.1  riastrad 
    505   1.1  riastrad /*
    506   1.1  riastrad  * psref_target_destroy(target, class)
    507   1.1  riastrad  *
    508   1.1  riastrad  *	Destroy a passive reference target.  Waits for all existing
    509   1.1  riastrad  *	references to drain.  Caller must guarantee no new references
    510   1.1  riastrad  *	will be acquired once it calls psref_target_destroy, e.g. by
    511   1.1  riastrad  *	removing the target from a global list first.  May sleep.
    512   1.1  riastrad  */
    513   1.1  riastrad void
    514   1.1  riastrad psref_target_destroy(struct psref_target *target, struct psref_class *class)
    515   1.1  riastrad {
    516   1.1  riastrad 
    517   1.1  riastrad 	ASSERT_SLEEPABLE();
    518   1.1  riastrad 
    519  1.14  riastrad 	KASSERTMSG(!target->prt_draining, "psref target already destroyed: %p",
    520  1.14  riastrad 	    target);
    521   1.1  riastrad 	KASSERTMSG((target->prt_class == class),
    522   1.1  riastrad 	    "mismatched psref target class: %p (ref) != %p (expected)",
    523   1.1  riastrad 	    target->prt_class, class);
    524   1.1  riastrad 
    525   1.1  riastrad 	/* Request psref_release to notify us when done.  */
    526   1.1  riastrad 	target->prt_draining = true;
    527   1.1  riastrad 
    528   1.1  riastrad 	/* Wait until there are no more references on any CPU.  */
    529   1.1  riastrad 	while (psreffed_p(target, class)) {
    530   1.1  riastrad 		/*
    531   1.1  riastrad 		 * This enter/wait/exit business looks wrong, but it is
    532   1.1  riastrad 		 * both necessary, because psreffed_p performs a
    533   1.1  riastrad 		 * low-priority xcall and hence cannot run while a
    534   1.1  riastrad 		 * mutex is locked, and OK, because the wait is timed
    535   1.1  riastrad 		 * -- explicit wakeups are only an optimization.
    536   1.1  riastrad 		 */
    537   1.1  riastrad 		mutex_enter(&class->prc_lock);
    538   1.1  riastrad 		(void)cv_timedwait(&class->prc_cv, &class->prc_lock, 1);
    539   1.1  riastrad 		mutex_exit(&class->prc_lock);
    540   1.1  riastrad 	}
    541   1.1  riastrad 
    542   1.1  riastrad 	/* No more references.  Cause subsequent psref_acquire to kassert.  */
    543   1.1  riastrad 	target->prt_class = NULL;
    544   1.1  riastrad }
    545   1.1  riastrad 
    546   1.4  riastrad static bool
    547   1.4  riastrad _psref_held(const struct psref_target *target, struct psref_class *class,
    548   1.4  riastrad     bool lwp_mismatch_ok)
    549   1.1  riastrad {
    550   1.1  riastrad 	const struct psref_cpu *pcpu;
    551   1.1  riastrad 	const struct psref *psref;
    552   1.1  riastrad 	int s;
    553   1.1  riastrad 	bool held = false;
    554   1.1  riastrad 
    555   1.1  riastrad 	KASSERTMSG((kpreempt_disabled() || cpu_softintr_p() ||
    556   1.1  riastrad 		ISSET(curlwp->l_pflag, LP_BOUND)),
    557   1.1  riastrad 	    "passive references are CPU-local,"
    558   1.1  riastrad 	    " but preemption is enabled and the caller is not"
    559   1.1  riastrad 	    " in a softint or CPU-bound LWP");
    560   1.1  riastrad 	KASSERTMSG((target->prt_class == class),
    561   1.1  riastrad 	    "mismatched psref target class: %p (ref) != %p (expected)",
    562   1.1  riastrad 	    target->prt_class, class);
    563   1.1  riastrad 
    564   1.1  riastrad 	/* Block interrupts and acquire the current CPU's reference list.  */
    565   1.1  riastrad 	s = splraiseipl(class->prc_iplcookie);
    566   1.1  riastrad 	pcpu = percpu_getref(class->prc_percpu);
    567   1.1  riastrad 
    568   1.1  riastrad 	/* Search through all the references on this CPU.  */
    569   1.8  knakahar 	SLIST_FOREACH(psref, &pcpu->pcpu_head, psref_entry) {
    570   1.5     ozaki 		/* Sanity-check the reference's CPU.  */
    571   1.5     ozaki 		KASSERTMSG((psref->psref_cpu == curcpu()),
    572   1.5     ozaki 		    "passive reference transferred from CPU %u to CPU %u",
    573   1.5     ozaki 		    cpu_index(psref->psref_cpu), cpu_index(curcpu()));
    574   1.5     ozaki 
    575   1.5     ozaki 		/* If it doesn't match, skip it and move on.  */
    576   1.5     ozaki 		if (psref->psref_target != target)
    577   1.5     ozaki 			continue;
    578   1.5     ozaki 
    579   1.5     ozaki 		/*
    580   1.5     ozaki 		 * Sanity-check the reference's LWP if we are asserting
    581   1.5     ozaki 		 * via psref_held that this LWP holds it, but not if we
    582   1.5     ozaki 		 * are testing in psref_target_destroy whether any LWP
    583   1.5     ozaki 		 * still holds it.
    584   1.5     ozaki 		 */
    585   1.4  riastrad 		KASSERTMSG((lwp_mismatch_ok || psref->psref_lwp == curlwp),
    586   1.1  riastrad 		    "passive reference transferred from lwp %p to lwp %p",
    587   1.1  riastrad 		    psref->psref_lwp, curlwp);
    588   1.1  riastrad 
    589   1.5     ozaki 		/* Stop here and report that we found it.  */
    590   1.5     ozaki 		held = true;
    591   1.5     ozaki 		break;
    592   1.1  riastrad 	}
    593   1.1  riastrad 
    594   1.1  riastrad 	/* Release the CPU list and restore interrupts.  */
    595   1.1  riastrad 	percpu_putref(class->prc_percpu);
    596   1.1  riastrad 	splx(s);
    597   1.1  riastrad 
    598   1.1  riastrad 	return held;
    599   1.1  riastrad }
    600   1.4  riastrad 
    601   1.4  riastrad /*
    602   1.4  riastrad  * psref_held(target, class)
    603   1.4  riastrad  *
    604   1.4  riastrad  *	True if the current CPU holds a passive reference to target,
    605   1.4  riastrad  *	false otherwise.  May be used only inside assertions.
    606   1.4  riastrad  */
    607   1.4  riastrad bool
    608   1.4  riastrad psref_held(const struct psref_target *target, struct psref_class *class)
    609   1.4  riastrad {
    610   1.4  riastrad 
    611   1.4  riastrad 	return _psref_held(target, class, false);
    612   1.4  riastrad }
    613  1.13     ozaki 
    614  1.13     ozaki #ifdef PSREF_DEBUG
    615  1.13     ozaki void
    616  1.13     ozaki psref_debug_init_lwp(struct lwp *l)
    617  1.13     ozaki {
    618  1.13     ozaki 	struct psref_debug *prd;
    619  1.13     ozaki 
    620  1.13     ozaki 	prd = kmem_zalloc(sizeof(*prd), KM_SLEEP);
    621  1.13     ozaki 	lwp_setspecific_by_lwp(l, psref_debug_lwp_key, prd);
    622  1.13     ozaki }
    623  1.13     ozaki 
    624  1.13     ozaki static void
    625  1.13     ozaki psref_debug_lwp_free(void *arg)
    626  1.13     ozaki {
    627  1.13     ozaki 	struct psref_debug *prd = arg;
    628  1.13     ozaki 
    629  1.13     ozaki 	kmem_free(prd, sizeof(*prd));
    630  1.13     ozaki }
    631  1.13     ozaki 
    632  1.13     ozaki static void
    633  1.13     ozaki psref_debug_acquire(struct psref *psref)
    634  1.13     ozaki {
    635  1.13     ozaki 	struct psref_debug *prd;
    636  1.13     ozaki 	struct lwp *l = curlwp;
    637  1.13     ozaki 	int s, i;
    638  1.13     ozaki 
    639  1.13     ozaki 	prd = lwp_getspecific(psref_debug_lwp_key);
    640  1.13     ozaki 	if (__predict_false(prd == NULL)) {
    641  1.13     ozaki 		psref->psref_debug = NULL;
    642  1.13     ozaki 		return;
    643  1.13     ozaki 	}
    644  1.13     ozaki 
    645  1.13     ozaki 	s = splserial();
    646  1.13     ozaki 	if (l->l_psrefs > prd->prd_refs_peek) {
    647  1.13     ozaki 		prd->prd_refs_peek = l->l_psrefs;
    648  1.13     ozaki 		if (__predict_false(prd->prd_refs_peek > PSREF_DEBUG_NITEMS))
    649  1.13     ozaki 			panic("exceeded PSREF_DEBUG_NITEMS");
    650  1.13     ozaki 	}
    651  1.13     ozaki 	for (i = 0; i < prd->prd_refs_peek; i++) {
    652  1.13     ozaki 		struct psref_debug_item *prdi = &prd->prd_items[i];
    653  1.13     ozaki 		if (prdi->prdi_psref != NULL)
    654  1.13     ozaki 			continue;
    655  1.13     ozaki 		prdi->prdi_caller = psref->psref_debug;
    656  1.13     ozaki 		prdi->prdi_psref = psref;
    657  1.13     ozaki 		psref->psref_debug = prdi;
    658  1.13     ozaki 		break;
    659  1.13     ozaki 	}
    660  1.13     ozaki 	if (__predict_false(i == prd->prd_refs_peek))
    661  1.13     ozaki 		panic("out of range: %d", i);
    662  1.13     ozaki 	splx(s);
    663  1.13     ozaki }
    664  1.13     ozaki 
    665  1.13     ozaki static void
    666  1.13     ozaki psref_debug_release(struct psref *psref)
    667  1.13     ozaki {
    668  1.13     ozaki 	int s;
    669  1.13     ozaki 
    670  1.13     ozaki 	s = splserial();
    671  1.13     ozaki 	if (__predict_true(psref->psref_debug != NULL)) {
    672  1.13     ozaki 		struct psref_debug_item *prdi = psref->psref_debug;
    673  1.13     ozaki 		prdi->prdi_psref = NULL;
    674  1.13     ozaki 	}
    675  1.13     ozaki 	splx(s);
    676  1.13     ozaki }
    677  1.13     ozaki 
    678  1.13     ozaki void
    679  1.13     ozaki psref_debug_barrier(void)
    680  1.13     ozaki {
    681  1.13     ozaki 	struct psref_debug *prd;
    682  1.13     ozaki 	struct lwp *l = curlwp;
    683  1.13     ozaki 	int s, i;
    684  1.13     ozaki 
    685  1.13     ozaki 	prd = lwp_getspecific(psref_debug_lwp_key);
    686  1.13     ozaki 	if (__predict_false(prd == NULL))
    687  1.13     ozaki 		return;
    688  1.13     ozaki 
    689  1.13     ozaki 	s = splserial();
    690  1.13     ozaki 	for (i = 0; i < prd->prd_refs_peek; i++) {
    691  1.13     ozaki 		struct psref_debug_item *prdi = &prd->prd_items[i];
    692  1.13     ozaki 		if (__predict_true(prdi->prdi_psref == NULL))
    693  1.13     ozaki 			continue;
    694  1.13     ozaki 		panic("psref leaked: lwp(%p) acquired at %p", l, prdi->prdi_caller);
    695  1.13     ozaki 	}
    696  1.13     ozaki 	prd->prd_refs_peek = 0; /* Reset the counter */
    697  1.13     ozaki 	splx(s);
    698  1.13     ozaki }
    699  1.13     ozaki #endif /* PSREF_DEBUG */
    700