Document that zones are deprecated and not MP-safe.
[dragonfly.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: 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.25 2008/01/21 20:00:42 nth Exp $
16  */
17
18 #include <sys/param.h>
19 #include <sys/systm.h>
20 #include <sys/kernel.h>
21 #include <sys/lock.h>
22 #include <sys/malloc.h>
23 #include <sys/sysctl.h>
24 #include <sys/vmmeter.h>
25
26 #include <vm/vm.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>
33 #include <sys/spinlock2.h>              /* XXX */
34
35 static MALLOC_DEFINE(M_ZONE, "ZONE", "Zone header");
36
37 #define ZONE_ERROR_INVALID 0
38 #define ZONE_ERROR_NOTFREE 1
39 #define ZONE_ERROR_ALREADYFREE 2
40
41 #define ZONE_ROUNDING   32
42
43 #define ZENTRY_FREE     0x12342378
44
45 static void *zget(vm_zone_t z);
46
47 /*
48  * Return an item from the specified zone.   This function is non-blocking for
49  * ZONE_INTERRUPT zones.
50  */
51 void *
52 zalloc(vm_zone_t z)
53 {
54         void *item;
55
56 #ifdef INVARIANTS
57         if (z == NULL)
58                 zerror(ZONE_ERROR_INVALID);
59 #endif
60         spin_lock_wr(&z->zlock);
61         if (z->zfreecnt > z->zfreemin) {
62                 item = z->zitems;
63 #ifdef INVARIANTS
64                 KASSERT(item != NULL, ("zitems unexpectedly NULL"));
65                 if (((void **) item)[1] != (void *) ZENTRY_FREE)
66                         zerror(ZONE_ERROR_NOTFREE);
67                 ((void **) item)[1] = 0;
68 #endif
69                 z->zitems = ((void **) item)[0];
70                 z->zfreecnt--;
71                 z->znalloc++;
72                 spin_unlock_wr(&z->zlock);
73         } else {
74                 spin_unlock_wr(&z->zlock);
75                 item = zget(z);
76                 /*
77                  * PANICFAIL allows the caller to assume that the zalloc()
78                  * will always succeed.  If it doesn't, we panic here.
79                  */
80                 if (item == NULL && (z->zflags & ZONE_PANICFAIL))
81                         panic("zalloc(%s) failed", z->zname);
82         }
83         return item;
84 }
85
86 /*
87  * Free an item to the specified zone.   
88  */
89 void
90 zfree(vm_zone_t z, void *item)
91 {
92
93         spin_lock_wr(&z->zlock);
94         ((void **) item)[0] = z->zitems;
95 #ifdef INVARIANTS
96         if (((void **) item)[1] == (void *) ZENTRY_FREE)
97                 zerror(ZONE_ERROR_ALREADYFREE);
98         ((void **) item)[1] = (void *) ZENTRY_FREE;
99 #endif
100         z->zitems = item;
101         z->zfreecnt++;
102         spin_unlock_wr(&z->zlock);
103 }
104
105 /*
106  * This file comprises a very simple zone allocator.  This is used
107  * in lieu of the malloc allocator, where needed or more optimal.
108  *
109  * Note that the initial implementation of this had coloring, and
110  * absolutely no improvement (actually perf degradation) occurred.
111  *
112  * Note also that the zones are type stable.  The only restriction is
113  * that the first two longwords of a data structure can be changed
114  * between allocations.  Any data that must be stable between allocations
115  * must reside in areas after the first two longwords.
116  *
117  * zinitna, zinit, zbootinit are the initialization routines.
118  * zalloc, zfree, are the allocation/free routines.
119  */
120
121 struct vm_zone *zlist;  /* exported to vmstat */
122 static int sysctl_vm_zone(SYSCTL_HANDLER_ARGS);
123 static int zone_kmem_pages, zone_kern_pages, zone_kmem_kvaspace;
124
125 /*
126  * Create a zone, but don't allocate the zone structure.  If the
127  * zone had been previously created by the zone boot code, initialize
128  * various parts of the zone code.
129  *
130  * If waits are not allowed during allocation (e.g. during interrupt
131  * code), a-priori allocate the kernel virtual space, and allocate
132  * only pages when needed.
133  *
134  * Arguments:
135  * z            pointer to zone structure.
136  * obj          pointer to VM object (opt).
137  * name         name of zone.
138  * size         size of zone entries.
139  * nentries     number of zone entries allocated (only ZONE_INTERRUPT.)
140  * flags        ZONE_INTERRUPT -- items can be allocated at interrupt time.
141  * zalloc       number of pages allocated when memory is needed.
142  *
143  * Note that when using ZONE_INTERRUPT, the size of the zone is limited
144  * by the nentries argument.  The size of the memory allocatable is
145  * unlimited if ZONE_INTERRUPT is not set.
146  *
147  */
148 int
149 zinitna(vm_zone_t z, vm_object_t obj, char *name, int size,
150         int nentries, int flags, int zalloc)
151 {
152         int totsize;
153
154         if ((z->zflags & ZONE_BOOT) == 0) {
155                 z->zsize = (size + ZONE_ROUNDING - 1) & ~(ZONE_ROUNDING - 1);
156                 spin_init(&z->zlock);
157                 z->zfreecnt = 0;
158                 z->ztotal = 0;
159                 z->zmax = 0;
160                 z->zname = name;
161                 z->znalloc = 0;
162                 z->zitems = NULL;
163
164                 z->znext = zlist;
165                 zlist = z;
166         }
167
168         z->zflags |= flags;
169
170         /*
171          * If we cannot wait, allocate KVA space up front, and we will fill
172          * in pages as needed.  This is particularly required when creating
173          * an allocation space for map entries in kernel_map, because we
174          * do not want to go into a recursion deadlock with 
175          * vm_map_entry_reserve().
176          */
177         if (z->zflags & ZONE_INTERRUPT) {
178                 totsize = round_page(z->zsize * nentries);
179                 zone_kmem_kvaspace += totsize;
180
181                 z->zkva = kmem_alloc_pageable(&kernel_map, totsize);
182                 if (z->zkva == 0) {
183                         zlist = z->znext;
184                         return 0;
185                 }
186
187                 z->zpagemax = totsize / PAGE_SIZE;
188                 if (obj == NULL) {
189                         z->zobj = vm_object_allocate(OBJT_DEFAULT, z->zpagemax);
190                 } else {
191                         z->zobj = obj;
192                         _vm_object_allocate(OBJT_DEFAULT, z->zpagemax, obj);
193                 }
194                 z->zallocflag = VM_ALLOC_SYSTEM | VM_ALLOC_INTERRUPT;
195                 z->zmax += nentries;
196         } else {
197                 z->zallocflag = VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM;
198                 z->zmax = 0;
199         }
200
201
202         if (z->zsize > PAGE_SIZE)
203                 z->zfreemin = 1;
204         else
205                 z->zfreemin = PAGE_SIZE / z->zsize;
206
207         z->zpagecount = 0;
208         if (zalloc)
209                 z->zalloc = zalloc;
210         else
211                 z->zalloc = 1;
212
213         /*
214          * Populate the interrrupt zone at creation time rather than
215          * on first allocation, as this is a potentially long operation.
216          */
217         if (z->zflags & ZONE_INTERRUPT) {
218                 void *buf;
219
220                 buf = zget(z);
221                 zfree(z, buf);
222         }
223
224         return 1;
225 }
226
227 /*
228  * Subroutine same as zinitna, except zone data structure is allocated
229  * automatically by malloc.  This routine should normally be used, except
230  * in certain tricky startup conditions in the VM system -- then
231  * zbootinit and zinitna can be used.  Zinit is the standard zone
232  * initialization call.
233  */
234 vm_zone_t
235 zinit(char *name, int size, int nentries, int flags, int zalloc)
236 {
237         vm_zone_t z;
238
239         z = (vm_zone_t) kmalloc(sizeof (struct vm_zone), M_ZONE, M_NOWAIT);
240         if (z == NULL)
241                 return NULL;
242
243         z->zflags = 0;
244         if (zinitna(z, NULL, name, size, nentries, flags, zalloc) == 0) {
245                 kfree(z, M_ZONE);
246                 return NULL;
247         }
248
249         return z;
250 }
251
252 /*
253  * Initialize a zone before the system is fully up.  This routine should
254  * only be called before full VM startup.
255  */
256 void
257 zbootinit(vm_zone_t z, char *name, int size, void *item, int nitems)
258 {
259         int i;
260
261         z->zname = name;
262         z->zsize = size;
263         z->zpagemax = 0;
264         z->zobj = NULL;
265         z->zflags = ZONE_BOOT;
266         z->zfreemin = 0;
267         z->zallocflag = 0;
268         z->zpagecount = 0;
269         z->zalloc = 0;
270         z->znalloc = 0;
271         spin_init(&z->zlock);
272
273         bzero(item, nitems * z->zsize);
274         z->zitems = NULL;
275         for (i = 0; i < nitems; i++) {
276                 ((void **) item)[0] = z->zitems;
277 #ifdef INVARIANTS
278                 ((void **) item)[1] = (void *) ZENTRY_FREE;
279 #endif
280                 z->zitems = item;
281                 item = (uint8_t *)item + z->zsize;
282         }
283         z->zfreecnt = nitems;
284         z->zmax = nitems;
285         z->ztotal = nitems;
286
287         if (zlist == 0) {
288                 zlist = z;
289         } else {
290                 z->znext = zlist;
291                 zlist = z;
292         }
293 }
294
295 /*
296  * void *zalloc(vm_zone_t zone) --
297  *      Returns an item from a specified zone.  May not be called from a
298  *      FAST interrupt or IPI function.
299  *
300  * void zfree(vm_zone_t zone, void *item) --
301  *      Frees an item back to a specified zone.  May not be called from a
302  *      FAST interrupt or IPI function.
303  */
304
305 /*
306  * Internal zone routine.  Not to be called from external (non vm_zone) code.
307  */
308 static void *
309 zget(vm_zone_t z)
310 {
311         int i;
312         vm_page_t m;
313         int nitems, nbytes;
314         void *item;
315
316         if (z == NULL)
317                 panic("zget: null zone");
318
319         if (z->zflags & ZONE_INTERRUPT) {
320                 /*
321                  * Interrupt zones do not mess with the kernel_map, they
322                  * simply populate an existing mapping.
323                  */
324                 nbytes = z->zpagecount * PAGE_SIZE;
325                 nbytes -= nbytes % z->zsize;
326                 item = (char *) z->zkva + nbytes;
327                 for (i = 0; ((i < z->zalloc) && (z->zpagecount < z->zpagemax));
328                      i++) {
329                         vm_offset_t zkva;
330
331                         m = vm_page_alloc(z->zobj, z->zpagecount,
332                                           z->zallocflag);
333                         /* note: z might be modified due to blocking */
334                         if (m == NULL)
335                                 break;
336
337                         zkva = z->zkva + z->zpagecount * PAGE_SIZE;
338                         pmap_kenter(zkva, VM_PAGE_TO_PHYS(m)); /* YYY */
339                         bzero((void *)zkva, PAGE_SIZE);
340                         z->zpagecount++;
341                         zone_kmem_pages++;
342                         vmstats.v_wire_count++;
343                 }
344                 nitems = ((z->zpagecount * PAGE_SIZE) - nbytes) / z->zsize;
345         } else if (z->zflags & ZONE_SPECIAL) {
346                 /*
347                  * The special zone is the one used for vm_map_entry_t's.
348                  * We have to avoid an infinite recursion in 
349                  * vm_map_entry_reserve() by using vm_map_entry_kreserve()
350                  * instead.  The map entries are pre-reserved by the kernel
351                  * by vm_map_entry_reserve_cpu_init().
352                  */
353                 nbytes = z->zalloc * PAGE_SIZE;
354
355                 item = (void *)kmem_alloc3(&kernel_map, nbytes, KM_KRESERVE);
356
357                 /* note: z might be modified due to blocking */
358                 if (item != NULL) {
359                         zone_kern_pages += z->zalloc;   /* not MP-safe XXX */
360                         bzero(item, nbytes);
361                 } else {
362                         nbytes = 0;
363                 }
364                 nitems = nbytes / z->zsize;
365         } else {
366                 /*
367                  * Otherwise allocate KVA from the kernel_map.
368                  */
369                 nbytes = z->zalloc * PAGE_SIZE;
370
371                 item = (void *)kmem_alloc3(&kernel_map, nbytes, 0);
372
373                 /* note: z might be modified due to blocking */
374                 if (item != NULL) {
375                         zone_kern_pages += z->zalloc;   /* not MP-safe XXX */
376                         bzero(item, nbytes);
377                 } else {
378                         nbytes = 0;
379                 }
380                 nitems = nbytes / z->zsize;
381         }
382
383         spin_lock_wr(&z->zlock);
384         z->ztotal += nitems;
385         /*
386          * Save one for immediate allocation
387          */
388         if (nitems != 0) {
389                 nitems -= 1;
390                 for (i = 0; i < nitems; i++) {
391                         ((void **) item)[0] = z->zitems;
392 #ifdef INVARIANTS
393                         ((void **) item)[1] = (void *) ZENTRY_FREE;
394 #endif
395                         z->zitems = item;
396                         item = (uint8_t *)item + z->zsize;
397                 }
398                 z->zfreecnt += nitems;
399                 z->znalloc++;
400         } else if (z->zfreecnt > 0) {
401                 item = z->zitems;
402                 z->zitems = ((void **) item)[0];
403 #ifdef INVARIANTS
404                 if (((void **) item)[1] != (void *) ZENTRY_FREE)
405                         zerror(ZONE_ERROR_NOTFREE);
406                 ((void **) item)[1] = 0;
407 #endif
408                 z->zfreecnt--;
409                 z->znalloc++;
410         } else {
411                 item = NULL;
412         }
413         spin_unlock_wr(&z->zlock);
414
415         /*
416          * A special zone may have used a kernel-reserved vm_map_entry.  If
417          * so we have to be sure to recover our reserve so we don't run out.
418          * We will panic if we run out.
419          */
420         if (z->zflags & ZONE_SPECIAL)
421                 vm_map_entry_reserve(0);
422
423         return item;
424 }
425
426 static int
427 sysctl_vm_zone(SYSCTL_HANDLER_ARGS)
428 {
429         int error=0;
430         vm_zone_t curzone, nextzone;
431         char tmpbuf[128];
432         char tmpname[14];
433
434         ksnprintf(tmpbuf, sizeof(tmpbuf),
435             "\nITEM            SIZE     LIMIT    USED    FREE  REQUESTS\n");
436         error = SYSCTL_OUT(req, tmpbuf, strlen(tmpbuf));
437         if (error)
438                 return (error);
439
440         for (curzone = zlist; curzone; curzone = nextzone) {
441                 int i;
442                 int len;
443                 int offset;
444
445                 nextzone = curzone->znext;
446                 len = strlen(curzone->zname);
447                 if (len >= (sizeof(tmpname) - 1))
448                         len = (sizeof(tmpname) - 1);
449                 for(i = 0; i < sizeof(tmpname) - 1; i++)
450                         tmpname[i] = ' ';
451                 tmpname[i] = 0;
452                 memcpy(tmpname, curzone->zname, len);
453                 tmpname[len] = ':';
454                 offset = 0;
455                 if (curzone == zlist) {
456                         offset = 1;
457                         tmpbuf[0] = '\n';
458                 }
459
460                 ksnprintf(tmpbuf + offset, sizeof(tmpbuf) - offset,
461                         "%s %6.6u, %8.8u, %6.6u, %6.6u, %8.8u\n",
462                         tmpname, curzone->zsize, curzone->zmax,
463                         (curzone->ztotal - curzone->zfreecnt),
464                         curzone->zfreecnt, curzone->znalloc);
465
466                 len = strlen((char *)tmpbuf);
467                 if (nextzone == NULL)
468                         tmpbuf[len - 1] = 0;
469
470                 error = SYSCTL_OUT(req, tmpbuf, len);
471
472                 if (error)
473                         return (error);
474         }
475         return (0);
476 }
477
478 #if defined(INVARIANTS)
479 void
480 zerror(int error)
481 {
482         char *msg;
483
484         switch (error) {
485         case ZONE_ERROR_INVALID:
486                 msg = "zone: invalid zone";
487                 break;
488         case ZONE_ERROR_NOTFREE:
489                 msg = "zone: entry not free";
490                 break;
491         case ZONE_ERROR_ALREADYFREE:
492                 msg = "zone: freeing free entry";
493                 break;
494         default:
495                 msg = "zone: invalid error";
496                 break;
497         }
498         panic(msg);
499 }
500 #endif
501
502 SYSCTL_OID(_vm, OID_AUTO, zone, CTLTYPE_STRING|CTLFLAG_RD, \
503         NULL, 0, sysctl_vm_zone, "A", "Zone Info");
504
505 SYSCTL_INT(_vm, OID_AUTO, zone_kmem_pages,
506         CTLFLAG_RD, &zone_kmem_pages, 0, "Number of interrupt safe pages allocated by zone");
507 SYSCTL_INT(_vm, OID_AUTO, zone_kmem_kvaspace,
508         CTLFLAG_RD, &zone_kmem_kvaspace, 0, "KVA space allocated by zone");
509 SYSCTL_INT(_vm, OID_AUTO, zone_kern_pages,
510         CTLFLAG_RD, &zone_kern_pages, 0, "Number of non-interrupt safe pages allocated by zone");