xref: /netbsd-src/sys/kern/subr_psref.c (revision 7ff6f3e72190c2c18dfa36459c9b417db7bab659)
1*7ff6f3e7Smacallan /*	$NetBSD: subr_psref.c,v 1.18 2022/02/12 16:31:06 macallan Exp $	*/
2c03dceb1Sriastradh 
3c03dceb1Sriastradh /*-
4c03dceb1Sriastradh  * Copyright (c) 2016 The NetBSD Foundation, Inc.
5c03dceb1Sriastradh  * All rights reserved.
6c03dceb1Sriastradh  *
7c03dceb1Sriastradh  * This code is derived from software contributed to The NetBSD Foundation
8c03dceb1Sriastradh  * by Taylor R. Campbell.
9c03dceb1Sriastradh  *
10c03dceb1Sriastradh  * Redistribution and use in source and binary forms, with or without
11c03dceb1Sriastradh  * modification, are permitted provided that the following conditions
12c03dceb1Sriastradh  * are met:
13c03dceb1Sriastradh  * 1. Redistributions of source code must retain the above copyright
14c03dceb1Sriastradh  *    notice, this list of conditions and the following disclaimer.
15c03dceb1Sriastradh  * 2. Redistributions in binary form must reproduce the above copyright
16c03dceb1Sriastradh  *    notice, this list of conditions and the following disclaimer in the
17c03dceb1Sriastradh  *    documentation and/or other materials provided with the distribution.
18c03dceb1Sriastradh  *
19c03dceb1Sriastradh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20c03dceb1Sriastradh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21c03dceb1Sriastradh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22c03dceb1Sriastradh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23c03dceb1Sriastradh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24c03dceb1Sriastradh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25c03dceb1Sriastradh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26c03dceb1Sriastradh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27c03dceb1Sriastradh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28c03dceb1Sriastradh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29c03dceb1Sriastradh  * POSSIBILITY OF SUCH DAMAGE.
30c03dceb1Sriastradh  */
31c03dceb1Sriastradh 
32c03dceb1Sriastradh /*
33c03dceb1Sriastradh  * Passive references
34c03dceb1Sriastradh  *
35c03dceb1Sriastradh  *	Passive references are references to objects that guarantee the
36c03dceb1Sriastradh  *	object will not be destroyed until the reference is released.
37c03dceb1Sriastradh  *
38c03dceb1Sriastradh  *	Passive references require no interprocessor synchronization to
39c03dceb1Sriastradh  *	acquire or release.  However, destroying the target of passive
40c03dceb1Sriastradh  *	references requires expensive interprocessor synchronization --
41c03dceb1Sriastradh  *	xcalls to determine on which CPUs the object is still in use.
42c03dceb1Sriastradh  *
43c03dceb1Sriastradh  *	Passive references may be held only on a single CPU and by a
44c03dceb1Sriastradh  *	single LWP.  They require the caller to allocate a little stack
45c03dceb1Sriastradh  *	space, a struct psref object.  Sleeping while a passive
46c03dceb1Sriastradh  *	reference is held is allowed, provided that the owner's LWP is
47c03dceb1Sriastradh  *	bound to a CPU -- e.g., the owner is a softint or a bound
48c03dceb1Sriastradh  *	kthread.  However, sleeping should be kept to a short duration,
49c03dceb1Sriastradh  *	e.g. sleeping on an adaptive lock.
50c03dceb1Sriastradh  *
51c03dceb1Sriastradh  *	Passive references serve as an intermediate stage between
52c03dceb1Sriastradh  *	reference counting and passive serialization (pserialize(9)):
53c03dceb1Sriastradh  *
54c03dceb1Sriastradh  *	- If you need references to transfer from CPU to CPU or LWP to
55c03dceb1Sriastradh  *	  LWP, or if you need long-term references, you must use
56c03dceb1Sriastradh  *	  reference counting, e.g. with atomic operations or locks,
57c03dceb1Sriastradh  *	  which incurs interprocessor synchronization for every use --
58c03dceb1Sriastradh  *	  cheaper than an xcall, but not scalable.
59c03dceb1Sriastradh  *
60c03dceb1Sriastradh  *	- If all users *guarantee* that they will not sleep, then it is
61c03dceb1Sriastradh  *	  not necessary to use passive references: you may as well just
62c03dceb1Sriastradh  *	  use the even cheaper pserialize(9), because you have
63c03dceb1Sriastradh  *	  satisfied the requirements of a pserialize read section.
64c03dceb1Sriastradh  */
65c03dceb1Sriastradh 
66c03dceb1Sriastradh #include <sys/cdefs.h>
67*7ff6f3e7Smacallan __KERNEL_RCSID(0, "$NetBSD: subr_psref.c,v 1.18 2022/02/12 16:31:06 macallan Exp $");
68c03dceb1Sriastradh 
69075d1d67Sskrll #include <sys/param.h>
70c03dceb1Sriastradh #include <sys/types.h>
71c03dceb1Sriastradh #include <sys/condvar.h>
72c03dceb1Sriastradh #include <sys/cpu.h>
73c03dceb1Sriastradh #include <sys/intr.h>
74c03dceb1Sriastradh #include <sys/kmem.h>
75c03dceb1Sriastradh #include <sys/lwp.h>
76c03dceb1Sriastradh #include <sys/mutex.h>
77c03dceb1Sriastradh #include <sys/percpu.h>
78c03dceb1Sriastradh #include <sys/psref.h>
79c03dceb1Sriastradh #include <sys/queue.h>
80c03dceb1Sriastradh #include <sys/xcall.h>
817fc219a5Sozaki-r #include <sys/lwp.h>
82c03dceb1Sriastradh 
83b2d6db80Sknakahara SLIST_HEAD(psref_head, psref);
84c03dceb1Sriastradh 
85421ddf53Sriastradh static bool	_psref_held(const struct psref_target *, struct psref_class *,
86421ddf53Sriastradh 		    bool);
87421ddf53Sriastradh 
88c03dceb1Sriastradh /*
89c03dceb1Sriastradh  * struct psref_class
90c03dceb1Sriastradh  *
91c03dceb1Sriastradh  *	Private global state for a class of passive reference targets.
92c03dceb1Sriastradh  *	Opaque to callers.
93c03dceb1Sriastradh  */
94c03dceb1Sriastradh struct psref_class {
95c03dceb1Sriastradh 	kmutex_t		prc_lock;
96c03dceb1Sriastradh 	kcondvar_t		prc_cv;
97c03dceb1Sriastradh 	struct percpu		*prc_percpu; /* struct psref_cpu */
98c03dceb1Sriastradh 	ipl_cookie_t		prc_iplcookie;
99d10d3470Sozaki-r 	unsigned int		prc_xc_flags;
100c03dceb1Sriastradh };
101c03dceb1Sriastradh 
102c03dceb1Sriastradh /*
103c03dceb1Sriastradh  * struct psref_cpu
104c03dceb1Sriastradh  *
105c03dceb1Sriastradh  *	Private per-CPU state for a class of passive reference targets.
106c03dceb1Sriastradh  *	Not exposed by the API.
107c03dceb1Sriastradh  */
108c03dceb1Sriastradh struct psref_cpu {
109c03dceb1Sriastradh 	struct psref_head	pcpu_head;
110c03dceb1Sriastradh };
111c03dceb1Sriastradh 
112c03dceb1Sriastradh /*
1137fc219a5Sozaki-r  * Data structures and functions for debugging.
1147fc219a5Sozaki-r  */
1157fc219a5Sozaki-r #ifndef PSREF_DEBUG_NITEMS
1167fc219a5Sozaki-r #define PSREF_DEBUG_NITEMS 16
1177fc219a5Sozaki-r #endif
1187fc219a5Sozaki-r 
1197fc219a5Sozaki-r struct psref_debug_item {
1207fc219a5Sozaki-r 	void			*prdi_caller;
1217fc219a5Sozaki-r 	struct psref		*prdi_psref;
1227fc219a5Sozaki-r };
1237fc219a5Sozaki-r 
1247fc219a5Sozaki-r struct psref_debug {
1257fc219a5Sozaki-r 	int			prd_refs_peek;
1267fc219a5Sozaki-r 	struct psref_debug_item prd_items[PSREF_DEBUG_NITEMS];
1277fc219a5Sozaki-r };
1287fc219a5Sozaki-r 
1297fc219a5Sozaki-r #ifdef PSREF_DEBUG
1307fc219a5Sozaki-r static void psref_debug_acquire(struct psref *);
1317fc219a5Sozaki-r static void psref_debug_release(struct psref *);
1327fc219a5Sozaki-r 
1337fc219a5Sozaki-r static void psref_debug_lwp_free(void *);
1347fc219a5Sozaki-r 
1357fc219a5Sozaki-r static specificdata_key_t psref_debug_lwp_key;
1367fc219a5Sozaki-r #endif
1377fc219a5Sozaki-r 
1387fc219a5Sozaki-r /*
1397fc219a5Sozaki-r  * psref_init()
1407fc219a5Sozaki-r  */
1417fc219a5Sozaki-r void
psref_init(void)1427fc219a5Sozaki-r psref_init(void)
1437fc219a5Sozaki-r {
1447fc219a5Sozaki-r 
1457fc219a5Sozaki-r #ifdef PSREF_DEBUG
1467fc219a5Sozaki-r 	lwp_specific_key_create(&psref_debug_lwp_key, psref_debug_lwp_free);
1477fc219a5Sozaki-r #endif
1487fc219a5Sozaki-r }
1497fc219a5Sozaki-r 
1507fc219a5Sozaki-r /*
151c03dceb1Sriastradh  * psref_class_create(name, ipl)
152c03dceb1Sriastradh  *
153c03dceb1Sriastradh  *	Create a new passive reference class, with the given wchan name
154c03dceb1Sriastradh  *	and ipl.
155c03dceb1Sriastradh  */
156c03dceb1Sriastradh struct psref_class *
psref_class_create(const char * name,int ipl)157c03dceb1Sriastradh psref_class_create(const char *name, int ipl)
158c03dceb1Sriastradh {
159c03dceb1Sriastradh 	struct psref_class *class;
160c03dceb1Sriastradh 
161c03dceb1Sriastradh 	ASSERT_SLEEPABLE();
162c03dceb1Sriastradh 
163c03dceb1Sriastradh 	class = kmem_alloc(sizeof(*class), KM_SLEEP);
164c03dceb1Sriastradh 	class->prc_percpu = percpu_alloc(sizeof(struct psref_cpu));
165c03dceb1Sriastradh 	mutex_init(&class->prc_lock, MUTEX_DEFAULT, ipl);
166c03dceb1Sriastradh 	cv_init(&class->prc_cv, name);
167c03dceb1Sriastradh 	class->prc_iplcookie = makeiplcookie(ipl);
168d10d3470Sozaki-r 	class->prc_xc_flags = XC_HIGHPRI_IPL(ipl);
169c03dceb1Sriastradh 
170c03dceb1Sriastradh 	return class;
171c03dceb1Sriastradh }
172c03dceb1Sriastradh 
173ae43ee51Sriastradh static void __diagused
psref_cpu_drained_p(void * p,void * cookie,struct cpu_info * ci __unused)174c03dceb1Sriastradh psref_cpu_drained_p(void *p, void *cookie, struct cpu_info *ci __unused)
175c03dceb1Sriastradh {
176c03dceb1Sriastradh 	const struct psref_cpu *pcpu = p;
177c03dceb1Sriastradh 	bool *retp = cookie;
178c03dceb1Sriastradh 
179b2d6db80Sknakahara 	if (!SLIST_EMPTY(&pcpu->pcpu_head))
180c03dceb1Sriastradh 		*retp = false;
181c03dceb1Sriastradh }
182c03dceb1Sriastradh 
183*7ff6f3e7Smacallan static bool __diagused
psref_class_drained_p(const struct psref_class * prc)184c03dceb1Sriastradh psref_class_drained_p(const struct psref_class *prc)
185c03dceb1Sriastradh {
186c03dceb1Sriastradh 	bool ret = true;
187c03dceb1Sriastradh 
188c03dceb1Sriastradh 	percpu_foreach(prc->prc_percpu, &psref_cpu_drained_p, &ret);
189c03dceb1Sriastradh 
190c03dceb1Sriastradh 	return ret;
191c03dceb1Sriastradh }
192c03dceb1Sriastradh 
193c03dceb1Sriastradh /*
194c03dceb1Sriastradh  * psref_class_destroy(class)
195c03dceb1Sriastradh  *
196c03dceb1Sriastradh  *	Destroy a passive reference class and free memory associated
197c03dceb1Sriastradh  *	with it.  All targets in this class must have been drained and
198c03dceb1Sriastradh  *	destroyed already.
199c03dceb1Sriastradh  */
200c03dceb1Sriastradh void
psref_class_destroy(struct psref_class * class)201c03dceb1Sriastradh psref_class_destroy(struct psref_class *class)
202c03dceb1Sriastradh {
203c03dceb1Sriastradh 
204c03dceb1Sriastradh 	KASSERT(psref_class_drained_p(class));
205c03dceb1Sriastradh 
206c03dceb1Sriastradh 	cv_destroy(&class->prc_cv);
207c03dceb1Sriastradh 	mutex_destroy(&class->prc_lock);
208c03dceb1Sriastradh 	percpu_free(class->prc_percpu, sizeof(struct psref_cpu));
209c03dceb1Sriastradh 	kmem_free(class, sizeof(*class));
210c03dceb1Sriastradh }
211c03dceb1Sriastradh 
212c03dceb1Sriastradh /*
213c03dceb1Sriastradh  * psref_target_init(target, class)
214c03dceb1Sriastradh  *
215c03dceb1Sriastradh  *	Initialize a passive reference target in the specified class.
216c03dceb1Sriastradh  *	The caller is responsible for issuing a membar_producer after
217c03dceb1Sriastradh  *	psref_target_init and before exposing a pointer to the target
218c03dceb1Sriastradh  *	to other CPUs.
219c03dceb1Sriastradh  */
220c03dceb1Sriastradh void
psref_target_init(struct psref_target * target,struct psref_class * class)221c03dceb1Sriastradh psref_target_init(struct psref_target *target,
222c03dceb1Sriastradh     struct psref_class *class)
223c03dceb1Sriastradh {
224c03dceb1Sriastradh 
225c03dceb1Sriastradh 	target->prt_class = class;
226c03dceb1Sriastradh 	target->prt_draining = false;
227c03dceb1Sriastradh }
228c03dceb1Sriastradh 
2298db94433Sozaki-r #ifdef DEBUG
2302b3456eaSozaki-r static bool
psref_exist(struct psref_cpu * pcpu,struct psref * psref)2312b3456eaSozaki-r psref_exist(struct psref_cpu *pcpu, struct psref *psref)
2322b3456eaSozaki-r {
2332b3456eaSozaki-r 	struct psref *_psref;
2342b3456eaSozaki-r 
2352b3456eaSozaki-r 	SLIST_FOREACH(_psref, &pcpu->pcpu_head, psref_entry) {
2362b3456eaSozaki-r 		if (_psref == psref)
2372b3456eaSozaki-r 			return true;
2382b3456eaSozaki-r 	}
2392b3456eaSozaki-r 	return false;
2402b3456eaSozaki-r }
2412b3456eaSozaki-r 
2428db94433Sozaki-r static void
psref_check_duplication(struct psref_cpu * pcpu,struct psref * psref,const struct psref_target * target)2438db94433Sozaki-r psref_check_duplication(struct psref_cpu *pcpu, struct psref *psref,
2448db94433Sozaki-r     const struct psref_target *target)
2458db94433Sozaki-r {
2468db94433Sozaki-r 	bool found = false;
2478db94433Sozaki-r 
2482b3456eaSozaki-r 	found = psref_exist(pcpu, psref);
2498db94433Sozaki-r 	if (found) {
2502b3456eaSozaki-r 		panic("The psref is already in the list (acquiring twice?): "
2512b3456eaSozaki-r 		    "psref=%p target=%p", psref, target);
2522b3456eaSozaki-r 	}
2532b3456eaSozaki-r }
2542b3456eaSozaki-r 
2552b3456eaSozaki-r static void
psref_check_existence(struct psref_cpu * pcpu,struct psref * psref,const struct psref_target * target)2562b3456eaSozaki-r psref_check_existence(struct psref_cpu *pcpu, struct psref *psref,
2572b3456eaSozaki-r     const struct psref_target *target)
2582b3456eaSozaki-r {
2592b3456eaSozaki-r 	bool found = false;
2602b3456eaSozaki-r 
2612b3456eaSozaki-r 	found = psref_exist(pcpu, psref);
2622b3456eaSozaki-r 	if (!found) {
2632b3456eaSozaki-r 		panic("The psref isn't in the list (releasing unused psref?): "
2648db94433Sozaki-r 		    "psref=%p target=%p", psref, target);
2658db94433Sozaki-r 	}
2668db94433Sozaki-r }
2678db94433Sozaki-r #endif /* DEBUG */
2688db94433Sozaki-r 
269c03dceb1Sriastradh /*
270c03dceb1Sriastradh  * psref_acquire(psref, target, class)
271c03dceb1Sriastradh  *
272c03dceb1Sriastradh  *	Acquire a passive reference to the specified target, which must
273c03dceb1Sriastradh  *	be in the specified class.
274c03dceb1Sriastradh  *
275c03dceb1Sriastradh  *	The caller must guarantee that the target will not be destroyed
276c03dceb1Sriastradh  *	before psref_acquire returns.
277c03dceb1Sriastradh  *
278c03dceb1Sriastradh  *	The caller must additionally guarantee that it will not switch
279c03dceb1Sriastradh  *	CPUs before releasing the passive reference, either by
280c03dceb1Sriastradh  *	disabling kpreemption and avoiding sleeps, or by being in a
281c03dceb1Sriastradh  *	softint or in an LWP bound to a CPU.
282c03dceb1Sriastradh  */
283c03dceb1Sriastradh void
psref_acquire(struct psref * psref,const struct psref_target * target,struct psref_class * class)284c03dceb1Sriastradh psref_acquire(struct psref *psref, const struct psref_target *target,
285c03dceb1Sriastradh     struct psref_class *class)
286c03dceb1Sriastradh {
287c03dceb1Sriastradh 	struct psref_cpu *pcpu;
288c03dceb1Sriastradh 	int s;
289c03dceb1Sriastradh 
290c03dceb1Sriastradh 	KASSERTMSG((kpreempt_disabled() || cpu_softintr_p() ||
291c03dceb1Sriastradh 		ISSET(curlwp->l_pflag, LP_BOUND)),
292c03dceb1Sriastradh 	    "passive references are CPU-local,"
293c03dceb1Sriastradh 	    " but preemption is enabled and the caller is not"
294c03dceb1Sriastradh 	    " in a softint or CPU-bound LWP");
295e721a9f9Sriastradh 	KASSERTMSG(!target->prt_draining, "psref target already destroyed: %p",
296e721a9f9Sriastradh 	    target);
297c03dceb1Sriastradh 	KASSERTMSG((target->prt_class == class),
298c03dceb1Sriastradh 	    "mismatched psref target class: %p (ref) != %p (expected)",
299c03dceb1Sriastradh 	    target->prt_class, class);
300c03dceb1Sriastradh 
301c03dceb1Sriastradh 	/* Block interrupts and acquire the current CPU's reference list.  */
302c03dceb1Sriastradh 	s = splraiseipl(class->prc_iplcookie);
303c03dceb1Sriastradh 	pcpu = percpu_getref(class->prc_percpu);
304c03dceb1Sriastradh 
3058db94433Sozaki-r #ifdef DEBUG
3068db94433Sozaki-r 	/* Sanity-check if the target is already acquired with the same psref.  */
3078db94433Sozaki-r 	psref_check_duplication(pcpu, psref, target);
3088db94433Sozaki-r #endif
3098db94433Sozaki-r 
310c03dceb1Sriastradh 	/* Record our reference.  */
311b2d6db80Sknakahara 	SLIST_INSERT_HEAD(&pcpu->pcpu_head, psref, psref_entry);
312c03dceb1Sriastradh 	psref->psref_target = target;
313c03dceb1Sriastradh 	psref->psref_lwp = curlwp;
314c03dceb1Sriastradh 	psref->psref_cpu = curcpu();
315c03dceb1Sriastradh 
316c03dceb1Sriastradh 	/* Release the CPU list and restore interrupts.  */
317c03dceb1Sriastradh 	percpu_putref(class->prc_percpu);
318c03dceb1Sriastradh 	splx(s);
3193843688cSozaki-r 
3207fc219a5Sozaki-r #if defined(DIAGNOSTIC) || defined(PSREF_DEBUG)
3213843688cSozaki-r 	curlwp->l_psrefs++;
3223843688cSozaki-r #endif
3237fc219a5Sozaki-r #ifdef PSREF_DEBUG
3247fc219a5Sozaki-r 	psref_debug_acquire(psref);
3257fc219a5Sozaki-r #endif
326c03dceb1Sriastradh }
327c03dceb1Sriastradh 
328c03dceb1Sriastradh /*
329c03dceb1Sriastradh  * psref_release(psref, target, class)
330c03dceb1Sriastradh  *
331c03dceb1Sriastradh  *	Release a passive reference to the specified target, which must
332c03dceb1Sriastradh  *	be in the specified class.
333c03dceb1Sriastradh  *
334c03dceb1Sriastradh  *	The caller must not have switched CPUs or LWPs since acquiring
335c03dceb1Sriastradh  *	the passive reference.
336c03dceb1Sriastradh  */
337c03dceb1Sriastradh void
psref_release(struct psref * psref,const struct psref_target * target,struct psref_class * class)338c03dceb1Sriastradh psref_release(struct psref *psref, const struct psref_target *target,
339c03dceb1Sriastradh     struct psref_class *class)
340c03dceb1Sriastradh {
341b2d6db80Sknakahara 	struct psref_cpu *pcpu;
342c03dceb1Sriastradh 	int s;
343c03dceb1Sriastradh 
344c03dceb1Sriastradh 	KASSERTMSG((kpreempt_disabled() || cpu_softintr_p() ||
345c03dceb1Sriastradh 		ISSET(curlwp->l_pflag, LP_BOUND)),
346c03dceb1Sriastradh 	    "passive references are CPU-local,"
347c03dceb1Sriastradh 	    " but preemption is enabled and the caller is not"
348c03dceb1Sriastradh 	    " in a softint or CPU-bound LWP");
349c03dceb1Sriastradh 	KASSERTMSG((target->prt_class == class),
350c03dceb1Sriastradh 	    "mismatched psref target class: %p (ref) != %p (expected)",
351c03dceb1Sriastradh 	    target->prt_class, class);
352c03dceb1Sriastradh 
353c03dceb1Sriastradh 	/* Make sure the psref looks sensible.  */
354c03dceb1Sriastradh 	KASSERTMSG((psref->psref_target == target),
355c03dceb1Sriastradh 	    "passive reference target mismatch: %p (ref) != %p (expected)",
356c03dceb1Sriastradh 	    psref->psref_target, target);
357c03dceb1Sriastradh 	KASSERTMSG((psref->psref_lwp == curlwp),
358c03dceb1Sriastradh 	    "passive reference transferred from lwp %p to lwp %p",
359c03dceb1Sriastradh 	    psref->psref_lwp, curlwp);
360c03dceb1Sriastradh 	KASSERTMSG((psref->psref_cpu == curcpu()),
361c03dceb1Sriastradh 	    "passive reference transferred from CPU %u to CPU %u",
362c03dceb1Sriastradh 	    cpu_index(psref->psref_cpu), cpu_index(curcpu()));
363c03dceb1Sriastradh 
364c03dceb1Sriastradh 	/*
365c03dceb1Sriastradh 	 * Block interrupts and remove the psref from the current CPU's
366c03dceb1Sriastradh 	 * list.  No need to percpu_getref or get the head of the list,
367c03dceb1Sriastradh 	 * and the caller guarantees that we are bound to a CPU anyway
368c03dceb1Sriastradh 	 * (as does blocking interrupts).
369c03dceb1Sriastradh 	 */
370c03dceb1Sriastradh 	s = splraiseipl(class->prc_iplcookie);
371b2d6db80Sknakahara 	pcpu = percpu_getref(class->prc_percpu);
3722b3456eaSozaki-r #ifdef DEBUG
3732b3456eaSozaki-r 	/* Sanity-check if the target is surely acquired before.  */
3742b3456eaSozaki-r 	psref_check_existence(pcpu, psref, target);
3752b3456eaSozaki-r #endif
376b2d6db80Sknakahara 	SLIST_REMOVE(&pcpu->pcpu_head, psref, psref, psref_entry);
377b2d6db80Sknakahara 	percpu_putref(class->prc_percpu);
378c03dceb1Sriastradh 	splx(s);
379c03dceb1Sriastradh 
3807fc219a5Sozaki-r #if defined(DIAGNOSTIC) || defined(PSREF_DEBUG)
3813843688cSozaki-r 	KASSERT(curlwp->l_psrefs > 0);
3823843688cSozaki-r 	curlwp->l_psrefs--;
3833843688cSozaki-r #endif
3847fc219a5Sozaki-r #ifdef PSREF_DEBUG
3857fc219a5Sozaki-r 	psref_debug_release(psref);
3867fc219a5Sozaki-r #endif
3873843688cSozaki-r 
388c03dceb1Sriastradh 	/* If someone is waiting for users to drain, notify 'em.  */
389c03dceb1Sriastradh 	if (__predict_false(target->prt_draining))
390c03dceb1Sriastradh 		cv_broadcast(&class->prc_cv);
391c03dceb1Sriastradh }
392c03dceb1Sriastradh 
393c03dceb1Sriastradh /*
394c03dceb1Sriastradh  * psref_copy(pto, pfrom, class)
395c03dceb1Sriastradh  *
396c03dceb1Sriastradh  *	Copy a passive reference from pfrom, which must be in the
397c03dceb1Sriastradh  *	specified class, to pto.  Both pfrom and pto must later be
398c03dceb1Sriastradh  *	released with psref_release.
399c03dceb1Sriastradh  *
400c03dceb1Sriastradh  *	The caller must not have switched CPUs or LWPs since acquiring
401c03dceb1Sriastradh  *	pfrom, and must not switch CPUs or LWPs before releasing both
402c03dceb1Sriastradh  *	pfrom and pto.
403c03dceb1Sriastradh  */
404c03dceb1Sriastradh void
psref_copy(struct psref * pto,const struct psref * pfrom,struct psref_class * class)405c03dceb1Sriastradh psref_copy(struct psref *pto, const struct psref *pfrom,
406c03dceb1Sriastradh     struct psref_class *class)
407c03dceb1Sriastradh {
408c03dceb1Sriastradh 	struct psref_cpu *pcpu;
409c03dceb1Sriastradh 	int s;
410c03dceb1Sriastradh 
411c03dceb1Sriastradh 	KASSERTMSG((kpreempt_disabled() || cpu_softintr_p() ||
412c03dceb1Sriastradh 		ISSET(curlwp->l_pflag, LP_BOUND)),
413c03dceb1Sriastradh 	    "passive references are CPU-local,"
414c03dceb1Sriastradh 	    " but preemption is enabled and the caller is not"
415c03dceb1Sriastradh 	    " in a softint or CPU-bound LWP");
416c03dceb1Sriastradh 	KASSERTMSG((pto != pfrom),
417c03dceb1Sriastradh 	    "can't copy passive reference to itself: %p",
418c03dceb1Sriastradh 	    pto);
419c03dceb1Sriastradh 
420c03dceb1Sriastradh 	/* Make sure the pfrom reference looks sensible.  */
421c03dceb1Sriastradh 	KASSERTMSG((pfrom->psref_lwp == curlwp),
422c03dceb1Sriastradh 	    "passive reference transferred from lwp %p to lwp %p",
423c03dceb1Sriastradh 	    pfrom->psref_lwp, curlwp);
424c03dceb1Sriastradh 	KASSERTMSG((pfrom->psref_cpu == curcpu()),
425c03dceb1Sriastradh 	    "passive reference transferred from CPU %u to CPU %u",
426c03dceb1Sriastradh 	    cpu_index(pfrom->psref_cpu), cpu_index(curcpu()));
427c03dceb1Sriastradh 	KASSERTMSG((pfrom->psref_target->prt_class == class),
428c03dceb1Sriastradh 	    "mismatched psref target class: %p (ref) != %p (expected)",
429c03dceb1Sriastradh 	    pfrom->psref_target->prt_class, class);
430c03dceb1Sriastradh 
431c03dceb1Sriastradh 	/* Block interrupts and acquire the current CPU's reference list.  */
432c03dceb1Sriastradh 	s = splraiseipl(class->prc_iplcookie);
433c03dceb1Sriastradh 	pcpu = percpu_getref(class->prc_percpu);
434c03dceb1Sriastradh 
435c03dceb1Sriastradh 	/* Record the new reference.  */
436b2d6db80Sknakahara 	SLIST_INSERT_HEAD(&pcpu->pcpu_head, pto, psref_entry);
437c03dceb1Sriastradh 	pto->psref_target = pfrom->psref_target;
438c03dceb1Sriastradh 	pto->psref_lwp = curlwp;
439c03dceb1Sriastradh 	pto->psref_cpu = curcpu();
440c03dceb1Sriastradh 
441c03dceb1Sriastradh 	/* Release the CPU list and restore interrupts.  */
442c03dceb1Sriastradh 	percpu_putref(class->prc_percpu);
443c03dceb1Sriastradh 	splx(s);
4443843688cSozaki-r 
4457fc219a5Sozaki-r #if defined(DIAGNOSTIC) || defined(PSREF_DEBUG)
4463843688cSozaki-r 	curlwp->l_psrefs++;
4473843688cSozaki-r #endif
448c03dceb1Sriastradh }
449c03dceb1Sriastradh 
450c03dceb1Sriastradh /*
451c03dceb1Sriastradh  * struct psreffed
452c03dceb1Sriastradh  *
453c03dceb1Sriastradh  *	Global state for draining a psref target.
454c03dceb1Sriastradh  */
455c03dceb1Sriastradh struct psreffed {
456c03dceb1Sriastradh 	struct psref_class	*class;
457c03dceb1Sriastradh 	struct psref_target	*target;
458c03dceb1Sriastradh 	bool			ret;
459c03dceb1Sriastradh };
460c03dceb1Sriastradh 
461c03dceb1Sriastradh static void
psreffed_p_xc(void * cookie0,void * cookie1 __unused)462c03dceb1Sriastradh psreffed_p_xc(void *cookie0, void *cookie1 __unused)
463c03dceb1Sriastradh {
464c03dceb1Sriastradh 	struct psreffed *P = cookie0;
465c03dceb1Sriastradh 
466c03dceb1Sriastradh 	/*
467c03dceb1Sriastradh 	 * If we hold a psref to the target, then answer true.
468c03dceb1Sriastradh 	 *
469c03dceb1Sriastradh 	 * This is the only dynamic decision that may be made with
470c03dceb1Sriastradh 	 * psref_held.
471c03dceb1Sriastradh 	 *
472c03dceb1Sriastradh 	 * No need to lock anything here: every write transitions from
473c03dceb1Sriastradh 	 * false to true, so there can be no conflicting writes.  No
474c03dceb1Sriastradh 	 * need for a memory barrier here because P->ret is read only
475c03dceb1Sriastradh 	 * after xc_wait, which has already issued any necessary memory
476c03dceb1Sriastradh 	 * barriers.
477c03dceb1Sriastradh 	 */
478421ddf53Sriastradh 	if (_psref_held(P->target, P->class, true))
479c03dceb1Sriastradh 		P->ret = true;
480c03dceb1Sriastradh }
481c03dceb1Sriastradh 
482c03dceb1Sriastradh static bool
psreffed_p(struct psref_target * target,struct psref_class * class)483c03dceb1Sriastradh psreffed_p(struct psref_target *target, struct psref_class *class)
484c03dceb1Sriastradh {
485c03dceb1Sriastradh 	struct psreffed P = {
486c03dceb1Sriastradh 		.class = class,
487c03dceb1Sriastradh 		.target = target,
488c03dceb1Sriastradh 		.ret = false,
489c03dceb1Sriastradh 	};
490c03dceb1Sriastradh 
491412ac21eSmsaitoh 	if (__predict_true(mp_online)) {
492412ac21eSmsaitoh 		/*
493412ac21eSmsaitoh 		 * Ask all CPUs to say whether they hold a psref to the
494412ac21eSmsaitoh 		 * target.
495412ac21eSmsaitoh 		 */
496d10d3470Sozaki-r 		xc_wait(xc_broadcast(class->prc_xc_flags, &psreffed_p_xc, &P,
497d10d3470Sozaki-r 		                     NULL));
498412ac21eSmsaitoh 	} else
499412ac21eSmsaitoh 		psreffed_p_xc(&P, NULL);
500c03dceb1Sriastradh 
501c03dceb1Sriastradh 	return P.ret;
502c03dceb1Sriastradh }
503c03dceb1Sriastradh 
504c03dceb1Sriastradh /*
505c03dceb1Sriastradh  * psref_target_destroy(target, class)
506c03dceb1Sriastradh  *
507c03dceb1Sriastradh  *	Destroy a passive reference target.  Waits for all existing
508c03dceb1Sriastradh  *	references to drain.  Caller must guarantee no new references
509c03dceb1Sriastradh  *	will be acquired once it calls psref_target_destroy, e.g. by
510c03dceb1Sriastradh  *	removing the target from a global list first.  May sleep.
511c03dceb1Sriastradh  */
512c03dceb1Sriastradh void
psref_target_destroy(struct psref_target * target,struct psref_class * class)513c03dceb1Sriastradh psref_target_destroy(struct psref_target *target, struct psref_class *class)
514c03dceb1Sriastradh {
515c03dceb1Sriastradh 
516c03dceb1Sriastradh 	ASSERT_SLEEPABLE();
517c03dceb1Sriastradh 
518e721a9f9Sriastradh 	KASSERTMSG(!target->prt_draining, "psref target already destroyed: %p",
519e721a9f9Sriastradh 	    target);
520c03dceb1Sriastradh 	KASSERTMSG((target->prt_class == class),
521c03dceb1Sriastradh 	    "mismatched psref target class: %p (ref) != %p (expected)",
522c03dceb1Sriastradh 	    target->prt_class, class);
523c03dceb1Sriastradh 
524c03dceb1Sriastradh 	/* Request psref_release to notify us when done.  */
525c03dceb1Sriastradh 	target->prt_draining = true;
526c03dceb1Sriastradh 
527c03dceb1Sriastradh 	/* Wait until there are no more references on any CPU.  */
528c03dceb1Sriastradh 	while (psreffed_p(target, class)) {
529c03dceb1Sriastradh 		/*
530c03dceb1Sriastradh 		 * This enter/wait/exit business looks wrong, but it is
531c03dceb1Sriastradh 		 * both necessary, because psreffed_p performs a
532c03dceb1Sriastradh 		 * low-priority xcall and hence cannot run while a
533c03dceb1Sriastradh 		 * mutex is locked, and OK, because the wait is timed
534c03dceb1Sriastradh 		 * -- explicit wakeups are only an optimization.
535c03dceb1Sriastradh 		 */
536c03dceb1Sriastradh 		mutex_enter(&class->prc_lock);
537c03dceb1Sriastradh 		(void)cv_timedwait(&class->prc_cv, &class->prc_lock, 1);
538c03dceb1Sriastradh 		mutex_exit(&class->prc_lock);
539c03dceb1Sriastradh 	}
540c03dceb1Sriastradh 
541c03dceb1Sriastradh 	/* No more references.  Cause subsequent psref_acquire to kassert.  */
542c03dceb1Sriastradh 	target->prt_class = NULL;
543c03dceb1Sriastradh }
544c03dceb1Sriastradh 
545421ddf53Sriastradh static bool
_psref_held(const struct psref_target * target,struct psref_class * class,bool lwp_mismatch_ok)546421ddf53Sriastradh _psref_held(const struct psref_target *target, struct psref_class *class,
547421ddf53Sriastradh     bool lwp_mismatch_ok)
548c03dceb1Sriastradh {
549c03dceb1Sriastradh 	const struct psref_cpu *pcpu;
550c03dceb1Sriastradh 	const struct psref *psref;
551c03dceb1Sriastradh 	int s;
552c03dceb1Sriastradh 	bool held = false;
553c03dceb1Sriastradh 
554c03dceb1Sriastradh 	KASSERTMSG((kpreempt_disabled() || cpu_softintr_p() ||
555c03dceb1Sriastradh 		ISSET(curlwp->l_pflag, LP_BOUND)),
556c03dceb1Sriastradh 	    "passive references are CPU-local,"
557c03dceb1Sriastradh 	    " but preemption is enabled and the caller is not"
558c03dceb1Sriastradh 	    " in a softint or CPU-bound LWP");
559c03dceb1Sriastradh 	KASSERTMSG((target->prt_class == class),
560c03dceb1Sriastradh 	    "mismatched psref target class: %p (ref) != %p (expected)",
561c03dceb1Sriastradh 	    target->prt_class, class);
562c03dceb1Sriastradh 
563c03dceb1Sriastradh 	/* Block interrupts and acquire the current CPU's reference list.  */
564c03dceb1Sriastradh 	s = splraiseipl(class->prc_iplcookie);
565c03dceb1Sriastradh 	pcpu = percpu_getref(class->prc_percpu);
566c03dceb1Sriastradh 
567c03dceb1Sriastradh 	/* Search through all the references on this CPU.  */
568b2d6db80Sknakahara 	SLIST_FOREACH(psref, &pcpu->pcpu_head, psref_entry) {
5698941dc11Sozaki-r 		/* Sanity-check the reference's CPU.  */
570c03dceb1Sriastradh 		KASSERTMSG((psref->psref_cpu == curcpu()),
571c03dceb1Sriastradh 		    "passive reference transferred from CPU %u to CPU %u",
572c03dceb1Sriastradh 		    cpu_index(psref->psref_cpu), cpu_index(curcpu()));
573c03dceb1Sriastradh 
5748941dc11Sozaki-r 		/* If it doesn't match, skip it and move on.  */
5758941dc11Sozaki-r 		if (psref->psref_target != target)
5768941dc11Sozaki-r 			continue;
5778941dc11Sozaki-r 
5788941dc11Sozaki-r 		/*
5798941dc11Sozaki-r 		 * Sanity-check the reference's LWP if we are asserting
5808941dc11Sozaki-r 		 * via psref_held that this LWP holds it, but not if we
5818941dc11Sozaki-r 		 * are testing in psref_target_destroy whether any LWP
5828941dc11Sozaki-r 		 * still holds it.
5838941dc11Sozaki-r 		 */
5848941dc11Sozaki-r 		KASSERTMSG((lwp_mismatch_ok || psref->psref_lwp == curlwp),
5858941dc11Sozaki-r 		    "passive reference transferred from lwp %p to lwp %p",
5868941dc11Sozaki-r 		    psref->psref_lwp, curlwp);
5878941dc11Sozaki-r 
5888941dc11Sozaki-r 		/* Stop here and report that we found it.  */
589c03dceb1Sriastradh 		held = true;
590c03dceb1Sriastradh 		break;
591c03dceb1Sriastradh 	}
592c03dceb1Sriastradh 
593c03dceb1Sriastradh 	/* Release the CPU list and restore interrupts.  */
594c03dceb1Sriastradh 	percpu_putref(class->prc_percpu);
595c03dceb1Sriastradh 	splx(s);
596c03dceb1Sriastradh 
597c03dceb1Sriastradh 	return held;
598c03dceb1Sriastradh }
599421ddf53Sriastradh 
600421ddf53Sriastradh /*
601421ddf53Sriastradh  * psref_held(target, class)
602421ddf53Sriastradh  *
603421ddf53Sriastradh  *	True if the current CPU holds a passive reference to target,
604421ddf53Sriastradh  *	false otherwise.  May be used only inside assertions.
605421ddf53Sriastradh  */
606421ddf53Sriastradh bool
psref_held(const struct psref_target * target,struct psref_class * class)607421ddf53Sriastradh psref_held(const struct psref_target *target, struct psref_class *class)
608421ddf53Sriastradh {
609421ddf53Sriastradh 
610421ddf53Sriastradh 	return _psref_held(target, class, false);
611421ddf53Sriastradh }
6127fc219a5Sozaki-r 
6137fc219a5Sozaki-r #ifdef PSREF_DEBUG
6147fc219a5Sozaki-r void
psref_debug_init_lwp(struct lwp * l)6157fc219a5Sozaki-r psref_debug_init_lwp(struct lwp *l)
6167fc219a5Sozaki-r {
6177fc219a5Sozaki-r 	struct psref_debug *prd;
6187fc219a5Sozaki-r 
6197fc219a5Sozaki-r 	prd = kmem_zalloc(sizeof(*prd), KM_SLEEP);
6207fc219a5Sozaki-r 	lwp_setspecific_by_lwp(l, psref_debug_lwp_key, prd);
6217fc219a5Sozaki-r }
6227fc219a5Sozaki-r 
6237fc219a5Sozaki-r static void
psref_debug_lwp_free(void * arg)6247fc219a5Sozaki-r psref_debug_lwp_free(void *arg)
6257fc219a5Sozaki-r {
6267fc219a5Sozaki-r 	struct psref_debug *prd = arg;
6277fc219a5Sozaki-r 
6287fc219a5Sozaki-r 	kmem_free(prd, sizeof(*prd));
6297fc219a5Sozaki-r }
6307fc219a5Sozaki-r 
6317fc219a5Sozaki-r static void
psref_debug_acquire(struct psref * psref)6327fc219a5Sozaki-r psref_debug_acquire(struct psref *psref)
6337fc219a5Sozaki-r {
6347fc219a5Sozaki-r 	struct psref_debug *prd;
6357fc219a5Sozaki-r 	struct lwp *l = curlwp;
6367fc219a5Sozaki-r 	int s, i;
6377fc219a5Sozaki-r 
6387fc219a5Sozaki-r 	prd = lwp_getspecific(psref_debug_lwp_key);
6397fc219a5Sozaki-r 	if (__predict_false(prd == NULL)) {
6407fc219a5Sozaki-r 		psref->psref_debug = NULL;
6417fc219a5Sozaki-r 		return;
6427fc219a5Sozaki-r 	}
6437fc219a5Sozaki-r 
6447fc219a5Sozaki-r 	s = splserial();
6457fc219a5Sozaki-r 	if (l->l_psrefs > prd->prd_refs_peek) {
6467fc219a5Sozaki-r 		prd->prd_refs_peek = l->l_psrefs;
6477fc219a5Sozaki-r 		if (__predict_false(prd->prd_refs_peek > PSREF_DEBUG_NITEMS))
6487fc219a5Sozaki-r 			panic("exceeded PSREF_DEBUG_NITEMS");
6497fc219a5Sozaki-r 	}
6507fc219a5Sozaki-r 	for (i = 0; i < prd->prd_refs_peek; i++) {
6517fc219a5Sozaki-r 		struct psref_debug_item *prdi = &prd->prd_items[i];
6527fc219a5Sozaki-r 		if (prdi->prdi_psref != NULL)
6537fc219a5Sozaki-r 			continue;
6547fc219a5Sozaki-r 		prdi->prdi_caller = psref->psref_debug;
6557fc219a5Sozaki-r 		prdi->prdi_psref = psref;
6567fc219a5Sozaki-r 		psref->psref_debug = prdi;
6577fc219a5Sozaki-r 		break;
6587fc219a5Sozaki-r 	}
6597fc219a5Sozaki-r 	if (__predict_false(i == prd->prd_refs_peek))
6607fc219a5Sozaki-r 		panic("out of range: %d", i);
6617fc219a5Sozaki-r 	splx(s);
6627fc219a5Sozaki-r }
6637fc219a5Sozaki-r 
6647fc219a5Sozaki-r static void
psref_debug_release(struct psref * psref)6657fc219a5Sozaki-r psref_debug_release(struct psref *psref)
6667fc219a5Sozaki-r {
6677fc219a5Sozaki-r 	int s;
6687fc219a5Sozaki-r 
6697fc219a5Sozaki-r 	s = splserial();
6707fc219a5Sozaki-r 	if (__predict_true(psref->psref_debug != NULL)) {
6717fc219a5Sozaki-r 		struct psref_debug_item *prdi = psref->psref_debug;
6727fc219a5Sozaki-r 		prdi->prdi_psref = NULL;
6737fc219a5Sozaki-r 	}
6747fc219a5Sozaki-r 	splx(s);
6757fc219a5Sozaki-r }
6767fc219a5Sozaki-r 
6777fc219a5Sozaki-r void
psref_debug_barrier(void)6787fc219a5Sozaki-r psref_debug_barrier(void)
6797fc219a5Sozaki-r {
6807fc219a5Sozaki-r 	struct psref_debug *prd;
6817fc219a5Sozaki-r 	struct lwp *l = curlwp;
6827fc219a5Sozaki-r 	int s, i;
6837fc219a5Sozaki-r 
6847fc219a5Sozaki-r 	prd = lwp_getspecific(psref_debug_lwp_key);
6857fc219a5Sozaki-r 	if (__predict_false(prd == NULL))
6867fc219a5Sozaki-r 		return;
6877fc219a5Sozaki-r 
6887fc219a5Sozaki-r 	s = splserial();
6897fc219a5Sozaki-r 	for (i = 0; i < prd->prd_refs_peek; i++) {
6907fc219a5Sozaki-r 		struct psref_debug_item *prdi = &prd->prd_items[i];
6917fc219a5Sozaki-r 		if (__predict_true(prdi->prdi_psref == NULL))
6927fc219a5Sozaki-r 			continue;
6937fc219a5Sozaki-r 		panic("psref leaked: lwp(%p) acquired at %p", l, prdi->prdi_caller);
6947fc219a5Sozaki-r 	}
6957fc219a5Sozaki-r 	prd->prd_refs_peek = 0; /* Reset the counter */
6967fc219a5Sozaki-r 	splx(s);
6977fc219a5Sozaki-r }
6987fc219a5Sozaki-r #endif /* PSREF_DEBUG */
699