Index: head/sys/kern/kern_malloc.c =================================================================== --- head/sys/kern/kern_malloc.c (revision 132378) +++ head/sys/kern/kern_malloc.c (revision 132379) @@ -1,681 +1,701 @@ /* * Copyright (c) 1987, 1991, 1993 * The Regents of the University of California. 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. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. * * @(#)kern_malloc.c 8.3 (Berkeley) 1/4/94 */ #include __FBSDID("$FreeBSD$"); #include "opt_vm.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if defined(INVARIANTS) && defined(__i386__) #include #endif /* * When realloc() is called, if the new size is sufficiently smaller than * the old size, realloc() will allocate a new, smaller block to avoid * wasting memory. 'Sufficiently smaller' is defined as: newsize <= * oldsize / 2^n, where REALLOC_FRACTION defines the value of 'n'. */ #ifndef REALLOC_FRACTION #define REALLOC_FRACTION 1 /* new block if <= half the size */ #endif MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches"); MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory"); MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers"); MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options"); MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery"); static void kmeminit(void *); SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_FIRST, kmeminit, NULL) static MALLOC_DEFINE(M_FREE, "free", "should be on free list"); static struct malloc_type *kmemstatistics; static char *kmembase; static char *kmemlimit; #define KMEM_ZSHIFT 4 #define KMEM_ZBASE 16 #define KMEM_ZMASK (KMEM_ZBASE - 1) #define KMEM_ZMAX PAGE_SIZE #define KMEM_ZSIZE (KMEM_ZMAX >> KMEM_ZSHIFT) static u_int8_t kmemsize[KMEM_ZSIZE + 1]; /* These won't be powers of two for long */ struct { int kz_size; char *kz_name; uma_zone_t kz_zone; } kmemzones[] = { {16, "16", NULL}, {32, "32", NULL}, {64, "64", NULL}, {128, "128", NULL}, {256, "256", NULL}, {512, "512", NULL}, {1024, "1024", NULL}, {2048, "2048", NULL}, {4096, "4096", NULL}, #if PAGE_SIZE > 4096 {8192, "8192", NULL}, #if PAGE_SIZE > 8192 {16384, "16384", NULL}, #if PAGE_SIZE > 16384 {32768, "32768", NULL}, #if PAGE_SIZE > 32768 {65536, "65536", NULL}, #if PAGE_SIZE > 65536 #error "Unsupported PAGE_SIZE" #endif /* 65536 */ #endif /* 32768 */ #endif /* 16384 */ #endif /* 8192 */ #endif /* 4096 */ {0, NULL}, }; u_int vm_kmem_size; SYSCTL_UINT(_vm, OID_AUTO, kmem_size, CTLFLAG_RD, &vm_kmem_size, 0, "Size of kernel memory"); /* * The malloc_mtx protects the kmemstatistics linked list. */ struct mtx malloc_mtx; #ifdef MALLOC_PROFILE uint64_t krequests[KMEM_ZSIZE + 1]; static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS); #endif static int sysctl_kern_malloc(SYSCTL_HANDLER_ARGS); /* time_uptime of last malloc(9) failure */ static time_t t_malloc_fail; #ifdef MALLOC_MAKE_FAILURES /* * Causes malloc failures every (n) mallocs with M_NOWAIT. If set to 0, * doesn't cause failures. */ SYSCTL_NODE(_debug, OID_AUTO, malloc, CTLFLAG_RD, 0, "Kernel malloc debugging options"); static int malloc_failure_rate; static int malloc_nowait_count; static int malloc_failure_count; SYSCTL_INT(_debug_malloc, OID_AUTO, failure_rate, CTLFLAG_RW, &malloc_failure_rate, 0, "Every (n) mallocs with M_NOWAIT will fail"); TUNABLE_INT("debug.malloc.failure_rate", &malloc_failure_rate); SYSCTL_INT(_debug_malloc, OID_AUTO, failure_count, CTLFLAG_RD, &malloc_failure_count, 0, "Number of imposed M_NOWAIT malloc failures"); #endif int malloc_last_fail(void) { return (time_uptime - t_malloc_fail); } /* + * Add this to the informational malloc_type bucket. + */ +static void +malloc_type_zone_allocated(struct malloc_type *ksp, unsigned long size, + int zindx) +{ + mtx_lock(&ksp->ks_mtx); + ksp->ks_calls++; + if (zindx != -1) + ksp->ks_size |= 1 << zindx; + if (size != 0) { + ksp->ks_memuse += size; + ksp->ks_inuse++; + if (ksp->ks_memuse > ksp->ks_maxused) + ksp->ks_maxused = ksp->ks_memuse; + } + mtx_unlock(&ksp->ks_mtx); +} + +void +malloc_type_allocated(struct malloc_type *ksp, unsigned long size) +{ + malloc_type_zone_allocated(ksp, size, -1); +} + +/* + * Remove this allocation from the informational malloc_type bucket. + */ +void +malloc_type_freed(struct malloc_type *ksp, unsigned long size) +{ + mtx_lock(&ksp->ks_mtx); + KASSERT(size <= ksp->ks_memuse, + ("malloc(9)/free(9) confusion.\n%s", + "Probably freeing with wrong type, but maybe not here.")); + ksp->ks_memuse -= size; + ksp->ks_inuse--; + mtx_unlock(&ksp->ks_mtx); +} + +/* * malloc: * * Allocate a block of memory. * * If M_NOWAIT is set, this routine will not block and return NULL if * the allocation fails. */ void * malloc(size, type, flags) unsigned long size; struct malloc_type *type; int flags; { int indx; caddr_t va; uma_zone_t zone; uma_keg_t keg; #ifdef DIAGNOSTIC unsigned long osize = size; #endif - register struct malloc_type *ksp = type; #ifdef INVARIANTS /* * To make sure that WAITOK or NOWAIT is set, but not more than * one, and check against the API botches that are common. */ indx = flags & (M_WAITOK | M_NOWAIT | M_DONTWAIT | M_TRYWAIT); if (indx != M_NOWAIT && indx != M_WAITOK) { static struct timeval lasterr; static int curerr, once; if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) { printf("Bad malloc flags: %x\n", indx); kdb_backtrace(); flags |= M_WAITOK; once++; } } #endif #if 0 if (size == 0) kdb_enter("zero size malloc"); #endif #ifdef MALLOC_MAKE_FAILURES if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) { atomic_add_int(&malloc_nowait_count, 1); if ((malloc_nowait_count % malloc_failure_rate) == 0) { atomic_add_int(&malloc_failure_count, 1); t_malloc_fail = time_uptime; return (NULL); } } #endif if (flags & M_WAITOK) KASSERT(curthread->td_intr_nesting_level == 0, ("malloc(M_WAITOK) in interrupt context")); if (size <= KMEM_ZMAX) { if (size & KMEM_ZMASK) size = (size & ~KMEM_ZMASK) + KMEM_ZBASE; indx = kmemsize[size >> KMEM_ZSHIFT]; zone = kmemzones[indx].kz_zone; keg = zone->uz_keg; #ifdef MALLOC_PROFILE krequests[size >> KMEM_ZSHIFT]++; #endif va = uma_zalloc(zone, flags); - mtx_lock(&ksp->ks_mtx); - if (va == NULL) - goto out; - - ksp->ks_size |= 1 << indx; - size = keg->uk_size; + if (va != NULL) + size = keg->uk_size; + malloc_type_zone_allocated(type, va == NULL ? 0 : size, indx); } else { size = roundup(size, PAGE_SIZE); zone = NULL; keg = NULL; va = uma_large_malloc(size, flags); - mtx_lock(&ksp->ks_mtx); - if (va == NULL) - goto out; + malloc_type_allocated(type, va == NULL ? 0 : size); } - ksp->ks_memuse += size; - ksp->ks_inuse++; -out: - ksp->ks_calls++; - if (ksp->ks_memuse > ksp->ks_maxused) - ksp->ks_maxused = ksp->ks_memuse; - - mtx_unlock(&ksp->ks_mtx); if (flags & M_WAITOK) KASSERT(va != NULL, ("malloc(M_WAITOK) returned NULL")); else if (va == NULL) t_malloc_fail = time_uptime; #ifdef DIAGNOSTIC if (va != NULL && !(flags & M_ZERO)) { memset(va, 0x70, osize); } #endif return ((void *) va); } /* * free: * * Free a block of memory allocated by malloc. * * This routine may not block. */ void free(addr, type) void *addr; struct malloc_type *type; { - register struct malloc_type *ksp = type; uma_slab_t slab; u_long size; /* free(NULL, ...) does nothing */ if (addr == NULL) return; - KASSERT(ksp->ks_memuse > 0, + KASSERT(type->ks_memuse > 0, ("malloc(9)/free(9) confusion.\n%s", "Probably freeing with wrong type, but maybe not here.")); size = 0; slab = vtoslab((vm_offset_t)addr & (~UMA_SLAB_MASK)); if (slab == NULL) panic("free: address %p(%p) has not been allocated.\n", addr, (void *)((u_long)addr & (~UMA_SLAB_MASK))); if (!(slab->us_flags & UMA_SLAB_MALLOC)) { #ifdef INVARIANTS struct malloc_type **mtp = addr; #endif size = slab->us_keg->uk_size; #ifdef INVARIANTS /* * Cache a pointer to the malloc_type that most recently freed * this memory here. This way we know who is most likely to * have stepped on it later. * * This code assumes that size is a multiple of 8 bytes for * 64 bit machines */ mtp = (struct malloc_type **) ((unsigned long)mtp & ~UMA_ALIGN_PTR); mtp += (size - sizeof(struct malloc_type *)) / sizeof(struct malloc_type *); *mtp = type; #endif uma_zfree_arg(LIST_FIRST(&slab->us_keg->uk_zones), addr, slab); } else { size = slab->us_size; uma_large_free(slab); } - mtx_lock(&ksp->ks_mtx); - KASSERT(size <= ksp->ks_memuse, - ("malloc(9)/free(9) confusion.\n%s", - "Probably freeing with wrong type, but maybe not here.")); - ksp->ks_memuse -= size; - ksp->ks_inuse--; - mtx_unlock(&ksp->ks_mtx); + malloc_type_freed(type, size); } /* * realloc: change the size of a memory block */ void * realloc(addr, size, type, flags) void *addr; unsigned long size; struct malloc_type *type; int flags; { uma_slab_t slab; unsigned long alloc; void *newaddr; /* realloc(NULL, ...) is equivalent to malloc(...) */ if (addr == NULL) return (malloc(size, type, flags)); slab = vtoslab((vm_offset_t)addr & ~(UMA_SLAB_MASK)); /* Sanity check */ KASSERT(slab != NULL, ("realloc: address %p out of range", (void *)addr)); /* Get the size of the original block */ if (slab->us_keg) alloc = slab->us_keg->uk_size; else alloc = slab->us_size; /* Reuse the original block if appropriate */ if (size <= alloc && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE)) return (addr); /* Allocate a new, bigger (or smaller) block */ if ((newaddr = malloc(size, type, flags)) == NULL) return (NULL); /* Copy over original contents */ bcopy(addr, newaddr, min(size, alloc)); free(addr, type); return (newaddr); } /* * reallocf: same as realloc() but free memory on failure. */ void * reallocf(addr, size, type, flags) void *addr; unsigned long size; struct malloc_type *type; int flags; { void *mem; if ((mem = realloc(addr, size, type, flags)) == NULL) free(addr, type); return (mem); } /* * Initialize the kernel memory allocator */ /* ARGSUSED*/ static void kmeminit(dummy) void *dummy; { u_int8_t indx; u_long mem_size; int i; mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF); /* * Try to auto-tune the kernel memory size, so that it is * more applicable for a wider range of machine sizes. * On an X86, a VM_KMEM_SIZE_SCALE value of 4 is good, while * a VM_KMEM_SIZE of 12MB is a fair compromise. The * VM_KMEM_SIZE_MAX is dependent on the maximum KVA space * available, and on an X86 with a total KVA space of 256MB, * try to keep VM_KMEM_SIZE_MAX at 80MB or below. * * Note that the kmem_map is also used by the zone allocator, * so make sure that there is enough space. */ vm_kmem_size = VM_KMEM_SIZE + nmbclusters * PAGE_SIZE; mem_size = cnt.v_page_count; #if defined(VM_KMEM_SIZE_SCALE) if ((mem_size / VM_KMEM_SIZE_SCALE) > (vm_kmem_size / PAGE_SIZE)) vm_kmem_size = (mem_size / VM_KMEM_SIZE_SCALE) * PAGE_SIZE; #endif #if defined(VM_KMEM_SIZE_MAX) if (vm_kmem_size >= VM_KMEM_SIZE_MAX) vm_kmem_size = VM_KMEM_SIZE_MAX; #endif /* Allow final override from the kernel environment */ #ifndef BURN_BRIDGES if (TUNABLE_INT_FETCH("kern.vm.kmem.size", &vm_kmem_size) != 0) printf("kern.vm.kmem.size is now called vm.kmem_size!\n"); #endif TUNABLE_INT_FETCH("vm.kmem_size", &vm_kmem_size); /* * Limit kmem virtual size to twice the physical memory. * This allows for kmem map sparseness, but limits the size * to something sane. Be careful to not overflow the 32bit * ints while doing the check. */ if (((vm_kmem_size / 2) / PAGE_SIZE) > cnt.v_page_count) vm_kmem_size = 2 * cnt.v_page_count * PAGE_SIZE; /* * Tune settings based on the kernel map's size at this time. */ init_param3(vm_kmem_size / PAGE_SIZE); kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase, (vm_offset_t *)&kmemlimit, vm_kmem_size); kmem_map->system_map = 1; uma_startup2(); for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) { int size = kmemzones[indx].kz_size; char *name = kmemzones[indx].kz_name; kmemzones[indx].kz_zone = uma_zcreate(name, size, #ifdef INVARIANTS mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini, #else NULL, NULL, NULL, NULL, #endif UMA_ALIGN_PTR, UMA_ZONE_MALLOC); for (;i <= size; i+= KMEM_ZBASE) kmemsize[i >> KMEM_ZSHIFT] = indx; } } void malloc_init(data) void *data; { struct malloc_type *type = (struct malloc_type *)data; mtx_lock(&malloc_mtx); if (type->ks_magic != M_MAGIC) panic("malloc type lacks magic"); if (cnt.v_page_count == 0) panic("malloc_init not allowed before vm init"); if (type->ks_next != NULL) return; type->ks_next = kmemstatistics; kmemstatistics = type; mtx_init(&type->ks_mtx, type->ks_shortdesc, "Malloc Stats", MTX_DEF); mtx_unlock(&malloc_mtx); } void malloc_uninit(data) void *data; { struct malloc_type *type = (struct malloc_type *)data; struct malloc_type *t; mtx_lock(&malloc_mtx); mtx_lock(&type->ks_mtx); if (type->ks_magic != M_MAGIC) panic("malloc type lacks magic"); if (cnt.v_page_count == 0) panic("malloc_uninit not allowed before vm init"); if (type == kmemstatistics) kmemstatistics = type->ks_next; else { for (t = kmemstatistics; t->ks_next != NULL; t = t->ks_next) { if (t->ks_next == type) { t->ks_next = type->ks_next; break; } } } type->ks_next = NULL; mtx_destroy(&type->ks_mtx); mtx_unlock(&malloc_mtx); } static int sysctl_kern_malloc(SYSCTL_HANDLER_ARGS) { struct malloc_type *type; int linesize = 128; int curline; int bufsize; int first; int error; char *buf; char *p; int cnt; int len; int i; cnt = 0; mtx_lock(&malloc_mtx); for (type = kmemstatistics; type != NULL; type = type->ks_next) cnt++; mtx_unlock(&malloc_mtx); bufsize = linesize * (cnt + 1); p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO); mtx_lock(&malloc_mtx); len = snprintf(p, linesize, "\n Type InUse MemUse HighUse Requests Size(s)\n"); p += len; for (type = kmemstatistics; cnt != 0 && type != NULL; type = type->ks_next, cnt--) { if (type->ks_calls == 0) continue; curline = linesize - 2; /* Leave room for the \n */ len = snprintf(p, curline, "%13s%6lu%6luK%7luK%9llu", type->ks_shortdesc, type->ks_inuse, (type->ks_memuse + 1023) / 1024, (type->ks_maxused + 1023) / 1024, (long long unsigned)type->ks_calls); curline -= len; p += len; first = 1; for (i = 0; i < sizeof(kmemzones) / sizeof(kmemzones[0]) - 1; i++) { if (type->ks_size & (1 << i)) { if (first) len = snprintf(p, curline, " "); else len = snprintf(p, curline, ","); curline -= len; p += len; len = snprintf(p, curline, "%s", kmemzones[i].kz_name); curline -= len; p += len; first = 0; } } len = snprintf(p, 2, "\n"); p += len; } mtx_unlock(&malloc_mtx); error = SYSCTL_OUT(req, buf, p - buf); free(buf, M_TEMP); return (error); } SYSCTL_OID(_kern, OID_AUTO, malloc, CTLTYPE_STRING|CTLFLAG_RD, NULL, 0, sysctl_kern_malloc, "A", "Malloc Stats"); #ifdef MALLOC_PROFILE static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS) { int linesize = 64; uint64_t count; uint64_t waste; uint64_t mem; int bufsize; int error; char *buf; int rsize; int size; char *p; int len; int i; bufsize = linesize * (KMEM_ZSIZE + 1); bufsize += 128; /* For the stats line */ bufsize += 128; /* For the banner line */ waste = 0; mem = 0; p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO); len = snprintf(p, bufsize, "\n Size Requests Real Size\n"); bufsize -= len; p += len; for (i = 0; i < KMEM_ZSIZE; i++) { size = i << KMEM_ZSHIFT; rsize = kmemzones[kmemsize[i]].kz_size; count = (long long unsigned)krequests[i]; len = snprintf(p, bufsize, "%6d%28llu%11d\n", size, (unsigned long long)count, rsize); bufsize -= len; p += len; if ((rsize * count) > (size * count)) waste += (rsize * count) - (size * count); mem += (rsize * count); } len = snprintf(p, bufsize, "\nTotal memory used:\t%30llu\nTotal Memory wasted:\t%30llu\n", (unsigned long long)mem, (unsigned long long)waste); p += len; error = SYSCTL_OUT(req, buf, p - buf); free(buf, M_TEMP); return (error); } SYSCTL_OID(_kern, OID_AUTO, mprof, CTLTYPE_STRING|CTLFLAG_RD, NULL, 0, sysctl_kern_mprof, "A", "Malloc Profiling"); #endif /* MALLOC_PROFILE */ Index: head/sys/sys/malloc.h =================================================================== --- head/sys/sys/malloc.h (revision 132378) +++ head/sys/sys/malloc.h (revision 132379) @@ -1,115 +1,117 @@ /* * Copyright (c) 1987, 1993 * The Regents of the University of California. 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. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. * * @(#)malloc.h 8.5 (Berkeley) 5/3/95 * $FreeBSD$ */ #ifndef _SYS_MALLOC_H_ #define _SYS_MALLOC_H_ #include #include #include #define MINALLOCSIZE UMA_SMALLEST_UNIT /* * flags to malloc. */ #define M_NOWAIT 0x0001 /* do not block */ #define M_WAITOK 0x0002 /* ok to block */ #define M_ZERO 0x0100 /* bzero the allocation */ #define M_NOVM 0x0200 /* don't ask VM for pages */ #define M_USE_RESERVE 0x0400 /* can alloc out of reserve memory */ #define M_MAGIC 877983977 /* time when first defined :-) */ struct malloc_type { struct malloc_type *ks_next; /* next in list */ u_long ks_memuse; /* total memory held in bytes */ u_long ks_size; /* sizes of this thing that are allocated */ u_long ks_inuse; /* # of packets of this type currently in use */ uint64_t ks_calls; /* total packets of this type ever allocated */ u_long ks_maxused; /* maximum number ever used */ u_long ks_magic; /* if it's not magic, don't touch it */ const char *ks_shortdesc; /* short description */ struct mtx ks_mtx; /* lock for stats */ }; #ifdef _KERNEL #define MALLOC_DEFINE(type, shortdesc, longdesc) \ struct malloc_type type[1] = { \ { NULL, 0, 0, 0, 0, 0, M_MAGIC, shortdesc, {} } \ }; \ SYSINIT(type##_init, SI_SUB_KMEM, SI_ORDER_SECOND, malloc_init, type); \ SYSUNINIT(type##_uninit, SI_SUB_KMEM, SI_ORDER_ANY, malloc_uninit, type) #define MALLOC_DECLARE(type) \ extern struct malloc_type type[1] MALLOC_DECLARE(M_CACHE); MALLOC_DECLARE(M_DEVBUF); MALLOC_DECLARE(M_TEMP); MALLOC_DECLARE(M_IP6OPT); /* for INET6 */ MALLOC_DECLARE(M_IP6NDP); /* for INET6 */ /* * Deprecated macro versions of not-quite-malloc() and free(). */ #define MALLOC(space, cast, size, type, flags) \ ((space) = (cast)malloc((u_long)(size), (type), (flags))) #define FREE(addr, type) free((addr), (type)) /* * XXX this should be declared in , but that tends to fail * because is included in a header before the source file * has a chance to include to get MALLOC_DECLARE() defined. */ MALLOC_DECLARE(M_IOV); extern struct mtx malloc_mtx; /* XXX struct malloc_type is unused for contig*(). */ void contigfree(void *addr, unsigned long size, struct malloc_type *type); void *contigmalloc(unsigned long size, struct malloc_type *type, int flags, vm_paddr_t low, vm_paddr_t high, unsigned long alignment, unsigned long boundary); void free(void *addr, struct malloc_type *type); void *malloc(unsigned long size, struct malloc_type *type, int flags); void malloc_init(void *); int malloc_last_fail(void); +void malloc_type_allocated(struct malloc_type *type, unsigned long size); +void malloc_type_freed(struct malloc_type *type, unsigned long size); void malloc_uninit(void *); void *realloc(void *addr, unsigned long size, struct malloc_type *type, int flags); void *reallocf(void *addr, unsigned long size, struct malloc_type *type, int flags); #endif /* _KERNEL */ #endif /* !_SYS_MALLOC_H_ */ Index: head/sys/vm/vm_contig.c =================================================================== --- head/sys/vm/vm_contig.c (revision 132378) +++ head/sys/vm/vm_contig.c (revision 132379) @@ -1,335 +1,569 @@ /* * Copyright (c) 1991 Regents of the University of California. * All rights reserved. * * This code is derived from software contributed to Berkeley by * The Mach Operating System project at Carnegie-Mellon University. * * 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. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. * * from: @(#)vm_page.c 7.4 (Berkeley) 5/7/91 */ /* * Copyright (c) 1987, 1990 Carnegie-Mellon University. * All rights reserved. * * Authors: Avadis Tevanian, Jr., Michael Wayne Young * * Permission to use, copy, modify and distribute this software and * its documentation is hereby granted, provided that both the copyright * notice and this permission notice appear in all copies of the * software, derivative works or modified versions, and any portions * thereof, and that both notices appear in supporting documentation. * * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. * * Carnegie Mellon requests users of this software to return to * * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU * School of Computer Science * Carnegie Mellon University * Pittsburgh PA 15213-3890 * * any improvements or extensions that they make and grant Carnegie the * rights to redistribute these changes. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include +#include +#include +#include #include #include #include #include #include #include #include #include #include #include #include #include static int -vm_contig_launder(int queue) +vm_contig_launder_page(vm_page_t m) { vm_object_t object; - vm_page_t m, m_tmp, next; + vm_page_t m_tmp; struct vnode *vp; + if (vm_page_sleep_if_busy(m, TRUE, "vpctw0")) { + vm_page_lock_queues(); + return (EBUSY); + } + if (!VM_OBJECT_TRYLOCK(m->object)) + return (EAGAIN); + vm_page_test_dirty(m); + if (m->dirty == 0 && m->hold_count == 0) + pmap_remove_all(m); + if (m->dirty) { + object = m->object; + if (object->type == OBJT_VNODE) { + vm_page_unlock_queues(); + vp = object->handle; + VM_OBJECT_UNLOCK(object); + vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread); + VM_OBJECT_LOCK(object); + vm_object_page_clean(object, 0, 0, OBJPC_SYNC); + VM_OBJECT_UNLOCK(object); + VOP_UNLOCK(vp, 0, curthread); + vm_page_lock_queues(); + return (0); + } else if (object->type == OBJT_SWAP || + object->type == OBJT_DEFAULT) { + m_tmp = m; + vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC); + VM_OBJECT_UNLOCK(object); + return (0); + } + } else if (m->hold_count == 0) + vm_page_cache(m); + VM_OBJECT_UNLOCK(m->object); + return (0); +} + +static int +vm_contig_launder(int queue) +{ + vm_page_t m, next; + int error; + for (m = TAILQ_FIRST(&vm_page_queues[queue].pl); m != NULL; m = next) { next = TAILQ_NEXT(m, pageq); KASSERT(m->queue == queue, ("vm_contig_launder: page %p's queue is not %d", m, queue)); - if (!VM_OBJECT_TRYLOCK(m->object)) - continue; - if (vm_page_sleep_if_busy(m, TRUE, "vpctw0")) { - VM_OBJECT_UNLOCK(m->object); - vm_page_lock_queues(); + error = vm_contig_launder_page(m); + if (error == 0) return (TRUE); - } - vm_page_test_dirty(m); - if (m->dirty == 0 && m->busy == 0 && m->hold_count == 0) - pmap_remove_all(m); - if (m->dirty) { - object = m->object; - if (object->type == OBJT_VNODE) { - vm_page_unlock_queues(); - vp = object->handle; - VM_OBJECT_UNLOCK(object); - vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread); - VM_OBJECT_LOCK(object); - vm_object_page_clean(object, 0, 0, OBJPC_SYNC); - VM_OBJECT_UNLOCK(object); - VOP_UNLOCK(vp, 0, curthread); - vm_page_lock_queues(); - return (TRUE); - } else if (object->type == OBJT_SWAP || - object->type == OBJT_DEFAULT) { - m_tmp = m; - vm_pageout_flush(&m_tmp, 1, VM_PAGER_PUT_SYNC); - VM_OBJECT_UNLOCK(object); - return (TRUE); - } - } else if (m->busy == 0 && m->hold_count == 0) - vm_page_cache(m); - VM_OBJECT_UNLOCK(m->object); + if (error == EBUSY) + return (FALSE); } return (FALSE); } /* * This interface is for merging with malloc() someday. * Even if we never implement compaction so that contiguous allocation * works after initialization time, malloc()'s data structures are good * for statistics and for allocations of less than a page. */ static void * contigmalloc1( unsigned long size, /* should be size_t here and for malloc() */ struct malloc_type *type, int flags, vm_paddr_t low, vm_paddr_t high, unsigned long alignment, unsigned long boundary, vm_map_t map) { int i, start; vm_paddr_t phys; vm_object_t object; vm_offset_t addr, tmp_addr; int pass, pqtype; int inactl, actl, inactmax, actmax; vm_page_t pga = vm_page_array; size = round_page(size); if (size == 0) panic("contigmalloc1: size must not be 0"); if ((alignment & (alignment - 1)) != 0) panic("contigmalloc1: alignment must be a power of 2"); if ((boundary & (boundary - 1)) != 0) panic("contigmalloc1: boundary must be a power of 2"); start = 0; for (pass = 2; pass >= 0; pass--) { vm_page_lock_queues(); again0: mtx_lock_spin(&vm_page_queue_free_mtx); again: /* * Find first page in array that is free, within range, * aligned, and such that the boundary won't be crossed. */ for (i = start; i < cnt.v_page_count; i++) { phys = VM_PAGE_TO_PHYS(&pga[i]); pqtype = pga[i].queue - pga[i].pc; if (((pqtype == PQ_FREE) || (pqtype == PQ_CACHE)) && (phys >= low) && (phys < high) && ((phys & (alignment - 1)) == 0) && (((phys ^ (phys + size - 1)) & ~(boundary - 1)) == 0)) break; } /* * If the above failed or we will exceed the upper bound, fail. */ if ((i == cnt.v_page_count) || ((VM_PAGE_TO_PHYS(&pga[i]) + size) > high)) { mtx_unlock_spin(&vm_page_queue_free_mtx); /* * Instead of racing to empty the inactive/active * queues, give up, even with more left to free, * if we try more than the initial amount of pages. * * There's no point attempting this on the last pass. */ if (pass > 0) { inactl = actl = 0; inactmax = vm_page_queues[PQ_INACTIVE].lcnt; actmax = vm_page_queues[PQ_ACTIVE].lcnt; again1: if (inactl < inactmax && vm_contig_launder(PQ_INACTIVE)) { inactl++; goto again1; } if (actl < actmax && vm_contig_launder(PQ_ACTIVE)) { actl++; goto again1; } } vm_page_unlock_queues(); continue; } start = i; /* * Check successive pages for contiguous and free. */ for (i = start + 1; i < (start + size / PAGE_SIZE); i++) { pqtype = pga[i].queue - pga[i].pc; if ((VM_PAGE_TO_PHYS(&pga[i]) != (VM_PAGE_TO_PHYS(&pga[i - 1]) + PAGE_SIZE)) || ((pqtype != PQ_FREE) && (pqtype != PQ_CACHE))) { start++; goto again; } } mtx_unlock_spin(&vm_page_queue_free_mtx); for (i = start; i < (start + size / PAGE_SIZE); i++) { vm_page_t m = &pga[i]; if ((m->queue - m->pc) == PQ_CACHE) { object = m->object; if (!VM_OBJECT_TRYLOCK(object)) { start++; goto again0; } vm_page_busy(m); vm_page_free(m); VM_OBJECT_UNLOCK(object); } } mtx_lock_spin(&vm_page_queue_free_mtx); for (i = start; i < (start + size / PAGE_SIZE); i++) { pqtype = pga[i].queue - pga[i].pc; if (pqtype != PQ_FREE) { start++; goto again; } } for (i = start; i < (start + size / PAGE_SIZE); i++) { vm_page_t m = &pga[i]; vm_pageq_remove_nowakeup(m); m->valid = VM_PAGE_BITS_ALL; if (m->flags & PG_ZERO) vm_page_zero_count--; /* Don't clear the PG_ZERO flag, we'll need it later. */ m->flags = PG_UNMANAGED | (m->flags & PG_ZERO); KASSERT(m->dirty == 0, ("contigmalloc1: page %p was dirty", m)); m->wire_count = 0; m->busy = 0; m->object = NULL; } mtx_unlock_spin(&vm_page_queue_free_mtx); vm_page_unlock_queues(); /* * We've found a contiguous chunk that meets are requirements. * Allocate kernel VM, unfree and assign the physical pages to * it and return kernel VM pointer. */ vm_map_lock(map); if (vm_map_findspace(map, vm_map_min(map), size, &addr) != KERN_SUCCESS) { /* * XXX We almost never run out of kernel virtual * space, so we don't make the allocated memory * above available. */ vm_map_unlock(map); return (NULL); } vm_object_reference(kernel_object); vm_map_insert(map, kernel_object, addr - VM_MIN_KERNEL_ADDRESS, addr, addr + size, VM_PROT_ALL, VM_PROT_ALL, 0); vm_map_unlock(map); tmp_addr = addr; VM_OBJECT_LOCK(kernel_object); for (i = start; i < (start + size / PAGE_SIZE); i++) { vm_page_t m = &pga[i]; vm_page_insert(m, kernel_object, OFF_TO_IDX(tmp_addr - VM_MIN_KERNEL_ADDRESS)); if ((flags & M_ZERO) && !(m->flags & PG_ZERO)) pmap_zero_page(m); tmp_addr += PAGE_SIZE; } VM_OBJECT_UNLOCK(kernel_object); vm_map_wire(map, addr, addr + size, VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES); return ((void *)addr); } return (NULL); } +static void +vm_page_release_contigl(vm_page_t m, vm_pindex_t count) +{ + mtx_lock_spin(&vm_page_queue_free_mtx); + while (count--) { + vm_page_free_toq(m); + m++; + } + mtx_unlock_spin(&vm_page_queue_free_mtx); +} + +void +vm_page_release_contig(vm_page_t m, vm_pindex_t count) +{ + vm_page_lock_queues(); + vm_page_release_contigl(m, count); + vm_page_unlock_queues(); +} + +static int +vm_contig_unqueue_free(vm_page_t m) +{ + int error = 0; + + mtx_lock_spin(&vm_page_queue_free_mtx); + if ((m->queue - m->pc) == PQ_FREE) + vm_pageq_remove_nowakeup(m); + else + error = EAGAIN; + mtx_unlock_spin(&vm_page_queue_free_mtx); + if (error) + return (error); + m->valid = VM_PAGE_BITS_ALL; + if (m->flags & PG_ZERO) + vm_page_zero_count--; + /* Don't clear the PG_ZERO flag; we'll need it later. */ + m->flags = PG_UNMANAGED | (m->flags & PG_ZERO); + KASSERT(m->dirty == 0, + ("contigmalloc2: page %p was dirty", m)); + m->wire_count = 0; + m->busy = 0; + m->object = NULL; + return (error); +} + +vm_page_t +vm_page_alloc_contig(vm_pindex_t npages, vm_paddr_t low, vm_paddr_t high, + vm_offset_t alignment, vm_offset_t boundary) +{ + vm_object_t object; + vm_offset_t size; + vm_paddr_t phys; + vm_page_t pga = vm_page_array; + int i, pass, pqtype, start; + + size = npages << PAGE_SHIFT; + if (size == 0) + panic("vm_page_alloc_contig: size must not be 0"); + if ((alignment & (alignment - 1)) != 0) + panic("vm_page_alloc_contig: alignment must be a power of 2"); + if ((boundary & (boundary - 1)) != 0) + panic("vm_page_alloc_contig: boundary must be a power of 2"); + + for (pass = 0; pass < 2; pass++) { + start = vm_page_array_size; + vm_page_lock_queues(); +retry: + start--; + /* + * Find last page in array that is free, within range, + * aligned, and such that the boundary won't be crossed. + */ + for (i = start; i >= 0; i--) { + phys = VM_PAGE_TO_PHYS(&pga[i]); + pqtype = pga[i].queue - pga[i].pc; + if (pass == 0) { + if (pqtype != PQ_FREE && pqtype != PQ_CACHE) + continue; + } else if (pqtype != PQ_FREE && pqtype != PQ_CACHE && + pga[i].queue != PQ_ACTIVE && + pga[i].queue != PQ_INACTIVE) + continue; + if (phys >= low && phys + size <= high && + ((phys & (alignment - 1)) == 0) && + ((phys ^ (phys + size - 1)) & ~(boundary - 1)) == 0) + break; + } + /* There are no candidates at all. */ + if (i == -1) { + vm_page_unlock_queues(); + continue; + } + start = i; + /* + * Check successive pages for contiguous and free. + */ + for (i = start + 1; i < start + npages; i++) { + pqtype = pga[i].queue - pga[i].pc; + if (VM_PAGE_TO_PHYS(&pga[i]) != + VM_PAGE_TO_PHYS(&pga[i - 1]) + PAGE_SIZE) + goto retry; + if (pass == 0) { + if (pqtype != PQ_FREE && pqtype != PQ_CACHE) + goto retry; + } else if (pqtype != PQ_FREE && pqtype != PQ_CACHE && + pga[i].queue != PQ_ACTIVE && + pga[i].queue != PQ_INACTIVE) + goto retry; + } + for (i = start; i < start + npages; i++) { + vm_page_t m = &pga[i]; + +retry_page: + pqtype = m->queue - m->pc; + if (pass != 0 && pqtype != PQ_FREE && + pqtype != PQ_CACHE) { + switch (m->queue) { + case PQ_ACTIVE: + case PQ_INACTIVE: + if (vm_contig_launder_page(m) != 0) + goto cleanup_freed; + pqtype = m->queue - m->pc; + if (pqtype == PQ_FREE || + pqtype == PQ_CACHE) + break; + default: +cleanup_freed: + vm_page_release_contigl(&pga[start], + i - start); + goto retry; + } + } + if (pqtype == PQ_CACHE) { + object = m->object; + if (!VM_OBJECT_TRYLOCK(object)) + goto retry; + vm_page_busy(m); + vm_page_free(m); + VM_OBJECT_UNLOCK(object); + } + /* + * There is no good API for freeing a page + * directly to PQ_NONE on our behalf, so spin. + */ + if (vm_contig_unqueue_free(m) != 0) + goto retry_page; + } + vm_page_unlock_queues(); + /* + * We've found a contiguous chunk that meets are requirements. + */ + return (&pga[start]); + } + return (NULL); +} + +static void * +contigmalloc2(vm_page_t m, vm_pindex_t npages, int flags) +{ + vm_object_t object = kernel_object; + vm_map_t map = kernel_map; + vm_offset_t addr, tmp_addr; + vm_pindex_t i; + + /* + * Allocate kernel VM, unfree and assign the physical pages to + * it and return kernel VM pointer. + */ + vm_map_lock(map); + if (vm_map_findspace(map, vm_map_min(map), npages << PAGE_SHIFT, &addr) + != KERN_SUCCESS) { + vm_map_unlock(map); + return (NULL); + } + vm_object_reference(object); + vm_map_insert(map, object, addr - VM_MIN_KERNEL_ADDRESS, + addr, addr + (npages << PAGE_SHIFT), VM_PROT_ALL, VM_PROT_ALL, 0); + vm_map_unlock(map); + tmp_addr = addr; + VM_OBJECT_LOCK(object); + for (i = 0; i < npages; i++) { + vm_page_insert(&m[i], object, + OFF_TO_IDX(tmp_addr - VM_MIN_KERNEL_ADDRESS)); + if ((flags & M_ZERO) && !(m->flags & PG_ZERO)) + pmap_zero_page(&m[i]); + tmp_addr += PAGE_SIZE; + } + VM_OBJECT_UNLOCK(object); + vm_map_wire(map, addr, addr + (npages << PAGE_SHIFT), + VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES); + return ((void *)addr); +} + +static int vm_old_contigmalloc = 1; +SYSCTL_INT(_vm, OID_AUTO, old_contigmalloc, + CTLFLAG_RW, &vm_old_contigmalloc, 0, "Use the old contigmalloc algorithm"); +TUNABLE_INT("vm.old_contigmalloc", &vm_old_contigmalloc); + void * contigmalloc( unsigned long size, /* should be size_t here and for malloc() */ struct malloc_type *type, int flags, vm_paddr_t low, vm_paddr_t high, unsigned long alignment, unsigned long boundary) { void * ret; + vm_page_t pages; + vm_pindex_t npgs; + npgs = round_page(size) >> PAGE_SHIFT; mtx_lock(&Giant); - ret = contigmalloc1(size, type, flags, low, high, alignment, boundary, - kernel_map); + if (vm_old_contigmalloc) { + ret = contigmalloc1(size, type, flags, low, high, alignment, + boundary, kernel_map); + } else { + pages = vm_page_alloc_contig(npgs, low, high, + alignment, boundary); + if (pages == NULL) { + ret = NULL; + } else { + ret = contigmalloc2(pages, npgs, flags); + if (ret == NULL) + vm_page_release_contig(pages, npgs); + } + + } mtx_unlock(&Giant); + malloc_type_allocated(type, ret == NULL ? 0 : npgs << PAGE_SHIFT); return (ret); } void contigfree(void *addr, unsigned long size, struct malloc_type *type) { + vm_pindex_t npgs; + npgs = round_page(size) >> PAGE_SHIFT; kmem_free(kernel_map, (vm_offset_t)addr, size); + malloc_type_freed(type, npgs << PAGE_SHIFT); } Index: head/sys/vm/vm_page.h =================================================================== --- head/sys/vm/vm_page.h (revision 132378) +++ head/sys/vm/vm_page.h (revision 132379) @@ -1,386 +1,389 @@ /* * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * The Mach Operating System project at Carnegie-Mellon University. * * 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. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. * * from: @(#)vm_page.h 8.2 (Berkeley) 12/13/93 * * * Copyright (c) 1987, 1990 Carnegie-Mellon University. * All rights reserved. * * Authors: Avadis Tevanian, Jr., Michael Wayne Young * * Permission to use, copy, modify and distribute this software and * its documentation is hereby granted, provided that both the copyright * notice and this permission notice appear in all copies of the * software, derivative works or modified versions, and any portions * thereof, and that both notices appear in supporting documentation. * * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. * * Carnegie Mellon requests users of this software to return to * * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU * School of Computer Science * Carnegie Mellon University * Pittsburgh PA 15213-3890 * * any improvements or extensions that they make and grant Carnegie the * rights to redistribute these changes. * * $FreeBSD$ */ /* * Resident memory system definitions. */ #ifndef _VM_PAGE_ #define _VM_PAGE_ #if !defined(KLD_MODULE) #include "opt_vmpage.h" #endif #include /* * Management of resident (logical) pages. * * A small structure is kept for each resident * page, indexed by page number. Each structure * is an element of several lists: * * A hash table bucket used to quickly * perform object/offset lookups * * A list of all pages for a given object, * so they can be quickly deactivated at * time of deallocation. * * An ordered list of pages due for pageout. * * In addition, the structure contains the object * and offset to which this page belongs (for pageout), * and sundry status bits. * * Fields in this structure are locked either by the lock on the * object that the page belongs to (O) or by the lock on the page * queues (P). * * The 'valid' and 'dirty' fields are distinct. A page may have dirty * bits set without having associated valid bits set. This is used by * NFS to implement piecemeal writes. */ TAILQ_HEAD(pglist, vm_page); struct vm_page { TAILQ_ENTRY(vm_page) pageq; /* queue info for FIFO queue or free list (P) */ TAILQ_ENTRY(vm_page) listq; /* pages in same object (O) */ struct vm_page *left; /* splay tree link (O) */ struct vm_page *right; /* splay tree link (O) */ vm_object_t object; /* which object am I in (O,P)*/ vm_pindex_t pindex; /* offset into object (O,P) */ vm_paddr_t phys_addr; /* physical address of page */ struct md_page md; /* machine dependant stuff */ u_short queue; /* page queue index */ u_short flags, /* see below */ pc; /* page color */ u_short wire_count; /* wired down maps refs (P) */ short hold_count; /* page hold count */ u_char act_count; /* page usage count */ u_char busy; /* page busy count */ /* NOTE that these must support one bit per DEV_BSIZE in a page!!! */ /* so, on normal X86 kernels, they must be at least 8 bits wide */ #if PAGE_SIZE == 4096 u_char valid; /* map of valid DEV_BSIZE chunks (O) */ u_char dirty; /* map of dirty DEV_BSIZE chunks */ #elif PAGE_SIZE == 8192 u_short valid; /* map of valid DEV_BSIZE chunks (O) */ u_short dirty; /* map of dirty DEV_BSIZE chunks */ #elif PAGE_SIZE == 16384 u_int valid; /* map of valid DEV_BSIZE chunks (O) */ u_int dirty; /* map of dirty DEV_BSIZE chunks */ #elif PAGE_SIZE == 32768 u_long valid; /* map of valid DEV_BSIZE chunks (O) */ u_long dirty; /* map of dirty DEV_BSIZE chunks */ #endif u_int cow; /* page cow mapping count */ }; /* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */ #if PAGE_SIZE == 32768 #ifdef CTASSERT CTASSERT(sizeof(u_long) >= 8); #endif #endif #if !defined(KLD_MODULE) /* * Page coloring parameters */ /* Backward compatibility for existing PQ_*CACHE config options. */ #if !defined(PQ_CACHESIZE) #if defined(PQ_HUGECACHE) #define PQ_CACHESIZE 1024 #elif defined(PQ_LARGECACHE) #define PQ_CACHESIZE 512 #elif defined(PQ_MEDIUMCACHE) #define PQ_CACHESIZE 256 #elif defined(PQ_NORMALCACHE) #define PQ_CACHESIZE 64 #elif defined(PQ_NOOPT) #define PQ_CACHESIZE 0 #else #define PQ_CACHESIZE 128 #endif #endif /* !defined(PQ_CACHESIZE) */ #if PQ_CACHESIZE >= 1024 #define PQ_PRIME1 31 /* Prime number somewhat less than PQ_L2_SIZE */ #define PQ_PRIME2 23 /* Prime number somewhat less than PQ_L2_SIZE */ #define PQ_L2_SIZE 256 /* A number of colors opt for 1M cache */ #elif PQ_CACHESIZE >= 512 #define PQ_PRIME1 31 /* Prime number somewhat less than PQ_L2_SIZE */ #define PQ_PRIME2 23 /* Prime number somewhat less than PQ_L2_SIZE */ #define PQ_L2_SIZE 128 /* A number of colors opt for 512K cache */ #elif PQ_CACHESIZE >= 256 #define PQ_PRIME1 13 /* Prime number somewhat less than PQ_L2_SIZE */ #define PQ_PRIME2 7 /* Prime number somewhat less than PQ_L2_SIZE */ #define PQ_L2_SIZE 64 /* A number of colors opt for 256K cache */ #elif PQ_CACHESIZE >= 128 #define PQ_PRIME1 9 /* Produces a good PQ_L2_SIZE/3 + PQ_PRIME1 */ #define PQ_PRIME2 5 /* Prime number somewhat less than PQ_L2_SIZE */ #define PQ_L2_SIZE 32 /* A number of colors opt for 128k cache */ #elif PQ_CACHESIZE >= 64 #define PQ_PRIME1 5 /* Prime number somewhat less than PQ_L2_SIZE */ #define PQ_PRIME2 3 /* Prime number somewhat less than PQ_L2_SIZE */ #define PQ_L2_SIZE 16 /* A reasonable number of colors (opt for 64K cache) */ #else #define PQ_PRIME1 1 /* Disable page coloring. */ #define PQ_PRIME2 1 #define PQ_L2_SIZE 1 #endif #define PQ_L2_MASK (PQ_L2_SIZE - 1) /* PQ_CACHE and PQ_FREE represent PQ_L2_SIZE consecutive queues. */ #define PQ_NONE 0 #define PQ_FREE 1 #define PQ_INACTIVE (1 + 1*PQ_L2_SIZE) #define PQ_ACTIVE (2 + 1*PQ_L2_SIZE) #define PQ_CACHE (3 + 1*PQ_L2_SIZE) #define PQ_HOLD (3 + 2*PQ_L2_SIZE) #define PQ_COUNT (4 + 2*PQ_L2_SIZE) struct vpgqueues { struct pglist pl; int *cnt; int lcnt; }; extern struct vpgqueues vm_page_queues[PQ_COUNT]; extern struct mtx vm_page_queue_free_mtx; #endif /* !defined(KLD_MODULE) */ /* * These are the flags defined for vm_page. * * Note: PG_UNMANAGED (used by OBJT_PHYS) indicates that the page is * not under PV management but otherwise should be treated as a * normal page. Pages not under PV management cannot be paged out * via the object/vm_page_t because there is no knowledge of their * pte mappings, nor can they be removed from their objects via * the object, and such pages are also not on any PQ queue. */ #define PG_BUSY 0x0001 /* page is in transit (O) */ #define PG_WANTED 0x0002 /* someone is waiting for page (O) */ #define PG_WINATCFLS 0x0004 /* flush dirty page on inactive q */ #define PG_FICTITIOUS 0x0008 /* physical page doesn't exist (O) */ #define PG_WRITEABLE 0x0010 /* page is mapped writeable */ #define PG_ZERO 0x0040 /* page is zeroed */ #define PG_REFERENCED 0x0080 /* page has been referenced */ #define PG_CLEANCHK 0x0100 /* page will be checked for cleaning */ #define PG_SWAPINPROG 0x0200 /* swap I/O in progress on page */ #define PG_NOSYNC 0x0400 /* do not collect for syncer */ #define PG_UNMANAGED 0x0800 /* No PV management for page */ #define PG_MARKER 0x1000 /* special queue marker page */ #define PG_SLAB 0x2000 /* object pointer is actually a slab */ /* * Misc constants. */ #define ACT_DECLINE 1 #define ACT_ADVANCE 3 #define ACT_INIT 5 #define ACT_MAX 64 #ifdef _KERNEL /* * Each pageable resident page falls into one of four lists: * * free * Available for allocation now. * * The following are all LRU sorted: * * cache * Almost available for allocation. Still in an * object, but clean and immediately freeable at * non-interrupt times. * * inactive * Low activity, candidates for reclamation. * This is the list of pages that should be * paged out next. * * active * Pages that are "active" i.e. they have been * recently referenced. * * zero * Pages that are really free and have been pre-zeroed * */ extern int vm_page_zero_count; extern vm_page_t vm_page_array; /* First resident page in table */ extern int vm_page_array_size; /* number of vm_page_t's */ extern long first_page; /* first physical page number */ #define VM_PAGE_TO_PHYS(entry) ((entry)->phys_addr) #define PHYS_TO_VM_PAGE(pa) \ (&vm_page_array[atop(pa) - first_page ]) extern struct mtx vm_page_queue_mtx; #define vm_page_lock_queues() mtx_lock(&vm_page_queue_mtx) #define vm_page_unlock_queues() mtx_unlock(&vm_page_queue_mtx) #if PAGE_SIZE == 4096 #define VM_PAGE_BITS_ALL 0xffu #elif PAGE_SIZE == 8192 #define VM_PAGE_BITS_ALL 0xffffu #elif PAGE_SIZE == 16384 #define VM_PAGE_BITS_ALL 0xffffffffu #elif PAGE_SIZE == 32768 #define VM_PAGE_BITS_ALL 0xfffffffffffffffflu #endif /* page allocation classes: */ #define VM_ALLOC_NORMAL 0 #define VM_ALLOC_INTERRUPT 1 #define VM_ALLOC_SYSTEM 2 #define VM_ALLOC_CLASS_MASK 3 /* page allocation flags: */ #define VM_ALLOC_WIRED 0x0020 /* non pageable */ #define VM_ALLOC_ZERO 0x0040 /* Try to obtain a zeroed page */ #define VM_ALLOC_RETRY 0x0080 /* vm_page_grab() only */ #define VM_ALLOC_NOOBJ 0x0100 /* No associated object */ void vm_page_flag_set(vm_page_t m, unsigned short bits); void vm_page_flag_clear(vm_page_t m, unsigned short bits); void vm_page_busy(vm_page_t m); void vm_page_flash(vm_page_t m); void vm_page_io_start(vm_page_t m); void vm_page_io_finish(vm_page_t m); void vm_page_hold(vm_page_t mem); void vm_page_unhold(vm_page_t mem); void vm_page_free(vm_page_t m); void vm_page_free_zero(vm_page_t m); int vm_page_sleep_if_busy(vm_page_t m, int also_m_busy, const char *msg); void vm_page_dirty(vm_page_t m); void vm_page_wakeup(vm_page_t m); void vm_pageq_init(void); vm_page_t vm_pageq_add_new_page(vm_paddr_t pa); void vm_pageq_enqueue(int queue, vm_page_t m); void vm_pageq_remove_nowakeup(vm_page_t m); void vm_pageq_remove(vm_page_t m); vm_page_t vm_pageq_find(int basequeue, int index, boolean_t prefer_zero); void vm_pageq_requeue(vm_page_t m); void vm_page_activate (vm_page_t); vm_page_t vm_page_alloc (vm_object_t, vm_pindex_t, int); +vm_page_t vm_page_alloc_contig (vm_pindex_t, vm_paddr_t, vm_paddr_t, + vm_offset_t, vm_offset_t); +void vm_page_release_contig (vm_page_t, vm_pindex_t); vm_page_t vm_page_grab (vm_object_t, vm_pindex_t, int); void vm_page_cache (register vm_page_t); int vm_page_try_to_cache (vm_page_t); int vm_page_try_to_free (vm_page_t); void vm_page_dontneed (register vm_page_t); void vm_page_deactivate (vm_page_t); void vm_page_insert (vm_page_t, vm_object_t, vm_pindex_t); vm_page_t vm_page_lookup (vm_object_t, vm_pindex_t); void vm_page_remove (vm_page_t); void vm_page_rename (vm_page_t, vm_object_t, vm_pindex_t); vm_page_t vm_page_select_cache(int); vm_page_t vm_page_splay(vm_pindex_t, vm_page_t); vm_offset_t vm_page_startup(vm_offset_t vaddr); void vm_page_unmanage (vm_page_t); void vm_page_unwire (vm_page_t, int); void vm_page_wire (vm_page_t); void vm_page_set_validclean (vm_page_t, int, int); void vm_page_clear_dirty (vm_page_t, int, int); void vm_page_set_invalid (vm_page_t, int, int); int vm_page_is_valid (vm_page_t, int, int); void vm_page_test_dirty (vm_page_t); int vm_page_bits (int, int); void vm_page_zero_invalid(vm_page_t m, boolean_t setvalid); void vm_page_free_toq(vm_page_t m); void vm_page_zero_idle_wakeup(void); void vm_page_cowfault (vm_page_t); void vm_page_cowsetup (vm_page_t); void vm_page_cowclear (vm_page_t); /* * vm_page_undirty: * * Set page to not be dirty. Note: does not clear pmap modify bits */ static __inline void vm_page_undirty(vm_page_t m) { m->dirty = 0; } #endif /* _KERNEL */ #endif /* !_VM_PAGE_ */