2015-07-27 12:24:28 +03:00
|
|
|
/* $NetBSD: subr_kmem.c,v 1.61 2015/07/27 09:24:28 maxv Exp $ */
|
2009-02-01 21:51:07 +03:00
|
|
|
|
|
|
|
/*-
|
2015-07-27 12:24:28 +03:00
|
|
|
* Copyright (c) 2009-2015 The NetBSD Foundation, Inc.
|
2009-02-01 21:51:07 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
2015-07-27 12:24:28 +03:00
|
|
|
* by Andrew Doran and Maxime Villard.
|
2009-02-01 21:51:07 +03: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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
|
|
|
|
*/
|
2006-06-25 12:00:01 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c)2006 YAMAMOTO Takashi,
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2014-06-25 20:05:22 +04:00
|
|
|
* Allocator of kernel wired memory. This allocator has some debug features
|
|
|
|
* enabled with "option DIAGNOSTIC" and "option DEBUG".
|
2013-04-22 17:22:25 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2014-06-25 20:05:22 +04:00
|
|
|
* KMEM_SIZE: detect alloc/free size mismatch bugs.
|
2014-07-01 16:08:33 +04:00
|
|
|
* Prefix each allocations with a fixed-sized, aligned header and record
|
|
|
|
* the exact user-requested allocation size in it. When freeing, compare
|
|
|
|
* it with kmem_free's "size" argument.
|
2014-07-22 11:38:41 +04:00
|
|
|
*
|
2014-06-25 20:05:22 +04:00
|
|
|
* KMEM_REDZONE: detect overrun bugs.
|
2014-07-01 16:08:33 +04:00
|
|
|
* Add a 2-byte pattern (allocate one more memory chunk if needed) at the
|
|
|
|
* end of each allocated buffer. Check this pattern on kmem_free.
|
2013-04-22 17:22:25 +04:00
|
|
|
*
|
2014-07-22 11:38:41 +04:00
|
|
|
* These options are enabled on DIAGNOSTIC.
|
|
|
|
*
|
|
|
|
* |CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|
|
|
|
|
* +-----+-----+-----+-----+-----+-----+-----+-----+-----+---+-+--+--+
|
|
|
|
* |/////| | | | | | | | | |*|**|UU|
|
|
|
|
* |/HSZ/| | | | | | | | | |*|**|UU|
|
|
|
|
* |/////| | | | | | | | | |*|**|UU|
|
|
|
|
* +-----+-----+-----+-----+-----+-----+-----+-----+-----+---+-+--+--+
|
|
|
|
* |Size | Buffer usable by the caller (requested size) |RedZ|Unused\
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2014-06-25 20:05:22 +04:00
|
|
|
* KMEM_POISON: detect modify-after-free bugs.
|
|
|
|
* Fill freed (in the sense of kmem_free) memory with a garbage pattern.
|
|
|
|
* Check the pattern on allocation.
|
2013-04-22 17:22:25 +04:00
|
|
|
*
|
2014-06-25 20:05:22 +04:00
|
|
|
* KMEM_GUARD
|
2015-07-27 12:24:28 +03:00
|
|
|
* A kernel with "option DEBUG" has "kmem_guard" debugging feature compiled
|
|
|
|
* in. See the comment below for what kind of bugs it tries to detect. Even
|
|
|
|
* if compiled in, it's disabled by default because it's very expensive.
|
|
|
|
* You can enable it on boot by:
|
2014-06-25 20:05:22 +04:00
|
|
|
* boot -d
|
|
|
|
* db> w kmem_guard_depth 0t30000
|
|
|
|
* db> c
|
2006-06-25 12:00:01 +04:00
|
|
|
*
|
2014-06-25 20:05:22 +04:00
|
|
|
* The default value of kmem_guard_depth is 0, which means disabled.
|
|
|
|
* It can be changed by KMEM_GUARD_DEPTH kernel config option.
|
2006-06-25 12:00:01 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2015-07-27 12:24:28 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.61 2015/07/27 09:24:28 maxv Exp $");
|
2006-06-25 12:00:01 +04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
2006-08-20 13:45:59 +04:00
|
|
|
#include <sys/callback.h>
|
2006-06-25 12:00:01 +04:00
|
|
|
#include <sys/kmem.h>
|
2012-01-27 23:48:38 +04:00
|
|
|
#include <sys/pool.h>
|
2007-02-10 00:55:00 +03:00
|
|
|
#include <sys/debug.h>
|
2007-11-07 03:23:13 +03:00
|
|
|
#include <sys/lockdebug.h>
|
2009-02-01 21:51:07 +03:00
|
|
|
#include <sys/cpu.h>
|
2006-06-25 12:00:01 +04:00
|
|
|
|
2006-08-20 13:45:59 +04:00
|
|
|
#include <uvm/uvm_extern.h>
|
|
|
|
#include <uvm/uvm_map.h>
|
|
|
|
|
2006-06-25 12:00:01 +04:00
|
|
|
#include <lib/libkern/libkern.h>
|
|
|
|
|
2012-07-21 15:45:04 +04:00
|
|
|
struct kmem_cache_info {
|
2012-01-29 03:09:06 +04:00
|
|
|
size_t kc_size;
|
|
|
|
const char * kc_name;
|
2012-07-21 15:45:04 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct kmem_cache_info kmem_cache_sizes[] = {
|
2012-01-27 23:48:38 +04:00
|
|
|
{ 8, "kmem-8" },
|
|
|
|
{ 16, "kmem-16" },
|
|
|
|
{ 24, "kmem-24" },
|
|
|
|
{ 32, "kmem-32" },
|
|
|
|
{ 40, "kmem-40" },
|
|
|
|
{ 48, "kmem-48" },
|
|
|
|
{ 56, "kmem-56" },
|
|
|
|
{ 64, "kmem-64" },
|
|
|
|
{ 80, "kmem-80" },
|
|
|
|
{ 96, "kmem-96" },
|
|
|
|
{ 112, "kmem-112" },
|
|
|
|
{ 128, "kmem-128" },
|
|
|
|
{ 160, "kmem-160" },
|
|
|
|
{ 192, "kmem-192" },
|
|
|
|
{ 224, "kmem-224" },
|
|
|
|
{ 256, "kmem-256" },
|
|
|
|
{ 320, "kmem-320" },
|
|
|
|
{ 384, "kmem-384" },
|
|
|
|
{ 448, "kmem-448" },
|
|
|
|
{ 512, "kmem-512" },
|
|
|
|
{ 768, "kmem-768" },
|
|
|
|
{ 1024, "kmem-1024" },
|
2012-07-21 15:45:04 +04:00
|
|
|
{ 0, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct kmem_cache_info kmem_cache_big_sizes[] = {
|
2012-01-27 23:48:38 +04:00
|
|
|
{ 2048, "kmem-2048" },
|
|
|
|
{ 4096, "kmem-4096" },
|
2012-07-21 15:45:04 +04:00
|
|
|
{ 8192, "kmem-8192" },
|
|
|
|
{ 16384, "kmem-16384" },
|
2012-01-27 23:48:38 +04:00
|
|
|
{ 0, NULL }
|
|
|
|
};
|
2009-02-01 21:51:07 +03:00
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
/*
|
2012-01-29 03:09:06 +04:00
|
|
|
* KMEM_ALIGN is the smallest guaranteed alignment and also the
|
2012-07-21 15:45:04 +04:00
|
|
|
* smallest allocateable quantum.
|
|
|
|
* Every cache size >= CACHE_LINE_SIZE gets CACHE_LINE_SIZE alignment.
|
2012-01-27 23:48:38 +04:00
|
|
|
*/
|
2012-01-29 03:09:06 +04:00
|
|
|
#define KMEM_ALIGN 8
|
|
|
|
#define KMEM_SHIFT 3
|
2012-07-21 15:45:04 +04:00
|
|
|
#define KMEM_MAXSIZE 1024
|
2012-01-29 03:09:06 +04:00
|
|
|
#define KMEM_CACHE_COUNT (KMEM_MAXSIZE >> KMEM_SHIFT)
|
2006-06-25 12:00:01 +04:00
|
|
|
|
2012-01-29 03:09:06 +04:00
|
|
|
static pool_cache_t kmem_cache[KMEM_CACHE_COUNT] __cacheline_aligned;
|
|
|
|
static size_t kmem_cache_maxidx __read_mostly;
|
2009-02-01 21:51:07 +03:00
|
|
|
|
2012-07-21 15:45:04 +04:00
|
|
|
#define KMEM_BIG_ALIGN 2048
|
|
|
|
#define KMEM_BIG_SHIFT 11
|
|
|
|
#define KMEM_BIG_MAXSIZE 16384
|
|
|
|
#define KMEM_CACHE_BIG_COUNT (KMEM_BIG_MAXSIZE >> KMEM_BIG_SHIFT)
|
|
|
|
|
|
|
|
static pool_cache_t kmem_cache_big[KMEM_CACHE_BIG_COUNT] __cacheline_aligned;
|
|
|
|
static size_t kmem_cache_big_maxidx __read_mostly;
|
|
|
|
|
2014-06-23 21:43:42 +04:00
|
|
|
#if defined(DIAGNOSTIC) && defined(_HARDKERNEL)
|
2014-07-01 16:08:33 +04:00
|
|
|
#define KMEM_SIZE
|
2014-07-22 11:38:41 +04:00
|
|
|
#define KMEM_REDZONE
|
2014-06-23 21:43:42 +04:00
|
|
|
#endif /* defined(DIAGNOSTIC) */
|
|
|
|
|
2012-04-15 23:07:40 +04:00
|
|
|
#if defined(DEBUG) && defined(_HARDKERNEL)
|
2015-07-27 12:24:28 +03:00
|
|
|
#define KMEM_SIZE
|
2008-02-09 15:56:20 +03:00
|
|
|
#define KMEM_POISON
|
2009-03-29 14:51:53 +04:00
|
|
|
#define KMEM_GUARD
|
2015-07-27 12:24:28 +03:00
|
|
|
static void *kmem_freecheck;
|
2008-02-09 15:56:20 +03:00
|
|
|
#endif /* defined(DEBUG) */
|
|
|
|
|
|
|
|
#if defined(KMEM_POISON)
|
2012-01-27 23:48:38 +04:00
|
|
|
static int kmem_poison_ctor(void *, void *, int);
|
2006-07-08 10:01:53 +04:00
|
|
|
static void kmem_poison_fill(void *, size_t);
|
|
|
|
static void kmem_poison_check(void *, size_t);
|
2008-02-09 15:56:20 +03:00
|
|
|
#else /* defined(KMEM_POISON) */
|
2012-01-29 03:09:06 +04:00
|
|
|
#define kmem_poison_fill(p, sz) /* nothing */
|
|
|
|
#define kmem_poison_check(p, sz) /* nothing */
|
2008-02-09 15:56:20 +03:00
|
|
|
#endif /* defined(KMEM_POISON) */
|
|
|
|
|
|
|
|
#if defined(KMEM_REDZONE)
|
2014-06-24 11:28:23 +04:00
|
|
|
#define REDZONE_SIZE 2
|
2014-07-01 16:08:33 +04:00
|
|
|
static void kmem_redzone_fill(void *, size_t);
|
|
|
|
static void kmem_redzone_check(void *, size_t);
|
2008-02-09 15:56:20 +03:00
|
|
|
#else /* defined(KMEM_REDZONE) */
|
|
|
|
#define REDZONE_SIZE 0
|
2014-06-24 11:28:23 +04:00
|
|
|
#define kmem_redzone_fill(p, sz) /* nothing */
|
|
|
|
#define kmem_redzone_check(p, sz) /* nothing */
|
2008-02-09 15:56:20 +03:00
|
|
|
#endif /* defined(KMEM_REDZONE) */
|
2006-07-08 10:01:53 +04:00
|
|
|
|
2009-02-01 21:51:07 +03:00
|
|
|
#if defined(KMEM_SIZE)
|
2014-07-01 16:08:33 +04:00
|
|
|
struct kmem_header {
|
|
|
|
size_t size;
|
|
|
|
} __aligned(KMEM_ALIGN);
|
|
|
|
#define SIZE_SIZE sizeof(struct kmem_header)
|
2009-02-01 21:51:07 +03:00
|
|
|
static void kmem_size_set(void *, size_t);
|
2012-01-27 23:48:38 +04:00
|
|
|
static void kmem_size_check(void *, size_t);
|
2009-02-01 21:51:07 +03:00
|
|
|
#else
|
|
|
|
#define SIZE_SIZE 0
|
|
|
|
#define kmem_size_set(p, sz) /* nothing */
|
|
|
|
#define kmem_size_check(p, sz) /* nothing */
|
|
|
|
#endif
|
|
|
|
|
2014-06-22 21:36:42 +04:00
|
|
|
#if defined(KMEM_GUARD)
|
|
|
|
#ifndef KMEM_GUARD_DEPTH
|
|
|
|
#define KMEM_GUARD_DEPTH 0
|
|
|
|
#endif
|
2015-07-27 12:24:28 +03:00
|
|
|
struct kmem_guard {
|
|
|
|
u_int kg_depth;
|
|
|
|
intptr_t * kg_fifo;
|
|
|
|
u_int kg_rotor;
|
|
|
|
vmem_t * kg_vmem;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool kmem_guard_init(struct kmem_guard *, u_int, vmem_t *);
|
|
|
|
static void *kmem_guard_alloc(struct kmem_guard *, size_t, bool);
|
|
|
|
static void kmem_guard_free(struct kmem_guard *, size_t, void *);
|
|
|
|
|
2014-06-22 21:36:42 +04:00
|
|
|
int kmem_guard_depth = KMEM_GUARD_DEPTH;
|
2015-07-27 12:24:28 +03:00
|
|
|
static bool kmem_guard_enabled;
|
|
|
|
static struct kmem_guard kmem_guard;
|
2014-06-22 21:36:42 +04:00
|
|
|
#endif /* defined(KMEM_GUARD) */
|
|
|
|
|
2010-01-31 14:54:32 +03:00
|
|
|
CTASSERT(KM_SLEEP == PR_WAITOK);
|
|
|
|
CTASSERT(KM_NOSLEEP == PR_NOWAIT);
|
|
|
|
|
2012-07-21 15:45:04 +04:00
|
|
|
/*
|
|
|
|
* kmem_intr_alloc: allocate wired memory.
|
|
|
|
*/
|
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
void *
|
2013-04-22 17:22:25 +04:00
|
|
|
kmem_intr_alloc(size_t requested_size, km_flag_t kmflags)
|
2006-06-25 12:00:01 +04:00
|
|
|
{
|
2012-01-29 03:09:06 +04:00
|
|
|
size_t allocsz, index;
|
2013-04-22 17:22:25 +04:00
|
|
|
size_t size;
|
2012-01-27 23:48:38 +04:00
|
|
|
pool_cache_t pc;
|
|
|
|
uint8_t *p;
|
2006-06-25 12:00:01 +04:00
|
|
|
|
2013-04-22 17:22:25 +04:00
|
|
|
KASSERT(requested_size > 0);
|
2006-06-25 12:00:01 +04:00
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
#ifdef KMEM_GUARD
|
2015-07-27 12:24:28 +03:00
|
|
|
if (kmem_guard_enabled) {
|
|
|
|
return kmem_guard_alloc(&kmem_guard, requested_size,
|
2012-01-27 23:48:38 +04:00
|
|
|
(kmflags & KM_SLEEP) != 0);
|
2006-06-25 12:00:01 +04:00
|
|
|
}
|
2012-01-27 23:48:38 +04:00
|
|
|
#endif
|
2013-04-22 17:22:25 +04:00
|
|
|
size = kmem_roundup_size(requested_size);
|
2014-06-24 11:28:23 +04:00
|
|
|
allocsz = size + SIZE_SIZE;
|
|
|
|
|
|
|
|
#ifdef KMEM_REDZONE
|
|
|
|
if (size - requested_size < REDZONE_SIZE) {
|
2014-07-01 16:08:33 +04:00
|
|
|
/* If there isn't enough space in the padding, allocate
|
|
|
|
* one more memory chunk for the red zone. */
|
2014-06-25 20:35:12 +04:00
|
|
|
allocsz += kmem_roundup_size(REDZONE_SIZE);
|
2014-06-24 11:28:23 +04:00
|
|
|
}
|
|
|
|
#endif
|
2012-07-21 15:45:04 +04:00
|
|
|
|
|
|
|
if ((index = ((allocsz -1) >> KMEM_SHIFT))
|
|
|
|
< kmem_cache_maxidx) {
|
|
|
|
pc = kmem_cache[index];
|
|
|
|
} else if ((index = ((allocsz - 1) >> KMEM_BIG_SHIFT))
|
2014-06-25 20:05:22 +04:00
|
|
|
< kmem_cache_big_maxidx) {
|
2012-07-21 15:45:04 +04:00
|
|
|
pc = kmem_cache_big[index];
|
2013-04-21 06:44:15 +04:00
|
|
|
} else {
|
2012-01-29 03:09:06 +04:00
|
|
|
int ret = uvm_km_kmem_alloc(kmem_va_arena,
|
2012-04-01 21:02:46 +04:00
|
|
|
(vsize_t)round_page(size),
|
2012-01-27 23:48:38 +04:00
|
|
|
((kmflags & KM_SLEEP) ? VM_SLEEP : VM_NOSLEEP)
|
|
|
|
| VM_INSTANTFIT, (vmem_addr_t *)&p);
|
2012-07-21 15:45:04 +04:00
|
|
|
if (ret) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
FREECHECK_OUT(&kmem_freecheck, p);
|
|
|
|
return p;
|
2006-06-25 12:00:01 +04:00
|
|
|
}
|
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
p = pool_cache_get(pc, kmflags);
|
|
|
|
|
|
|
|
if (__predict_true(p != NULL)) {
|
2014-07-02 19:00:28 +04:00
|
|
|
kmem_poison_check(p, allocsz);
|
2012-01-27 23:48:38 +04:00
|
|
|
FREECHECK_OUT(&kmem_freecheck, p);
|
2013-04-22 17:22:25 +04:00
|
|
|
kmem_size_set(p, requested_size);
|
2014-06-24 11:28:23 +04:00
|
|
|
kmem_redzone_fill(p, requested_size + SIZE_SIZE);
|
2013-04-17 01:13:38 +04:00
|
|
|
|
|
|
|
return p + SIZE_SIZE;
|
2012-01-27 23:48:38 +04:00
|
|
|
}
|
2013-04-17 01:13:38 +04:00
|
|
|
return p;
|
2006-06-25 12:00:01 +04:00
|
|
|
}
|
|
|
|
|
2012-07-21 15:45:04 +04:00
|
|
|
/*
|
|
|
|
* kmem_intr_zalloc: allocate zeroed wired memory.
|
|
|
|
*/
|
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
void *
|
|
|
|
kmem_intr_zalloc(size_t size, km_flag_t kmflags)
|
2009-02-01 21:51:07 +03:00
|
|
|
{
|
2012-01-27 23:48:38 +04:00
|
|
|
void *p;
|
2009-02-01 21:51:07 +03:00
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
p = kmem_intr_alloc(size, kmflags);
|
|
|
|
if (p != NULL) {
|
|
|
|
memset(p, 0, size);
|
|
|
|
}
|
|
|
|
return p;
|
2009-02-01 21:51:07 +03:00
|
|
|
}
|
|
|
|
|
2012-07-21 15:45:04 +04:00
|
|
|
/*
|
|
|
|
* kmem_intr_free: free wired memory allocated by kmem_alloc.
|
|
|
|
*/
|
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
void
|
2013-04-22 17:22:25 +04:00
|
|
|
kmem_intr_free(void *p, size_t requested_size)
|
2009-02-01 21:51:07 +03:00
|
|
|
{
|
2012-01-29 03:09:06 +04:00
|
|
|
size_t allocsz, index;
|
2013-04-22 17:22:25 +04:00
|
|
|
size_t size;
|
2012-01-27 23:48:38 +04:00
|
|
|
pool_cache_t pc;
|
|
|
|
|
|
|
|
KASSERT(p != NULL);
|
2013-04-22 17:22:25 +04:00
|
|
|
KASSERT(requested_size > 0);
|
2009-02-01 21:51:07 +03:00
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
#ifdef KMEM_GUARD
|
2015-07-27 12:24:28 +03:00
|
|
|
if (kmem_guard_enabled) {
|
|
|
|
kmem_guard_free(&kmem_guard, requested_size, p);
|
2012-01-27 23:48:38 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2014-06-24 11:28:23 +04:00
|
|
|
|
2013-04-22 17:22:25 +04:00
|
|
|
size = kmem_roundup_size(requested_size);
|
2014-06-24 11:28:23 +04:00
|
|
|
allocsz = size + SIZE_SIZE;
|
|
|
|
|
|
|
|
#ifdef KMEM_REDZONE
|
|
|
|
if (size - requested_size < REDZONE_SIZE) {
|
2014-06-25 20:35:12 +04:00
|
|
|
allocsz += kmem_roundup_size(REDZONE_SIZE);
|
2014-06-24 11:28:23 +04:00
|
|
|
}
|
|
|
|
#endif
|
2012-07-21 15:45:04 +04:00
|
|
|
|
|
|
|
if ((index = ((allocsz -1) >> KMEM_SHIFT))
|
|
|
|
< kmem_cache_maxidx) {
|
|
|
|
pc = kmem_cache[index];
|
|
|
|
} else if ((index = ((allocsz - 1) >> KMEM_BIG_SHIFT))
|
2014-06-25 20:05:22 +04:00
|
|
|
< kmem_cache_big_maxidx) {
|
2012-07-21 15:45:04 +04:00
|
|
|
pc = kmem_cache_big[index];
|
|
|
|
} else {
|
|
|
|
FREECHECK_IN(&kmem_freecheck, p);
|
2012-01-27 23:48:38 +04:00
|
|
|
uvm_km_kmem_free(kmem_va_arena, (vaddr_t)p,
|
2012-04-01 21:02:46 +04:00
|
|
|
round_page(size));
|
2012-01-27 23:48:38 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-21 15:45:04 +04:00
|
|
|
p = (uint8_t *)p - SIZE_SIZE;
|
2013-04-22 17:22:25 +04:00
|
|
|
kmem_size_check(p, requested_size);
|
2014-06-24 11:28:23 +04:00
|
|
|
kmem_redzone_check(p, requested_size + SIZE_SIZE);
|
2012-01-27 23:48:38 +04:00
|
|
|
FREECHECK_IN(&kmem_freecheck, p);
|
2012-07-21 15:45:04 +04:00
|
|
|
LOCKDEBUG_MEM_CHECK(p, size);
|
2012-01-27 23:48:38 +04:00
|
|
|
kmem_poison_fill(p, allocsz);
|
|
|
|
|
|
|
|
pool_cache_put(pc, p);
|
2009-02-01 21:51:07 +03:00
|
|
|
}
|
|
|
|
|
2006-06-25 12:00:01 +04:00
|
|
|
/* ---- kmem API */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* kmem_alloc: allocate wired memory.
|
|
|
|
* => must not be called from interrupt context.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void *
|
|
|
|
kmem_alloc(size_t size, km_flag_t kmflags)
|
|
|
|
{
|
2012-01-29 03:09:06 +04:00
|
|
|
KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()),
|
|
|
|
"kmem(9) should not be used from the interrupt context");
|
2012-01-27 23:48:38 +04:00
|
|
|
return kmem_intr_alloc(size, kmflags);
|
2006-06-25 12:00:01 +04:00
|
|
|
}
|
|
|
|
|
2006-06-25 12:10:04 +04:00
|
|
|
/*
|
2012-01-27 23:48:38 +04:00
|
|
|
* kmem_zalloc: allocate zeroed wired memory.
|
2006-06-25 12:10:04 +04:00
|
|
|
* => must not be called from interrupt context.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void *
|
|
|
|
kmem_zalloc(size_t size, km_flag_t kmflags)
|
|
|
|
{
|
2012-01-29 03:09:06 +04:00
|
|
|
KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()),
|
|
|
|
"kmem(9) should not be used from the interrupt context");
|
2012-01-27 23:48:38 +04:00
|
|
|
return kmem_intr_zalloc(size, kmflags);
|
2006-06-25 12:10:04 +04:00
|
|
|
}
|
|
|
|
|
2006-06-25 12:00:01 +04:00
|
|
|
/*
|
|
|
|
* kmem_free: free wired memory allocated by kmem_alloc.
|
|
|
|
* => must not be called from interrupt context.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
kmem_free(void *p, size_t size)
|
|
|
|
{
|
2009-02-01 21:51:07 +03:00
|
|
|
KASSERT(!cpu_intr_p());
|
2009-03-29 14:51:53 +04:00
|
|
|
KASSERT(!cpu_softintr_p());
|
2012-01-27 23:48:38 +04:00
|
|
|
kmem_intr_free(p, size);
|
2006-06-25 12:00:01 +04:00
|
|
|
}
|
|
|
|
|
2012-07-21 15:45:04 +04:00
|
|
|
static size_t
|
2012-01-27 23:48:38 +04:00
|
|
|
kmem_create_caches(const struct kmem_cache_info *array,
|
2012-07-21 15:45:04 +04:00
|
|
|
pool_cache_t alloc_table[], size_t maxsize, int shift, int ipl)
|
2006-06-25 12:00:01 +04:00
|
|
|
{
|
2012-07-21 15:45:04 +04:00
|
|
|
size_t maxidx = 0;
|
|
|
|
size_t table_unit = (1 << shift);
|
2012-01-27 23:48:38 +04:00
|
|
|
size_t size = table_unit;
|
2009-02-01 21:51:07 +03:00
|
|
|
int i;
|
2006-06-25 12:00:01 +04:00
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
for (i = 0; array[i].kc_size != 0 ; i++) {
|
2012-01-29 03:09:06 +04:00
|
|
|
const char *name = array[i].kc_name;
|
2012-01-27 23:48:38 +04:00
|
|
|
size_t cache_size = array[i].kc_size;
|
2012-07-21 15:45:04 +04:00
|
|
|
struct pool_allocator *pa;
|
2012-01-29 03:09:06 +04:00
|
|
|
int flags = PR_NOALIGN;
|
|
|
|
pool_cache_t pc;
|
2012-01-27 23:48:38 +04:00
|
|
|
size_t align;
|
2009-03-29 14:51:53 +04:00
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
if ((cache_size & (CACHE_LINE_SIZE - 1)) == 0)
|
|
|
|
align = CACHE_LINE_SIZE;
|
|
|
|
else if ((cache_size & (PAGE_SIZE - 1)) == 0)
|
|
|
|
align = PAGE_SIZE;
|
|
|
|
else
|
|
|
|
align = KMEM_ALIGN;
|
2006-06-25 12:00:01 +04:00
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
if (cache_size < CACHE_LINE_SIZE)
|
|
|
|
flags |= PR_NOTOUCH;
|
2006-06-25 12:00:01 +04:00
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
/* check if we reached the requested size */
|
2012-07-21 15:45:04 +04:00
|
|
|
if (cache_size > maxsize || cache_size > PAGE_SIZE) {
|
2012-01-27 23:48:38 +04:00
|
|
|
break;
|
2012-01-29 03:09:06 +04:00
|
|
|
}
|
2012-07-21 15:45:04 +04:00
|
|
|
if ((cache_size >> shift) > maxidx) {
|
|
|
|
maxidx = cache_size >> shift;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((cache_size >> shift) > maxidx) {
|
|
|
|
maxidx = cache_size >> shift;
|
2012-01-29 03:09:06 +04:00
|
|
|
}
|
2006-06-25 12:00:01 +04:00
|
|
|
|
2012-07-21 15:45:04 +04:00
|
|
|
pa = &pool_allocator_kmem;
|
2012-01-27 23:48:38 +04:00
|
|
|
#if defined(KMEM_POISON)
|
|
|
|
pc = pool_cache_init(cache_size, align, 0, flags,
|
2013-04-22 17:13:20 +04:00
|
|
|
name, pa, ipl, kmem_poison_ctor,
|
2012-01-27 23:48:38 +04:00
|
|
|
NULL, (void *)cache_size);
|
|
|
|
#else /* defined(KMEM_POISON) */
|
|
|
|
pc = pool_cache_init(cache_size, align, 0, flags,
|
2012-07-21 15:45:04 +04:00
|
|
|
name, pa, ipl, NULL, NULL, NULL);
|
2012-01-27 23:48:38 +04:00
|
|
|
#endif /* defined(KMEM_POISON) */
|
2006-06-25 12:00:01 +04:00
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
while (size <= cache_size) {
|
2012-07-21 15:45:04 +04:00
|
|
|
alloc_table[(size - 1) >> shift] = pc;
|
2012-01-27 23:48:38 +04:00
|
|
|
size += table_unit;
|
|
|
|
}
|
2006-06-25 12:00:01 +04:00
|
|
|
}
|
2012-07-21 15:45:04 +04:00
|
|
|
return maxidx;
|
2006-06-25 12:00:01 +04:00
|
|
|
}
|
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
void
|
|
|
|
kmem_init(void)
|
2006-06-25 12:00:01 +04:00
|
|
|
{
|
2012-01-27 23:48:38 +04:00
|
|
|
#ifdef KMEM_GUARD
|
2015-07-27 12:24:28 +03:00
|
|
|
kmem_guard_enabled = kmem_guard_init(&kmem_guard, kmem_guard_depth,
|
2012-02-05 07:40:07 +04:00
|
|
|
kmem_va_arena);
|
2012-01-27 23:48:38 +04:00
|
|
|
#endif
|
2012-07-21 15:45:04 +04:00
|
|
|
kmem_cache_maxidx = kmem_create_caches(kmem_cache_sizes,
|
|
|
|
kmem_cache, KMEM_MAXSIZE, KMEM_SHIFT, IPL_VM);
|
2014-06-25 20:05:22 +04:00
|
|
|
kmem_cache_big_maxidx = kmem_create_caches(kmem_cache_big_sizes,
|
2012-07-21 15:45:04 +04:00
|
|
|
kmem_cache_big, PAGE_SIZE, KMEM_BIG_SHIFT, IPL_VM);
|
2006-06-25 12:00:01 +04:00
|
|
|
}
|
2006-07-08 10:01:53 +04:00
|
|
|
|
2012-01-27 23:48:38 +04:00
|
|
|
size_t
|
|
|
|
kmem_roundup_size(size_t size)
|
2006-08-20 17:08:11 +04:00
|
|
|
{
|
2012-01-27 23:48:38 +04:00
|
|
|
return (size + (KMEM_ALIGN - 1)) & ~(KMEM_ALIGN - 1);
|
2006-08-20 17:08:11 +04:00
|
|
|
}
|
|
|
|
|
2015-07-27 12:24:28 +03:00
|
|
|
/*
|
|
|
|
* Used to dynamically allocate string with kmem accordingly to format.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
kmem_asprintf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
int size __diagused, len;
|
|
|
|
va_list va;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
va_start(va, fmt);
|
|
|
|
len = vsnprintf(NULL, 0, fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
str = kmem_alloc(len + 1, KM_SLEEP);
|
|
|
|
|
|
|
|
va_start(va, fmt);
|
|
|
|
size = vsnprintf(str, len + 1, fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
KASSERT(size == len);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2014-06-24 11:28:23 +04:00
|
|
|
/* ------------------ DEBUG / DIAGNOSTIC ------------------ */
|
2006-07-08 10:01:53 +04:00
|
|
|
|
2014-06-24 11:28:23 +04:00
|
|
|
#if defined(KMEM_POISON) || defined(KMEM_REDZONE)
|
2006-07-08 10:01:53 +04:00
|
|
|
#if defined(_LP64)
|
2012-01-27 23:48:38 +04:00
|
|
|
#define PRIME 0x9e37fffffffc0000UL
|
2006-07-08 10:01:53 +04:00
|
|
|
#else /* defined(_LP64) */
|
2012-01-27 23:48:38 +04:00
|
|
|
#define PRIME 0x9e3779b1
|
2006-07-08 10:01:53 +04:00
|
|
|
#endif /* defined(_LP64) */
|
|
|
|
|
|
|
|
static inline uint8_t
|
2014-07-03 12:43:49 +04:00
|
|
|
kmem_pattern_generate(const void *p)
|
2006-07-08 10:01:53 +04:00
|
|
|
{
|
2012-01-27 23:48:38 +04:00
|
|
|
return (uint8_t)(((uintptr_t)p) * PRIME
|
|
|
|
>> ((sizeof(uintptr_t) - sizeof(uint8_t))) * CHAR_BIT);
|
|
|
|
}
|
2014-07-03 12:43:49 +04:00
|
|
|
#endif /* defined(KMEM_POISON) || defined(KMEM_REDZONE) */
|
2012-01-27 23:48:38 +04:00
|
|
|
|
2014-07-03 12:43:49 +04:00
|
|
|
#if defined(KMEM_POISON)
|
2012-01-27 23:48:38 +04:00
|
|
|
static int
|
|
|
|
kmem_poison_ctor(void *arg, void *obj, int flag)
|
|
|
|
{
|
|
|
|
size_t sz = (size_t)arg;
|
|
|
|
|
|
|
|
kmem_poison_fill(obj, sz);
|
|
|
|
|
|
|
|
return 0;
|
2006-07-08 10:01:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
kmem_poison_fill(void *p, size_t sz)
|
|
|
|
{
|
|
|
|
uint8_t *cp;
|
|
|
|
const uint8_t *ep;
|
|
|
|
|
|
|
|
cp = p;
|
|
|
|
ep = cp + sz;
|
|
|
|
while (cp < ep) {
|
2014-07-03 12:43:49 +04:00
|
|
|
*cp = kmem_pattern_generate(cp);
|
2006-07-08 10:01:53 +04:00
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
kmem_poison_check(void *p, size_t sz)
|
|
|
|
{
|
|
|
|
uint8_t *cp;
|
|
|
|
const uint8_t *ep;
|
|
|
|
|
|
|
|
cp = p;
|
|
|
|
ep = cp + sz;
|
|
|
|
while (cp < ep) {
|
2014-07-03 12:43:49 +04:00
|
|
|
const uint8_t expected = kmem_pattern_generate(cp);
|
2006-07-08 10:01:53 +04:00
|
|
|
|
|
|
|
if (*cp != expected) {
|
|
|
|
panic("%s: %p: 0x%02x != 0x%02x\n",
|
2012-01-27 23:48:38 +04:00
|
|
|
__func__, cp, *cp, expected);
|
2006-07-08 10:01:53 +04:00
|
|
|
}
|
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
}
|
2008-02-09 15:56:20 +03:00
|
|
|
#endif /* defined(KMEM_POISON) */
|
2009-02-01 21:51:07 +03:00
|
|
|
|
|
|
|
#if defined(KMEM_SIZE)
|
|
|
|
static void
|
|
|
|
kmem_size_set(void *p, size_t sz)
|
|
|
|
{
|
2014-07-01 16:08:33 +04:00
|
|
|
struct kmem_header *hd;
|
|
|
|
hd = (struct kmem_header *)p;
|
|
|
|
hd->size = sz;
|
2009-02-01 21:51:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-01-27 23:48:38 +04:00
|
|
|
kmem_size_check(void *p, size_t sz)
|
2009-02-01 21:51:07 +03:00
|
|
|
{
|
2014-07-01 16:08:33 +04:00
|
|
|
struct kmem_header *hd;
|
|
|
|
size_t hsz;
|
2009-02-01 21:51:07 +03:00
|
|
|
|
2014-07-01 16:08:33 +04:00
|
|
|
hd = (struct kmem_header *)p;
|
|
|
|
hsz = hd->size;
|
|
|
|
|
|
|
|
if (hsz != sz) {
|
2009-02-01 21:51:07 +03:00
|
|
|
panic("kmem_free(%p, %zu) != allocated size %zu",
|
2014-07-01 16:08:33 +04:00
|
|
|
(const uint8_t *)p + SIZE_SIZE, sz, hsz);
|
2009-02-01 21:51:07 +03:00
|
|
|
}
|
|
|
|
}
|
2014-06-24 11:28:23 +04:00
|
|
|
#endif /* defined(KMEM_SIZE) */
|
|
|
|
|
|
|
|
#if defined(KMEM_REDZONE)
|
2014-07-03 12:43:49 +04:00
|
|
|
#define STATIC_BYTE 0xFE
|
|
|
|
CTASSERT(REDZONE_SIZE > 1);
|
2014-06-24 11:28:23 +04:00
|
|
|
static void
|
|
|
|
kmem_redzone_fill(void *p, size_t sz)
|
|
|
|
{
|
2014-07-03 12:43:49 +04:00
|
|
|
uint8_t *cp, pat;
|
2014-06-24 11:28:23 +04:00
|
|
|
const uint8_t *ep;
|
|
|
|
|
|
|
|
cp = (uint8_t *)p + sz;
|
|
|
|
ep = cp + REDZONE_SIZE;
|
2014-07-03 12:43:49 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We really don't want the first byte of the red zone to be '\0';
|
|
|
|
* an off-by-one in a string may not be properly detected.
|
|
|
|
*/
|
|
|
|
pat = kmem_pattern_generate(cp);
|
|
|
|
*cp = (pat == '\0') ? STATIC_BYTE: pat;
|
|
|
|
cp++;
|
|
|
|
|
2014-06-24 11:28:23 +04:00
|
|
|
while (cp < ep) {
|
2014-07-03 12:43:49 +04:00
|
|
|
*cp = kmem_pattern_generate(cp);
|
2014-06-24 11:28:23 +04:00
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
kmem_redzone_check(void *p, size_t sz)
|
|
|
|
{
|
2014-07-03 12:43:49 +04:00
|
|
|
uint8_t *cp, pat, expected;
|
2014-06-24 11:28:23 +04:00
|
|
|
const uint8_t *ep;
|
|
|
|
|
|
|
|
cp = (uint8_t *)p + sz;
|
2014-07-01 16:08:33 +04:00
|
|
|
ep = cp + REDZONE_SIZE;
|
2014-06-24 11:28:23 +04:00
|
|
|
|
2014-07-03 12:43:49 +04:00
|
|
|
pat = kmem_pattern_generate(cp);
|
|
|
|
expected = (pat == '\0') ? STATIC_BYTE: pat;
|
|
|
|
if (expected != *cp) {
|
|
|
|
panic("%s: %p: 0x%02x != 0x%02x\n",
|
|
|
|
__func__, cp, *cp, expected);
|
|
|
|
}
|
|
|
|
cp++;
|
|
|
|
|
|
|
|
while (cp < ep) {
|
|
|
|
expected = kmem_pattern_generate(cp);
|
2014-06-24 11:28:23 +04:00
|
|
|
if (*cp != expected) {
|
|
|
|
panic("%s: %p: 0x%02x != 0x%02x\n",
|
|
|
|
__func__, cp, *cp, expected);
|
|
|
|
}
|
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* defined(KMEM_REDZONE) */
|
|
|
|
|
2010-02-12 02:13:46 +03:00
|
|
|
|
2015-07-27 12:24:28 +03:00
|
|
|
#if defined(KMEM_GUARD)
|
2010-02-12 02:13:46 +03:00
|
|
|
/*
|
2015-07-27 12:24:28 +03:00
|
|
|
* The ultimate memory allocator for debugging, baby. It tries to catch:
|
|
|
|
*
|
|
|
|
* 1. Overflow, in realtime. A guard page sits immediately after the
|
|
|
|
* requested area; a read/write overflow therefore triggers a page
|
|
|
|
* fault.
|
|
|
|
* 2. Invalid pointer/size passed, at free. A kmem_header structure sits
|
|
|
|
* just before the requested area, and holds the allocated size. Any
|
|
|
|
* difference with what is given at free triggers a panic.
|
|
|
|
* 3. Underflow, at free. If an underflow occurs, the kmem header will be
|
|
|
|
* modified, and 2. will trigger a panic.
|
|
|
|
* 4. Use-after-free. When freeing, the memory is unmapped, and depending
|
|
|
|
* on the value of kmem_guard_depth, the kernel will more or less delay
|
|
|
|
* the recycling of that memory. Which means that any ulterior read/write
|
|
|
|
* access to the memory will trigger a page fault, given it hasn't been
|
|
|
|
* recycled yet.
|
2010-02-12 02:13:46 +03:00
|
|
|
*/
|
2015-07-27 12:24:28 +03:00
|
|
|
|
|
|
|
#include <sys/atomic.h>
|
|
|
|
#include <uvm/uvm.h>
|
|
|
|
|
|
|
|
static bool
|
|
|
|
kmem_guard_init(struct kmem_guard *kg, u_int depth, vmem_t *vm)
|
2010-02-12 02:13:46 +03:00
|
|
|
{
|
2015-07-27 12:24:28 +03:00
|
|
|
vaddr_t va;
|
2013-04-21 06:44:15 +04:00
|
|
|
|
2015-07-27 12:24:28 +03:00
|
|
|
/* If not enabled, we have nothing to do. */
|
|
|
|
if (depth == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
depth = roundup(depth, PAGE_SIZE / sizeof(void *));
|
|
|
|
KASSERT(depth != 0);
|
2010-02-12 02:13:46 +03:00
|
|
|
|
2015-07-27 12:24:28 +03:00
|
|
|
/*
|
|
|
|
* Allocate fifo.
|
|
|
|
*/
|
|
|
|
va = uvm_km_alloc(kernel_map, depth * sizeof(void *), PAGE_SIZE,
|
|
|
|
UVM_KMF_WIRED | UVM_KMF_ZERO);
|
|
|
|
if (va == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-02-12 02:13:46 +03:00
|
|
|
|
2015-07-27 12:24:28 +03:00
|
|
|
/*
|
|
|
|
* Init object.
|
|
|
|
*/
|
|
|
|
kg->kg_vmem = vm;
|
|
|
|
kg->kg_fifo = (void *)va;
|
|
|
|
kg->kg_depth = depth;
|
|
|
|
kg->kg_rotor = 0;
|
2011-11-21 02:58:31 +04:00
|
|
|
|
2015-07-27 12:24:28 +03:00
|
|
|
printf("kmem_guard(%p): depth %d\n", kg, depth);
|
|
|
|
return true;
|
|
|
|
}
|
2010-02-12 02:13:46 +03:00
|
|
|
|
2015-07-27 12:24:28 +03:00
|
|
|
static void *
|
|
|
|
kmem_guard_alloc(struct kmem_guard *kg, size_t requested_size, bool waitok)
|
|
|
|
{
|
|
|
|
struct vm_page *pg;
|
|
|
|
vm_flag_t flags;
|
|
|
|
vmem_addr_t va;
|
|
|
|
vaddr_t loopva;
|
|
|
|
vsize_t loopsize;
|
|
|
|
size_t size;
|
|
|
|
void **p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute the size: take the kmem header into account, and add a guard
|
|
|
|
* page at the end.
|
|
|
|
*/
|
|
|
|
size = round_page(requested_size + SIZE_SIZE) + PAGE_SIZE;
|
|
|
|
|
|
|
|
/* Allocate pages of kernel VA, but do not map anything in yet. */
|
|
|
|
flags = VM_BESTFIT | (waitok ? VM_SLEEP : VM_NOSLEEP);
|
|
|
|
if (vmem_alloc(kg->kg_vmem, size, flags, &va) != 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
loopva = va;
|
|
|
|
loopsize = size - PAGE_SIZE;
|
|
|
|
|
|
|
|
while (loopsize) {
|
|
|
|
pg = uvm_pagealloc(NULL, loopva, NULL, 0);
|
|
|
|
if (__predict_false(pg == NULL)) {
|
|
|
|
if (waitok) {
|
|
|
|
uvm_wait("kmem_guard");
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
uvm_km_pgremove_intrsafe(kernel_map, va,
|
|
|
|
va + size);
|
|
|
|
vmem_free(kg->kg_vmem, va, size);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pg->flags &= ~PG_BUSY; /* new page */
|
|
|
|
UVM_PAGE_OWN(pg, NULL);
|
|
|
|
pmap_kenter_pa(loopva, VM_PAGE_TO_PHYS(pg),
|
|
|
|
VM_PROT_READ|VM_PROT_WRITE, PMAP_KMPAGE);
|
|
|
|
|
|
|
|
loopva += PAGE_SIZE;
|
|
|
|
loopsize -= PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
pmap_update(pmap_kernel());
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Offset the returned pointer so that the unmapped guard page sits
|
|
|
|
* immediately after the returned object.
|
|
|
|
*/
|
|
|
|
p = (void **)((va + (size - PAGE_SIZE) - requested_size) & ~(uintptr_t)ALIGNBYTES);
|
|
|
|
kmem_size_set((uint8_t *)p - SIZE_SIZE, requested_size);
|
|
|
|
return (void *)p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
kmem_guard_free(struct kmem_guard *kg, size_t requested_size, void *p)
|
|
|
|
{
|
|
|
|
vaddr_t va;
|
|
|
|
u_int rotor;
|
|
|
|
size_t size;
|
|
|
|
uint8_t *ptr;
|
|
|
|
|
|
|
|
ptr = (uint8_t *)p - SIZE_SIZE;
|
|
|
|
kmem_size_check(ptr, requested_size);
|
|
|
|
va = trunc_page((vaddr_t)ptr);
|
|
|
|
size = round_page(requested_size + SIZE_SIZE) + PAGE_SIZE;
|
|
|
|
|
|
|
|
KASSERT(pmap_extract(pmap_kernel(), va, NULL));
|
|
|
|
KASSERT(!pmap_extract(pmap_kernel(), va + (size - PAGE_SIZE), NULL));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unmap and free the pages. The last one is never allocated.
|
|
|
|
*/
|
|
|
|
uvm_km_pgremove_intrsafe(kernel_map, va, va + size);
|
|
|
|
pmap_update(pmap_kernel());
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
* XXX: Here, we need to atomically register the va and its size in the
|
|
|
|
* fifo.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Put the VA allocation into the list and swap an old one out to free.
|
|
|
|
* This behaves mostly like a fifo.
|
|
|
|
*/
|
|
|
|
rotor = atomic_inc_uint_nv(&kg->kg_rotor) % kg->kg_depth;
|
|
|
|
va = (vaddr_t)atomic_swap_ptr(&kg->kg_fifo[rotor], (void *)va);
|
|
|
|
if (va != 0) {
|
|
|
|
vmem_free(kg->kg_vmem, va, size);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
(void)rotor;
|
|
|
|
vmem_free(kg->kg_vmem, va, size);
|
|
|
|
#endif
|
2010-02-12 02:13:46 +03:00
|
|
|
}
|
2015-07-27 12:24:28 +03:00
|
|
|
|
|
|
|
#endif /* defined(KMEM_GUARD) */
|