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