]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/vm/vm_zone.c
Fix some unused variables.
[FreeBSD/FreeBSD.git] / sys / vm / vm_zone.c
1 /*
2  * Copyright (c) 1997, 1998 John S. Dyson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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
12  *      John S. Dyson.
13  *
14  * $FreeBSD$
15  */
16
17 #include <sys/param.h>
18 #include <sys/systm.h>
19 #include <sys/kernel.h>
20 #include <sys/lock.h>
21 #include <sys/malloc.h>
22 #include <sys/proc.h>
23 #include <sys/mutex.h>
24 #include <sys/queue.h>
25 #include <sys/sysctl.h>
26 #include <sys/vmmeter.h>
27
28 #include <vm/vm.h>
29 #include <vm/vm_object.h>
30 #include <vm/vm_page.h>
31 #include <vm/vm_param.h>
32 #include <vm/vm_map.h>
33 #include <vm/vm_kern.h>
34 #include <vm/vm_extern.h>
35 #include <vm/vm_zone.h>
36
37 static MALLOC_DEFINE(M_ZONE, "ZONE", "Zone header");
38
39 #define ZENTRY_FREE             (void*)0x12342378
40
41 #define ZONE_ROUNDING           32
42
43 /*
44  * This file comprises a very simple zone allocator.  This is used
45  * in lieu of the malloc allocator, where needed or more optimal.
46  *
47  * Note that the initial implementation of this had coloring, and
48  * absolutely no improvement (actually perf degradation) occurred.
49  *
50  * Note also that the zones are type stable.  The only restriction is
51  * that the first two longwords of a data structure can be changed
52  * between allocations.  Any data that must be stable between allocations
53  * must reside in areas after the first two longwords.
54  *
55  * zinitna, zinit, zbootinit are the initialization routines.
56  * zalloc, zfree, are the allocation/free routines.
57  */
58
59 /*
60  * Subsystem lock.  Never grab it while holding a zone lock.
61  */
62 static struct mtx zone_mtx;
63
64 /*
65  * Singly-linked list of zones, for book-keeping purposes
66  */
67 static SLIST_HEAD(vm_zone_list, vm_zone) zlist;
68
69 /*
70  * Statistics
71  */
72 static int zone_kmem_pages;     /* Number of interrupt-safe pages allocated */
73 static int zone_kern_pages;     /* Number of KVA pages allocated */
74 static int zone_kmem_kvaspace;  /* Number of non-intsafe pages allocated */
75
76 /*
77  * Subsystem initialization, called from vm_mem_init()
78  */
79 void
80 vm_zone_init(void)
81 {
82         mtx_init(&zone_mtx, "zone subsystem", MTX_DEF);
83         SLIST_INIT(&zlist);
84 }
85
86 void
87 vm_zone_init2(void)
88 {
89         /*
90          * LATER: traverse zlist looking for partially initialized
91          * LATER: zones and finish initializing them.
92          */
93 }
94
95 /*
96  * Create a zone, but don't allocate the zone structure.  If the
97  * zone had been previously created by the zone boot code, initialize
98  * various parts of the zone code.
99  *
100  * If waits are not allowed during allocation (e.g. during interrupt
101  * code), a-priori allocate the kernel virtual space, and allocate
102  * only pages when needed.
103  *
104  * Arguments:
105  * z            pointer to zone structure.
106  * obj          pointer to VM object (opt).
107  * name         name of zone.
108  * size         size of zone entries.
109  * nentries     number of zone entries allocated (only ZONE_INTERRUPT.)
110  * flags        ZONE_INTERRUPT -- items can be allocated at interrupt time.
111  * zalloc       number of pages allocated when memory is needed.
112  *
113  * Note that when using ZONE_INTERRUPT, the size of the zone is limited
114  * by the nentries argument.  The size of the memory allocatable is
115  * unlimited if ZONE_INTERRUPT is not set.
116  *
117  */
118 int
119 zinitna(vm_zone_t z, vm_object_t obj, char *name, int size,
120         int nentries, int flags, int zalloc)
121 {
122         int totsize, oldzflags;
123
124         GIANT_REQUIRED;
125
126         oldzflags = z->zflags;
127         if ((z->zflags & ZONE_BOOT) == 0) {
128                 z->zsize = (size + ZONE_ROUNDING - 1) & ~(ZONE_ROUNDING - 1);
129                 z->zfreecnt = 0;
130                 z->ztotal = 0;
131                 z->zmax = 0;
132                 z->zname = name;
133                 z->znalloc = 0;
134                 z->zitems = NULL;
135         }
136
137         z->zflags |= flags;
138
139         /*
140          * If we cannot wait, allocate KVA space up front, and we will fill
141          * in pages as needed.
142          */
143         if (z->zflags & ZONE_INTERRUPT) {
144                 totsize = round_page(z->zsize * nentries);
145                 atomic_add_int(&zone_kmem_kvaspace, totsize);
146                 z->zkva = kmem_alloc_pageable(kernel_map, totsize);
147                 if (z->zkva == 0)
148                         return 0;
149
150                 z->zpagemax = totsize / PAGE_SIZE;
151                 if (obj == NULL) {
152                         z->zobj = vm_object_allocate(OBJT_DEFAULT, z->zpagemax);
153                 } else {
154                         z->zobj = obj;
155                         _vm_object_allocate(OBJT_DEFAULT, z->zpagemax, obj);
156                 }
157                 z->zallocflag = VM_ALLOC_INTERRUPT;
158                 z->zmax += nentries;
159         } else {
160                 z->zallocflag = VM_ALLOC_SYSTEM;
161                 z->zmax = 0;
162         }
163
164
165         if (z->zsize > PAGE_SIZE)
166                 z->zfreemin = 1;
167         else
168                 z->zfreemin = PAGE_SIZE / z->zsize;
169
170         z->zpagecount = 0;
171         if (zalloc)
172                 z->zalloc = zalloc;
173         else
174                 z->zalloc = 1;
175
176         /* our zone is good and ready, add it to the list */
177         if ((z->zflags & ZONE_BOOT) == 0) {
178                 mtx_init(&(z)->zmtx, "zone", MTX_DEF);
179                 mtx_lock(&zone_mtx);
180                 SLIST_INSERT_HEAD(&zlist, z, zent);
181                 mtx_unlock(&zone_mtx);
182         }
183         
184         return 1;
185 }
186
187 /*
188  * Subroutine same as zinitna, except zone data structure is allocated
189  * automatically by malloc.  This routine should normally be used, except
190  * in certain tricky startup conditions in the VM system -- then
191  * zbootinit and zinitna can be used.  Zinit is the standard zone
192  * initialization call.
193  */
194 vm_zone_t
195 zinit(char *name, int size, int nentries, int flags, int zalloc)
196 {
197         vm_zone_t z;
198
199         z = (vm_zone_t) malloc(sizeof (struct vm_zone), M_ZONE, M_NOWAIT | M_ZERO);
200         if (z == NULL)
201                 return NULL;
202
203         if (zinitna(z, NULL, name, size, nentries, flags, zalloc) == 0) {
204                 free(z, M_ZONE);
205                 return NULL;
206         }
207
208         return z;
209 }
210
211 /*
212  * Initialize a zone before the system is fully up.
213  *
214  * We can't rely on being able to allocate items dynamically, so we
215  * kickstart the zone with a number of static items provided by the
216  * caller.
217  *
218  * This routine should only be called before full VM startup.
219  */
220 void
221 zbootinit(vm_zone_t z, char *name, int size, void *item, int nitems)
222 {
223         int i;
224
225         z->zname = name;
226         z->zsize = size;
227         z->zpagemax = 0;
228         z->zobj = NULL;
229         z->zflags = ZONE_BOOT;
230         z->zfreemin = 0;
231         z->zallocflag = 0;
232         z->zpagecount = 0;
233         z->zalloc = 0;
234         z->znalloc = 0;
235         mtx_init(&(z)->zmtx, "zone", MTX_DEF);
236
237         bzero(item, nitems * z->zsize);
238         z->zitems = NULL;
239         for (i = 0; i < nitems; i++) {
240                 ((void **) item)[0] = z->zitems;
241 #ifdef INVARIANTS
242                 ((void **) item)[1] = ZENTRY_FREE;
243 #endif
244                 z->zitems = item;
245                 (char *) item += z->zsize;
246         }
247         z->zfreecnt = nitems;
248         z->zmax = nitems;
249         z->ztotal = nitems;
250
251         mtx_lock(&zone_mtx);
252         SLIST_INSERT_HEAD(&zlist, z, zent);
253         mtx_unlock(&zone_mtx);
254 }
255
256 /*
257  * Destroy a zone, freeing the allocated memory.
258  * This does not do any locking for the zone; make sure it is not used
259  * any more before calling. All zalloc()'ated memory in the zone must have
260  * been zfree()'d.
261  * zdestroy() may not be used with zbootinit()'ed zones.
262  */
263 void
264 zdestroy(vm_zone_t z)
265 {
266         int i, nitems, nbytes;
267         void *item, *min, **itp;
268         vm_map_t map;
269         vm_map_entry_t entry;
270         vm_object_t obj;
271         vm_pindex_t pindex;
272         vm_prot_t prot;
273         boolean_t wired;
274
275         GIANT_REQUIRED;
276         KASSERT(z != NULL, ("invalid zone"));
277         /*
278          * This is needed, or the algorithm used for non-interrupt zones will
279          * blow up badly.
280          */
281         KASSERT(z->ztotal == z->zfreecnt,
282             ("zdestroy() used with an active zone"));
283         KASSERT((z->zflags & ZONE_BOOT) == 0,
284             ("zdestroy() used with a zbootinit()'ed zone"));
285
286         if (z->zflags & ZONE_INTERRUPT) {
287                 kmem_free(kernel_map, z->zkva, z->zpagemax * PAGE_SIZE);
288                 vm_object_deallocate(z->zobj);
289                 atomic_subtract_int(&zone_kmem_kvaspace,
290                     z->zpagemax * PAGE_SIZE);
291                 atomic_subtract_int(&zone_kmem_pages,
292                     z->zpagecount);
293                 cnt.v_wire_count -= z->zpagecount;
294         } else {
295                 /*
296                  * This is evil h0h0 magic:
297                  * The items may be in z->zitems in a random oder; we have to
298                  * free the start of an allocated area, but do not want to save
299                  * extra information. Additionally, we may not access items that
300                  * were in a freed area.
301                  * This is achieved in the following way: the smallest address
302                  * is selected, and, after removing all items that are in a
303                  * range of z->zalloc * PAGE_SIZE (one allocation unit) from
304                  * it, kmem_free is called on it (since it is the smallest one,
305                  * it must be the start of an area). This is repeated until all
306                  * items are gone.
307                  */
308                 nbytes = z->zalloc * PAGE_SIZE;
309                 nitems = nbytes / z->zsize;
310                 while (z->zitems != NULL) {
311                         /* Find minimal element. */
312                         item = min = z->zitems;
313                         while (item != NULL) {
314                                 if (item < min)
315                                         min = item;
316                                 item = ((void **)item)[0];
317                         }
318                         /* Free. */
319                         itp = &z->zitems;
320                         i = 0;
321                         while (*itp != NULL && i < nitems) {
322                                 if ((char *)*itp >= (char *)min &&
323                                     (char *)*itp < (char *)min + nbytes) {
324                                         *itp = ((void **)*itp)[0];
325                                         i++;
326                                 } else
327                                         itp = &((void **)*itp)[0];
328                         }
329                         KASSERT(i == nitems, ("zdestroy(): corrupt zone"));
330                         /*
331                          * We can allocate from kmem_map (kmem_malloc) or
332                          * kernel_map (kmem_alloc).
333                          * kmem_map is a submap of kernel_map, so we can use
334                          * vm_map_lookup to retrieve the map we need to use.
335                          */
336                         map = kernel_map;
337                         if (vm_map_lookup(&map, (vm_offset_t)min, VM_PROT_NONE,
338                             &entry, &obj, &pindex, &prot, &wired) !=
339                             KERN_SUCCESS)
340                                 panic("zalloc mapping lost");
341                         /* Need to unlock. */
342                         vm_map_lookup_done(map, entry);
343                         if (map == kmem_map) {
344                                 atomic_subtract_int(&zone_kmem_pages,
345                                     z->zalloc);
346                         } else if (map == kernel_map) {
347                                 atomic_subtract_int(&zone_kern_pages,
348                                     z->zalloc);
349                         } else
350                                 panic("zdestroy(): bad map");
351                         kmem_free(map, (vm_offset_t)min, nbytes);
352                 }
353         }
354
355         mtx_lock(&zone_mtx);
356         SLIST_REMOVE(&zlist, z, vm_zone, zent);
357         mtx_unlock(&zone_mtx);
358         mtx_destroy(&z->zmtx);
359         free(z, M_ZONE);
360 }
361
362 /*
363  * Grow the specified zone to accomodate more items.
364  */
365 static void *
366 _zget(vm_zone_t z)
367 {
368         int i;
369         vm_page_t m;
370         int nitems, nbytes;
371         void *item;
372
373         KASSERT(z != NULL, ("invalid zone"));
374
375         if (z->zflags & ZONE_INTERRUPT) {
376                 nbytes = z->zpagecount * PAGE_SIZE;
377                 nbytes -= nbytes % z->zsize;
378                 item = (char *) z->zkva + nbytes;
379                 for (i = 0; ((i < z->zalloc) && (z->zpagecount < z->zpagemax));
380                      i++) {
381                         vm_offset_t zkva;
382
383                         m = vm_page_alloc(z->zobj, z->zpagecount,
384                                           z->zallocflag);
385                         if (m == NULL)
386                                 break;
387
388                         zkva = z->zkva + z->zpagecount * PAGE_SIZE;
389                         pmap_qenter(zkva, &m, 1);
390                         bzero((caddr_t) zkva, PAGE_SIZE);
391                         z->zpagecount++;
392                         atomic_add_int(&zone_kmem_pages, 1);
393                         cnt.v_wire_count++;
394                 }
395                 nitems = ((z->zpagecount * PAGE_SIZE) - nbytes) / z->zsize;
396         } else {
397                 /* Please check zdestroy() when changing this! */
398                 nbytes = z->zalloc * PAGE_SIZE;
399
400                 /*
401                  * Check to see if the kernel map is already locked.  We could allow
402                  * for recursive locks, but that eliminates a valuable debugging
403                  * mechanism, and opens up the kernel map for potential corruption
404                  * by inconsistent data structure manipulation.  We could also use
405                  * the interrupt allocation mechanism, but that has size limitations.
406                  * Luckily, we have kmem_map that is a submap of kernel map available
407                  * for memory allocation, and manipulation of that map doesn't affect
408                  * the kernel map structures themselves.
409                  *
410                  * We can wait, so just do normal map allocation in the appropriate
411                  * map.
412                  */
413                 mtx_unlock(&z->zmtx);
414                 if (lockstatus(&kernel_map->lock, NULL)) {
415                         item = (void *) kmem_malloc(kmem_map, nbytes, M_WAITOK);
416                         if (item != NULL)
417                                 atomic_add_int(&zone_kmem_pages, z->zalloc);
418                 } else {
419                         item = (void *) kmem_alloc(kernel_map, nbytes);
420                         if (item != NULL)
421                                 atomic_add_int(&zone_kern_pages, z->zalloc);
422                 }
423                 if (item != NULL) {
424                         bzero(item, nbytes);
425                 } else {
426                         nbytes = 0;
427                 }
428                 nitems = nbytes / z->zsize;
429                 mtx_lock(&z->zmtx);
430         }
431         z->ztotal += nitems;
432
433         /*
434          * Save one for immediate allocation
435          */
436         if (nitems != 0) {
437                 nitems -= 1;
438                 for (i = 0; i < nitems; i++) {
439                         ((void **) item)[0] = z->zitems;
440 #ifdef INVARIANTS
441                         ((void **) item)[1] = ZENTRY_FREE;
442 #endif
443                         z->zitems = item;
444                         (char *) item += z->zsize;
445                 }
446                 z->zfreecnt += nitems;
447                 z->znalloc++;
448         } else if (z->zfreecnt > 0) {
449                 item = z->zitems;
450                 z->zitems = ((void **) item)[0];
451 #ifdef INVARIANTS
452                 KASSERT(((void **) item)[1] == ZENTRY_FREE,
453                     ("item is not free"));
454                 ((void **) item)[1] = 0;
455 #endif
456                 z->zfreecnt--;
457                 z->znalloc++;
458         } else {
459                 item = NULL;
460         }
461
462         mtx_assert(&z->zmtx, MA_OWNED);
463         return item;
464 }
465
466 /*
467  * Allocates an item from the specified zone.
468  */
469 void *
470 zalloc(vm_zone_t z)
471 {
472         void *item;
473
474         KASSERT(z != NULL, ("invalid zone"));
475         mtx_lock(&z->zmtx);
476         
477         if (z->zfreecnt <= z->zfreemin) {
478                 item = _zget(z);
479                 goto out;
480         }
481
482         item = z->zitems;
483         z->zitems = ((void **) item)[0];
484 #ifdef INVARIANTS
485         KASSERT(((void **) item)[1] == ZENTRY_FREE,
486             ("item is not free"));
487         ((void **) item)[1] = 0;
488 #endif
489
490         z->zfreecnt--;
491         z->znalloc++;
492
493 out:    
494         mtx_unlock(&z->zmtx);
495         return item;
496 }
497
498 /*
499  * Frees an item back to the specified zone.
500  */
501 void
502 zfree(vm_zone_t z, void *item)
503 {
504         KASSERT(z != NULL, ("invalid zone"));
505         KASSERT(item != NULL, ("invalid item"));
506         mtx_lock(&z->zmtx);
507         
508         ((void **) item)[0] = z->zitems;
509 #ifdef INVARIANTS
510         KASSERT(((void **) item)[1] != ZENTRY_FREE,
511             ("item is already free"));
512         ((void **) item)[1] = (void *) ZENTRY_FREE;
513 #endif
514         z->zitems = item;
515         z->zfreecnt++;
516
517         mtx_unlock(&z->zmtx);
518 }
519
520 /*
521  * Sysctl handler for vm.zone 
522  */
523 static int
524 sysctl_vm_zone(SYSCTL_HANDLER_ARGS)
525 {
526         int error, len, cnt;
527         const int linesize = 128;       /* conservative */
528         char *tmpbuf, *offset;
529         vm_zone_t z;
530         char *p;
531
532         cnt = 0;
533         mtx_lock(&zone_mtx);
534         SLIST_FOREACH(z, &zlist, zent)
535                 cnt++;
536         mtx_unlock(&zone_mtx);
537         MALLOC(tmpbuf, char *, (cnt == 0 ? 1 : cnt) * linesize,
538                         M_TEMP, M_WAITOK);
539         len = snprintf(tmpbuf, linesize,
540             "\nITEM            SIZE     LIMIT    USED    FREE  REQUESTS\n\n");
541         if (cnt == 0)
542                 tmpbuf[len - 1] = '\0';
543         error = SYSCTL_OUT(req, tmpbuf, cnt == 0 ? len-1 : len);
544         if (error || cnt == 0)
545                 goto out;
546         offset = tmpbuf;
547         mtx_lock(&zone_mtx);
548         SLIST_FOREACH(z, &zlist, zent) {
549                 if (cnt == 0)   /* list may have changed size */
550                         break;
551                 mtx_lock(&z->zmtx);
552                 len = snprintf(offset, linesize,
553                     "%-12.12s  %6.6u, %8.8u, %6.6u, %6.6u, %8.8u\n",
554                     z->zname, z->zsize, z->zmax, (z->ztotal - z->zfreecnt),
555                     z->zfreecnt, z->znalloc);
556                 mtx_unlock(&z->zmtx);
557                 for (p = offset + 12; p > offset && *p == ' '; --p)
558                         /* nothing */ ;
559                 p[1] = ':';
560                 cnt--;
561                 offset += len;
562         }
563         mtx_unlock(&zone_mtx);
564         *offset++ = '\0';
565         error = SYSCTL_OUT(req, tmpbuf, offset - tmpbuf);
566 out:
567         FREE(tmpbuf, M_TEMP);
568         return (error);
569 }
570
571 SYSCTL_OID(_vm, OID_AUTO, zone, CTLTYPE_STRING|CTLFLAG_RD,
572     NULL, 0, sysctl_vm_zone, "A", "Zone Info");
573
574 SYSCTL_INT(_vm, OID_AUTO, zone_kmem_pages, CTLFLAG_RD, &zone_kmem_pages, 0,
575     "Number of interrupt safe pages allocated by zone");
576 SYSCTL_INT(_vm, OID_AUTO, zone_kmem_kvaspace, CTLFLAG_RD, &zone_kmem_kvaspace, 0,
577     "KVA space allocated by zone");
578 SYSCTL_INT(_vm, OID_AUTO, zone_kern_pages, CTLFLAG_RD, &zone_kern_pages, 0,
579     "Number of non-interrupt safe pages allocated by zone");