Home | History | Annotate | Line # | Download | only in kern
subr_psref.c revision 1.10
      1  1.10   msaitoh /*	$NetBSD: subr_psref.c,v 1.10 2017/12/28 03:39:48 msaitoh 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.10   msaitoh __KERNEL_RCSID(0, "$NetBSD: subr_psref.c,v 1.10 2017/12/28 03:39:48 msaitoh 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.1  riastrad 
     81   1.8  knakahar SLIST_HEAD(psref_head, psref);
     82   1.1  riastrad 
     83   1.4  riastrad static bool	_psref_held(const struct psref_target *, struct psref_class *,
     84   1.4  riastrad 		    bool);
     85   1.4  riastrad 
     86   1.1  riastrad /*
     87   1.1  riastrad  * struct psref_class
     88   1.1  riastrad  *
     89   1.1  riastrad  *	Private global state for a class of passive reference targets.
     90   1.1  riastrad  *	Opaque to callers.
     91   1.1  riastrad  */
     92   1.1  riastrad struct psref_class {
     93   1.1  riastrad 	kmutex_t		prc_lock;
     94   1.1  riastrad 	kcondvar_t		prc_cv;
     95   1.1  riastrad 	struct percpu		*prc_percpu; /* struct psref_cpu */
     96   1.1  riastrad 	ipl_cookie_t		prc_iplcookie;
     97   1.1  riastrad };
     98   1.1  riastrad 
     99   1.1  riastrad /*
    100   1.1  riastrad  * struct psref_cpu
    101   1.1  riastrad  *
    102   1.1  riastrad  *	Private per-CPU state for a class of passive reference targets.
    103   1.1  riastrad  *	Not exposed by the API.
    104   1.1  riastrad  */
    105   1.1  riastrad struct psref_cpu {
    106   1.1  riastrad 	struct psref_head	pcpu_head;
    107   1.1  riastrad };
    108   1.1  riastrad 
    109   1.1  riastrad /*
    110   1.1  riastrad  * psref_class_create(name, ipl)
    111   1.1  riastrad  *
    112   1.1  riastrad  *	Create a new passive reference class, with the given wchan name
    113   1.1  riastrad  *	and ipl.
    114   1.1  riastrad  */
    115   1.1  riastrad struct psref_class *
    116   1.1  riastrad psref_class_create(const char *name, int ipl)
    117   1.1  riastrad {
    118   1.1  riastrad 	struct psref_class *class;
    119   1.1  riastrad 
    120   1.1  riastrad 	ASSERT_SLEEPABLE();
    121   1.1  riastrad 
    122   1.1  riastrad 	class = kmem_alloc(sizeof(*class), KM_SLEEP);
    123   1.1  riastrad 	class->prc_percpu = percpu_alloc(sizeof(struct psref_cpu));
    124   1.1  riastrad 	mutex_init(&class->prc_lock, MUTEX_DEFAULT, ipl);
    125   1.1  riastrad 	cv_init(&class->prc_cv, name);
    126   1.1  riastrad 	class->prc_iplcookie = makeiplcookie(ipl);
    127   1.1  riastrad 
    128   1.1  riastrad 	return class;
    129   1.1  riastrad }
    130   1.1  riastrad 
    131   1.1  riastrad #ifdef DIAGNOSTIC
    132   1.1  riastrad static void
    133   1.1  riastrad psref_cpu_drained_p(void *p, void *cookie, struct cpu_info *ci __unused)
    134   1.1  riastrad {
    135   1.1  riastrad 	const struct psref_cpu *pcpu = p;
    136   1.1  riastrad 	bool *retp = cookie;
    137   1.1  riastrad 
    138   1.8  knakahar 	if (!SLIST_EMPTY(&pcpu->pcpu_head))
    139   1.1  riastrad 		*retp = false;
    140   1.1  riastrad }
    141   1.1  riastrad 
    142   1.1  riastrad static bool
    143   1.1  riastrad psref_class_drained_p(const struct psref_class *prc)
    144   1.1  riastrad {
    145   1.1  riastrad 	bool ret = true;
    146   1.1  riastrad 
    147   1.1  riastrad 	percpu_foreach(prc->prc_percpu, &psref_cpu_drained_p, &ret);
    148   1.1  riastrad 
    149   1.1  riastrad 	return ret;
    150   1.1  riastrad }
    151   1.1  riastrad #endif	/* DIAGNOSTIC */
    152   1.1  riastrad 
    153   1.1  riastrad /*
    154   1.1  riastrad  * psref_class_destroy(class)
    155   1.1  riastrad  *
    156   1.1  riastrad  *	Destroy a passive reference class and free memory associated
    157   1.1  riastrad  *	with it.  All targets in this class must have been drained and
    158   1.1  riastrad  *	destroyed already.
    159   1.1  riastrad  */
    160   1.1  riastrad void
    161   1.1  riastrad psref_class_destroy(struct psref_class *class)
    162   1.1  riastrad {
    163   1.1  riastrad 
    164   1.1  riastrad 	KASSERT(psref_class_drained_p(class));
    165   1.1  riastrad 
    166   1.1  riastrad 	cv_destroy(&class->prc_cv);
    167   1.1  riastrad 	mutex_destroy(&class->prc_lock);
    168   1.1  riastrad 	percpu_free(class->prc_percpu, sizeof(struct psref_cpu));
    169   1.1  riastrad 	kmem_free(class, sizeof(*class));
    170   1.1  riastrad }
    171   1.1  riastrad 
    172   1.1  riastrad /*
    173   1.1  riastrad  * psref_target_init(target, class)
    174   1.1  riastrad  *
    175   1.1  riastrad  *	Initialize a passive reference target in the specified class.
    176   1.1  riastrad  *	The caller is responsible for issuing a membar_producer after
    177   1.1  riastrad  *	psref_target_init and before exposing a pointer to the target
    178   1.1  riastrad  *	to other CPUs.
    179   1.1  riastrad  */
    180   1.1  riastrad void
    181   1.1  riastrad psref_target_init(struct psref_target *target,
    182   1.1  riastrad     struct psref_class *class)
    183   1.1  riastrad {
    184   1.1  riastrad 
    185   1.1  riastrad 	target->prt_class = class;
    186   1.1  riastrad 	target->prt_draining = false;
    187   1.1  riastrad }
    188   1.1  riastrad 
    189   1.6     ozaki #ifdef DEBUG
    190   1.9     ozaki static bool
    191   1.9     ozaki psref_exist(struct psref_cpu *pcpu, struct psref *psref)
    192   1.9     ozaki {
    193   1.9     ozaki 	struct psref *_psref;
    194   1.9     ozaki 
    195   1.9     ozaki 	SLIST_FOREACH(_psref, &pcpu->pcpu_head, psref_entry) {
    196   1.9     ozaki 		if (_psref == psref)
    197   1.9     ozaki 			return true;
    198   1.9     ozaki 	}
    199   1.9     ozaki 	return false;
    200   1.9     ozaki }
    201   1.9     ozaki 
    202   1.6     ozaki static void
    203   1.6     ozaki psref_check_duplication(struct psref_cpu *pcpu, struct psref *psref,
    204   1.6     ozaki     const struct psref_target *target)
    205   1.6     ozaki {
    206   1.6     ozaki 	bool found = false;
    207   1.6     ozaki 
    208   1.9     ozaki 	found = psref_exist(pcpu, psref);
    209   1.9     ozaki 	if (found) {
    210   1.9     ozaki 		panic("The psref is already in the list (acquiring twice?): "
    211   1.9     ozaki 		    "psref=%p target=%p", psref, target);
    212   1.6     ozaki 	}
    213   1.9     ozaki }
    214   1.9     ozaki 
    215   1.9     ozaki static void
    216   1.9     ozaki psref_check_existence(struct psref_cpu *pcpu, struct psref *psref,
    217   1.9     ozaki     const struct psref_target *target)
    218   1.9     ozaki {
    219   1.9     ozaki 	bool found = false;
    220   1.9     ozaki 
    221   1.9     ozaki 	found = psref_exist(pcpu, psref);
    222   1.9     ozaki 	if (!found) {
    223   1.9     ozaki 		panic("The psref isn't in the list (releasing unused psref?): "
    224   1.6     ozaki 		    "psref=%p target=%p", psref, target);
    225   1.6     ozaki 	}
    226   1.6     ozaki }
    227   1.6     ozaki #endif /* DEBUG */
    228   1.6     ozaki 
    229   1.1  riastrad /*
    230   1.1  riastrad  * psref_acquire(psref, target, class)
    231   1.1  riastrad  *
    232   1.1  riastrad  *	Acquire a passive reference to the specified target, which must
    233   1.1  riastrad  *	be in the specified class.
    234   1.1  riastrad  *
    235   1.1  riastrad  *	The caller must guarantee that the target will not be destroyed
    236   1.1  riastrad  *	before psref_acquire returns.
    237   1.1  riastrad  *
    238   1.1  riastrad  *	The caller must additionally guarantee that it will not switch
    239   1.1  riastrad  *	CPUs before releasing the passive reference, either by
    240   1.1  riastrad  *	disabling kpreemption and avoiding sleeps, or by being in a
    241   1.1  riastrad  *	softint or in an LWP bound to a CPU.
    242   1.1  riastrad  */
    243   1.1  riastrad void
    244   1.1  riastrad psref_acquire(struct psref *psref, const struct psref_target *target,
    245   1.1  riastrad     struct psref_class *class)
    246   1.1  riastrad {
    247   1.1  riastrad 	struct psref_cpu *pcpu;
    248   1.1  riastrad 	int s;
    249   1.1  riastrad 
    250   1.1  riastrad 	KASSERTMSG((kpreempt_disabled() || cpu_softintr_p() ||
    251   1.1  riastrad 		ISSET(curlwp->l_pflag, LP_BOUND)),
    252   1.1  riastrad 	    "passive references are CPU-local,"
    253   1.1  riastrad 	    " but preemption is enabled and the caller is not"
    254   1.1  riastrad 	    " in a softint or CPU-bound LWP");
    255   1.1  riastrad 	KASSERTMSG((target->prt_class == class),
    256   1.1  riastrad 	    "mismatched psref target class: %p (ref) != %p (expected)",
    257   1.1  riastrad 	    target->prt_class, class);
    258   1.1  riastrad 	KASSERTMSG(!target->prt_draining, "psref target already destroyed: %p",
    259   1.1  riastrad 	    target);
    260   1.1  riastrad 
    261   1.1  riastrad 	/* Block interrupts and acquire the current CPU's reference list.  */
    262   1.1  riastrad 	s = splraiseipl(class->prc_iplcookie);
    263   1.1  riastrad 	pcpu = percpu_getref(class->prc_percpu);
    264   1.1  riastrad 
    265   1.6     ozaki #ifdef DEBUG
    266   1.6     ozaki 	/* Sanity-check if the target is already acquired with the same psref.  */
    267   1.6     ozaki 	psref_check_duplication(pcpu, psref, target);
    268   1.6     ozaki #endif
    269   1.6     ozaki 
    270   1.1  riastrad 	/* Record our reference.  */
    271   1.8  knakahar 	SLIST_INSERT_HEAD(&pcpu->pcpu_head, psref, psref_entry);
    272   1.1  riastrad 	psref->psref_target = target;
    273   1.1  riastrad 	psref->psref_lwp = curlwp;
    274   1.1  riastrad 	psref->psref_cpu = curcpu();
    275   1.1  riastrad 
    276   1.1  riastrad 	/* Release the CPU list and restore interrupts.  */
    277   1.1  riastrad 	percpu_putref(class->prc_percpu);
    278   1.1  riastrad 	splx(s);
    279   1.1  riastrad }
    280   1.1  riastrad 
    281   1.1  riastrad /*
    282   1.1  riastrad  * psref_release(psref, target, class)
    283   1.1  riastrad  *
    284   1.1  riastrad  *	Release a passive reference to the specified target, which must
    285   1.1  riastrad  *	be in the specified class.
    286   1.1  riastrad  *
    287   1.1  riastrad  *	The caller must not have switched CPUs or LWPs since acquiring
    288   1.1  riastrad  *	the passive reference.
    289   1.1  riastrad  */
    290   1.1  riastrad void
    291   1.1  riastrad psref_release(struct psref *psref, const struct psref_target *target,
    292   1.1  riastrad     struct psref_class *class)
    293   1.1  riastrad {
    294   1.8  knakahar 	struct psref_cpu *pcpu;
    295   1.1  riastrad 	int s;
    296   1.1  riastrad 
    297   1.1  riastrad 	KASSERTMSG((kpreempt_disabled() || cpu_softintr_p() ||
    298   1.1  riastrad 		ISSET(curlwp->l_pflag, LP_BOUND)),
    299   1.1  riastrad 	    "passive references are CPU-local,"
    300   1.1  riastrad 	    " but preemption is enabled and the caller is not"
    301   1.1  riastrad 	    " in a softint or CPU-bound LWP");
    302   1.1  riastrad 	KASSERTMSG((target->prt_class == class),
    303   1.1  riastrad 	    "mismatched psref target class: %p (ref) != %p (expected)",
    304   1.1  riastrad 	    target->prt_class, class);
    305   1.1  riastrad 
    306   1.1  riastrad 	/* Make sure the psref looks sensible.  */
    307   1.1  riastrad 	KASSERTMSG((psref->psref_target == target),
    308   1.1  riastrad 	    "passive reference target mismatch: %p (ref) != %p (expected)",
    309   1.1  riastrad 	    psref->psref_target, target);
    310   1.1  riastrad 	KASSERTMSG((psref->psref_lwp == curlwp),
    311   1.1  riastrad 	    "passive reference transferred from lwp %p to lwp %p",
    312   1.1  riastrad 	    psref->psref_lwp, curlwp);
    313   1.1  riastrad 	KASSERTMSG((psref->psref_cpu == curcpu()),
    314   1.1  riastrad 	    "passive reference transferred from CPU %u to CPU %u",
    315   1.1  riastrad 	    cpu_index(psref->psref_cpu), cpu_index(curcpu()));
    316   1.1  riastrad 
    317   1.1  riastrad 	/*
    318   1.1  riastrad 	 * Block interrupts and remove the psref from the current CPU's
    319   1.1  riastrad 	 * list.  No need to percpu_getref or get the head of the list,
    320   1.1  riastrad 	 * and the caller guarantees that we are bound to a CPU anyway
    321   1.1  riastrad 	 * (as does blocking interrupts).
    322   1.1  riastrad 	 */
    323   1.1  riastrad 	s = splraiseipl(class->prc_iplcookie);
    324   1.8  knakahar 	pcpu = percpu_getref(class->prc_percpu);
    325   1.9     ozaki #ifdef DEBUG
    326   1.9     ozaki 	/* Sanity-check if the target is surely acquired before.  */
    327   1.9     ozaki 	psref_check_existence(pcpu, psref, target);
    328   1.9     ozaki #endif
    329   1.8  knakahar 	SLIST_REMOVE(&pcpu->pcpu_head, psref, psref, psref_entry);
    330   1.8  knakahar 	percpu_putref(class->prc_percpu);
    331   1.1  riastrad 	splx(s);
    332   1.1  riastrad 
    333   1.1  riastrad 	/* If someone is waiting for users to drain, notify 'em.  */
    334   1.1  riastrad 	if (__predict_false(target->prt_draining))
    335   1.1  riastrad 		cv_broadcast(&class->prc_cv);
    336   1.1  riastrad }
    337   1.1  riastrad 
    338   1.1  riastrad /*
    339   1.1  riastrad  * psref_copy(pto, pfrom, class)
    340   1.1  riastrad  *
    341   1.1  riastrad  *	Copy a passive reference from pfrom, which must be in the
    342   1.1  riastrad  *	specified class, to pto.  Both pfrom and pto must later be
    343   1.1  riastrad  *	released with psref_release.
    344   1.1  riastrad  *
    345   1.1  riastrad  *	The caller must not have switched CPUs or LWPs since acquiring
    346   1.1  riastrad  *	pfrom, and must not switch CPUs or LWPs before releasing both
    347   1.1  riastrad  *	pfrom and pto.
    348   1.1  riastrad  */
    349   1.1  riastrad void
    350   1.1  riastrad psref_copy(struct psref *pto, const struct psref *pfrom,
    351   1.1  riastrad     struct psref_class *class)
    352   1.1  riastrad {
    353   1.1  riastrad 	struct psref_cpu *pcpu;
    354   1.1  riastrad 	int s;
    355   1.1  riastrad 
    356   1.1  riastrad 	KASSERTMSG((kpreempt_disabled() || cpu_softintr_p() ||
    357   1.1  riastrad 		ISSET(curlwp->l_pflag, LP_BOUND)),
    358   1.1  riastrad 	    "passive references are CPU-local,"
    359   1.1  riastrad 	    " but preemption is enabled and the caller is not"
    360   1.1  riastrad 	    " in a softint or CPU-bound LWP");
    361   1.1  riastrad 	KASSERTMSG((pto != pfrom),
    362   1.1  riastrad 	    "can't copy passive reference to itself: %p",
    363   1.1  riastrad 	    pto);
    364   1.1  riastrad 
    365   1.1  riastrad 	/* Make sure the pfrom reference looks sensible.  */
    366   1.1  riastrad 	KASSERTMSG((pfrom->psref_lwp == curlwp),
    367   1.1  riastrad 	    "passive reference transferred from lwp %p to lwp %p",
    368   1.1  riastrad 	    pfrom->psref_lwp, curlwp);
    369   1.1  riastrad 	KASSERTMSG((pfrom->psref_cpu == curcpu()),
    370   1.1  riastrad 	    "passive reference transferred from CPU %u to CPU %u",
    371   1.1  riastrad 	    cpu_index(pfrom->psref_cpu), cpu_index(curcpu()));
    372   1.1  riastrad 	KASSERTMSG((pfrom->psref_target->prt_class == class),
    373   1.1  riastrad 	    "mismatched psref target class: %p (ref) != %p (expected)",
    374   1.1  riastrad 	    pfrom->psref_target->prt_class, class);
    375   1.1  riastrad 
    376   1.1  riastrad 	/* Block interrupts and acquire the current CPU's reference list.  */
    377   1.1  riastrad 	s = splraiseipl(class->prc_iplcookie);
    378   1.1  riastrad 	pcpu = percpu_getref(class->prc_percpu);
    379   1.1  riastrad 
    380   1.1  riastrad 	/* Record the new reference.  */
    381   1.8  knakahar 	SLIST_INSERT_HEAD(&pcpu->pcpu_head, pto, psref_entry);
    382   1.1  riastrad 	pto->psref_target = pfrom->psref_target;
    383   1.1  riastrad 	pto->psref_lwp = curlwp;
    384   1.1  riastrad 	pto->psref_cpu = curcpu();
    385   1.1  riastrad 
    386   1.1  riastrad 	/* Release the CPU list and restore interrupts.  */
    387   1.1  riastrad 	percpu_putref(class->prc_percpu);
    388   1.1  riastrad 	splx(s);
    389   1.1  riastrad }
    390   1.1  riastrad 
    391   1.1  riastrad /*
    392   1.1  riastrad  * struct psreffed
    393   1.1  riastrad  *
    394   1.1  riastrad  *	Global state for draining a psref target.
    395   1.1  riastrad  */
    396   1.1  riastrad struct psreffed {
    397   1.1  riastrad 	struct psref_class	*class;
    398   1.1  riastrad 	struct psref_target	*target;
    399   1.1  riastrad 	bool			ret;
    400   1.1  riastrad };
    401   1.1  riastrad 
    402   1.1  riastrad static void
    403   1.1  riastrad psreffed_p_xc(void *cookie0, void *cookie1 __unused)
    404   1.1  riastrad {
    405   1.1  riastrad 	struct psreffed *P = cookie0;
    406   1.1  riastrad 
    407   1.1  riastrad 	/*
    408   1.1  riastrad 	 * If we hold a psref to the target, then answer true.
    409   1.1  riastrad 	 *
    410   1.1  riastrad 	 * This is the only dynamic decision that may be made with
    411   1.1  riastrad 	 * psref_held.
    412   1.1  riastrad 	 *
    413   1.1  riastrad 	 * No need to lock anything here: every write transitions from
    414   1.1  riastrad 	 * false to true, so there can be no conflicting writes.  No
    415   1.1  riastrad 	 * need for a memory barrier here because P->ret is read only
    416   1.1  riastrad 	 * after xc_wait, which has already issued any necessary memory
    417   1.1  riastrad 	 * barriers.
    418   1.1  riastrad 	 */
    419   1.4  riastrad 	if (_psref_held(P->target, P->class, true))
    420   1.1  riastrad 		P->ret = true;
    421   1.1  riastrad }
    422   1.1  riastrad 
    423   1.1  riastrad static bool
    424   1.1  riastrad psreffed_p(struct psref_target *target, struct psref_class *class)
    425   1.1  riastrad {
    426   1.1  riastrad 	struct psreffed P = {
    427   1.1  riastrad 		.class = class,
    428   1.1  riastrad 		.target = target,
    429   1.1  riastrad 		.ret = false,
    430   1.1  riastrad 	};
    431   1.1  riastrad 
    432  1.10   msaitoh 	if (__predict_true(mp_online)) {
    433  1.10   msaitoh 		/*
    434  1.10   msaitoh 		 * Ask all CPUs to say whether they hold a psref to the
    435  1.10   msaitoh 		 * target.
    436  1.10   msaitoh 		 */
    437  1.10   msaitoh 		xc_wait(xc_broadcast(0, &psreffed_p_xc, &P, NULL));
    438  1.10   msaitoh 	} else
    439  1.10   msaitoh 		psreffed_p_xc(&P, NULL);
    440   1.1  riastrad 
    441   1.1  riastrad 	return P.ret;
    442   1.1  riastrad }
    443   1.1  riastrad 
    444   1.1  riastrad /*
    445   1.1  riastrad  * psref_target_destroy(target, class)
    446   1.1  riastrad  *
    447   1.1  riastrad  *	Destroy a passive reference target.  Waits for all existing
    448   1.1  riastrad  *	references to drain.  Caller must guarantee no new references
    449   1.1  riastrad  *	will be acquired once it calls psref_target_destroy, e.g. by
    450   1.1  riastrad  *	removing the target from a global list first.  May sleep.
    451   1.1  riastrad  */
    452   1.1  riastrad void
    453   1.1  riastrad psref_target_destroy(struct psref_target *target, struct psref_class *class)
    454   1.1  riastrad {
    455   1.1  riastrad 
    456   1.1  riastrad 	ASSERT_SLEEPABLE();
    457   1.1  riastrad 
    458   1.1  riastrad 	KASSERTMSG((target->prt_class == class),
    459   1.1  riastrad 	    "mismatched psref target class: %p (ref) != %p (expected)",
    460   1.1  riastrad 	    target->prt_class, class);
    461   1.1  riastrad 
    462   1.1  riastrad 	/* Request psref_release to notify us when done.  */
    463   1.1  riastrad 	KASSERTMSG(!target->prt_draining, "psref target already destroyed: %p",
    464   1.1  riastrad 	    target);
    465   1.1  riastrad 	target->prt_draining = true;
    466   1.1  riastrad 
    467   1.1  riastrad 	/* Wait until there are no more references on any CPU.  */
    468   1.1  riastrad 	while (psreffed_p(target, class)) {
    469   1.1  riastrad 		/*
    470   1.1  riastrad 		 * This enter/wait/exit business looks wrong, but it is
    471   1.1  riastrad 		 * both necessary, because psreffed_p performs a
    472   1.1  riastrad 		 * low-priority xcall and hence cannot run while a
    473   1.1  riastrad 		 * mutex is locked, and OK, because the wait is timed
    474   1.1  riastrad 		 * -- explicit wakeups are only an optimization.
    475   1.1  riastrad 		 */
    476   1.1  riastrad 		mutex_enter(&class->prc_lock);
    477   1.1  riastrad 		(void)cv_timedwait(&class->prc_cv, &class->prc_lock, 1);
    478   1.1  riastrad 		mutex_exit(&class->prc_lock);
    479   1.1  riastrad 	}
    480   1.1  riastrad 
    481   1.1  riastrad 	/* No more references.  Cause subsequent psref_acquire to kassert.  */
    482   1.1  riastrad 	target->prt_class = NULL;
    483   1.1  riastrad }
    484   1.1  riastrad 
    485   1.4  riastrad static bool
    486   1.4  riastrad _psref_held(const struct psref_target *target, struct psref_class *class,
    487   1.4  riastrad     bool lwp_mismatch_ok)
    488   1.1  riastrad {
    489   1.1  riastrad 	const struct psref_cpu *pcpu;
    490   1.1  riastrad 	const struct psref *psref;
    491   1.1  riastrad 	int s;
    492   1.1  riastrad 	bool held = false;
    493   1.1  riastrad 
    494   1.1  riastrad 	KASSERTMSG((kpreempt_disabled() || cpu_softintr_p() ||
    495   1.1  riastrad 		ISSET(curlwp->l_pflag, LP_BOUND)),
    496   1.1  riastrad 	    "passive references are CPU-local,"
    497   1.1  riastrad 	    " but preemption is enabled and the caller is not"
    498   1.1  riastrad 	    " in a softint or CPU-bound LWP");
    499   1.1  riastrad 	KASSERTMSG((target->prt_class == class),
    500   1.1  riastrad 	    "mismatched psref target class: %p (ref) != %p (expected)",
    501   1.1  riastrad 	    target->prt_class, class);
    502   1.1  riastrad 
    503   1.1  riastrad 	/* Block interrupts and acquire the current CPU's reference list.  */
    504   1.1  riastrad 	s = splraiseipl(class->prc_iplcookie);
    505   1.1  riastrad 	pcpu = percpu_getref(class->prc_percpu);
    506   1.1  riastrad 
    507   1.1  riastrad 	/* Search through all the references on this CPU.  */
    508   1.8  knakahar 	SLIST_FOREACH(psref, &pcpu->pcpu_head, psref_entry) {
    509   1.5     ozaki 		/* Sanity-check the reference's CPU.  */
    510   1.5     ozaki 		KASSERTMSG((psref->psref_cpu == curcpu()),
    511   1.5     ozaki 		    "passive reference transferred from CPU %u to CPU %u",
    512   1.5     ozaki 		    cpu_index(psref->psref_cpu), cpu_index(curcpu()));
    513   1.5     ozaki 
    514   1.5     ozaki 		/* If it doesn't match, skip it and move on.  */
    515   1.5     ozaki 		if (psref->psref_target != target)
    516   1.5     ozaki 			continue;
    517   1.5     ozaki 
    518   1.5     ozaki 		/*
    519   1.5     ozaki 		 * Sanity-check the reference's LWP if we are asserting
    520   1.5     ozaki 		 * via psref_held that this LWP holds it, but not if we
    521   1.5     ozaki 		 * are testing in psref_target_destroy whether any LWP
    522   1.5     ozaki 		 * still holds it.
    523   1.5     ozaki 		 */
    524   1.4  riastrad 		KASSERTMSG((lwp_mismatch_ok || psref->psref_lwp == curlwp),
    525   1.1  riastrad 		    "passive reference transferred from lwp %p to lwp %p",
    526   1.1  riastrad 		    psref->psref_lwp, curlwp);
    527   1.1  riastrad 
    528   1.5     ozaki 		/* Stop here and report that we found it.  */
    529   1.5     ozaki 		held = true;
    530   1.5     ozaki 		break;
    531   1.1  riastrad 	}
    532   1.1  riastrad 
    533   1.1  riastrad 	/* Release the CPU list and restore interrupts.  */
    534   1.1  riastrad 	percpu_putref(class->prc_percpu);
    535   1.1  riastrad 	splx(s);
    536   1.1  riastrad 
    537   1.1  riastrad 	return held;
    538   1.1  riastrad }
    539   1.4  riastrad 
    540   1.4  riastrad /*
    541   1.4  riastrad  * psref_held(target, class)
    542   1.4  riastrad  *
    543   1.4  riastrad  *	True if the current CPU holds a passive reference to target,
    544   1.4  riastrad  *	false otherwise.  May be used only inside assertions.
    545   1.4  riastrad  */
    546   1.4  riastrad bool
    547   1.4  riastrad psref_held(const struct psref_target *target, struct psref_class *class)
    548   1.4  riastrad {
    549   1.4  riastrad 
    550   1.4  riastrad 	return _psref_held(target, class, false);
    551   1.4  riastrad }
    552