4 * Copyright (c) 1997, 1998 John S. Dyson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice immediately at the beginning of the file, without modification,
12 * this list of conditions, and the following disclaimer.
13 * 2. Absolutely no warranty of function or purpose is made by the author
16 * $FreeBSD: src/sys/vm/vm_zone.c,v 1.30.2.6 2002/10/10 19:50:16 dillon Exp $
19 #include <sys/param.h>
20 #include <sys/queue.h>
21 #include <sys/systm.h>
22 #include <sys/kernel.h>
24 #include <sys/malloc.h>
25 #include <sys/sysctl.h>
26 #include <sys/vmmeter.h>
29 #include <vm/vm_object.h>
30 #include <vm/vm_page.h>
31 #include <vm/vm_map.h>
32 #include <vm/vm_kern.h>
33 #include <vm/vm_extern.h>
34 #include <vm/vm_zone.h>
36 #include <sys/spinlock2.h>
37 #include <vm/vm_page2.h>
39 static MALLOC_DEFINE(M_ZONE
, "ZONE", "Zone header");
41 #define ZONE_ERROR_INVALID 0
42 #define ZONE_ERROR_NOTFREE 1
43 #define ZONE_ERROR_ALREADYFREE 2
45 #define ZONE_ROUNDING 32
47 #define ZENTRY_FREE 0x12342378
51 static void *zget(vm_zone_t z
);
54 * Return an item from the specified zone. This function is non-blocking for
55 * ZONE_INTERRUPT zones.
62 globaldata_t gd
= mycpu
;
68 zerror(ZONE_ERROR_INVALID
);
72 * Avoid spinlock contention by allocating from a per-cpu queue
74 if (z
->zfreecnt_pcpu
[gd
->gd_cpuid
] > 0) {
76 if (z
->zfreecnt_pcpu
[gd
->gd_cpuid
] > 0) {
77 item
= z
->zitems_pcpu
[gd
->gd_cpuid
];
80 ("zitems_pcpu unexpectedly NULL"));
81 if (((void **)item
)[1] != (void *)ZENTRY_FREE
)
82 zerror(ZONE_ERROR_NOTFREE
);
83 ((void **)item
)[1] = NULL
;
85 z
->zitems_pcpu
[gd
->gd_cpuid
] = ((void **) item
)[0];
86 --z
->zfreecnt_pcpu
[gd
->gd_cpuid
];
95 * Per-zone spinlock for the remainder. Always load at least one
99 if (z
->zfreecnt
> z
->zfreemin
) {
104 KASSERT(item
!= NULL
, ("zitems unexpectedly NULL"));
105 if (((void **)item
)[1] != (void *)ZENTRY_FREE
)
106 zerror(ZONE_ERROR_NOTFREE
);
108 z
->zitems
= ((void **)item
)[0];
110 ((void **)item
)[0] = z
->zitems_pcpu
[gd
->gd_cpuid
];
111 z
->zitems_pcpu
[gd
->gd_cpuid
] = item
;
112 ++z
->zfreecnt_pcpu
[gd
->gd_cpuid
];
113 } while (--n
> 0 && z
->zfreecnt
> z
->zfreemin
);
114 spin_unlock(&z
->zlock
);
117 spin_unlock(&z
->zlock
);
120 * PANICFAIL allows the caller to assume that the zalloc()
121 * will always succeed. If it doesn't, we panic here.
123 if (item
== NULL
&& (z
->zflags
& ZONE_PANICFAIL
))
124 panic("zalloc(%s) failed", z
->zname
);
130 * Free an item to the specified zone.
135 zfree(vm_zone_t z
, void *item
)
137 globaldata_t gd
= mycpu
;
141 * Avoid spinlock contention by freeing into a per-cpu queue
143 if ((zmax
= z
->zmax
) != 0)
144 zmax
= zmax
/ ncpus
/ 16;
148 if (z
->zfreecnt_pcpu
[gd
->gd_cpuid
] < zmax
) {
150 ((void **)item
)[0] = z
->zitems_pcpu
[gd
->gd_cpuid
];
152 if (((void **)item
)[1] == (void *)ZENTRY_FREE
)
153 zerror(ZONE_ERROR_ALREADYFREE
);
154 ((void **)item
)[1] = (void *)ZENTRY_FREE
;
156 z
->zitems_pcpu
[gd
->gd_cpuid
] = item
;
157 ++z
->zfreecnt_pcpu
[gd
->gd_cpuid
];
163 * Per-zone spinlock for the remainder.
165 spin_lock(&z
->zlock
);
166 ((void **)item
)[0] = z
->zitems
;
168 if (((void **)item
)[1] == (void *)ZENTRY_FREE
)
169 zerror(ZONE_ERROR_ALREADYFREE
);
170 ((void **)item
)[1] = (void *)ZENTRY_FREE
;
174 spin_unlock(&z
->zlock
);
178 * This file comprises a very simple zone allocator. This is used
179 * in lieu of the malloc allocator, where needed or more optimal.
181 * Note that the initial implementation of this had coloring, and
182 * absolutely no improvement (actually perf degradation) occurred.
184 * Note also that the zones are type stable. The only restriction is
185 * that the first two longwords of a data structure can be changed
186 * between allocations. Any data that must be stable between allocations
187 * must reside in areas after the first two longwords.
189 * zinitna, zinit, zbootinit are the initialization routines.
190 * zalloc, zfree, are the allocation/free routines.
193 LIST_HEAD(zlist
, vm_zone
) zlist
= LIST_HEAD_INITIALIZER(zlist
);
194 static int sysctl_vm_zone(SYSCTL_HANDLER_ARGS
);
195 static int zone_kmem_pages
, zone_kern_pages
;
196 static long zone_kmem_kvaspace
;
199 * Create a zone, but don't allocate the zone structure. If the
200 * zone had been previously created by the zone boot code, initialize
201 * various parts of the zone code.
203 * If waits are not allowed during allocation (e.g. during interrupt
204 * code), a-priori allocate the kernel virtual space, and allocate
205 * only pages when needed.
208 * z pointer to zone structure.
209 * obj pointer to VM object (opt).
211 * size size of zone entries.
212 * nentries number of zone entries allocated (only ZONE_INTERRUPT.)
213 * flags ZONE_INTERRUPT -- items can be allocated at interrupt time.
214 * zalloc number of pages allocated when memory is needed.
216 * Note that when using ZONE_INTERRUPT, the size of the zone is limited
217 * by the nentries argument. The size of the memory allocatable is
218 * unlimited if ZONE_INTERRUPT is not set.
223 zinitna(vm_zone_t z
, vm_object_t obj
, char *name
, int size
,
224 int nentries
, int flags
, int zalloc
)
229 * Only zones created with zinit() are destroyable.
231 if (z
->zflags
& ZONE_DESTROYABLE
)
232 panic("zinitna: can't create destroyable zone");
235 * NOTE: We can only adjust zsize if we previously did not
238 if ((z
->zflags
& ZONE_BOOT
) == 0) {
239 z
->zsize
= roundup2(size
, ZONE_ROUNDING
);
240 spin_init(&z
->zlock
, "zinitna");
248 lwkt_gettoken(&vm_token
);
249 LIST_INSERT_HEAD(&zlist
, z
, zlink
);
250 lwkt_reltoken(&vm_token
);
252 bzero(z
->zitems_pcpu
, sizeof(z
->zitems_pcpu
));
253 bzero(z
->zfreecnt_pcpu
, sizeof(z
->zfreecnt_pcpu
));
257 z
->zkmcur
= z
->zkmmax
= 0;
261 * If we cannot wait, allocate KVA space up front, and we will fill
262 * in pages as needed. This is particularly required when creating
263 * an allocation space for map entries in kernel_map, because we
264 * do not want to go into a recursion deadlock with
265 * vm_map_entry_reserve().
267 if (z
->zflags
& ZONE_INTERRUPT
) {
268 totsize
= round_page((size_t)z
->zsize
* nentries
);
269 atomic_add_long(&zone_kmem_kvaspace
, totsize
);
271 z
->zkva
= kmem_alloc_pageable(&kernel_map
, totsize
);
273 LIST_REMOVE(z
, zlink
);
277 z
->zpagemax
= totsize
/ PAGE_SIZE
;
279 z
->zobj
= vm_object_allocate(OBJT_DEFAULT
, z
->zpagemax
);
282 _vm_object_allocate(OBJT_DEFAULT
, z
->zpagemax
, obj
);
285 z
->zallocflag
= VM_ALLOC_SYSTEM
| VM_ALLOC_INTERRUPT
|
286 VM_ALLOC_NORMAL
| VM_ALLOC_RETRY
;
289 z
->zallocflag
= VM_ALLOC_NORMAL
| VM_ALLOC_SYSTEM
;
294 if (z
->zsize
> PAGE_SIZE
)
297 z
->zfreemin
= PAGE_SIZE
/ z
->zsize
;
306 * Populate the interrrupt zone at creation time rather than
307 * on first allocation, as this is a potentially long operation.
309 if (z
->zflags
& ZONE_INTERRUPT
) {
320 * Subroutine same as zinitna, except zone data structure is allocated
321 * automatically by malloc. This routine should normally be used, except
322 * in certain tricky startup conditions in the VM system -- then
323 * zbootinit and zinitna can be used. Zinit is the standard zone
324 * initialization call.
329 zinit(char *name
, int size
, int nentries
, int flags
, int zalloc
)
333 z
= (vm_zone_t
) kmalloc(sizeof (struct vm_zone
), M_ZONE
, M_NOWAIT
);
338 if (zinitna(z
, NULL
, name
, size
, nentries
,
339 flags
& ~ZONE_DESTROYABLE
, zalloc
) == 0) {
344 if (flags
& ZONE_DESTROYABLE
)
345 z
->zflags
|= ZONE_DESTROYABLE
;
351 * Initialize a zone before the system is fully up. This routine should
352 * only be called before full VM startup.
354 * Called from the low level boot code only.
357 zbootinit(vm_zone_t z
, char *name
, int size
, void *item
, int nitems
)
361 bzero(z
->zitems_pcpu
, sizeof(z
->zitems_pcpu
));
362 bzero(z
->zfreecnt_pcpu
, sizeof(z
->zfreecnt_pcpu
));
368 z
->zflags
= ZONE_BOOT
;
374 spin_init(&z
->zlock
, "zbootinit");
376 bzero(item
, (size_t)nitems
* z
->zsize
);
378 for (i
= 0; i
< nitems
; i
++) {
379 ((void **)item
)[0] = z
->zitems
;
381 ((void **)item
)[1] = (void *)ZENTRY_FREE
;
384 item
= (uint8_t *)item
+ z
->zsize
;
386 z
->zfreecnt
= nitems
;
390 lwkt_gettoken(&vm_token
);
391 LIST_INSERT_HEAD(&zlist
, z
, zlink
);
392 lwkt_reltoken(&vm_token
);
396 * Release all resources owned by zone created with zinit().
401 zdestroy(vm_zone_t z
)
407 panic("zdestroy: null zone");
408 if ((z
->zflags
& ZONE_DESTROYABLE
) == 0)
409 panic("zdestroy: undestroyable zone");
411 lwkt_gettoken(&vm_token
);
412 LIST_REMOVE(z
, zlink
);
413 lwkt_reltoken(&vm_token
);
416 * Release virtual mappings, physical memory and update sysctl stats.
418 if (z
->zflags
& ZONE_INTERRUPT
) {
420 * Pages mapped via pmap_kenter() must be removed from the
421 * kernel_pmap() before calling kmem_free() to avoid issues
422 * with kernel_pmap.pm_stats.resident_count.
424 pmap_qremove(z
->zkva
, z
->zpagemax
);
425 vm_object_hold(z
->zobj
);
426 for (i
= 0; i
< z
->zpagecount
; ++i
) {
427 m
= vm_page_lookup_busy_wait(z
->zobj
, i
, TRUE
, "vmzd");
428 vm_page_unwire(m
, 0);
435 kmem_free(&kernel_map
, z
->zkva
,
436 (size_t)z
->zpagemax
* PAGE_SIZE
);
437 atomic_subtract_long(&zone_kmem_kvaspace
,
438 (size_t)z
->zpagemax
* PAGE_SIZE
);
441 * Free the backing object and physical pages.
443 vm_object_deallocate(z
->zobj
);
444 vm_object_drop(z
->zobj
);
445 atomic_subtract_int(&zone_kmem_pages
, z
->zpagecount
);
447 for (i
=0; i
< z
->zkmcur
; i
++) {
448 kmem_free(&kernel_map
, z
->zkmvec
[i
],
449 (size_t)z
->zalloc
* PAGE_SIZE
);
450 atomic_subtract_int(&zone_kern_pages
, z
->zalloc
);
452 if (z
->zkmvec
!= NULL
)
453 kfree(z
->zkmvec
, M_ZONE
);
456 spin_uninit(&z
->zlock
);
462 * void *zalloc(vm_zone_t zone) --
463 * Returns an item from a specified zone. May not be called from a
464 * FAST interrupt or IPI function.
466 * void zfree(vm_zone_t zone, void *item) --
467 * Frees an item back to a specified zone. May not be called from a
468 * FAST interrupt or IPI function.
472 * Internal zone routine. Not to be called from external (non vm_zone) code.
489 panic("zget: null zone");
491 if (z
->zflags
& ZONE_INTERRUPT
) {
493 * Interrupt zones do not mess with the kernel_map, they
494 * simply populate an existing mapping.
496 * First reserve the required space.
498 vm_object_hold(z
->zobj
);
499 noffset
= (size_t)z
->zpagecount
* PAGE_SIZE
;
500 noffset
-= noffset
% z
->zsize
;
501 savezpc
= z
->zpagecount
;
502 if (z
->zpagecount
+ z
->zalloc
> z
->zpagemax
)
503 z
->zpagecount
= z
->zpagemax
;
505 z
->zpagecount
+= z
->zalloc
;
506 item
= (char *)z
->zkva
+ noffset
;
507 npages
= z
->zpagecount
- savezpc
;
508 nitems
= ((size_t)(savezpc
+ npages
) * PAGE_SIZE
- noffset
) /
510 atomic_add_int(&zone_kmem_pages
, npages
);
513 * Now allocate the pages. Note that we can block in the
514 * loop, so we've already done all the necessary calculations
515 * and reservations above.
517 for (i
= 0; i
< npages
; ++i
) {
520 m
= vm_page_alloc(z
->zobj
, savezpc
+ i
, z
->zallocflag
);
522 /* note: z might be modified due to blocking */
524 KKASSERT(m
->queue
== PQ_NONE
);
525 m
->valid
= VM_PAGE_BITS_ALL
;
529 zkva
= z
->zkva
+ (size_t)(savezpc
+ i
) * PAGE_SIZE
;
530 pmap_kenter(zkva
, VM_PAGE_TO_PHYS(m
));
531 bzero((void *)zkva
, PAGE_SIZE
);
533 vm_object_drop(z
->zobj
);
534 } else if (z
->zflags
& ZONE_SPECIAL
) {
536 * The special zone is the one used for vm_map_entry_t's.
537 * We have to avoid an infinite recursion in
538 * vm_map_entry_reserve() by using vm_map_entry_kreserve()
539 * instead. The map entries are pre-reserved by the kernel
540 * by vm_map_entry_reserve_cpu_init().
542 nbytes
= (size_t)z
->zalloc
* PAGE_SIZE
;
544 item
= (void *)kmem_alloc3(&kernel_map
, nbytes
, KM_KRESERVE
);
546 /* note: z might be modified due to blocking */
548 zone_kern_pages
+= z
->zalloc
; /* not MP-safe XXX */
553 nitems
= nbytes
/ z
->zsize
;
556 * Otherwise allocate KVA from the kernel_map.
558 nbytes
= (size_t)z
->zalloc
* PAGE_SIZE
;
560 item
= (void *)kmem_alloc3(&kernel_map
, nbytes
, 0);
562 /* note: z might be modified due to blocking */
564 zone_kern_pages
+= z
->zalloc
; /* not MP-safe XXX */
567 if (z
->zflags
& ZONE_DESTROYABLE
) {
568 if (z
->zkmcur
== z
->zkmmax
) {
570 z
->zkmmax
==0 ? 1 : z
->zkmmax
*2;
571 z
->zkmvec
= krealloc(z
->zkmvec
,
572 z
->zkmmax
* sizeof(z
->zkmvec
[0]),
575 z
->zkmvec
[z
->zkmcur
++] = (vm_offset_t
)item
;
580 nitems
= nbytes
/ z
->zsize
;
583 spin_lock(&z
->zlock
);
586 * Save one for immediate allocation
590 for (i
= 0; i
< nitems
; i
++) {
591 ((void **)item
)[0] = z
->zitems
;
593 ((void **)item
)[1] = (void *)ZENTRY_FREE
;
596 item
= (uint8_t *)item
+ z
->zsize
;
598 z
->zfreecnt
+= nitems
;
600 } else if (z
->zfreecnt
> 0) {
602 z
->zitems
= ((void **)item
)[0];
604 if (((void **)item
)[1] != (void *)ZENTRY_FREE
)
605 zerror(ZONE_ERROR_NOTFREE
);
606 ((void **) item
)[1] = NULL
;
613 spin_unlock(&z
->zlock
);
616 * A special zone may have used a kernel-reserved vm_map_entry. If
617 * so we have to be sure to recover our reserve so we don't run out.
618 * We will panic if we run out.
620 if (z
->zflags
& ZONE_SPECIAL
)
621 vm_map_entry_reserve(0);
630 sysctl_vm_zone(SYSCTL_HANDLER_ARGS
)
637 ksnprintf(tmpbuf
, sizeof(tmpbuf
),
638 "\nITEM SIZE LIMIT USED FREE REQUESTS\n");
639 error
= SYSCTL_OUT(req
, tmpbuf
, strlen(tmpbuf
));
643 lwkt_gettoken(&vm_token
);
644 LIST_FOREACH(curzone
, &zlist
, zlink
) {
651 len
= strlen(curzone
->zname
);
652 if (len
>= (sizeof(tmpname
) - 1))
653 len
= (sizeof(tmpname
) - 1);
654 for(i
= 0; i
< sizeof(tmpname
) - 1; i
++)
657 memcpy(tmpname
, curzone
->zname
, len
);
660 if (curzone
== LIST_FIRST(&zlist
)) {
664 freecnt
= curzone
->zfreecnt
;
665 for (n
= 0; n
< ncpus
; ++n
)
666 freecnt
+= curzone
->zfreecnt_pcpu
[n
];
668 ksnprintf(tmpbuf
+ offset
, sizeof(tmpbuf
) - offset
,
669 "%s %6.6u, %8.8u, %6.6u, %6.6u, %8.8u\n",
670 tmpname
, curzone
->zsize
, curzone
->zmax
,
671 (curzone
->ztotal
- freecnt
),
672 freecnt
, curzone
->znalloc
);
674 len
= strlen((char *)tmpbuf
);
675 if (LIST_NEXT(curzone
, zlink
) == NULL
)
678 error
= SYSCTL_OUT(req
, tmpbuf
, len
);
683 lwkt_reltoken(&vm_token
);
687 #if defined(INVARIANTS)
698 case ZONE_ERROR_INVALID
:
699 msg
= "zone: invalid zone";
701 case ZONE_ERROR_NOTFREE
:
702 msg
= "zone: entry not free";
704 case ZONE_ERROR_ALREADYFREE
:
705 msg
= "zone: freeing free entry";
708 msg
= "zone: invalid error";
715 SYSCTL_OID(_vm
, OID_AUTO
, zone
, CTLTYPE_STRING
|CTLFLAG_RD
, \
716 NULL
, 0, sysctl_vm_zone
, "A", "Zone Info");
718 SYSCTL_INT(_vm
, OID_AUTO
, zone_kmem_pages
,
719 CTLFLAG_RD
, &zone_kmem_pages
, 0, "Number of interrupt safe pages allocated by zone");
720 SYSCTL_INT(_vm
, OID_AUTO
, zone_burst
,
721 CTLFLAG_RW
, &zone_burst
, 0, "Burst from depot to pcpu cache");
722 SYSCTL_LONG(_vm
, OID_AUTO
, zone_kmem_kvaspace
,
723 CTLFLAG_RD
, &zone_kmem_kvaspace
, 0, "KVA space allocated by zone");
724 SYSCTL_INT(_vm
, OID_AUTO
, zone_kern_pages
,
725 CTLFLAG_RD
, &zone_kern_pages
, 0, "Number of non-interrupt safe pages allocated by zone");