2005-01-06 23:35:40 +00:00
|
|
|
/*-
|
2001-05-10 17:45:49 +00:00
|
|
|
* Copyright (c) 2001 Wind River Systems, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
* Written by: John Baldwin <jhb@FreeBSD.org>
|
|
|
|
*
|
Implement a facility for dynamic per-cpu variables.
- Modules and kernel code alike may use DPCPU_DEFINE(),
DPCPU_GET(), DPCPU_SET(), etc. akin to the statically defined
PCPU_*. Requires only one extra instruction more than PCPU_* and is
virtually the same as __thread for builtin and much faster for shared
objects. DPCPU variables can be initialized when defined.
- Modules are supported by relocating the module's per-cpu linker set
over space reserved in the kernel. Modules may fail to load if there
is insufficient space available.
- Track space available for modules with a one-off extent allocator.
Free may block for memory to allocate space for an extent.
Reviewed by: jhb, rwatson, kan, sam, grehan, marius, marcel, stas
2009-06-23 22:42:39 +00:00
|
|
|
* Copyright (c) 2009 Jeffrey Roberson <jeff@freebsd.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
2001-05-10 17:45:49 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2016-09-15 13:16:20 +00:00
|
|
|
* 3. Neither the name of the author nor the names of any co-contributors
|
2001-05-10 17:45:49 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This module provides MI support for per-cpu data.
|
2001-10-24 22:15:38 +00:00
|
|
|
*
|
|
|
|
* Each architecture determines the mapping of logical CPU IDs to physical
|
|
|
|
* CPUs. The requirements of this mapping are as follows:
|
|
|
|
* - Logical CPU IDs must reside in the range 0 ... MAXCPU - 1.
|
|
|
|
* - The mapping is not required to be dense. That is, there may be
|
|
|
|
* gaps in the mappings.
|
|
|
|
* - The platform sets the value of MAXCPU in <machine/param.h>.
|
|
|
|
* - It is suggested, but not required, that in the non-SMP case, the
|
|
|
|
* platform define MAXCPU to be 1 and define the logical ID of the
|
|
|
|
* sole CPU as 0.
|
2001-05-10 17:45:49 +00:00
|
|
|
*/
|
|
|
|
|
2003-06-11 00:56:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2001-12-11 23:33:44 +00:00
|
|
|
#include "opt_ddb.h"
|
|
|
|
|
2001-05-10 17:45:49 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
Implement a facility for dynamic per-cpu variables.
- Modules and kernel code alike may use DPCPU_DEFINE(),
DPCPU_GET(), DPCPU_SET(), etc. akin to the statically defined
PCPU_*. Requires only one extra instruction more than PCPU_* and is
virtually the same as __thread for builtin and much faster for shared
objects. DPCPU variables can be initialized when defined.
- Modules are supported by relocating the module's per-cpu linker set
over space reserved in the kernel. Modules may fail to load if there
is insufficient space available.
- Track space available for modules with a one-off extent allocator.
Free may block for memory to allocate space for an extent.
Reviewed by: jhb, rwatson, kan, sam, grehan, marius, marcel, stas
2009-06-23 22:42:39 +00:00
|
|
|
#include <sys/sysctl.h>
|
2001-12-11 23:33:44 +00:00
|
|
|
#include <sys/lock.h>
|
Implement a facility for dynamic per-cpu variables.
- Modules and kernel code alike may use DPCPU_DEFINE(),
DPCPU_GET(), DPCPU_SET(), etc. akin to the statically defined
PCPU_*. Requires only one extra instruction more than PCPU_* and is
virtually the same as __thread for builtin and much faster for shared
objects. DPCPU variables can be initialized when defined.
- Modules are supported by relocating the module's per-cpu linker set
over space reserved in the kernel. Modules may fail to load if there
is insufficient space available.
- Track space available for modules with a one-off extent allocator.
Free may block for memory to allocate space for an extent.
Reviewed by: jhb, rwatson, kan, sam, grehan, marius, marcel, stas
2009-06-23 22:42:39 +00:00
|
|
|
#include <sys/malloc.h>
|
2001-05-10 17:45:49 +00:00
|
|
|
#include <sys/pcpu.h>
|
2001-12-11 23:33:44 +00:00
|
|
|
#include <sys/proc.h>
|
2005-09-26 16:55:11 +00:00
|
|
|
#include <sys/smp.h>
|
Implement a facility for dynamic per-cpu variables.
- Modules and kernel code alike may use DPCPU_DEFINE(),
DPCPU_GET(), DPCPU_SET(), etc. akin to the statically defined
PCPU_*. Requires only one extra instruction more than PCPU_* and is
virtually the same as __thread for builtin and much faster for shared
objects. DPCPU variables can be initialized when defined.
- Modules are supported by relocating the module's per-cpu linker set
over space reserved in the kernel. Modules may fail to load if there
is insufficient space available.
- Track space available for modules with a one-off extent allocator.
Free may block for memory to allocate space for an extent.
Reviewed by: jhb, rwatson, kan, sam, grehan, marius, marcel, stas
2009-06-23 22:42:39 +00:00
|
|
|
#include <sys/sx.h>
|
2014-02-10 19:59:46 +00:00
|
|
|
#include <vm/uma.h>
|
2001-12-11 23:33:44 +00:00
|
|
|
#include <ddb/ddb.h>
|
2001-05-10 17:45:49 +00:00
|
|
|
|
2011-11-07 06:44:47 +00:00
|
|
|
static MALLOC_DEFINE(M_PCPU, "Per-cpu", "Per-cpu resource accouting.");
|
Implement a facility for dynamic per-cpu variables.
- Modules and kernel code alike may use DPCPU_DEFINE(),
DPCPU_GET(), DPCPU_SET(), etc. akin to the statically defined
PCPU_*. Requires only one extra instruction more than PCPU_* and is
virtually the same as __thread for builtin and much faster for shared
objects. DPCPU variables can be initialized when defined.
- Modules are supported by relocating the module's per-cpu linker set
over space reserved in the kernel. Modules may fail to load if there
is insufficient space available.
- Track space available for modules with a one-off extent allocator.
Free may block for memory to allocate space for an extent.
Reviewed by: jhb, rwatson, kan, sam, grehan, marius, marcel, stas
2009-06-23 22:42:39 +00:00
|
|
|
|
|
|
|
struct dpcpu_free {
|
|
|
|
uintptr_t df_start;
|
|
|
|
int df_len;
|
|
|
|
TAILQ_ENTRY(dpcpu_free) df_link;
|
|
|
|
};
|
|
|
|
|
2010-11-22 19:32:54 +00:00
|
|
|
static DPCPU_DEFINE(char, modspace[DPCPU_MODMIN]);
|
Implement a facility for dynamic per-cpu variables.
- Modules and kernel code alike may use DPCPU_DEFINE(),
DPCPU_GET(), DPCPU_SET(), etc. akin to the statically defined
PCPU_*. Requires only one extra instruction more than PCPU_* and is
virtually the same as __thread for builtin and much faster for shared
objects. DPCPU variables can be initialized when defined.
- Modules are supported by relocating the module's per-cpu linker set
over space reserved in the kernel. Modules may fail to load if there
is insufficient space available.
- Track space available for modules with a one-off extent allocator.
Free may block for memory to allocate space for an extent.
Reviewed by: jhb, rwatson, kan, sam, grehan, marius, marcel, stas
2009-06-23 22:42:39 +00:00
|
|
|
static TAILQ_HEAD(, dpcpu_free) dpcpu_head = TAILQ_HEAD_INITIALIZER(dpcpu_head);
|
|
|
|
static struct sx dpcpu_lock;
|
|
|
|
uintptr_t dpcpu_off[MAXCPU];
|
2007-11-08 14:47:55 +00:00
|
|
|
struct pcpu *cpuid_to_pcpu[MAXCPU];
|
2011-05-31 21:22:44 +00:00
|
|
|
struct cpuhead cpuhead = STAILQ_HEAD_INITIALIZER(cpuhead);
|
2001-05-10 17:45:49 +00:00
|
|
|
|
|
|
|
/*
|
2001-12-11 23:33:44 +00:00
|
|
|
* Initialize the MI portions of a struct pcpu.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
|
|
|
|
{
|
|
|
|
|
|
|
|
bzero(pcpu, size);
|
|
|
|
KASSERT(cpuid >= 0 && cpuid < MAXCPU,
|
|
|
|
("pcpu_init: invalid cpuid %d", cpuid));
|
|
|
|
pcpu->pc_cpuid = cpuid;
|
|
|
|
cpuid_to_pcpu[cpuid] = pcpu;
|
2011-05-31 21:22:44 +00:00
|
|
|
STAILQ_INSERT_TAIL(&cpuhead, pcpu, pc_allcpu);
|
2001-12-11 23:33:44 +00:00
|
|
|
cpu_pcpu_init(pcpu, cpuid, size);
|
2007-11-08 14:47:55 +00:00
|
|
|
pcpu->pc_rm_queue.rmq_next = &pcpu->pc_rm_queue;
|
|
|
|
pcpu->pc_rm_queue.rmq_prev = &pcpu->pc_rm_queue;
|
Implement a facility for dynamic per-cpu variables.
- Modules and kernel code alike may use DPCPU_DEFINE(),
DPCPU_GET(), DPCPU_SET(), etc. akin to the statically defined
PCPU_*. Requires only one extra instruction more than PCPU_* and is
virtually the same as __thread for builtin and much faster for shared
objects. DPCPU variables can be initialized when defined.
- Modules are supported by relocating the module's per-cpu linker set
over space reserved in the kernel. Modules may fail to load if there
is insufficient space available.
- Track space available for modules with a one-off extent allocator.
Free may block for memory to allocate space for an extent.
Reviewed by: jhb, rwatson, kan, sam, grehan, marius, marcel, stas
2009-06-23 22:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dpcpu_init(void *dpcpu, int cpuid)
|
|
|
|
{
|
|
|
|
struct pcpu *pcpu;
|
|
|
|
|
|
|
|
pcpu = pcpu_find(cpuid);
|
|
|
|
pcpu->pc_dynamic = (uintptr_t)dpcpu - DPCPU_START;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize defaults from our linker section.
|
|
|
|
*/
|
|
|
|
memcpy(dpcpu, (void *)DPCPU_START, DPCPU_BYTES);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Place it in the global pcpu offset array.
|
|
|
|
*/
|
|
|
|
dpcpu_off[cpuid] = pcpu->pc_dynamic;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dpcpu_startup(void *dummy __unused)
|
|
|
|
{
|
|
|
|
struct dpcpu_free *df;
|
|
|
|
|
|
|
|
df = malloc(sizeof(*df), M_PCPU, M_WAITOK | M_ZERO);
|
|
|
|
df->df_start = (uintptr_t)&DPCPU_NAME(modspace);
|
2010-05-14 21:11:58 +00:00
|
|
|
df->df_len = DPCPU_MODMIN;
|
Implement a facility for dynamic per-cpu variables.
- Modules and kernel code alike may use DPCPU_DEFINE(),
DPCPU_GET(), DPCPU_SET(), etc. akin to the statically defined
PCPU_*. Requires only one extra instruction more than PCPU_* and is
virtually the same as __thread for builtin and much faster for shared
objects. DPCPU variables can be initialized when defined.
- Modules are supported by relocating the module's per-cpu linker set
over space reserved in the kernel. Modules may fail to load if there
is insufficient space available.
- Track space available for modules with a one-off extent allocator.
Free may block for memory to allocate space for an extent.
Reviewed by: jhb, rwatson, kan, sam, grehan, marius, marcel, stas
2009-06-23 22:42:39 +00:00
|
|
|
TAILQ_INSERT_HEAD(&dpcpu_head, df, df_link);
|
|
|
|
sx_init(&dpcpu_lock, "dpcpu alloc lock");
|
|
|
|
}
|
|
|
|
SYSINIT(dpcpu, SI_SUB_KLD, SI_ORDER_FIRST, dpcpu_startup, 0);
|
|
|
|
|
2014-02-10 19:59:46 +00:00
|
|
|
/*
|
|
|
|
* UMA_PCPU_ZONE zones, that are available for all kernel
|
|
|
|
* consumers. Right now 64 bit zone is used for counter(9)
|
|
|
|
* and pointer zone is used by flowtable.
|
|
|
|
*/
|
|
|
|
|
|
|
|
uma_zone_t pcpu_zone_64;
|
|
|
|
uma_zone_t pcpu_zone_ptr;
|
|
|
|
|
|
|
|
static void
|
|
|
|
pcpu_zones_startup(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
pcpu_zone_64 = uma_zcreate("64 pcpu", sizeof(uint64_t),
|
|
|
|
NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_PCPU);
|
|
|
|
|
|
|
|
if (sizeof(uint64_t) == sizeof(void *))
|
|
|
|
pcpu_zone_ptr = pcpu_zone_64;
|
|
|
|
else
|
|
|
|
pcpu_zone_ptr = uma_zcreate("ptr pcpu", sizeof(void *),
|
|
|
|
NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_PCPU);
|
|
|
|
}
|
|
|
|
SYSINIT(pcpu_zones, SI_SUB_KMEM, SI_ORDER_ANY, pcpu_zones_startup, NULL);
|
|
|
|
|
Implement a facility for dynamic per-cpu variables.
- Modules and kernel code alike may use DPCPU_DEFINE(),
DPCPU_GET(), DPCPU_SET(), etc. akin to the statically defined
PCPU_*. Requires only one extra instruction more than PCPU_* and is
virtually the same as __thread for builtin and much faster for shared
objects. DPCPU variables can be initialized when defined.
- Modules are supported by relocating the module's per-cpu linker set
over space reserved in the kernel. Modules may fail to load if there
is insufficient space available.
- Track space available for modules with a one-off extent allocator.
Free may block for memory to allocate space for an extent.
Reviewed by: jhb, rwatson, kan, sam, grehan, marius, marcel, stas
2009-06-23 22:42:39 +00:00
|
|
|
/*
|
|
|
|
* First-fit extent based allocator for allocating space in the per-cpu
|
|
|
|
* region reserved for modules. This is only intended for use by the
|
|
|
|
* kernel linkers to place module linker sets.
|
|
|
|
*/
|
|
|
|
void *
|
|
|
|
dpcpu_alloc(int size)
|
|
|
|
{
|
|
|
|
struct dpcpu_free *df;
|
|
|
|
void *s;
|
|
|
|
|
|
|
|
s = NULL;
|
|
|
|
size = roundup2(size, sizeof(void *));
|
|
|
|
sx_xlock(&dpcpu_lock);
|
|
|
|
TAILQ_FOREACH(df, &dpcpu_head, df_link) {
|
|
|
|
if (df->df_len < size)
|
|
|
|
continue;
|
|
|
|
if (df->df_len == size) {
|
|
|
|
s = (void *)df->df_start;
|
|
|
|
TAILQ_REMOVE(&dpcpu_head, df, df_link);
|
|
|
|
free(df, M_PCPU);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s = (void *)df->df_start;
|
|
|
|
df->df_len -= size;
|
|
|
|
df->df_start = df->df_start + size;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sx_xunlock(&dpcpu_lock);
|
|
|
|
|
|
|
|
return (s);
|
|
|
|
}
|
2007-11-08 14:47:55 +00:00
|
|
|
|
Implement a facility for dynamic per-cpu variables.
- Modules and kernel code alike may use DPCPU_DEFINE(),
DPCPU_GET(), DPCPU_SET(), etc. akin to the statically defined
PCPU_*. Requires only one extra instruction more than PCPU_* and is
virtually the same as __thread for builtin and much faster for shared
objects. DPCPU variables can be initialized when defined.
- Modules are supported by relocating the module's per-cpu linker set
over space reserved in the kernel. Modules may fail to load if there
is insufficient space available.
- Track space available for modules with a one-off extent allocator.
Free may block for memory to allocate space for an extent.
Reviewed by: jhb, rwatson, kan, sam, grehan, marius, marcel, stas
2009-06-23 22:42:39 +00:00
|
|
|
/*
|
|
|
|
* Free dynamic per-cpu space at module unload time.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
dpcpu_free(void *s, int size)
|
|
|
|
{
|
|
|
|
struct dpcpu_free *df;
|
|
|
|
struct dpcpu_free *dn;
|
|
|
|
uintptr_t start;
|
|
|
|
uintptr_t end;
|
|
|
|
|
|
|
|
size = roundup2(size, sizeof(void *));
|
|
|
|
start = (uintptr_t)s;
|
|
|
|
end = start + size;
|
|
|
|
/*
|
|
|
|
* Free a region of space and merge it with as many neighbors as
|
|
|
|
* possible. Keeping the list sorted simplifies this operation.
|
|
|
|
*/
|
|
|
|
sx_xlock(&dpcpu_lock);
|
|
|
|
TAILQ_FOREACH(df, &dpcpu_head, df_link) {
|
|
|
|
if (df->df_start > end)
|
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* If we expand at the end of an entry we may have to
|
|
|
|
* merge it with the one following it as well.
|
|
|
|
*/
|
|
|
|
if (df->df_start + df->df_len == start) {
|
|
|
|
df->df_len += size;
|
|
|
|
dn = TAILQ_NEXT(df, df_link);
|
|
|
|
if (df->df_start + df->df_len == dn->df_start) {
|
|
|
|
df->df_len += dn->df_len;
|
|
|
|
TAILQ_REMOVE(&dpcpu_head, dn, df_link);
|
|
|
|
free(dn, M_PCPU);
|
|
|
|
}
|
|
|
|
sx_xunlock(&dpcpu_lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (df->df_start == end) {
|
|
|
|
df->df_start = start;
|
|
|
|
df->df_len += size;
|
|
|
|
sx_xunlock(&dpcpu_lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dn = malloc(sizeof(*df), M_PCPU, M_WAITOK | M_ZERO);
|
|
|
|
dn->df_start = start;
|
|
|
|
dn->df_len = size;
|
|
|
|
if (df)
|
|
|
|
TAILQ_INSERT_BEFORE(df, dn, df_link);
|
|
|
|
else
|
|
|
|
TAILQ_INSERT_TAIL(&dpcpu_head, dn, df_link);
|
|
|
|
sx_xunlock(&dpcpu_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the per-cpu storage from an updated linker-set region.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
dpcpu_copy(void *s, int size)
|
|
|
|
{
|
|
|
|
#ifdef SMP
|
|
|
|
uintptr_t dpcpu;
|
|
|
|
int i;
|
|
|
|
|
2016-07-06 14:09:49 +00:00
|
|
|
CPU_FOREACH(i) {
|
Implement a facility for dynamic per-cpu variables.
- Modules and kernel code alike may use DPCPU_DEFINE(),
DPCPU_GET(), DPCPU_SET(), etc. akin to the statically defined
PCPU_*. Requires only one extra instruction more than PCPU_* and is
virtually the same as __thread for builtin and much faster for shared
objects. DPCPU variables can be initialized when defined.
- Modules are supported by relocating the module's per-cpu linker set
over space reserved in the kernel. Modules may fail to load if there
is insufficient space available.
- Track space available for modules with a one-off extent allocator.
Free may block for memory to allocate space for an extent.
Reviewed by: jhb, rwatson, kan, sam, grehan, marius, marcel, stas
2009-06-23 22:42:39 +00:00
|
|
|
dpcpu = dpcpu_off[i];
|
|
|
|
if (dpcpu == 0)
|
|
|
|
continue;
|
|
|
|
memcpy((void *)(dpcpu + (uintptr_t)s), s, size);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
memcpy((void *)(dpcpu_off[0] + (uintptr_t)s), s, size);
|
|
|
|
#endif
|
2001-12-11 23:33:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy a struct pcpu.
|
2001-05-10 17:45:49 +00:00
|
|
|
*/
|
|
|
|
void
|
2001-12-11 23:33:44 +00:00
|
|
|
pcpu_destroy(struct pcpu *pcpu)
|
2001-05-10 17:45:49 +00:00
|
|
|
{
|
|
|
|
|
2011-05-31 21:22:44 +00:00
|
|
|
STAILQ_REMOVE(&cpuhead, pcpu, pcpu, pc_allcpu);
|
2001-12-11 23:33:44 +00:00
|
|
|
cpuid_to_pcpu[pcpu->pc_cpuid] = NULL;
|
Implement a facility for dynamic per-cpu variables.
- Modules and kernel code alike may use DPCPU_DEFINE(),
DPCPU_GET(), DPCPU_SET(), etc. akin to the statically defined
PCPU_*. Requires only one extra instruction more than PCPU_* and is
virtually the same as __thread for builtin and much faster for shared
objects. DPCPU variables can be initialized when defined.
- Modules are supported by relocating the module's per-cpu linker set
over space reserved in the kernel. Modules may fail to load if there
is insufficient space available.
- Track space available for modules with a one-off extent allocator.
Free may block for memory to allocate space for an extent.
Reviewed by: jhb, rwatson, kan, sam, grehan, marius, marcel, stas
2009-06-23 22:42:39 +00:00
|
|
|
dpcpu_off[pcpu->pc_cpuid] = 0;
|
2001-05-10 17:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-12-11 23:33:44 +00:00
|
|
|
* Locate a struct pcpu by cpu id.
|
2001-05-10 17:45:49 +00:00
|
|
|
*/
|
2001-12-11 23:33:44 +00:00
|
|
|
struct pcpu *
|
|
|
|
pcpu_find(u_int cpuid)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (cpuid_to_pcpu[cpuid]);
|
|
|
|
}
|
|
|
|
|
Implement a facility for dynamic per-cpu variables.
- Modules and kernel code alike may use DPCPU_DEFINE(),
DPCPU_GET(), DPCPU_SET(), etc. akin to the statically defined
PCPU_*. Requires only one extra instruction more than PCPU_* and is
virtually the same as __thread for builtin and much faster for shared
objects. DPCPU variables can be initialized when defined.
- Modules are supported by relocating the module's per-cpu linker set
over space reserved in the kernel. Modules may fail to load if there
is insufficient space available.
- Track space available for modules with a one-off extent allocator.
Free may block for memory to allocate space for an extent.
Reviewed by: jhb, rwatson, kan, sam, grehan, marius, marcel, stas
2009-06-23 22:42:39 +00:00
|
|
|
int
|
|
|
|
sysctl_dpcpu_quad(SYSCTL_HANDLER_ARGS)
|
|
|
|
{
|
|
|
|
uintptr_t dpcpu;
|
2009-06-25 01:31:59 +00:00
|
|
|
int64_t count;
|
Implement a facility for dynamic per-cpu variables.
- Modules and kernel code alike may use DPCPU_DEFINE(),
DPCPU_GET(), DPCPU_SET(), etc. akin to the statically defined
PCPU_*. Requires only one extra instruction more than PCPU_* and is
virtually the same as __thread for builtin and much faster for shared
objects. DPCPU variables can be initialized when defined.
- Modules are supported by relocating the module's per-cpu linker set
over space reserved in the kernel. Modules may fail to load if there
is insufficient space available.
- Track space available for modules with a one-off extent allocator.
Free may block for memory to allocate space for an extent.
Reviewed by: jhb, rwatson, kan, sam, grehan, marius, marcel, stas
2009-06-23 22:42:39 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
count = 0;
|
2016-07-06 14:09:49 +00:00
|
|
|
CPU_FOREACH(i) {
|
Implement a facility for dynamic per-cpu variables.
- Modules and kernel code alike may use DPCPU_DEFINE(),
DPCPU_GET(), DPCPU_SET(), etc. akin to the statically defined
PCPU_*. Requires only one extra instruction more than PCPU_* and is
virtually the same as __thread for builtin and much faster for shared
objects. DPCPU variables can be initialized when defined.
- Modules are supported by relocating the module's per-cpu linker set
over space reserved in the kernel. Modules may fail to load if there
is insufficient space available.
- Track space available for modules with a one-off extent allocator.
Free may block for memory to allocate space for an extent.
Reviewed by: jhb, rwatson, kan, sam, grehan, marius, marcel, stas
2009-06-23 22:42:39 +00:00
|
|
|
dpcpu = dpcpu_off[i];
|
|
|
|
if (dpcpu == 0)
|
|
|
|
continue;
|
|
|
|
count += *(int64_t *)(dpcpu + (uintptr_t)arg1);
|
|
|
|
}
|
2009-06-25 01:31:59 +00:00
|
|
|
return (SYSCTL_OUT(req, &count, sizeof(count)));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sysctl_dpcpu_long(SYSCTL_HANDLER_ARGS)
|
|
|
|
{
|
|
|
|
uintptr_t dpcpu;
|
|
|
|
long count;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
count = 0;
|
2016-07-06 14:09:49 +00:00
|
|
|
CPU_FOREACH(i) {
|
2009-06-25 01:31:59 +00:00
|
|
|
dpcpu = dpcpu_off[i];
|
|
|
|
if (dpcpu == 0)
|
|
|
|
continue;
|
|
|
|
count += *(long *)(dpcpu + (uintptr_t)arg1);
|
|
|
|
}
|
Implement a facility for dynamic per-cpu variables.
- Modules and kernel code alike may use DPCPU_DEFINE(),
DPCPU_GET(), DPCPU_SET(), etc. akin to the statically defined
PCPU_*. Requires only one extra instruction more than PCPU_* and is
virtually the same as __thread for builtin and much faster for shared
objects. DPCPU variables can be initialized when defined.
- Modules are supported by relocating the module's per-cpu linker set
over space reserved in the kernel. Modules may fail to load if there
is insufficient space available.
- Track space available for modules with a one-off extent allocator.
Free may block for memory to allocate space for an extent.
Reviewed by: jhb, rwatson, kan, sam, grehan, marius, marcel, stas
2009-06-23 22:42:39 +00:00
|
|
|
return (SYSCTL_OUT(req, &count, sizeof(count)));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sysctl_dpcpu_int(SYSCTL_HANDLER_ARGS)
|
|
|
|
{
|
|
|
|
uintptr_t dpcpu;
|
2009-06-25 01:31:59 +00:00
|
|
|
int count;
|
Implement a facility for dynamic per-cpu variables.
- Modules and kernel code alike may use DPCPU_DEFINE(),
DPCPU_GET(), DPCPU_SET(), etc. akin to the statically defined
PCPU_*. Requires only one extra instruction more than PCPU_* and is
virtually the same as __thread for builtin and much faster for shared
objects. DPCPU variables can be initialized when defined.
- Modules are supported by relocating the module's per-cpu linker set
over space reserved in the kernel. Modules may fail to load if there
is insufficient space available.
- Track space available for modules with a one-off extent allocator.
Free may block for memory to allocate space for an extent.
Reviewed by: jhb, rwatson, kan, sam, grehan, marius, marcel, stas
2009-06-23 22:42:39 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
count = 0;
|
2016-07-06 14:09:49 +00:00
|
|
|
CPU_FOREACH(i) {
|
Implement a facility for dynamic per-cpu variables.
- Modules and kernel code alike may use DPCPU_DEFINE(),
DPCPU_GET(), DPCPU_SET(), etc. akin to the statically defined
PCPU_*. Requires only one extra instruction more than PCPU_* and is
virtually the same as __thread for builtin and much faster for shared
objects. DPCPU variables can be initialized when defined.
- Modules are supported by relocating the module's per-cpu linker set
over space reserved in the kernel. Modules may fail to load if there
is insufficient space available.
- Track space available for modules with a one-off extent allocator.
Free may block for memory to allocate space for an extent.
Reviewed by: jhb, rwatson, kan, sam, grehan, marius, marcel, stas
2009-06-23 22:42:39 +00:00
|
|
|
dpcpu = dpcpu_off[i];
|
|
|
|
if (dpcpu == 0)
|
|
|
|
continue;
|
|
|
|
count += *(int *)(dpcpu + (uintptr_t)arg1);
|
|
|
|
}
|
|
|
|
return (SYSCTL_OUT(req, &count, sizeof(count)));
|
|
|
|
}
|
|
|
|
|
2001-12-11 23:33:44 +00:00
|
|
|
#ifdef DDB
|
2009-08-12 12:06:16 +00:00
|
|
|
DB_SHOW_COMMAND(dpcpu_off, db_show_dpcpu_off)
|
|
|
|
{
|
|
|
|
int id;
|
|
|
|
|
2010-06-11 18:46:34 +00:00
|
|
|
CPU_FOREACH(id) {
|
2009-08-12 12:06:16 +00:00
|
|
|
db_printf("dpcpu_off[%2d] = 0x%jx (+ DPCPU_START = %p)\n",
|
|
|
|
id, (uintmax_t)dpcpu_off[id],
|
|
|
|
(void *)(uintptr_t)(dpcpu_off[id] + DPCPU_START));
|
|
|
|
}
|
|
|
|
}
|
2005-09-26 16:55:11 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
show_pcpu(struct pcpu *pc)
|
2001-05-10 17:45:49 +00:00
|
|
|
{
|
2001-12-11 23:33:44 +00:00
|
|
|
struct thread *td;
|
2001-05-10 17:45:49 +00:00
|
|
|
|
2001-12-11 23:33:44 +00:00
|
|
|
db_printf("cpuid = %d\n", pc->pc_cpuid);
|
2010-05-21 17:17:56 +00:00
|
|
|
db_printf("dynamic pcpu = %p\n", (void *)pc->pc_dynamic);
|
2001-12-11 23:33:44 +00:00
|
|
|
db_printf("curthread = ");
|
|
|
|
td = pc->pc_curthread;
|
|
|
|
if (td != NULL)
|
2016-08-14 15:52:00 +00:00
|
|
|
db_printf("%p: pid %d tid %d \"%s\"\n", td, td->td_proc->p_pid,
|
|
|
|
td->td_tid, td->td_name);
|
2001-12-11 23:33:44 +00:00
|
|
|
else
|
|
|
|
db_printf("none\n");
|
|
|
|
db_printf("curpcb = %p\n", pc->pc_curpcb);
|
|
|
|
db_printf("fpcurthread = ");
|
|
|
|
td = pc->pc_fpcurthread;
|
|
|
|
if (td != NULL)
|
|
|
|
db_printf("%p: pid %d \"%s\"\n", td, td->td_proc->p_pid,
|
2007-11-14 06:21:24 +00:00
|
|
|
td->td_name);
|
2001-12-11 23:33:44 +00:00
|
|
|
else
|
|
|
|
db_printf("none\n");
|
|
|
|
db_printf("idlethread = ");
|
|
|
|
td = pc->pc_idlethread;
|
|
|
|
if (td != NULL)
|
2010-05-21 17:17:56 +00:00
|
|
|
db_printf("%p: tid %d \"%s\"\n", td, td->td_tid, td->td_name);
|
2001-12-11 23:33:44 +00:00
|
|
|
else
|
|
|
|
db_printf("none\n");
|
|
|
|
db_show_mdpcpu(pc);
|
2010-05-21 17:17:56 +00:00
|
|
|
|
Change the curvnet variable from a global const struct vnet *,
previously always pointing to the default vnet context, to a
dynamically changing thread-local one. The currvnet context
should be set on entry to networking code via CURVNET_SET() macros,
and reverted to previous state via CURVNET_RESTORE(). Recursions
on curvnet are permitted, though strongly discuouraged.
This change should have no functional impact on nooptions VIMAGE
kernel builds, where CURVNET_* macros expand to whitespace.
The curthread->td_vnet (aka curvnet) variable's purpose is to be an
indicator of the vnet context in which the current network-related
operation takes place, in case we cannot deduce the current vnet
context from any other source, such as by looking at mbuf's
m->m_pkthdr.rcvif->if_vnet, sockets's so->so_vnet etc. Moreover, so
far curvnet has turned out to be an invaluable consistency checking
aid: it helps to catch cases when sockets, ifnets or any other
vnet-aware structures may have leaked from one vnet to another.
The exact placement of the CURVNET_SET() / CURVNET_RESTORE() macros
was a result of an empirical iterative process, whith an aim to
reduce recursions on CURVNET_SET() to a minimum, while still reducing
the scope of CURVNET_SET() to networking only operations - the
alternative would be calling CURVNET_SET() on each system call entry.
In general, curvnet has to be set in three typicall cases: when
processing socket-related requests from userspace or from within the
kernel; when processing inbound traffic flowing from device drivers
to upper layers of the networking stack, and when executing
timer-driven networking functions.
This change also introduces a DDB subcommand to show the list of all
vnet instances.
Approved by: julian (mentor)
2009-05-05 10:56:12 +00:00
|
|
|
#ifdef VIMAGE
|
|
|
|
db_printf("curvnet = %p\n", pc->pc_curthread->td_vnet);
|
|
|
|
#endif
|
|
|
|
|
2001-12-11 23:33:44 +00:00
|
|
|
#ifdef WITNESS
|
|
|
|
db_printf("spin locks held:\n");
|
2010-05-11 18:24:22 +00:00
|
|
|
witness_list_locks(&pc->pc_spinlocks, db_printf);
|
2001-12-11 23:33:44 +00:00
|
|
|
#endif
|
2001-05-10 17:45:49 +00:00
|
|
|
}
|
2005-09-26 16:55:11 +00:00
|
|
|
|
|
|
|
DB_SHOW_COMMAND(pcpu, db_show_pcpu)
|
|
|
|
{
|
|
|
|
struct pcpu *pc;
|
|
|
|
int id;
|
|
|
|
|
|
|
|
if (have_addr)
|
|
|
|
id = ((addr >> 4) % 16) * 10 + (addr % 16);
|
|
|
|
else
|
|
|
|
id = PCPU_GET(cpuid);
|
|
|
|
pc = pcpu_find(id);
|
|
|
|
if (pc == NULL) {
|
|
|
|
db_printf("CPU %d not found\n", id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
show_pcpu(pc);
|
|
|
|
}
|
|
|
|
|
2008-09-15 22:45:14 +00:00
|
|
|
DB_SHOW_ALL_COMMAND(pcpu, db_show_cpu_all)
|
2005-09-26 16:55:11 +00:00
|
|
|
{
|
|
|
|
struct pcpu *pc;
|
|
|
|
int id;
|
|
|
|
|
|
|
|
db_printf("Current CPU: %d\n\n", PCPU_GET(cpuid));
|
2005-11-03 21:06:29 +00:00
|
|
|
for (id = 0; id <= mp_maxid; id++) {
|
2005-09-26 16:55:11 +00:00
|
|
|
pc = pcpu_find(id);
|
|
|
|
if (pc != NULL) {
|
|
|
|
show_pcpu(pc);
|
|
|
|
db_printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-09-15 22:45:14 +00:00
|
|
|
DB_SHOW_ALIAS(allpcpu, db_show_cpu_all);
|
2001-12-11 23:33:44 +00:00
|
|
|
#endif
|