]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/subr_pcpu.c
Import device-tree files from Linux 6.2
[FreeBSD/FreeBSD.git] / sys / kern / subr_pcpu.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2001 Wind River Systems, Inc.
5  * All rights reserved.
6  * Written by: John Baldwin <jhb@FreeBSD.org>
7  *
8  * Copyright (c) 2009 Jeffrey Roberson <jeff@freebsd.org>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the author nor the names of any co-contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 /*
37  * This module provides MI support for per-cpu data.
38  *
39  * Each architecture determines the mapping of logical CPU IDs to physical
40  * CPUs.  The requirements of this mapping are as follows:
41  *  - Logical CPU IDs must reside in the range 0 ... MAXCPU - 1.
42  *  - The mapping is not required to be dense.  That is, there may be
43  *    gaps in the mappings.
44  *  - The platform sets the value of MAXCPU in <machine/param.h>.
45  *  - It is suggested, but not required, that in the non-SMP case, the
46  *    platform define MAXCPU to be 1 and define the logical ID of the
47  *    sole CPU as 0.
48  */
49
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
52
53 #include "opt_ddb.h"
54
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/sysctl.h>
58 #include <sys/lock.h>
59 #include <sys/malloc.h>
60 #include <sys/pcpu.h>
61 #include <sys/proc.h>
62 #include <sys/smp.h>
63 #include <sys/sx.h>
64 #include <vm/uma.h>
65 #include <ddb/ddb.h>
66
67 static MALLOC_DEFINE(M_PCPU, "Per-cpu", "Per-cpu resource accouting.");
68
69 struct dpcpu_free {
70         uintptr_t       df_start;
71         int             df_len;
72         TAILQ_ENTRY(dpcpu_free) df_link;
73 };
74
75 DPCPU_DEFINE_STATIC(char, modspace[DPCPU_MODMIN] __aligned(__alignof(void *)));
76 static TAILQ_HEAD(, dpcpu_free) dpcpu_head = TAILQ_HEAD_INITIALIZER(dpcpu_head);
77 static struct sx dpcpu_lock;
78 uintptr_t dpcpu_off[MAXCPU];
79 struct pcpu *cpuid_to_pcpu[MAXCPU];
80 struct cpuhead cpuhead = STAILQ_HEAD_INITIALIZER(cpuhead);
81
82 /*
83  * Initialize the MI portions of a struct pcpu.
84  */
85 void
86 pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
87 {
88
89         bzero(pcpu, size);
90         KASSERT(cpuid >= 0 && cpuid < MAXCPU,
91             ("pcpu_init: invalid cpuid %d", cpuid));
92         pcpu->pc_cpuid = cpuid;
93         cpuid_to_pcpu[cpuid] = pcpu;
94         STAILQ_INSERT_TAIL(&cpuhead, pcpu, pc_allcpu);
95         cpu_pcpu_init(pcpu, cpuid, size);
96         pcpu->pc_rm_queue.rmq_next = &pcpu->pc_rm_queue;
97         pcpu->pc_rm_queue.rmq_prev = &pcpu->pc_rm_queue;
98         pcpu->pc_zpcpu_offset = zpcpu_offset_cpu(cpuid);
99 }
100
101 void
102 dpcpu_init(void *dpcpu, int cpuid)
103 {
104         struct pcpu *pcpu;
105
106         TSENTER();
107         pcpu = pcpu_find(cpuid);
108         pcpu->pc_dynamic = (uintptr_t)dpcpu - DPCPU_START;
109
110         /*
111          * Initialize defaults from our linker section.
112          */
113         memcpy(dpcpu, (void *)DPCPU_START, DPCPU_BYTES);
114
115         /*
116          * Place it in the global pcpu offset array.
117          */
118         dpcpu_off[cpuid] = pcpu->pc_dynamic;
119         TSEXIT();
120 }
121
122 static void
123 dpcpu_startup(void *dummy __unused)
124 {
125         struct dpcpu_free *df;
126
127         df = malloc(sizeof(*df), M_PCPU, M_WAITOK | M_ZERO);
128         df->df_start = (uintptr_t)&DPCPU_NAME(modspace);
129         df->df_len = DPCPU_MODMIN;
130         TAILQ_INSERT_HEAD(&dpcpu_head, df, df_link);
131         sx_init(&dpcpu_lock, "dpcpu alloc lock");
132 }
133 SYSINIT(dpcpu, SI_SUB_KLD, SI_ORDER_FIRST, dpcpu_startup, NULL);
134
135 /*
136  * UMA_ZONE_PCPU zones for general kernel use.
137  */
138 uma_zone_t pcpu_zone_4;
139 uma_zone_t pcpu_zone_8;
140 uma_zone_t pcpu_zone_16;
141 uma_zone_t pcpu_zone_32;
142 uma_zone_t pcpu_zone_64;
143
144 static void
145 pcpu_zones_startup(void)
146 {
147
148         pcpu_zone_4 = uma_zcreate("pcpu-4", 4,
149             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_PCPU);
150         pcpu_zone_8 = uma_zcreate("pcpu-8", 8,
151             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_PCPU);
152         pcpu_zone_16 = uma_zcreate("pcpu-16", 16,
153             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_PCPU);
154         pcpu_zone_32 = uma_zcreate("pcpu-32", 32,
155             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_PCPU);
156         pcpu_zone_64 = uma_zcreate("pcpu-64", 64,
157             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_PCPU);
158 }
159 SYSINIT(pcpu_zones, SI_SUB_COUNTER, SI_ORDER_FIRST, pcpu_zones_startup, NULL);
160
161 /*
162  * First-fit extent based allocator for allocating space in the per-cpu
163  * region reserved for modules.  This is only intended for use by the
164  * kernel linkers to place module linker sets.
165  */
166 void *
167 dpcpu_alloc(int size)
168 {
169         struct dpcpu_free *df;
170         void *s;
171
172         s = NULL;
173         size = roundup2(size, sizeof(void *));
174         sx_xlock(&dpcpu_lock);
175         TAILQ_FOREACH(df, &dpcpu_head, df_link) {
176                 if (df->df_len < size)
177                         continue;
178                 if (df->df_len == size) {
179                         s = (void *)df->df_start;
180                         TAILQ_REMOVE(&dpcpu_head, df, df_link);
181                         free(df, M_PCPU);
182                         break;
183                 }
184                 s = (void *)df->df_start;
185                 df->df_len -= size;
186                 df->df_start = df->df_start + size;
187                 break;
188         }
189         sx_xunlock(&dpcpu_lock);
190
191         return (s);
192 }
193
194 /*
195  * Free dynamic per-cpu space at module unload time. 
196  */
197 void
198 dpcpu_free(void *s, int size)
199 {
200         struct dpcpu_free *df;
201         struct dpcpu_free *dn;
202         uintptr_t start;
203         uintptr_t end;
204
205         size = roundup2(size, sizeof(void *));
206         start = (uintptr_t)s;
207         end = start + size;
208         /*
209          * Free a region of space and merge it with as many neighbors as
210          * possible.  Keeping the list sorted simplifies this operation.
211          */
212         sx_xlock(&dpcpu_lock);
213         TAILQ_FOREACH(df, &dpcpu_head, df_link) {
214                 if (df->df_start > end)
215                         break;
216                 /*
217                  * If we expand at the end of an entry we may have to
218                  * merge it with the one following it as well.
219                  */
220                 if (df->df_start + df->df_len == start) {
221                         df->df_len += size;
222                         dn = TAILQ_NEXT(df, df_link);
223                         if (df->df_start + df->df_len == dn->df_start) {
224                                 df->df_len += dn->df_len;
225                                 TAILQ_REMOVE(&dpcpu_head, dn, df_link);
226                                 free(dn, M_PCPU);
227                         }
228                         sx_xunlock(&dpcpu_lock);
229                         return;
230                 }
231                 if (df->df_start == end) {
232                         df->df_start = start;
233                         df->df_len += size;
234                         sx_xunlock(&dpcpu_lock);
235                         return;
236                 }
237         }
238         dn = malloc(sizeof(*df), M_PCPU, M_WAITOK | M_ZERO);
239         dn->df_start = start;
240         dn->df_len = size;
241         if (df)
242                 TAILQ_INSERT_BEFORE(df, dn, df_link);
243         else
244                 TAILQ_INSERT_TAIL(&dpcpu_head, dn, df_link);
245         sx_xunlock(&dpcpu_lock);
246 }
247
248 /*
249  * Initialize the per-cpu storage from an updated linker-set region.
250  */
251 void
252 dpcpu_copy(void *s, int size)
253 {
254 #ifdef SMP
255         uintptr_t dpcpu;
256         int i;
257
258         CPU_FOREACH(i) {
259                 dpcpu = dpcpu_off[i];
260                 if (dpcpu == 0)
261                         continue;
262                 memcpy((void *)(dpcpu + (uintptr_t)s), s, size);
263         }
264 #else
265         memcpy((void *)(dpcpu_off[0] + (uintptr_t)s), s, size);
266 #endif
267 }
268
269 /*
270  * Destroy a struct pcpu.
271  */
272 void
273 pcpu_destroy(struct pcpu *pcpu)
274 {
275
276         STAILQ_REMOVE(&cpuhead, pcpu, pcpu, pc_allcpu);
277         cpuid_to_pcpu[pcpu->pc_cpuid] = NULL;
278         dpcpu_off[pcpu->pc_cpuid] = 0;
279 }
280
281 /*
282  * Locate a struct pcpu by cpu id.
283  */
284 struct pcpu *
285 pcpu_find(u_int cpuid)
286 {
287
288         return (cpuid_to_pcpu[cpuid]);
289 }
290
291 int
292 sysctl_dpcpu_quad(SYSCTL_HANDLER_ARGS)
293 {
294         uintptr_t dpcpu;
295         int64_t count;
296         int i;
297
298         count = 0;
299         CPU_FOREACH(i) {
300                 dpcpu = dpcpu_off[i];
301                 if (dpcpu == 0)
302                         continue;
303                 count += *(int64_t *)(dpcpu + (uintptr_t)arg1);
304         }
305         return (SYSCTL_OUT(req, &count, sizeof(count)));
306 }
307
308 int
309 sysctl_dpcpu_long(SYSCTL_HANDLER_ARGS)
310 {
311         uintptr_t dpcpu;
312         long count;
313         int i;
314
315         count = 0;
316         CPU_FOREACH(i) {
317                 dpcpu = dpcpu_off[i];
318                 if (dpcpu == 0)
319                         continue;
320                 count += *(long *)(dpcpu + (uintptr_t)arg1);
321         }
322         return (SYSCTL_OUT(req, &count, sizeof(count)));
323 }
324
325 int
326 sysctl_dpcpu_int(SYSCTL_HANDLER_ARGS)
327 {
328         uintptr_t dpcpu;
329         int count;
330         int i;
331
332         count = 0;
333         CPU_FOREACH(i) {
334                 dpcpu = dpcpu_off[i];
335                 if (dpcpu == 0)
336                         continue;
337                 count += *(int *)(dpcpu + (uintptr_t)arg1);
338         }
339         return (SYSCTL_OUT(req, &count, sizeof(count)));
340 }
341
342 #ifdef DDB
343 DB_SHOW_COMMAND_FLAGS(dpcpu_off, db_show_dpcpu_off, DB_CMD_MEMSAFE)
344 {
345         int id;
346
347         CPU_FOREACH(id) {
348                 db_printf("dpcpu_off[%2d] = 0x%jx (+ DPCPU_START = %p)\n",
349                     id, (uintmax_t)dpcpu_off[id],
350                     (void *)(uintptr_t)(dpcpu_off[id] + DPCPU_START));
351         }
352 }
353
354 static void
355 show_pcpu(struct pcpu *pc)
356 {
357         struct thread *td;
358
359         db_printf("cpuid        = %d\n", pc->pc_cpuid);
360         db_printf("dynamic pcpu = %p\n", (void *)pc->pc_dynamic);
361         db_printf("curthread    = ");
362         td = pc->pc_curthread;
363         if (td != NULL)
364                 db_printf("%p: pid %d tid %d critnest %d \"%s\"\n", td,
365                     td->td_proc->p_pid, td->td_tid, td->td_critnest,
366                     td->td_name);
367         else
368                 db_printf("none\n");
369         db_printf("curpcb       = %p\n", pc->pc_curpcb);
370         db_printf("fpcurthread  = ");
371         td = pc->pc_fpcurthread;
372         if (td != NULL)
373                 db_printf("%p: pid %d \"%s\"\n", td, td->td_proc->p_pid,
374                     td->td_name);
375         else
376                 db_printf("none\n");
377         db_printf("idlethread   = ");
378         td = pc->pc_idlethread;
379         if (td != NULL)
380                 db_printf("%p: tid %d \"%s\"\n", td, td->td_tid, td->td_name);
381         else
382                 db_printf("none\n");
383         db_show_mdpcpu(pc);
384
385 #ifdef VIMAGE
386         db_printf("curvnet      = %p\n", pc->pc_curthread->td_vnet);
387 #endif
388
389 #ifdef WITNESS
390         db_printf("spin locks held:\n");
391         witness_list_locks(&pc->pc_spinlocks, db_printf);
392 #endif
393 }
394
395 DB_SHOW_COMMAND_FLAGS(pcpu, db_show_pcpu, DB_CMD_MEMSAFE)
396 {
397         struct pcpu *pc;
398         int id;
399
400         if (have_addr)
401                 id = ((addr >> 4) % 16) * 10 + (addr % 16);
402         else
403                 id = PCPU_GET(cpuid);
404         pc = pcpu_find(id);
405         if (pc == NULL) {
406                 db_printf("CPU %d not found\n", id);
407                 return;
408         }
409         show_pcpu(pc);
410 }
411
412 DB_SHOW_ALL_COMMAND(pcpu, db_show_cpu_all)
413 {
414         struct pcpu *pc;
415         int id;
416
417         db_printf("Current CPU: %d\n\n", PCPU_GET(cpuid));
418         CPU_FOREACH(id) {
419                 pc = pcpu_find(id);
420                 if (pc != NULL) {
421                         show_pcpu(pc);
422                         db_printf("\n");
423                 }
424         }
425 }
426 DB_SHOW_ALIAS_FLAGS(allpcpu, db_show_cpu_all, DB_CMD_MEMSAFE);
427 #endif