xref: /netbsd-src/sys/kern/subr_percpu.c (revision 1f0e72f10bf44222bc86f9fecfd6a3b6704f3c5a)
1*1f0e72f1Sriastradh /*	$NetBSD: subr_percpu.c,v 1.25 2020/05/11 21:37:31 riastradh Exp $	*/
2ea8e7591Syamt 
3ea8e7591Syamt /*-
4ea8e7591Syamt  * Copyright (c)2007,2008 YAMAMOTO Takashi,
5ea8e7591Syamt  * All rights reserved.
6ea8e7591Syamt  *
7ea8e7591Syamt  * Redistribution and use in source and binary forms, with or without
8ea8e7591Syamt  * modification, are permitted provided that the following conditions
9ea8e7591Syamt  * are met:
10ea8e7591Syamt  * 1. Redistributions of source code must retain the above copyright
11ea8e7591Syamt  *    notice, this list of conditions and the following disclaimer.
12ea8e7591Syamt  * 2. Redistributions in binary form must reproduce the above copyright
13ea8e7591Syamt  *    notice, this list of conditions and the following disclaimer in the
14ea8e7591Syamt  *    documentation and/or other materials provided with the distribution.
15ea8e7591Syamt  *
16ea8e7591Syamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17ea8e7591Syamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18ea8e7591Syamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19ea8e7591Syamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20ea8e7591Syamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21ea8e7591Syamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22ea8e7591Syamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23ea8e7591Syamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24ea8e7591Syamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25ea8e7591Syamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26ea8e7591Syamt  * SUCH DAMAGE.
27ea8e7591Syamt  */
28ea8e7591Syamt 
29ea8e7591Syamt /*
30ea8e7591Syamt  * per-cpu storage.
31ea8e7591Syamt  */
32ea8e7591Syamt 
33ea8e7591Syamt #include <sys/cdefs.h>
34*1f0e72f1Sriastradh __KERNEL_RCSID(0, "$NetBSD: subr_percpu.c,v 1.25 2020/05/11 21:37:31 riastradh Exp $");
35ea8e7591Syamt 
36ea8e7591Syamt #include <sys/param.h>
37ea8e7591Syamt #include <sys/cpu.h>
38ea8e7591Syamt #include <sys/kernel.h>
394bf5723bSriastradh #include <sys/kmem.h>
40ea8e7591Syamt #include <sys/mutex.h>
41ea8e7591Syamt #include <sys/percpu.h>
42ea8e7591Syamt #include <sys/rwlock.h>
43ea8e7591Syamt #include <sys/vmem.h>
44ea8e7591Syamt #include <sys/xcall.h>
45ea8e7591Syamt 
46ea8e7591Syamt #define	PERCPU_QUANTUM_SIZE	(ALIGNBYTES + 1)
47ea8e7591Syamt #define	PERCPU_QCACHE_MAX	0
48ea8e7591Syamt #define	PERCPU_IMPORT_SIZE	2048
49ea8e7591Syamt 
50d92a26fbSriastradh struct percpu {
51d92a26fbSriastradh 	unsigned		pc_offset;
52d92a26fbSriastradh 	size_t			pc_size;
53*1f0e72f1Sriastradh 	percpu_callback_t	pc_ctor;
54d92a26fbSriastradh 	percpu_callback_t	pc_dtor;
55d92a26fbSriastradh 	void			*pc_cookie;
56*1f0e72f1Sriastradh 	LIST_ENTRY(percpu)	pc_list;
57d92a26fbSriastradh };
586d3b5bc3Syamt 
59f132c365Srmind static krwlock_t	percpu_swap_lock	__cacheline_aligned;
601cae704dSriastradh static vmem_t *		percpu_offset_arena	__read_mostly;
611cae704dSriastradh static struct {
621cae704dSriastradh 	kmutex_t	lock;
631cae704dSriastradh 	unsigned int	nextoff;
64*1f0e72f1Sriastradh 	LIST_HEAD(, percpu) ctor_list;
65*1f0e72f1Sriastradh 	struct lwp	*busy;
66*1f0e72f1Sriastradh 	kcondvar_t	cv;
671cae704dSriastradh } percpu_allocation __cacheline_aligned;
687c89190bSad 
69ea8e7591Syamt static percpu_cpu_t *
cpu_percpu(struct cpu_info * ci)70ea8e7591Syamt cpu_percpu(struct cpu_info *ci)
71ea8e7591Syamt {
72ea8e7591Syamt 
73ea8e7591Syamt 	return &ci->ci_data.cpu_percpu;
74ea8e7591Syamt }
75ea8e7591Syamt 
76ea8e7591Syamt static unsigned int
percpu_offset(percpu_t * pc)77ea8e7591Syamt percpu_offset(percpu_t *pc)
78ea8e7591Syamt {
79d92a26fbSriastradh 	const unsigned int off = pc->pc_offset;
80ea8e7591Syamt 
811cae704dSriastradh 	KASSERT(off < percpu_allocation.nextoff);
826d3b5bc3Syamt 	return off;
83ea8e7591Syamt }
84ea8e7591Syamt 
85ea8e7591Syamt /*
86ea8e7591Syamt  * percpu_cpu_swap: crosscall handler for percpu_cpu_enlarge
87ea8e7591Syamt  */
888a4b4e84Skamil __noubsan
89ea8e7591Syamt static void
percpu_cpu_swap(void * p1,void * p2)90ea8e7591Syamt percpu_cpu_swap(void *p1, void *p2)
91ea8e7591Syamt {
92ea8e7591Syamt 	struct cpu_info * const ci = p1;
93ea8e7591Syamt 	percpu_cpu_t * const newpcc = p2;
94ea8e7591Syamt 	percpu_cpu_t * const pcc = cpu_percpu(ci);
95ea8e7591Syamt 
96ffaba5deSmartin 	KASSERT(ci == curcpu() || !mp_online);
97fdd122f0Smatt 
98ea8e7591Syamt 	/*
99ea8e7591Syamt 	 * swap *pcc and *newpcc unless anyone has beaten us.
100ea8e7591Syamt 	 */
101ea8e7591Syamt 	rw_enter(&percpu_swap_lock, RW_WRITER);
102ea8e7591Syamt 	if (newpcc->pcc_size > pcc->pcc_size) {
103ea8e7591Syamt 		percpu_cpu_t tmp;
104ea8e7591Syamt 		int s;
105ea8e7591Syamt 
106ea8e7591Syamt 		tmp = *pcc;
107ea8e7591Syamt 
108ea8e7591Syamt 		/*
109ea8e7591Syamt 		 * block interrupts so that we don't lose their modifications.
110ea8e7591Syamt 		 */
111ea8e7591Syamt 
112ea8e7591Syamt 		s = splhigh();
113ea8e7591Syamt 
114ea8e7591Syamt 		/*
115ea8e7591Syamt 		 * copy data to new storage.
116ea8e7591Syamt 		 */
117ea8e7591Syamt 
118ea8e7591Syamt 		memcpy(newpcc->pcc_data, pcc->pcc_data, pcc->pcc_size);
119ea8e7591Syamt 
120ea8e7591Syamt 		/*
121ea8e7591Syamt 		 * this assignment needs to be atomic for percpu_getptr_remote.
122ea8e7591Syamt 		 */
123ea8e7591Syamt 
124ea8e7591Syamt 		pcc->pcc_data = newpcc->pcc_data;
125ea8e7591Syamt 
126ea8e7591Syamt 		splx(s);
127ea8e7591Syamt 
128ea8e7591Syamt 		pcc->pcc_size = newpcc->pcc_size;
129ea8e7591Syamt 		*newpcc = tmp;
130ea8e7591Syamt 	}
131ea8e7591Syamt 	rw_exit(&percpu_swap_lock);
132ea8e7591Syamt }
133ea8e7591Syamt 
134ea8e7591Syamt /*
135ea8e7591Syamt  * percpu_cpu_enlarge: ensure that percpu_cpu_t of each cpus have enough space
136ea8e7591Syamt  */
137ea8e7591Syamt 
138ea8e7591Syamt static void
percpu_cpu_enlarge(size_t size)139ea8e7591Syamt percpu_cpu_enlarge(size_t size)
140ea8e7591Syamt {
141ea8e7591Syamt 	CPU_INFO_ITERATOR cii;
142ea8e7591Syamt 	struct cpu_info *ci;
143ea8e7591Syamt 
144ea8e7591Syamt 	for (CPU_INFO_FOREACH(cii, ci)) {
145ea8e7591Syamt 		percpu_cpu_t pcc;
146ea8e7591Syamt 
147ea8e7591Syamt 		pcc.pcc_data = kmem_alloc(size, KM_SLEEP); /* XXX cacheline */
148ea8e7591Syamt 		pcc.pcc_size = size;
149ea8e7591Syamt 		if (!mp_online) {
150ea8e7591Syamt 			percpu_cpu_swap(ci, &pcc);
151ea8e7591Syamt 		} else {
152ea8e7591Syamt 			uint64_t where;
153ea8e7591Syamt 
154ea8e7591Syamt 			where = xc_unicast(0, percpu_cpu_swap, ci, &pcc, ci);
155ea8e7591Syamt 			xc_wait(where);
156ea8e7591Syamt 		}
157de3acc9dSriastradh 		KASSERT(pcc.pcc_size <= size);
158ea8e7591Syamt 		if (pcc.pcc_data != NULL) {
159ea8e7591Syamt 			kmem_free(pcc.pcc_data, pcc.pcc_size);
160ea8e7591Syamt 		}
161ea8e7591Syamt 	}
162ea8e7591Syamt }
163ea8e7591Syamt 
164ea8e7591Syamt /*
165ea8e7591Syamt  * percpu_backend_alloc: vmem import callback for percpu_offset_arena
166ea8e7591Syamt  */
167ea8e7591Syamt 
16878b0e183Sdyoung static int
percpu_backend_alloc(vmem_t * dummy,vmem_size_t size,vmem_size_t * resultsize,vm_flag_t vmflags,vmem_addr_t * addrp)169e62ee4d4Spara percpu_backend_alloc(vmem_t *dummy, vmem_size_t size, vmem_size_t *resultsize,
17078b0e183Sdyoung     vm_flag_t vmflags, vmem_addr_t *addrp)
171ea8e7591Syamt {
172ea8e7591Syamt 	unsigned int offset;
173ea8e7591Syamt 	unsigned int nextoff;
174ea8e7591Syamt 
175a67bae0bSyamt 	ASSERT_SLEEPABLE();
176ea8e7591Syamt 	KASSERT(dummy == NULL);
177ea8e7591Syamt 
178ea8e7591Syamt 	if ((vmflags & VM_NOSLEEP) != 0)
17978b0e183Sdyoung 		return ENOMEM;
180ea8e7591Syamt 
181ea8e7591Syamt 	size = roundup(size, PERCPU_IMPORT_SIZE);
1821cae704dSriastradh 	mutex_enter(&percpu_allocation.lock);
1831cae704dSriastradh 	offset = percpu_allocation.nextoff;
1841cae704dSriastradh 	percpu_allocation.nextoff = nextoff = percpu_allocation.nextoff + size;
1851cae704dSriastradh 	mutex_exit(&percpu_allocation.lock);
186ea8e7591Syamt 
187ea8e7591Syamt 	percpu_cpu_enlarge(nextoff);
188ea8e7591Syamt 
189ea8e7591Syamt 	*resultsize = size;
19078b0e183Sdyoung 	*addrp = (vmem_addr_t)offset;
19178b0e183Sdyoung 	return 0;
192ea8e7591Syamt }
193ea8e7591Syamt 
1942c871b80Syamt static void
percpu_zero_cb(void * vp,void * vp2,struct cpu_info * ci)1952c871b80Syamt percpu_zero_cb(void *vp, void *vp2, struct cpu_info *ci)
1962c871b80Syamt {
1972c871b80Syamt 	size_t sz = (uintptr_t)vp2;
1982c871b80Syamt 
1992c871b80Syamt 	memset(vp, 0, sz);
2002c871b80Syamt }
2012c871b80Syamt 
2022c871b80Syamt /*
2032c871b80Syamt  * percpu_zero: initialize percpu storage with zero.
2042c871b80Syamt  */
2052c871b80Syamt 
2062c871b80Syamt static void
percpu_zero(percpu_t * pc,size_t sz)2072c871b80Syamt percpu_zero(percpu_t *pc, size_t sz)
2082c871b80Syamt {
2092c871b80Syamt 
2102c871b80Syamt 	percpu_foreach(pc, percpu_zero_cb, (void *)(uintptr_t)sz);
2112c871b80Syamt }
2122c871b80Syamt 
213ea8e7591Syamt /*
214ea8e7591Syamt  * percpu_init: subsystem initialization
215ea8e7591Syamt  */
216ea8e7591Syamt 
217ea8e7591Syamt void
percpu_init(void)218ea8e7591Syamt percpu_init(void)
219ea8e7591Syamt {
220ea8e7591Syamt 
221a67bae0bSyamt 	ASSERT_SLEEPABLE();
222ea8e7591Syamt 	rw_init(&percpu_swap_lock);
2231cae704dSriastradh 	mutex_init(&percpu_allocation.lock, MUTEX_DEFAULT, IPL_NONE);
2241cae704dSriastradh 	percpu_allocation.nextoff = PERCPU_QUANTUM_SIZE;
225*1f0e72f1Sriastradh 	LIST_INIT(&percpu_allocation.ctor_list);
226*1f0e72f1Sriastradh 	percpu_allocation.busy = NULL;
227*1f0e72f1Sriastradh 	cv_init(&percpu_allocation.cv, "percpu");
228ea8e7591Syamt 
229e62ee4d4Spara 	percpu_offset_arena = vmem_xcreate("percpu", 0, 0, PERCPU_QUANTUM_SIZE,
230ea8e7591Syamt 	    percpu_backend_alloc, NULL, NULL, PERCPU_QCACHE_MAX, VM_SLEEP,
231ea8e7591Syamt 	    IPL_NONE);
232ea8e7591Syamt }
233ea8e7591Syamt 
234ea8e7591Syamt /*
235ea8e7591Syamt  * percpu_init_cpu: cpu initialization
236ea8e7591Syamt  *
237ea8e7591Syamt  * => should be called before the cpu appears on the list for CPU_INFO_FOREACH.
238*1f0e72f1Sriastradh  * => may be called for static CPUs afterward (typically just primary CPU)
239ea8e7591Syamt  */
240ea8e7591Syamt 
241ea8e7591Syamt void
percpu_init_cpu(struct cpu_info * ci)242ea8e7591Syamt percpu_init_cpu(struct cpu_info *ci)
243ea8e7591Syamt {
244ea8e7591Syamt 	percpu_cpu_t * const pcc = cpu_percpu(ci);
245*1f0e72f1Sriastradh 	struct percpu *pc;
2461cae704dSriastradh 	size_t size = percpu_allocation.nextoff; /* XXX racy */
247ea8e7591Syamt 
248a67bae0bSyamt 	ASSERT_SLEEPABLE();
249*1f0e72f1Sriastradh 
250*1f0e72f1Sriastradh 	/*
251*1f0e72f1Sriastradh 	 * For the primary CPU, prior percpu_create may have already
252*1f0e72f1Sriastradh 	 * triggered allocation, so there's nothing more for us to do
253*1f0e72f1Sriastradh 	 * here.
254*1f0e72f1Sriastradh 	 */
255*1f0e72f1Sriastradh 	if (pcc->pcc_size)
256*1f0e72f1Sriastradh 		return;
257*1f0e72f1Sriastradh 	KASSERT(pcc->pcc_data == NULL);
258*1f0e72f1Sriastradh 
259*1f0e72f1Sriastradh 	/*
260*1f0e72f1Sriastradh 	 * Otherwise, allocate storage and, while the constructor list
261*1f0e72f1Sriastradh 	 * is locked, run constructors for all percpus on this CPU.
262*1f0e72f1Sriastradh 	 */
263ea8e7591Syamt 	pcc->pcc_size = size;
264ea8e7591Syamt 	if (size) {
265ea8e7591Syamt 		pcc->pcc_data = kmem_zalloc(pcc->pcc_size, KM_SLEEP);
266*1f0e72f1Sriastradh 		mutex_enter(&percpu_allocation.lock);
267*1f0e72f1Sriastradh 		while (percpu_allocation.busy)
268*1f0e72f1Sriastradh 			cv_wait(&percpu_allocation.cv,
269*1f0e72f1Sriastradh 			    &percpu_allocation.lock);
270*1f0e72f1Sriastradh 		percpu_allocation.busy = curlwp;
271*1f0e72f1Sriastradh 		LIST_FOREACH(pc, &percpu_allocation.ctor_list, pc_list) {
272*1f0e72f1Sriastradh 			KASSERT(pc->pc_ctor);
273*1f0e72f1Sriastradh 			mutex_exit(&percpu_allocation.lock);
274*1f0e72f1Sriastradh 			(*pc->pc_ctor)((char *)pcc->pcc_data + pc->pc_offset,
275*1f0e72f1Sriastradh 			    pc->pc_cookie, ci);
276*1f0e72f1Sriastradh 			mutex_enter(&percpu_allocation.lock);
277*1f0e72f1Sriastradh 		}
278*1f0e72f1Sriastradh 		KASSERT(percpu_allocation.busy == curlwp);
279*1f0e72f1Sriastradh 		percpu_allocation.busy = NULL;
280*1f0e72f1Sriastradh 		cv_broadcast(&percpu_allocation.cv);
281*1f0e72f1Sriastradh 		mutex_exit(&percpu_allocation.lock);
282ea8e7591Syamt 	}
283ea8e7591Syamt }
284ea8e7591Syamt 
285ea8e7591Syamt /*
286ea8e7591Syamt  * percpu_alloc: allocate percpu storage
287ea8e7591Syamt  *
288ea8e7591Syamt  * => called in thread context.
289ea8e7591Syamt  * => considered as an expensive and rare operation.
2902c871b80Syamt  * => allocated storage is initialized with zeros.
291ea8e7591Syamt  */
292ea8e7591Syamt 
293ea8e7591Syamt percpu_t *
percpu_alloc(size_t size)294ea8e7591Syamt percpu_alloc(size_t size)
295ea8e7591Syamt {
296d92a26fbSriastradh 
297d92a26fbSriastradh 	return percpu_create(size, NULL, NULL, NULL);
298d92a26fbSriastradh }
299d92a26fbSriastradh 
300d92a26fbSriastradh /*
301d92a26fbSriastradh  * percpu_create: allocate percpu storage and associate ctor/dtor with it
302d92a26fbSriastradh  *
303d92a26fbSriastradh  * => called in thread context.
304d92a26fbSriastradh  * => considered as an expensive and rare operation.
305d92a26fbSriastradh  * => allocated storage is initialized by ctor, or zeros if ctor is null
306d92a26fbSriastradh  * => percpu_free will call dtor first, if dtor is nonnull
307d92a26fbSriastradh  * => ctor or dtor may sleep, even on allocation
308d92a26fbSriastradh  */
309d92a26fbSriastradh 
310d92a26fbSriastradh percpu_t *
percpu_create(size_t size,percpu_callback_t ctor,percpu_callback_t dtor,void * cookie)311d92a26fbSriastradh percpu_create(size_t size, percpu_callback_t ctor, percpu_callback_t dtor,
312d92a26fbSriastradh     void *cookie)
313d92a26fbSriastradh {
31478b0e183Sdyoung 	vmem_addr_t offset;
315ea8e7591Syamt 	percpu_t *pc;
316ea8e7591Syamt 
317a67bae0bSyamt 	ASSERT_SLEEPABLE();
3181f0e1671Schs 	(void)vmem_alloc(percpu_offset_arena, size, VM_SLEEP | VM_BESTFIT,
3191f0e1671Schs 	    &offset);
320d92a26fbSriastradh 
321d92a26fbSriastradh 	pc = kmem_alloc(sizeof(*pc), KM_SLEEP);
322d92a26fbSriastradh 	pc->pc_offset = offset;
323d92a26fbSriastradh 	pc->pc_size = size;
324*1f0e72f1Sriastradh 	pc->pc_ctor = ctor;
325d92a26fbSriastradh 	pc->pc_dtor = dtor;
326d92a26fbSriastradh 	pc->pc_cookie = cookie;
327d92a26fbSriastradh 
328d92a26fbSriastradh 	if (ctor) {
329d92a26fbSriastradh 		CPU_INFO_ITERATOR cii;
330d92a26fbSriastradh 		struct cpu_info *ci;
331d92a26fbSriastradh 		void *buf;
332d92a26fbSriastradh 
333*1f0e72f1Sriastradh 		/*
334*1f0e72f1Sriastradh 		 * Wait until nobody is using the list of percpus with
335*1f0e72f1Sriastradh 		 * constructors.
336*1f0e72f1Sriastradh 		 */
337*1f0e72f1Sriastradh 		mutex_enter(&percpu_allocation.lock);
338*1f0e72f1Sriastradh 		while (percpu_allocation.busy)
339*1f0e72f1Sriastradh 			cv_wait(&percpu_allocation.cv,
340*1f0e72f1Sriastradh 			    &percpu_allocation.lock);
341*1f0e72f1Sriastradh 		percpu_allocation.busy = curlwp;
342*1f0e72f1Sriastradh 		mutex_exit(&percpu_allocation.lock);
343*1f0e72f1Sriastradh 
344*1f0e72f1Sriastradh 		/*
345*1f0e72f1Sriastradh 		 * Run the constructor for all CPUs.  We use a
346*1f0e72f1Sriastradh 		 * temporary buffer wo that we need not hold the
347*1f0e72f1Sriastradh 		 * percpu_swap_lock while running the constructor.
348*1f0e72f1Sriastradh 		 */
349d92a26fbSriastradh 		buf = kmem_alloc(size, KM_SLEEP);
350d92a26fbSriastradh 		for (CPU_INFO_FOREACH(cii, ci)) {
351d92a26fbSriastradh 			memset(buf, 0, size);
352d92a26fbSriastradh 			(*ctor)(buf, cookie, ci);
353d92a26fbSriastradh 			percpu_traverse_enter();
354d92a26fbSriastradh 			memcpy(percpu_getptr_remote(pc, ci), buf, size);
355d92a26fbSriastradh 			percpu_traverse_exit();
356d92a26fbSriastradh 		}
357d92a26fbSriastradh 		explicit_memset(buf, 0, size);
358d92a26fbSriastradh 		kmem_free(buf, size);
359*1f0e72f1Sriastradh 
360*1f0e72f1Sriastradh 		/*
361*1f0e72f1Sriastradh 		 * Insert the percpu into the list of percpus with
362*1f0e72f1Sriastradh 		 * constructors.  We are now done using the list, so it
363*1f0e72f1Sriastradh 		 * is safe for concurrent percpu_create or concurrent
364*1f0e72f1Sriastradh 		 * percpu_init_cpu to run.
365*1f0e72f1Sriastradh 		 */
366*1f0e72f1Sriastradh 		mutex_enter(&percpu_allocation.lock);
367*1f0e72f1Sriastradh 		KASSERT(percpu_allocation.busy == curlwp);
368*1f0e72f1Sriastradh 		percpu_allocation.busy = NULL;
369*1f0e72f1Sriastradh 		cv_broadcast(&percpu_allocation.cv);
370*1f0e72f1Sriastradh 		LIST_INSERT_HEAD(&percpu_allocation.ctor_list, pc, pc_list);
371*1f0e72f1Sriastradh 		mutex_exit(&percpu_allocation.lock);
372d92a26fbSriastradh 	} else {
373ea8e7591Syamt 		percpu_zero(pc, size);
374d92a26fbSriastradh 	}
375d92a26fbSriastradh 
376ea8e7591Syamt 	return pc;
377ea8e7591Syamt }
378ea8e7591Syamt 
379ea8e7591Syamt /*
380582ad655Syamt  * percpu_free: free percpu storage
381ea8e7591Syamt  *
382ea8e7591Syamt  * => called in thread context.
383ea8e7591Syamt  * => considered as an expensive and rare operation.
384ea8e7591Syamt  */
385ea8e7591Syamt 
386ea8e7591Syamt void
percpu_free(percpu_t * pc,size_t size)387ea8e7591Syamt percpu_free(percpu_t *pc, size_t size)
388ea8e7591Syamt {
389ea8e7591Syamt 
390a67bae0bSyamt 	ASSERT_SLEEPABLE();
391d92a26fbSriastradh 	KASSERT(size == pc->pc_size);
392d92a26fbSriastradh 
393*1f0e72f1Sriastradh 	/*
394*1f0e72f1Sriastradh 	 * If there's a constructor, take the percpu off the list of
395*1f0e72f1Sriastradh 	 * percpus with constructors, but first wait until nobody is
396*1f0e72f1Sriastradh 	 * using the list.
397*1f0e72f1Sriastradh 	 */
398*1f0e72f1Sriastradh 	if (pc->pc_ctor) {
399*1f0e72f1Sriastradh 		mutex_enter(&percpu_allocation.lock);
400*1f0e72f1Sriastradh 		while (percpu_allocation.busy)
401*1f0e72f1Sriastradh 			cv_wait(&percpu_allocation.cv,
402*1f0e72f1Sriastradh 			    &percpu_allocation.lock);
403*1f0e72f1Sriastradh 		LIST_REMOVE(pc, pc_list);
404*1f0e72f1Sriastradh 		mutex_exit(&percpu_allocation.lock);
405*1f0e72f1Sriastradh 	}
406*1f0e72f1Sriastradh 
407*1f0e72f1Sriastradh 	/* If there's a destructor, run it now for all CPUs.  */
408d92a26fbSriastradh 	if (pc->pc_dtor) {
409d92a26fbSriastradh 		CPU_INFO_ITERATOR cii;
410d92a26fbSriastradh 		struct cpu_info *ci;
411d92a26fbSriastradh 		void *buf;
412d92a26fbSriastradh 
413d92a26fbSriastradh 		buf = kmem_alloc(size, KM_SLEEP);
414d92a26fbSriastradh 		for (CPU_INFO_FOREACH(cii, ci)) {
415d92a26fbSriastradh 			percpu_traverse_enter();
416d92a26fbSriastradh 			memcpy(buf, percpu_getptr_remote(pc, ci), size);
417d92a26fbSriastradh 			explicit_memset(percpu_getptr_remote(pc, ci), 0, size);
418d92a26fbSriastradh 			percpu_traverse_exit();
419d92a26fbSriastradh 			(*pc->pc_dtor)(buf, pc->pc_cookie, ci);
420d92a26fbSriastradh 		}
421d92a26fbSriastradh 		explicit_memset(buf, 0, size);
422d92a26fbSriastradh 		kmem_free(buf, size);
423d92a26fbSriastradh 	}
424d92a26fbSriastradh 
425ea8e7591Syamt 	vmem_free(percpu_offset_arena, (vmem_addr_t)percpu_offset(pc), size);
426d92a26fbSriastradh 	kmem_free(pc, sizeof(*pc));
427ea8e7591Syamt }
428ea8e7591Syamt 
429ea8e7591Syamt /*
4300cfa6e74Sthorpej  * percpu_getref:
431ea8e7591Syamt  *
432ea8e7591Syamt  * => safe to be used in either thread or interrupt context
4330cfa6e74Sthorpej  * => disables preemption; must be bracketed with a percpu_putref()
434ea8e7591Syamt  */
435ea8e7591Syamt 
436ea8e7591Syamt void *
percpu_getref(percpu_t * pc)4370cfa6e74Sthorpej percpu_getref(percpu_t *pc)
438ea8e7591Syamt {
439ea8e7591Syamt 
440fb969037Suebayasi 	kpreempt_disable();
441ea8e7591Syamt 	return percpu_getptr_remote(pc, curcpu());
442ea8e7591Syamt }
443ea8e7591Syamt 
444ea8e7591Syamt /*
4450cfa6e74Sthorpej  * percpu_putref:
4460cfa6e74Sthorpej  *
4470cfa6e74Sthorpej  * => drops the preemption-disabled count after caller is done with per-cpu
4480cfa6e74Sthorpej  *    data
4490cfa6e74Sthorpej  */
4500cfa6e74Sthorpej 
4510cfa6e74Sthorpej void
percpu_putref(percpu_t * pc)4520cfa6e74Sthorpej percpu_putref(percpu_t *pc)
4530cfa6e74Sthorpej {
4540cfa6e74Sthorpej 
455fb969037Suebayasi 	kpreempt_enable();
4560cfa6e74Sthorpej }
4570cfa6e74Sthorpej 
4580cfa6e74Sthorpej /*
459ea8e7591Syamt  * percpu_traverse_enter, percpu_traverse_exit, percpu_getptr_remote:
460ea8e7591Syamt  * helpers to access remote cpu's percpu data.
461ea8e7591Syamt  *
462ea8e7591Syamt  * => called in thread context.
4632c871b80Syamt  * => percpu_traverse_enter can block low-priority xcalls.
464ea8e7591Syamt  * => typical usage would be:
465ea8e7591Syamt  *
466ea8e7591Syamt  *	sum = 0;
467ea8e7591Syamt  *	percpu_traverse_enter();
468ea8e7591Syamt  *	for (CPU_INFO_FOREACH(cii, ci)) {
469ea8e7591Syamt  *		unsigned int *p = percpu_getptr_remote(pc, ci);
470ea8e7591Syamt  *		sum += *p;
471ea8e7591Syamt  *	}
472ea8e7591Syamt  *	percpu_traverse_exit();
473ea8e7591Syamt  */
474ea8e7591Syamt 
475ea8e7591Syamt void
percpu_traverse_enter(void)476ea8e7591Syamt percpu_traverse_enter(void)
477ea8e7591Syamt {
478ea8e7591Syamt 
479a67bae0bSyamt 	ASSERT_SLEEPABLE();
480ea8e7591Syamt 	rw_enter(&percpu_swap_lock, RW_READER);
481ea8e7591Syamt }
482ea8e7591Syamt 
483ea8e7591Syamt void
percpu_traverse_exit(void)484ea8e7591Syamt percpu_traverse_exit(void)
485ea8e7591Syamt {
486ea8e7591Syamt 
487ea8e7591Syamt 	rw_exit(&percpu_swap_lock);
488ea8e7591Syamt }
489ea8e7591Syamt 
490ea8e7591Syamt void *
percpu_getptr_remote(percpu_t * pc,struct cpu_info * ci)491ea8e7591Syamt percpu_getptr_remote(percpu_t *pc, struct cpu_info *ci)
492ea8e7591Syamt {
493ea8e7591Syamt 
494ea8e7591Syamt 	return &((char *)cpu_percpu(ci)->pcc_data)[percpu_offset(pc)];
495ea8e7591Syamt }
496ea8e7591Syamt 
497ea8e7591Syamt /*
498ea8e7591Syamt  * percpu_foreach: call the specified callback function for each cpus.
499ea8e7591Syamt  *
500f7c4d5efSthorpej  * => must be called from thread context.
501f7c4d5efSthorpej  * => callback executes on **current** CPU (or, really, arbitrary CPU,
502f7c4d5efSthorpej  *    in case of preemption)
503ea8e7591Syamt  * => caller should not rely on the cpu iteration order.
5042c871b80Syamt  * => the callback function should be minimum because it is executed with
5052c871b80Syamt  *    holding a global lock, which can block low-priority xcalls.
5062c871b80Syamt  *    eg. it's illegal for a callback function to sleep for memory allocation.
507ea8e7591Syamt  */
508ea8e7591Syamt void
percpu_foreach(percpu_t * pc,percpu_callback_t cb,void * arg)509ea8e7591Syamt percpu_foreach(percpu_t *pc, percpu_callback_t cb, void *arg)
510ea8e7591Syamt {
511ea8e7591Syamt 	CPU_INFO_ITERATOR cii;
512ea8e7591Syamt 	struct cpu_info *ci;
513ea8e7591Syamt 
514ea8e7591Syamt 	percpu_traverse_enter();
515ea8e7591Syamt 	for (CPU_INFO_FOREACH(cii, ci)) {
5162c871b80Syamt 		(*cb)(percpu_getptr_remote(pc, ci), arg, ci);
517ea8e7591Syamt 	}
518ea8e7591Syamt 	percpu_traverse_exit();
519ea8e7591Syamt }
520f7c4d5efSthorpej 
521f7c4d5efSthorpej struct percpu_xcall_ctx {
522f7c4d5efSthorpej 	percpu_callback_t  ctx_cb;
523f7c4d5efSthorpej 	void		  *ctx_arg;
524f7c4d5efSthorpej };
525f7c4d5efSthorpej 
526f7c4d5efSthorpej static void
percpu_xcfunc(void * const v1,void * const v2)527f7c4d5efSthorpej percpu_xcfunc(void * const v1, void * const v2)
528f7c4d5efSthorpej {
529f7c4d5efSthorpej 	percpu_t * const pc = v1;
530f7c4d5efSthorpej 	struct percpu_xcall_ctx * const ctx = v2;
531f7c4d5efSthorpej 
532f7c4d5efSthorpej 	(*ctx->ctx_cb)(percpu_getref(pc), ctx->ctx_arg, curcpu());
533f7c4d5efSthorpej 	percpu_putref(pc);
534f7c4d5efSthorpej }
535f7c4d5efSthorpej 
536f7c4d5efSthorpej /*
537f7c4d5efSthorpej  * percpu_foreach_xcall: call the specified callback function for each
538f7c4d5efSthorpej  * cpu.  This version uses an xcall to run the callback on each cpu.
539f7c4d5efSthorpej  *
540f7c4d5efSthorpej  * => must be called from thread context.
541f7c4d5efSthorpej  * => callback executes on **remote** CPU in soft-interrupt context
542f7c4d5efSthorpej  *    (at the specified soft interrupt priority).
543f7c4d5efSthorpej  * => caller should not rely on the cpu iteration order.
544f7c4d5efSthorpej  * => the callback function should be minimum because it may be
545f7c4d5efSthorpej  *    executed in soft-interrupt context.  eg. it's illegal for
546f7c4d5efSthorpej  *    a callback function to sleep for memory allocation.
547f7c4d5efSthorpej  */
548f7c4d5efSthorpej void
percpu_foreach_xcall(percpu_t * pc,u_int xcflags,percpu_callback_t cb,void * arg)549f7c4d5efSthorpej percpu_foreach_xcall(percpu_t *pc, u_int xcflags, percpu_callback_t cb,
550f7c4d5efSthorpej 		     void *arg)
551f7c4d5efSthorpej {
552f7c4d5efSthorpej 	struct percpu_xcall_ctx ctx = {
553f7c4d5efSthorpej 		.ctx_cb = cb,
554f7c4d5efSthorpej 		.ctx_arg = arg,
555f7c4d5efSthorpej 	};
556f7c4d5efSthorpej 	CPU_INFO_ITERATOR cii;
557f7c4d5efSthorpej 	struct cpu_info *ci;
558f7c4d5efSthorpej 
559f7c4d5efSthorpej 	for (CPU_INFO_FOREACH(cii, ci)) {
560f7c4d5efSthorpej 		xc_wait(xc_unicast(xcflags, percpu_xcfunc, pc, &ctx, ci));
561f7c4d5efSthorpej 	}
562f7c4d5efSthorpej }
563