subr_pcpu.c (e7153b2583ec32ced588706fe1996d909b23bc3c) | subr_pcpu.c (50c202c592c4019c36a40d861f498c3a243946f4) |
---|---|
1/*- 2 * Copyright (c) 2001 Wind River Systems, Inc. 3 * All rights reserved. 4 * Written by: John Baldwin <jhb@FreeBSD.org> 5 * | 1/*- 2 * Copyright (c) 2001 Wind River Systems, Inc. 3 * All rights reserved. 4 * Written by: John Baldwin <jhb@FreeBSD.org> 5 * |
6 * Copyright (c) 2009 Jeffrey Roberson <jeff@freebsd.org> 7 * All rights reserved. 8 * |
|
6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. --- 30 unchanged lines hidden (view full) --- 44 45#include <sys/cdefs.h> 46__FBSDID("$FreeBSD$"); 47 48#include "opt_ddb.h" 49 50#include <sys/param.h> 51#include <sys/systm.h> | 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. --- 30 unchanged lines hidden (view full) --- 47 48#include <sys/cdefs.h> 49__FBSDID("$FreeBSD$"); 50 51#include "opt_ddb.h" 52 53#include <sys/param.h> 54#include <sys/systm.h> |
55#include <sys/sysctl.h> |
|
52#include <sys/linker_set.h> 53#include <sys/lock.h> | 56#include <sys/linker_set.h> 57#include <sys/lock.h> |
58#include <sys/malloc.h> |
|
54#include <sys/pcpu.h> 55#include <sys/proc.h> 56#include <sys/smp.h> | 59#include <sys/pcpu.h> 60#include <sys/proc.h> 61#include <sys/smp.h> |
62#include <sys/sx.h> |
|
57#include <ddb/ddb.h> 58 | 63#include <ddb/ddb.h> 64 |
65MALLOC_DEFINE(M_PCPU, "Per-cpu", "Per-cpu resource accouting."); 66 67struct dpcpu_free { 68 uintptr_t df_start; 69 int df_len; 70 TAILQ_ENTRY(dpcpu_free) df_link; 71}; 72 73static DPCPU_DEFINE(char, modspace[DPCPU_MODMIN]); 74static TAILQ_HEAD(, dpcpu_free) dpcpu_head = TAILQ_HEAD_INITIALIZER(dpcpu_head); 75static struct sx dpcpu_lock; 76uintptr_t dpcpu_off[MAXCPU]; |
|
59struct pcpu *cpuid_to_pcpu[MAXCPU]; 60struct cpuhead cpuhead = SLIST_HEAD_INITIALIZER(cpuhead); 61 62/* 63 * Initialize the MI portions of a struct pcpu. 64 */ 65void 66pcpu_init(struct pcpu *pcpu, int cpuid, size_t size) --- 7 unchanged lines hidden (view full) --- 74 cpuid_to_pcpu[cpuid] = pcpu; 75 SLIST_INSERT_HEAD(&cpuhead, pcpu, pc_allcpu); 76 cpu_pcpu_init(pcpu, cpuid, size); 77 pcpu->pc_rm_queue.rmq_next = &pcpu->pc_rm_queue; 78 pcpu->pc_rm_queue.rmq_prev = &pcpu->pc_rm_queue; 79#ifdef KTR 80 snprintf(pcpu->pc_name, sizeof(pcpu->pc_name), "CPU %d", cpuid); 81#endif | 77struct pcpu *cpuid_to_pcpu[MAXCPU]; 78struct cpuhead cpuhead = SLIST_HEAD_INITIALIZER(cpuhead); 79 80/* 81 * Initialize the MI portions of a struct pcpu. 82 */ 83void 84pcpu_init(struct pcpu *pcpu, int cpuid, size_t size) --- 7 unchanged lines hidden (view full) --- 92 cpuid_to_pcpu[cpuid] = pcpu; 93 SLIST_INSERT_HEAD(&cpuhead, pcpu, pc_allcpu); 94 cpu_pcpu_init(pcpu, cpuid, size); 95 pcpu->pc_rm_queue.rmq_next = &pcpu->pc_rm_queue; 96 pcpu->pc_rm_queue.rmq_prev = &pcpu->pc_rm_queue; 97#ifdef KTR 98 snprintf(pcpu->pc_name, sizeof(pcpu->pc_name), "CPU %d", cpuid); 99#endif |
100} |
|
82 | 101 |
102void 103dpcpu_init(void *dpcpu, int cpuid) 104{ 105 struct pcpu *pcpu; 106 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; |
|
83} 84 | 119} 120 |
121static void 122dpcpu_startup(void *dummy __unused) 123{ 124 struct dpcpu_free *df; 125 126 df = malloc(sizeof(*df), M_PCPU, M_WAITOK | M_ZERO); 127 df->df_start = (uintptr_t)&DPCPU_NAME(modspace); 128 df->df_len = DPCPU_MODSIZE; 129 TAILQ_INSERT_HEAD(&dpcpu_head, df, df_link); 130 sx_init(&dpcpu_lock, "dpcpu alloc lock"); 131} 132SYSINIT(dpcpu, SI_SUB_KLD, SI_ORDER_FIRST, dpcpu_startup, 0); 133 |
|
85/* | 134/* |
135 * First-fit extent based allocator for allocating space in the per-cpu 136 * region reserved for modules. This is only intended for use by the 137 * kernel linkers to place module linker sets. 138 */ 139void * 140dpcpu_alloc(int size) 141{ 142 struct dpcpu_free *df; 143 void *s; 144 145 s = NULL; 146 size = roundup2(size, sizeof(void *)); 147 sx_xlock(&dpcpu_lock); 148 TAILQ_FOREACH(df, &dpcpu_head, df_link) { 149 if (df->df_len < size) 150 continue; 151 if (df->df_len == size) { 152 s = (void *)df->df_start; 153 TAILQ_REMOVE(&dpcpu_head, df, df_link); 154 free(df, M_PCPU); 155 break; 156 } 157 s = (void *)df->df_start; 158 df->df_len -= size; 159 df->df_start = df->df_start + size; 160 break; 161 } 162 sx_xunlock(&dpcpu_lock); 163 164 return (s); 165} 166 167/* 168 * Free dynamic per-cpu space at module unload time. 169 */ 170void 171dpcpu_free(void *s, int size) 172{ 173 struct dpcpu_free *df; 174 struct dpcpu_free *dn; 175 uintptr_t start; 176 uintptr_t end; 177 178 size = roundup2(size, sizeof(void *)); 179 start = (uintptr_t)s; 180 end = start + size; 181 /* 182 * Free a region of space and merge it with as many neighbors as 183 * possible. Keeping the list sorted simplifies this operation. 184 */ 185 sx_xlock(&dpcpu_lock); 186 TAILQ_FOREACH(df, &dpcpu_head, df_link) { 187 if (df->df_start > end) 188 break; 189 /* 190 * If we expand at the end of an entry we may have to 191 * merge it with the one following it as well. 192 */ 193 if (df->df_start + df->df_len == start) { 194 df->df_len += size; 195 dn = TAILQ_NEXT(df, df_link); 196 if (df->df_start + df->df_len == dn->df_start) { 197 df->df_len += dn->df_len; 198 TAILQ_REMOVE(&dpcpu_head, dn, df_link); 199 free(dn, M_PCPU); 200 } 201 sx_xunlock(&dpcpu_lock); 202 return; 203 } 204 if (df->df_start == end) { 205 df->df_start = start; 206 df->df_len += size; 207 sx_xunlock(&dpcpu_lock); 208 return; 209 } 210 } 211 dn = malloc(sizeof(*df), M_PCPU, M_WAITOK | M_ZERO); 212 dn->df_start = start; 213 dn->df_len = size; 214 if (df) 215 TAILQ_INSERT_BEFORE(df, dn, df_link); 216 else 217 TAILQ_INSERT_TAIL(&dpcpu_head, dn, df_link); 218 sx_xunlock(&dpcpu_lock); 219} 220 221/* 222 * Initialize the per-cpu storage from an updated linker-set region. 223 */ 224void 225dpcpu_copy(void *s, int size) 226{ 227#ifdef SMP 228 uintptr_t dpcpu; 229 int i; 230 231 for (i = 0; i < mp_ncpus; ++i) { 232 dpcpu = dpcpu_off[i]; 233 if (dpcpu == 0) 234 continue; 235 memcpy((void *)(dpcpu + (uintptr_t)s), s, size); 236 } 237#else 238 memcpy((void *)(dpcpu_off[0] + (uintptr_t)s), s, size); 239#endif 240} 241 242/* |
|
86 * Destroy a struct pcpu. 87 */ 88void 89pcpu_destroy(struct pcpu *pcpu) 90{ 91 92 SLIST_REMOVE(&cpuhead, pcpu, pcpu, pc_allcpu); 93 cpuid_to_pcpu[pcpu->pc_cpuid] = NULL; | 243 * Destroy a struct pcpu. 244 */ 245void 246pcpu_destroy(struct pcpu *pcpu) 247{ 248 249 SLIST_REMOVE(&cpuhead, pcpu, pcpu, pc_allcpu); 250 cpuid_to_pcpu[pcpu->pc_cpuid] = NULL; |
251 dpcpu_off[pcpu->pc_cpuid] = 0; |
|
94} 95 96/* 97 * Locate a struct pcpu by cpu id. 98 */ 99struct pcpu * 100pcpu_find(u_int cpuid) 101{ 102 103 return (cpuid_to_pcpu[cpuid]); 104} 105 | 252} 253 254/* 255 * Locate a struct pcpu by cpu id. 256 */ 257struct pcpu * 258pcpu_find(u_int cpuid) 259{ 260 261 return (cpuid_to_pcpu[cpuid]); 262} 263 |
264int 265sysctl_dpcpu_quad(SYSCTL_HANDLER_ARGS) 266{ 267 int64_t count; 268#ifdef SMP 269 uintptr_t dpcpu; 270 int i; 271 272 count = 0; 273 for (i = 0; i < mp_ncpus; ++i) { 274 dpcpu = dpcpu_off[i]; 275 if (dpcpu == 0) 276 continue; 277 count += *(int64_t *)(dpcpu + (uintptr_t)arg1); 278 } 279#else 280 count = *(int64_t *)(dpcpu_off[0] + (uintptr_t)arg1); 281#endif 282 return (SYSCTL_OUT(req, &count, sizeof(count))); 283} 284 285int 286sysctl_dpcpu_int(SYSCTL_HANDLER_ARGS) 287{ 288 int count; 289#ifdef SMP 290 uintptr_t dpcpu; 291 int i; 292 293 count = 0; 294 for (i = 0; i < mp_ncpus; ++i) { 295 dpcpu = dpcpu_off[i]; 296 if (dpcpu == 0) 297 continue; 298 count += *(int *)(dpcpu + (uintptr_t)arg1); 299 } 300#else 301 count = *(int *)(dpcpu_off[0] + (uintptr_t)arg1); 302#endif 303 return (SYSCTL_OUT(req, &count, sizeof(count))); 304} 305 |
|
106#ifdef DDB 107 108static void 109show_pcpu(struct pcpu *pc) 110{ 111 struct thread *td; 112 113 db_printf("cpuid = %d\n", pc->pc_cpuid); | 306#ifdef DDB 307 308static void 309show_pcpu(struct pcpu *pc) 310{ 311 struct thread *td; 312 313 db_printf("cpuid = %d\n", pc->pc_cpuid); |
314 db_printf("dynamic pcpu = %p\n", (void *)pc->pc_dynamic); |
|
114 db_printf("curthread = "); 115 td = pc->pc_curthread; 116 if (td != NULL) 117 db_printf("%p: pid %d \"%s\"\n", td, td->td_proc->p_pid, 118 td->td_name); 119 else 120 db_printf("none\n"); 121 db_printf("curpcb = %p\n", pc->pc_curpcb); --- 59 unchanged lines hidden --- | 315 db_printf("curthread = "); 316 td = pc->pc_curthread; 317 if (td != NULL) 318 db_printf("%p: pid %d \"%s\"\n", td, td->td_proc->p_pid, 319 td->td_name); 320 else 321 db_printf("none\n"); 322 db_printf("curpcb = %p\n", pc->pc_curpcb); --- 59 unchanged lines hidden --- |