2 * Copyright (c) 1997, 1998 John S. Dyson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice immediately at the beginning of the file, without modification,
10 * this list of conditions, and the following disclaimer.
11 * 2. Absolutely no warranty of function or purpose is made by the author
14 * $FreeBSD: src/sys/vm/vm_zone.c,v 1.30.2.6 2002/10/10 19:50:16 dillon Exp $
15 * $DragonFly: src/sys/vm/vm_zone.c,v 1.11 2003/10/19 00:23:30 dillon Exp $
18 #include <sys/param.h>
19 #include <sys/systm.h>
20 #include <sys/kernel.h>
22 #include <sys/malloc.h>
23 #include <sys/sysctl.h>
24 #include <sys/vmmeter.h>
27 #include <vm/vm_object.h>
28 #include <vm/vm_page.h>
29 #include <vm/vm_map.h>
30 #include <vm/vm_kern.h>
31 #include <vm/vm_extern.h>
32 #include <vm/vm_zone.h>
34 static MALLOC_DEFINE(M_ZONE, "ZONE", "Zone header");
36 #define ZONE_ERROR_INVALID 0
37 #define ZONE_ERROR_NOTFREE 1
38 #define ZONE_ERROR_ALREADYFREE 2
40 #define ZONE_ROUNDING 32
42 #define ZENTRY_FREE 0x12342378
44 static void *zget(vm_zone_t z);
47 * Return an item from the specified zone. This function is interrupt/MP
48 * thread safe, but might block.
57 zerror(ZONE_ERROR_INVALID);
59 lwkt_gettoken(&z->zlock);
60 if (z->zfreecnt <= z->zfreemin) {
63 * PANICFAIL allows the caller to assume that the zalloc()
64 * will always succeed. If it doesn't, we panic here.
66 if (item == NULL && (z->zflags & ZONE_PANICFAIL))
67 panic("zalloc(%s) failed", z->zname);
70 z->zitems = ((void **) item)[0];
72 KASSERT(item != NULL, ("zitems unexpectedly NULL"));
73 if (((void **) item)[1] != (void *) ZENTRY_FREE)
74 zerror(ZONE_ERROR_NOTFREE);
75 ((void **) item)[1] = 0;
80 lwkt_reltoken(&z->zlock);
85 * Free an item to the specified zone. This function is interrupt/MP
86 * thread safe, but might block.
89 zfree(vm_zone_t z, void *item)
91 lwkt_gettoken(&z->zlock);
92 ((void **) item)[0] = z->zitems;
94 if (((void **) item)[1] == (void *) ZENTRY_FREE)
95 zerror(ZONE_ERROR_ALREADYFREE);
96 ((void **) item)[1] = (void *) ZENTRY_FREE;
100 lwkt_reltoken(&z->zlock);
104 * This file comprises a very simple zone allocator. This is used
105 * in lieu of the malloc allocator, where needed or more optimal.
107 * Note that the initial implementation of this had coloring, and
108 * absolutely no improvement (actually perf degradation) occurred.
110 * Note also that the zones are type stable. The only restriction is
111 * that the first two longwords of a data structure can be changed
112 * between allocations. Any data that must be stable between allocations
113 * must reside in areas after the first two longwords.
115 * zinitna, zinit, zbootinit are the initialization routines.
116 * zalloc, zfree, are the allocation/free routines.
119 static struct vm_zone *zlist;
120 static int sysctl_vm_zone(SYSCTL_HANDLER_ARGS);
121 static int zone_kmem_pages, zone_kern_pages, zone_kmem_kvaspace;
124 * Create a zone, but don't allocate the zone structure. If the
125 * zone had been previously created by the zone boot code, initialize
126 * various parts of the zone code.
128 * If waits are not allowed during allocation (e.g. during interrupt
129 * code), a-priori allocate the kernel virtual space, and allocate
130 * only pages when needed.
133 * z pointer to zone structure.
134 * obj pointer to VM object (opt).
136 * size size of zone entries.
137 * nentries number of zone entries allocated (only ZONE_INTERRUPT.)
138 * flags ZONE_INTERRUPT -- items can be allocated at interrupt time.
139 * zalloc number of pages allocated when memory is needed.
141 * Note that when using ZONE_INTERRUPT, the size of the zone is limited
142 * by the nentries argument. The size of the memory allocatable is
143 * unlimited if ZONE_INTERRUPT is not set.
147 zinitna(vm_zone_t z, vm_object_t obj, char *name, int size,
148 int nentries, int flags, int zalloc)
152 if ((z->zflags & ZONE_BOOT) == 0) {
153 z->zsize = (size + ZONE_ROUNDING - 1) & ~(ZONE_ROUNDING - 1);
154 lwkt_inittoken(&z->zlock);
169 * If we cannot wait, allocate KVA space up front, and we will fill
170 * in pages as needed. This is particularly required when creating
171 * an allocation space for map entries in kernel_map, because we
172 * do not want to go into a recursion deadlock with
173 * vm_map_entry_reserve().
175 if (z->zflags & ZONE_INTERRUPT) {
177 totsize = round_page(z->zsize * nentries);
178 zone_kmem_kvaspace += totsize;
180 z->zkva = kmem_alloc_pageable(kernel_map, totsize);
186 z->zpagemax = totsize / PAGE_SIZE;
188 z->zobj = vm_object_allocate(OBJT_DEFAULT, z->zpagemax);
191 _vm_object_allocate(OBJT_DEFAULT, z->zpagemax, obj);
193 z->zallocflag = VM_ALLOC_INTERRUPT;
196 z->zallocflag = VM_ALLOC_SYSTEM;
201 if (z->zsize > PAGE_SIZE)
204 z->zfreemin = PAGE_SIZE / z->zsize;
216 * Subroutine same as zinitna, except zone data structure is allocated
217 * automatically by malloc. This routine should normally be used, except
218 * in certain tricky startup conditions in the VM system -- then
219 * zbootinit and zinitna can be used. Zinit is the standard zone
220 * initialization call.
223 zinit(char *name, int size, int nentries, int flags, int zalloc)
227 z = (vm_zone_t) malloc(sizeof (struct vm_zone), M_ZONE, M_NOWAIT);
232 if (zinitna(z, NULL, name, size, nentries, flags, zalloc) == 0) {
241 * Initialize a zone before the system is fully up. This routine should
242 * only be called before full VM startup.
245 zbootinit(vm_zone_t z, char *name, int size, void *item, int nitems)
253 z->zflags = ZONE_BOOT;
259 lwkt_inittoken(&z->zlock);
261 bzero(item, nitems * z->zsize);
263 for (i = 0; i < nitems; i++) {
264 ((void **) item)[0] = z->zitems;
266 ((void **) item)[1] = (void *) ZENTRY_FREE;
269 (char *) item += z->zsize;
271 z->zfreecnt = nitems;
284 * void *zalloc(vm_zone_t zone) --
285 * Returns an item from a specified zone. May not be called from a
286 * FAST interrupt or IPI function.
288 * void zfree(vm_zone_t zone, void *item) --
289 * Frees an item back to a specified zone. May not be called from a
290 * FAST interrupt or IPI function.
294 * Internal zone routine. Not to be called from external (non vm_zone) code.
305 panic("zget: null zone");
307 if (z->zflags & ZONE_INTERRUPT) {
308 nbytes = z->zpagecount * PAGE_SIZE;
309 nbytes -= nbytes % z->zsize;
310 item = (char *) z->zkva + nbytes;
311 for (i = 0; ((i < z->zalloc) && (z->zpagecount < z->zpagemax));
315 m = vm_page_alloc(z->zobj, z->zpagecount,
319 lwkt_regettoken(&z->zlock);
321 zkva = z->zkva + z->zpagecount * PAGE_SIZE;
322 pmap_kenter(zkva, VM_PAGE_TO_PHYS(m)); /* YYY */
323 bzero((caddr_t) zkva, PAGE_SIZE);
326 vmstats.v_wire_count++;
328 nitems = ((z->zpagecount * PAGE_SIZE) - nbytes) / z->zsize;
330 nbytes = z->zalloc * PAGE_SIZE;
333 item = (void *) kmem_alloc(kernel_map, nbytes);
334 lwkt_regettoken(&z->zlock);
336 zone_kern_pages += z->zalloc;
343 nitems = nbytes / z->zsize;
348 * Save one for immediate allocation
352 for (i = 0; i < nitems; i++) {
353 ((void **) item)[0] = z->zitems;
355 ((void **) item)[1] = (void *) ZENTRY_FREE;
358 (char *) item += z->zsize;
360 z->zfreecnt += nitems;
362 } else if (z->zfreecnt > 0) {
364 z->zitems = ((void **) item)[0];
366 if (((void **) item)[1] != (void *) ZENTRY_FREE)
367 zerror(ZONE_ERROR_NOTFREE);
368 ((void **) item)[1] = 0;
377 * Recover any reserve missing due to a zalloc/kreserve/krelease
380 vm_map_entry_reserve(0);
386 sysctl_vm_zone(SYSCTL_HANDLER_ARGS)
389 vm_zone_t curzone, nextzone;
393 snprintf(tmpbuf, sizeof(tmpbuf),
394 "\nITEM SIZE LIMIT USED FREE REQUESTS\n");
395 error = SYSCTL_OUT(req, tmpbuf, strlen(tmpbuf));
399 for (curzone = zlist; curzone; curzone = nextzone) {
404 nextzone = curzone->znext;
405 len = strlen(curzone->zname);
406 if (len >= (sizeof(tmpname) - 1))
407 len = (sizeof(tmpname) - 1);
408 for(i = 0; i < sizeof(tmpname) - 1; i++)
411 memcpy(tmpname, curzone->zname, len);
414 if (curzone == zlist) {
419 snprintf(tmpbuf + offset, sizeof(tmpbuf) - offset,
420 "%s %6.6u, %8.8u, %6.6u, %6.6u, %8.8u\n",
421 tmpname, curzone->zsize, curzone->zmax,
422 (curzone->ztotal - curzone->zfreecnt),
423 curzone->zfreecnt, curzone->znalloc);
425 len = strlen((char *)tmpbuf);
426 if (nextzone == NULL)
429 error = SYSCTL_OUT(req, tmpbuf, len);
437 #if defined(INVARIANTS)
444 case ZONE_ERROR_INVALID:
445 msg = "zone: invalid zone";
447 case ZONE_ERROR_NOTFREE:
448 msg = "zone: entry not free";
450 case ZONE_ERROR_ALREADYFREE:
451 msg = "zone: freeing free entry";
454 msg = "zone: invalid error";
461 SYSCTL_OID(_vm, OID_AUTO, zone, CTLTYPE_STRING|CTLFLAG_RD, \
462 NULL, 0, sysctl_vm_zone, "A", "Zone Info");
464 SYSCTL_INT(_vm, OID_AUTO, zone_kmem_pages,
465 CTLFLAG_RD, &zone_kmem_pages, 0, "Number of interrupt safe pages allocated by zone");
466 SYSCTL_INT(_vm, OID_AUTO, zone_kmem_kvaspace,
467 CTLFLAG_RD, &zone_kmem_kvaspace, 0, "KVA space allocated by zone");
468 SYSCTL_INT(_vm, OID_AUTO, zone_kern_pages,
469 CTLFLAG_RD, &zone_kern_pages, 0, "Number of non-interrupt safe pages allocated by zone");