xref: /dflybsd-src/sys/kern/kern_slaballoc.c (revision c4be1ed54eff4542e7a8605007f73b381092df37)
1 /*
2  * (MPSAFE)
3  *
4  * KERN_SLABALLOC.C	- Kernel SLAB memory allocator
5  *
6  * Copyright (c) 2003,2004,2010 The DragonFly Project.  All rights reserved.
7  *
8  * This code is derived from software contributed to The DragonFly Project
9  * by Matthew Dillon <dillon@backplane.com>
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in
19  *    the documentation and/or other materials provided with the
20  *    distribution.
21  * 3. Neither the name of The DragonFly Project nor the names of its
22  *    contributors may be used to endorse or promote products derived
23  *    from this software without specific, prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
29  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
33  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
35  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * This module implements a slab allocator drop-in replacement for the
39  * kernel malloc().
40  *
41  * A slab allocator reserves a ZONE for each chunk size, then lays the
42  * chunks out in an array within the zone.  Allocation and deallocation
43  * is nearly instantanious, and fragmentation/overhead losses are limited
44  * to a fixed worst-case amount.
45  *
46  * The downside of this slab implementation is in the chunk size
47  * multiplied by the number of zones.  ~80 zones * 128K = 10MB of VM per cpu.
48  * In a kernel implementation all this memory will be physical so
49  * the zone size is adjusted downward on machines with less physical
50  * memory.  The upside is that overhead is bounded... this is the *worst*
51  * case overhead.
52  *
53  * Slab management is done on a per-cpu basis and no locking or mutexes
54  * are required, only a critical section.  When one cpu frees memory
55  * belonging to another cpu's slab manager an asynchronous IPI message
56  * will be queued to execute the operation.   In addition, both the
57  * high level slab allocator and the low level zone allocator optimize
58  * M_ZERO requests, and the slab allocator does not have to pre initialize
59  * the linked list of chunks.
60  *
61  * XXX Balancing is needed between cpus.  Balance will be handled through
62  * asynchronous IPIs primarily by reassigning the z_Cpu ownership of chunks.
63  *
64  * XXX If we have to allocate a new zone and M_USE_RESERVE is set, use of
65  * the new zone should be restricted to M_USE_RESERVE requests only.
66  *
67  *	Alloc Size	Chunking        Number of zones
68  *	0-127		8		16
69  *	128-255		16		8
70  *	256-511		32		8
71  *	512-1023	64		8
72  *	1024-2047	128		8
73  *	2048-4095	256		8
74  *	4096-8191	512		8
75  *	8192-16383	1024		8
76  *	16384-32767	2048		8
77  *	(if PAGE_SIZE is 4K the maximum zone allocation is 16383)
78  *
79  *	Allocations >= ZoneLimit go directly to kmem.
80  *
81  * Alignment properties:
82  * - All power-of-2 sized allocations are power-of-2 aligned.
83  * - Allocations with M_POWEROF2 are power-of-2 aligned on the nearest
84  *   power-of-2 round up of 'size'.
85  * - Non-power-of-2 sized allocations are zone chunk size aligned (see the
86  *   above table 'Chunking' column).
87  *
88  *			API REQUIREMENTS AND SIDE EFFECTS
89  *
90  *    To operate as a drop-in replacement to the FreeBSD-4.x malloc() we
91  *    have remained compatible with the following API requirements:
92  *
93  *    + malloc(0) is allowed and returns non-NULL (ahc driver)
94  *    + ability to allocate arbitrarily large chunks of memory
95  */
96 
97 #include "opt_vm.h"
98 
99 #include <sys/param.h>
100 #include <sys/systm.h>
101 #include <sys/kernel.h>
102 #include <sys/slaballoc.h>
103 #include <sys/mbuf.h>
104 #include <sys/vmmeter.h>
105 #include <sys/lock.h>
106 #include <sys/thread.h>
107 #include <sys/globaldata.h>
108 #include <sys/sysctl.h>
109 #include <sys/ktr.h>
110 
111 #include <vm/vm.h>
112 #include <vm/vm_param.h>
113 #include <vm/vm_kern.h>
114 #include <vm/vm_extern.h>
115 #include <vm/vm_object.h>
116 #include <vm/pmap.h>
117 #include <vm/vm_map.h>
118 #include <vm/vm_page.h>
119 #include <vm/vm_pageout.h>
120 
121 #include <machine/cpu.h>
122 
123 #include <sys/thread2.h>
124 #include <vm/vm_page2.h>
125 
126 #define btokup(z)	(&pmap_kvtom((vm_offset_t)(z))->ku_pagecnt)
127 
128 #define MEMORY_STRING	"ptr=%p type=%p size=%lu flags=%04x"
129 #define MEMORY_ARGS	void *ptr, void *type, unsigned long size, int flags
130 
131 #if !defined(KTR_MEMORY)
132 #define KTR_MEMORY	KTR_ALL
133 #endif
134 KTR_INFO_MASTER(memory);
135 KTR_INFO(KTR_MEMORY, memory, malloc_beg, 0, "malloc begin");
136 KTR_INFO(KTR_MEMORY, memory, malloc_end, 1, MEMORY_STRING, MEMORY_ARGS);
137 KTR_INFO(KTR_MEMORY, memory, free_zero, 2, MEMORY_STRING, MEMORY_ARGS);
138 KTR_INFO(KTR_MEMORY, memory, free_ovsz, 3, MEMORY_STRING, MEMORY_ARGS);
139 KTR_INFO(KTR_MEMORY, memory, free_ovsz_delayed, 4, MEMORY_STRING, MEMORY_ARGS);
140 KTR_INFO(KTR_MEMORY, memory, free_chunk, 5, MEMORY_STRING, MEMORY_ARGS);
141 KTR_INFO(KTR_MEMORY, memory, free_request, 6, MEMORY_STRING, MEMORY_ARGS);
142 KTR_INFO(KTR_MEMORY, memory, free_rem_beg, 7, MEMORY_STRING, MEMORY_ARGS);
143 KTR_INFO(KTR_MEMORY, memory, free_rem_end, 8, MEMORY_STRING, MEMORY_ARGS);
144 KTR_INFO(KTR_MEMORY, memory, free_beg, 9, "free begin");
145 KTR_INFO(KTR_MEMORY, memory, free_end, 10, "free end");
146 
147 #define logmemory(name, ptr, type, size, flags)				\
148 	KTR_LOG(memory_ ## name, ptr, type, size, flags)
149 #define logmemory_quick(name)						\
150 	KTR_LOG(memory_ ## name)
151 
152 /*
153  * Fixed globals (not per-cpu)
154  */
155 static int ZoneSize;
156 static int ZoneLimit;
157 static int ZonePageCount;
158 static uintptr_t ZoneMask;
159 static int ZoneBigAlloc;		/* in KB */
160 static int ZoneGenAlloc;		/* in KB */
161 struct malloc_type *kmemstatistics;	/* exported to vmstat */
162 static int32_t weirdary[16];
163 
164 static void *kmem_slab_alloc(vm_size_t bytes, vm_offset_t align, int flags);
165 static void kmem_slab_free(void *ptr, vm_size_t bytes);
166 
167 #if defined(INVARIANTS)
168 static void chunk_mark_allocated(SLZone *z, void *chunk);
169 static void chunk_mark_free(SLZone *z, void *chunk);
170 #else
171 #define chunk_mark_allocated(z, chunk)
172 #define chunk_mark_free(z, chunk)
173 #endif
174 
175 /*
176  * Misc constants.  Note that allocations that are exact multiples of
177  * PAGE_SIZE, or exceed the zone limit, fall through to the kmem module.
178  */
179 #define ZONE_RELS_THRESH	32		/* threshold number of zones */
180 
181 /*
182  * The WEIRD_ADDR is used as known text to copy into free objects to
183  * try to create deterministic failure cases if the data is accessed after
184  * free.
185  */
186 #define WEIRD_ADDR      0xdeadc0de
187 #define MAX_COPY        sizeof(weirdary)
188 #define ZERO_LENGTH_PTR	((void *)-8)
189 
190 /*
191  * Misc global malloc buckets
192  */
193 
194 MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches");
195 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
196 MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers");
197 
198 MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
199 MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery");
200 
201 /*
202  * Initialize the slab memory allocator.  We have to choose a zone size based
203  * on available physical memory.  We choose a zone side which is approximately
204  * 1/1024th of our memory, so if we have 128MB of ram we have a zone size of
205  * 128K.  The zone size is limited to the bounds set in slaballoc.h
206  * (typically 32K min, 128K max).
207  */
208 static void kmeminit(void *dummy);
209 
210 char *ZeroPage;
211 
212 SYSINIT(kmem, SI_BOOT1_ALLOCATOR, SI_ORDER_FIRST, kmeminit, NULL)
213 
214 #ifdef INVARIANTS
215 /*
216  * If enabled any memory allocated without M_ZERO is initialized to -1.
217  */
218 static int  use_malloc_pattern;
219 SYSCTL_INT(_debug, OID_AUTO, use_malloc_pattern, CTLFLAG_RW,
220     &use_malloc_pattern, 0,
221     "Initialize memory to -1 if M_ZERO not specified");
222 #endif
223 
224 static int ZoneRelsThresh = ZONE_RELS_THRESH;
225 SYSCTL_INT(_kern, OID_AUTO, zone_big_alloc, CTLFLAG_RD, &ZoneBigAlloc, 0, "");
226 SYSCTL_INT(_kern, OID_AUTO, zone_gen_alloc, CTLFLAG_RD, &ZoneGenAlloc, 0, "");
227 SYSCTL_INT(_kern, OID_AUTO, zone_cache, CTLFLAG_RW, &ZoneRelsThresh, 0, "");
228 static long SlabsAllocated;
229 static long SlabsFreed;
230 SYSCTL_LONG(_kern, OID_AUTO, slabs_allocated, CTLFLAG_RD, &SlabsAllocated, 0, "");
231 SYSCTL_LONG(_kern, OID_AUTO, slabs_freed, CTLFLAG_RD, &SlabsFreed, 0, "");
232 
233 /*
234  * Returns the kernel memory size limit for the purposes of initializing
235  * various subsystem caches.  The smaller of available memory and the KVM
236  * memory space is returned.
237  *
238  * The size in megabytes is returned.
239  */
240 size_t
241 kmem_lim_size(void)
242 {
243     size_t limsize;
244 
245     limsize = (size_t)vmstats.v_page_count * PAGE_SIZE;
246     if (limsize > KvaSize)
247 	limsize = KvaSize;
248     return (limsize / (1024 * 1024));
249 }
250 
251 static void
252 kmeminit(void *dummy)
253 {
254     size_t limsize;
255     int usesize;
256     int i;
257 
258     limsize = kmem_lim_size();
259     usesize = (int)(limsize * 1024);	/* convert to KB */
260 
261     /*
262      * If the machine has a large KVM space and more than 8G of ram,
263      * double the zone release threshold to reduce SMP invalidations.
264      * If more than 16G of ram, do it again.
265      *
266      * The BIOS eats a little ram so add some slop.  We want 8G worth of
267      * memory sticks to trigger the first adjustment.
268      */
269     if (ZoneRelsThresh == ZONE_RELS_THRESH) {
270 	    if (limsize >= 7 * 1024)
271 		    ZoneRelsThresh *= 2;
272 	    if (limsize >= 15 * 1024)
273 		    ZoneRelsThresh *= 2;
274     }
275 
276     /*
277      * Calculate the zone size.  This typically calculates to
278      * ZALLOC_MAX_ZONE_SIZE
279      */
280     ZoneSize = ZALLOC_MIN_ZONE_SIZE;
281     while (ZoneSize < ZALLOC_MAX_ZONE_SIZE && (ZoneSize << 1) < usesize)
282 	ZoneSize <<= 1;
283     ZoneLimit = ZoneSize / 4;
284     if (ZoneLimit > ZALLOC_ZONE_LIMIT)
285 	ZoneLimit = ZALLOC_ZONE_LIMIT;
286     ZoneMask = ~(uintptr_t)(ZoneSize - 1);
287     ZonePageCount = ZoneSize / PAGE_SIZE;
288 
289     for (i = 0; i < NELEM(weirdary); ++i)
290 	weirdary[i] = WEIRD_ADDR;
291 
292     ZeroPage = kmem_slab_alloc(PAGE_SIZE, PAGE_SIZE, M_WAITOK|M_ZERO);
293 
294     if (bootverbose)
295 	kprintf("Slab ZoneSize set to %dKB\n", ZoneSize / 1024);
296 }
297 
298 /*
299  * Initialize a malloc type tracking structure.
300  */
301 void
302 malloc_init(void *data)
303 {
304     struct malloc_type *type = data;
305     size_t limsize;
306 
307     if (type->ks_magic != M_MAGIC)
308 	panic("malloc type lacks magic");
309 
310     if (type->ks_limit != 0)
311 	return;
312 
313     if (vmstats.v_page_count == 0)
314 	panic("malloc_init not allowed before vm init");
315 
316     limsize = kmem_lim_size() * (1024 * 1024);
317     type->ks_limit = limsize / 10;
318 
319     type->ks_next = kmemstatistics;
320     kmemstatistics = type;
321 }
322 
323 void
324 malloc_uninit(void *data)
325 {
326     struct malloc_type *type = data;
327     struct malloc_type *t;
328 #ifdef INVARIANTS
329     int i;
330     long ttl;
331 #endif
332 
333     if (type->ks_magic != M_MAGIC)
334 	panic("malloc type lacks magic");
335 
336     if (vmstats.v_page_count == 0)
337 	panic("malloc_uninit not allowed before vm init");
338 
339     if (type->ks_limit == 0)
340 	panic("malloc_uninit on uninitialized type");
341 
342     /* Make sure that all pending kfree()s are finished. */
343     lwkt_synchronize_ipiqs("muninit");
344 
345 #ifdef INVARIANTS
346     /*
347      * memuse is only correct in aggregation.  Due to memory being allocated
348      * on one cpu and freed on another individual array entries may be
349      * negative or positive (canceling each other out).
350      */
351     for (i = ttl = 0; i < ncpus; ++i)
352 	ttl += type->ks_memuse[i];
353     if (ttl) {
354 	kprintf("malloc_uninit: %ld bytes of '%s' still allocated on cpu %d\n",
355 	    ttl, type->ks_shortdesc, i);
356     }
357 #endif
358     if (type == kmemstatistics) {
359 	kmemstatistics = type->ks_next;
360     } else {
361 	for (t = kmemstatistics; t->ks_next != NULL; t = t->ks_next) {
362 	    if (t->ks_next == type) {
363 		t->ks_next = type->ks_next;
364 		break;
365 	    }
366 	}
367     }
368     type->ks_next = NULL;
369     type->ks_limit = 0;
370 }
371 
372 /*
373  * Increase the kmalloc pool limit for the specified pool.  No changes
374  * are the made if the pool would shrink.
375  */
376 void
377 kmalloc_raise_limit(struct malloc_type *type, size_t bytes)
378 {
379     if (type->ks_limit == 0)
380 	malloc_init(type);
381     if (bytes == 0)
382 	bytes = KvaSize;
383     if (type->ks_limit < bytes)
384 	type->ks_limit = bytes;
385 }
386 
387 /*
388  * Dynamically create a malloc pool.  This function is a NOP if *typep is
389  * already non-NULL.
390  */
391 void
392 kmalloc_create(struct malloc_type **typep, const char *descr)
393 {
394 	struct malloc_type *type;
395 
396 	if (*typep == NULL) {
397 		type = kmalloc(sizeof(*type), M_TEMP, M_WAITOK | M_ZERO);
398 		type->ks_magic = M_MAGIC;
399 		type->ks_shortdesc = descr;
400 		malloc_init(type);
401 		*typep = type;
402 	}
403 }
404 
405 /*
406  * Destroy a dynamically created malloc pool.  This function is a NOP if
407  * the pool has already been destroyed.
408  */
409 void
410 kmalloc_destroy(struct malloc_type **typep)
411 {
412 	if (*typep != NULL) {
413 		malloc_uninit(*typep);
414 		kfree(*typep, M_TEMP);
415 		*typep = NULL;
416 	}
417 }
418 
419 /*
420  * Calculate the zone index for the allocation request size and set the
421  * allocation request size to that particular zone's chunk size.
422  */
423 static __inline int
424 zoneindex(unsigned long *bytes, unsigned long *align)
425 {
426     unsigned int n = (unsigned int)*bytes;	/* unsigned for shift opt */
427     if (n < 128) {
428 	*bytes = n = (n + 7) & ~7;
429 	*align = 8;
430 	return(n / 8 - 1);		/* 8 byte chunks, 16 zones */
431     }
432     if (n < 256) {
433 	*bytes = n = (n + 15) & ~15;
434 	*align = 16;
435 	return(n / 16 + 7);
436     }
437     if (n < 8192) {
438 	if (n < 512) {
439 	    *bytes = n = (n + 31) & ~31;
440 	    *align = 32;
441 	    return(n / 32 + 15);
442 	}
443 	if (n < 1024) {
444 	    *bytes = n = (n + 63) & ~63;
445 	    *align = 64;
446 	    return(n / 64 + 23);
447 	}
448 	if (n < 2048) {
449 	    *bytes = n = (n + 127) & ~127;
450 	    *align = 128;
451 	    return(n / 128 + 31);
452 	}
453 	if (n < 4096) {
454 	    *bytes = n = (n + 255) & ~255;
455 	    *align = 256;
456 	    return(n / 256 + 39);
457 	}
458 	*bytes = n = (n + 511) & ~511;
459 	*align = 512;
460 	return(n / 512 + 47);
461     }
462 #if ZALLOC_ZONE_LIMIT > 8192
463     if (n < 16384) {
464 	*bytes = n = (n + 1023) & ~1023;
465 	*align = 1024;
466 	return(n / 1024 + 55);
467     }
468 #endif
469 #if ZALLOC_ZONE_LIMIT > 16384
470     if (n < 32768) {
471 	*bytes = n = (n + 2047) & ~2047;
472 	*align = 2048;
473 	return(n / 2048 + 63);
474     }
475 #endif
476     panic("Unexpected byte count %d", n);
477     return(0);
478 }
479 
480 static __inline
481 void
482 clean_zone_rchunks(SLZone *z)
483 {
484     SLChunk *bchunk;
485 
486     while ((bchunk = z->z_RChunks) != NULL) {
487 	cpu_ccfence();
488 	if (atomic_cmpset_ptr(&z->z_RChunks, bchunk, NULL)) {
489 	    *z->z_LChunksp = bchunk;
490 	    while (bchunk) {
491 		chunk_mark_free(z, bchunk);
492 		z->z_LChunksp = &bchunk->c_Next;
493 		bchunk = bchunk->c_Next;
494 		++z->z_NFree;
495 	    }
496 	    break;
497 	}
498 	/* retry */
499     }
500 }
501 
502 /*
503  * If the zone becomes totally free, and there are other zones we
504  * can allocate from, move this zone to the FreeZones list.  Since
505  * this code can be called from an IPI callback, do *NOT* try to mess
506  * with kernel_map here.  Hysteresis will be performed at malloc() time.
507  */
508 static __inline
509 SLZone *
510 check_zone_free(SLGlobalData *slgd, SLZone *z)
511 {
512     if (z->z_NFree == z->z_NMax &&
513 	(z->z_Next || slgd->ZoneAry[z->z_ZoneIndex] != z) &&
514 	z->z_RCount == 0
515     ) {
516 	SLZone **pz;
517 	int *kup;
518 
519 	for (pz = &slgd->ZoneAry[z->z_ZoneIndex]; z != *pz; pz = &(*pz)->z_Next)
520 	    ;
521 	*pz = z->z_Next;
522 	z->z_Magic = -1;
523 	z->z_Next = slgd->FreeZones;
524 	slgd->FreeZones = z;
525 	++slgd->NFreeZones;
526 	kup = btokup(z);
527 	*kup = 0;
528 	z = *pz;
529     } else {
530 	z = z->z_Next;
531     }
532     return z;
533 }
534 
535 #ifdef SLAB_DEBUG
536 /*
537  * Used to debug memory corruption issues.  Record up to (typically 32)
538  * allocation sources for this zone (for a particular chunk size).
539  */
540 
541 static void
542 slab_record_source(SLZone *z, const char *file, int line)
543 {
544     int i;
545     int b = line & (SLAB_DEBUG_ENTRIES - 1);
546 
547     i = b;
548     do {
549 	if (z->z_Sources[i].file == file && z->z_Sources[i].line == line)
550 		return;
551 	if (z->z_Sources[i].file == NULL)
552 		break;
553 	i = (i + 1) & (SLAB_DEBUG_ENTRIES - 1);
554     } while (i != b);
555     z->z_Sources[i].file = file;
556     z->z_Sources[i].line = line;
557 }
558 
559 #endif
560 
561 static __inline unsigned long
562 powerof2_size(unsigned long size)
563 {
564 	int i;
565 
566 	if (size == 0 || powerof2(size))
567 		return size;
568 
569 	i = flsl(size);
570 	return (1UL << i);
571 }
572 
573 /*
574  * kmalloc()	(SLAB ALLOCATOR)
575  *
576  *	Allocate memory via the slab allocator.  If the request is too large,
577  *	or if it page-aligned beyond a certain size, we fall back to the
578  *	KMEM subsystem.  A SLAB tracking descriptor must be specified, use
579  *	&SlabMisc if you don't care.
580  *
581  *	M_RNOWAIT	- don't block.
582  *	M_NULLOK	- return NULL instead of blocking.
583  *	M_ZERO		- zero the returned memory.
584  *	M_USE_RESERVE	- allow greater drawdown of the free list
585  *	M_USE_INTERRUPT_RESERVE - allow the freelist to be exhausted
586  *	M_POWEROF2	- roundup size to the nearest power of 2
587  *
588  * MPSAFE
589  */
590 
591 #ifdef SLAB_DEBUG
592 void *
593 kmalloc_debug(unsigned long size, struct malloc_type *type, int flags,
594 	      const char *file, int line)
595 #else
596 void *
597 kmalloc(unsigned long size, struct malloc_type *type, int flags)
598 #endif
599 {
600     SLZone *z;
601     SLChunk *chunk;
602     SLGlobalData *slgd;
603     struct globaldata *gd;
604     unsigned long align;
605     int zi;
606 #ifdef INVARIANTS
607     int i;
608 #endif
609 
610     logmemory_quick(malloc_beg);
611     gd = mycpu;
612     slgd = &gd->gd_slab;
613 
614     /*
615      * XXX silly to have this in the critical path.
616      */
617     if (type->ks_limit == 0) {
618 	crit_enter();
619 	malloc_init(type);
620 	crit_exit();
621     }
622     ++type->ks_calls;
623 
624     if (flags & M_POWEROF2)
625 	size = powerof2_size(size);
626 
627     /*
628      * Handle the case where the limit is reached.  Panic if we can't return
629      * NULL.  The original malloc code looped, but this tended to
630      * simply deadlock the computer.
631      *
632      * ks_loosememuse is an up-only limit that is NOT MP-synchronized, used
633      * to determine if a more complete limit check should be done.  The
634      * actual memory use is tracked via ks_memuse[cpu].
635      */
636     while (type->ks_loosememuse >= type->ks_limit) {
637 	int i;
638 	long ttl;
639 
640 	for (i = ttl = 0; i < ncpus; ++i)
641 	    ttl += type->ks_memuse[i];
642 	type->ks_loosememuse = ttl;	/* not MP synchronized */
643 	if ((ssize_t)ttl < 0)		/* deal with occassional race */
644 		ttl = 0;
645 	if (ttl >= type->ks_limit) {
646 	    if (flags & M_NULLOK) {
647 		logmemory(malloc_end, NULL, type, size, flags);
648 		return(NULL);
649 	    }
650 	    panic("%s: malloc limit exceeded", type->ks_shortdesc);
651 	}
652     }
653 
654     /*
655      * Handle the degenerate size == 0 case.  Yes, this does happen.
656      * Return a special pointer.  This is to maintain compatibility with
657      * the original malloc implementation.  Certain devices, such as the
658      * adaptec driver, not only allocate 0 bytes, they check for NULL and
659      * also realloc() later on.  Joy.
660      */
661     if (size == 0) {
662 	logmemory(malloc_end, ZERO_LENGTH_PTR, type, size, flags);
663 	return(ZERO_LENGTH_PTR);
664     }
665 
666     /*
667      * Handle hysteresis from prior frees here in malloc().  We cannot
668      * safely manipulate the kernel_map in free() due to free() possibly
669      * being called via an IPI message or from sensitive interrupt code.
670      *
671      * NOTE: ku_pagecnt must be cleared before we free the slab or we
672      *	     might race another cpu allocating the kva and setting
673      *	     ku_pagecnt.
674      */
675     while (slgd->NFreeZones > ZoneRelsThresh && (flags & M_RNOWAIT) == 0) {
676 	crit_enter();
677 	if (slgd->NFreeZones > ZoneRelsThresh) {	/* crit sect race */
678 	    int *kup;
679 
680 	    z = slgd->FreeZones;
681 	    slgd->FreeZones = z->z_Next;
682 	    --slgd->NFreeZones;
683 	    kup = btokup(z);
684 	    *kup = 0;
685 	    kmem_slab_free(z, ZoneSize);	/* may block */
686 	    atomic_add_int(&ZoneGenAlloc, -ZoneSize / 1024);
687 	}
688 	crit_exit();
689     }
690 
691     /*
692      * XXX handle oversized frees that were queued from kfree().
693      */
694     while (slgd->FreeOvZones && (flags & M_RNOWAIT) == 0) {
695 	crit_enter();
696 	if ((z = slgd->FreeOvZones) != NULL) {
697 	    vm_size_t tsize;
698 
699 	    KKASSERT(z->z_Magic == ZALLOC_OVSZ_MAGIC);
700 	    slgd->FreeOvZones = z->z_Next;
701 	    tsize = z->z_ChunkSize;
702 	    kmem_slab_free(z, tsize);	/* may block */
703 	    atomic_add_int(&ZoneBigAlloc, -(int)tsize / 1024);
704 	}
705 	crit_exit();
706     }
707 
708     /*
709      * Handle large allocations directly.  There should not be very many of
710      * these so performance is not a big issue.
711      *
712      * The backend allocator is pretty nasty on a SMP system.   Use the
713      * slab allocator for one and two page-sized chunks even though we lose
714      * some efficiency.  XXX maybe fix mmio and the elf loader instead.
715      */
716     if (size >= ZoneLimit || ((size & PAGE_MASK) == 0 && size > PAGE_SIZE*2)) {
717 	int *kup;
718 
719 	size = round_page(size);
720 	chunk = kmem_slab_alloc(size, PAGE_SIZE, flags);
721 	if (chunk == NULL) {
722 	    logmemory(malloc_end, NULL, type, size, flags);
723 	    return(NULL);
724 	}
725 	atomic_add_int(&ZoneBigAlloc, (int)size / 1024);
726 	flags &= ~M_ZERO;	/* result already zero'd if M_ZERO was set */
727 	flags |= M_PASSIVE_ZERO;
728 	kup = btokup(chunk);
729 	*kup = size / PAGE_SIZE;
730 	crit_enter();
731 	goto done;
732     }
733 
734     /*
735      * Attempt to allocate out of an existing zone.  First try the free list,
736      * then allocate out of unallocated space.  If we find a good zone move
737      * it to the head of the list so later allocations find it quickly
738      * (we might have thousands of zones in the list).
739      *
740      * Note: zoneindex() will panic of size is too large.
741      */
742     zi = zoneindex(&size, &align);
743     KKASSERT(zi < NZONES);
744     crit_enter();
745 
746     if ((z = slgd->ZoneAry[zi]) != NULL) {
747 	/*
748 	 * Locate a chunk - we have to have at least one.  If this is the
749 	 * last chunk go ahead and do the work to retrieve chunks freed
750 	 * from remote cpus, and if the zone is still empty move it off
751 	 * the ZoneAry.
752 	 */
753 	if (--z->z_NFree <= 0) {
754 	    KKASSERT(z->z_NFree == 0);
755 
756 	    /*
757 	     * WARNING! This code competes with other cpus.  It is ok
758 	     * for us to not drain RChunks here but we might as well, and
759 	     * it is ok if more accumulate after we're done.
760 	     *
761 	     * Set RSignal before pulling rchunks off, indicating that we
762 	     * will be moving ourselves off of the ZoneAry.  Remote ends will
763 	     * read RSignal before putting rchunks on thus interlocking
764 	     * their IPI signaling.
765 	     */
766 	    if (z->z_RChunks == NULL)
767 		atomic_swap_int(&z->z_RSignal, 1);
768 
769 	    clean_zone_rchunks(z);
770 
771 	    /*
772 	     * Remove from the zone list if no free chunks remain.
773 	     * Clear RSignal
774 	     */
775 	    if (z->z_NFree == 0) {
776 		slgd->ZoneAry[zi] = z->z_Next;
777 		z->z_Next = NULL;
778 	    } else {
779 		z->z_RSignal = 0;
780 	    }
781 	}
782 
783 	/*
784 	 * Fast path, we have chunks available in z_LChunks.
785 	 */
786 	chunk = z->z_LChunks;
787 	if (chunk) {
788 		chunk_mark_allocated(z, chunk);
789 		z->z_LChunks = chunk->c_Next;
790 		if (z->z_LChunks == NULL)
791 			z->z_LChunksp = &z->z_LChunks;
792 #ifdef SLAB_DEBUG
793 		slab_record_source(z, file, line);
794 #endif
795 		goto done;
796 	}
797 
798 	/*
799 	 * No chunks are available in LChunks, the free chunk MUST be
800 	 * in the never-before-used memory area, controlled by UIndex.
801 	 *
802 	 * The consequences are very serious if our zone got corrupted so
803 	 * we use an explicit panic rather than a KASSERT.
804 	 */
805 	if (z->z_UIndex + 1 != z->z_NMax)
806 	    ++z->z_UIndex;
807 	else
808 	    z->z_UIndex = 0;
809 
810 	if (z->z_UIndex == z->z_UEndIndex)
811 	    panic("slaballoc: corrupted zone");
812 
813 	chunk = (SLChunk *)(z->z_BasePtr + z->z_UIndex * size);
814 	if ((z->z_Flags & SLZF_UNOTZEROD) == 0) {
815 	    flags &= ~M_ZERO;
816 	    flags |= M_PASSIVE_ZERO;
817 	}
818 	chunk_mark_allocated(z, chunk);
819 #ifdef SLAB_DEBUG
820 	slab_record_source(z, file, line);
821 #endif
822 	goto done;
823     }
824 
825     /*
826      * If all zones are exhausted we need to allocate a new zone for this
827      * index.  Use M_ZERO to take advantage of pre-zerod pages.  Also see
828      * UAlloc use above in regards to M_ZERO.  Note that when we are reusing
829      * a zone from the FreeZones list UAlloc'd data will not be zero'd, and
830      * we do not pre-zero it because we do not want to mess up the L1 cache.
831      *
832      * At least one subsystem, the tty code (see CROUND) expects power-of-2
833      * allocations to be power-of-2 aligned.  We maintain compatibility by
834      * adjusting the base offset below.
835      */
836     {
837 	int off;
838 	int *kup;
839 
840 	if ((z = slgd->FreeZones) != NULL) {
841 	    slgd->FreeZones = z->z_Next;
842 	    --slgd->NFreeZones;
843 	    bzero(z, sizeof(SLZone));
844 	    z->z_Flags |= SLZF_UNOTZEROD;
845 	} else {
846 	    z = kmem_slab_alloc(ZoneSize, ZoneSize, flags|M_ZERO);
847 	    if (z == NULL)
848 		goto fail;
849 	    atomic_add_int(&ZoneGenAlloc, ZoneSize / 1024);
850 	}
851 
852 	/*
853 	 * How big is the base structure?
854 	 */
855 #if defined(INVARIANTS)
856 	/*
857 	 * Make room for z_Bitmap.  An exact calculation is somewhat more
858 	 * complicated so don't make an exact calculation.
859 	 */
860 	off = offsetof(SLZone, z_Bitmap[(ZoneSize / size + 31) / 32]);
861 	bzero(z->z_Bitmap, (ZoneSize / size + 31) / 8);
862 #else
863 	off = sizeof(SLZone);
864 #endif
865 
866 	/*
867 	 * Guarentee power-of-2 alignment for power-of-2-sized chunks.
868 	 * Otherwise properly align the data according to the chunk size.
869 	 */
870 	if (powerof2(size))
871 	    align = size;
872 	off = (off + align - 1) & ~(align - 1);
873 
874 	z->z_Magic = ZALLOC_SLAB_MAGIC;
875 	z->z_ZoneIndex = zi;
876 	z->z_NMax = (ZoneSize - off) / size;
877 	z->z_NFree = z->z_NMax - 1;
878 	z->z_BasePtr = (char *)z + off;
879 	z->z_UIndex = z->z_UEndIndex = slgd->JunkIndex % z->z_NMax;
880 	z->z_ChunkSize = size;
881 	z->z_CpuGd = gd;
882 	z->z_Cpu = gd->gd_cpuid;
883 	z->z_LChunksp = &z->z_LChunks;
884 #ifdef SLAB_DEBUG
885 	bcopy(z->z_Sources, z->z_AltSources, sizeof(z->z_Sources));
886 	bzero(z->z_Sources, sizeof(z->z_Sources));
887 #endif
888 	chunk = (SLChunk *)(z->z_BasePtr + z->z_UIndex * size);
889 	z->z_Next = slgd->ZoneAry[zi];
890 	slgd->ZoneAry[zi] = z;
891 	if ((z->z_Flags & SLZF_UNOTZEROD) == 0) {
892 	    flags &= ~M_ZERO;	/* already zero'd */
893 	    flags |= M_PASSIVE_ZERO;
894 	}
895 	kup = btokup(z);
896 	*kup = -(z->z_Cpu + 1);	/* -1 to -(N+1) */
897 	chunk_mark_allocated(z, chunk);
898 #ifdef SLAB_DEBUG
899 	slab_record_source(z, file, line);
900 #endif
901 
902 	/*
903 	 * Slide the base index for initial allocations out of the next
904 	 * zone we create so we do not over-weight the lower part of the
905 	 * cpu memory caches.
906 	 */
907 	slgd->JunkIndex = (slgd->JunkIndex + ZALLOC_SLAB_SLIDE)
908 				& (ZALLOC_MAX_ZONE_SIZE - 1);
909     }
910 
911 done:
912     ++type->ks_inuse[gd->gd_cpuid];
913     type->ks_memuse[gd->gd_cpuid] += size;
914     type->ks_loosememuse += size;	/* not MP synchronized */
915     crit_exit();
916 
917     if (flags & M_ZERO)
918 	bzero(chunk, size);
919 #ifdef INVARIANTS
920     else if ((flags & (M_ZERO|M_PASSIVE_ZERO)) == 0) {
921 	if (use_malloc_pattern) {
922 	    for (i = 0; i < size; i += sizeof(int)) {
923 		*(int *)((char *)chunk + i) = -1;
924 	    }
925 	}
926 	chunk->c_Next = (void *)-1; /* avoid accidental double-free check */
927     }
928 #endif
929     logmemory(malloc_end, chunk, type, size, flags);
930     return(chunk);
931 fail:
932     crit_exit();
933     logmemory(malloc_end, NULL, type, size, flags);
934     return(NULL);
935 }
936 
937 /*
938  * kernel realloc.  (SLAB ALLOCATOR) (MP SAFE)
939  *
940  * Generally speaking this routine is not called very often and we do
941  * not attempt to optimize it beyond reusing the same pointer if the
942  * new size fits within the chunking of the old pointer's zone.
943  */
944 #ifdef SLAB_DEBUG
945 void *
946 krealloc_debug(void *ptr, unsigned long size,
947 	       struct malloc_type *type, int flags,
948 	       const char *file, int line)
949 #else
950 void *
951 krealloc(void *ptr, unsigned long size, struct malloc_type *type, int flags)
952 #endif
953 {
954     unsigned long osize;
955     unsigned long align;
956     SLZone *z;
957     void *nptr;
958     int *kup;
959 
960     KKASSERT((flags & M_ZERO) == 0);	/* not supported */
961 
962     if (ptr == NULL || ptr == ZERO_LENGTH_PTR)
963 	return(kmalloc_debug(size, type, flags, file, line));
964     if (size == 0) {
965 	kfree(ptr, type);
966 	return(NULL);
967     }
968 
969     /*
970      * Handle oversized allocations.  XXX we really should require that a
971      * size be passed to free() instead of this nonsense.
972      */
973     kup = btokup(ptr);
974     if (*kup > 0) {
975 	osize = *kup << PAGE_SHIFT;
976 	if (osize == round_page(size))
977 	    return(ptr);
978 	if ((nptr = kmalloc_debug(size, type, flags, file, line)) == NULL)
979 	    return(NULL);
980 	bcopy(ptr, nptr, min(size, osize));
981 	kfree(ptr, type);
982 	return(nptr);
983     }
984 
985     /*
986      * Get the original allocation's zone.  If the new request winds up
987      * using the same chunk size we do not have to do anything.
988      */
989     z = (SLZone *)((uintptr_t)ptr & ZoneMask);
990     kup = btokup(z);
991     KKASSERT(*kup < 0);
992     KKASSERT(z->z_Magic == ZALLOC_SLAB_MAGIC);
993 
994     /*
995      * Allocate memory for the new request size.  Note that zoneindex has
996      * already adjusted the request size to the appropriate chunk size, which
997      * should optimize our bcopy().  Then copy and return the new pointer.
998      *
999      * Resizing a non-power-of-2 allocation to a power-of-2 size does not
1000      * necessary align the result.
1001      *
1002      * We can only zoneindex (to align size to the chunk size) if the new
1003      * size is not too large.
1004      */
1005     if (size < ZoneLimit) {
1006 	zoneindex(&size, &align);
1007 	if (z->z_ChunkSize == size)
1008 	    return(ptr);
1009     }
1010     if ((nptr = kmalloc_debug(size, type, flags, file, line)) == NULL)
1011 	return(NULL);
1012     bcopy(ptr, nptr, min(size, z->z_ChunkSize));
1013     kfree(ptr, type);
1014     return(nptr);
1015 }
1016 
1017 /*
1018  * Return the kmalloc limit for this type, in bytes.
1019  */
1020 long
1021 kmalloc_limit(struct malloc_type *type)
1022 {
1023     if (type->ks_limit == 0) {
1024 	crit_enter();
1025 	if (type->ks_limit == 0)
1026 	    malloc_init(type);
1027 	crit_exit();
1028     }
1029     return(type->ks_limit);
1030 }
1031 
1032 /*
1033  * Allocate a copy of the specified string.
1034  *
1035  * (MP SAFE) (MAY BLOCK)
1036  */
1037 #ifdef SLAB_DEBUG
1038 char *
1039 kstrdup_debug(const char *str, struct malloc_type *type,
1040 	      const char *file, int line)
1041 #else
1042 char *
1043 kstrdup(const char *str, struct malloc_type *type)
1044 #endif
1045 {
1046     int zlen;	/* length inclusive of terminating NUL */
1047     char *nstr;
1048 
1049     if (str == NULL)
1050 	return(NULL);
1051     zlen = strlen(str) + 1;
1052     nstr = kmalloc_debug(zlen, type, M_WAITOK, file, line);
1053     bcopy(str, nstr, zlen);
1054     return(nstr);
1055 }
1056 
1057 /*
1058  * Notify our cpu that a remote cpu has freed some chunks in a zone that
1059  * we own.  RCount will be bumped so the memory should be good, but validate
1060  * that it really is.
1061  */
1062 static
1063 void
1064 kfree_remote(void *ptr)
1065 {
1066     SLGlobalData *slgd;
1067     SLZone *z;
1068     int nfree;
1069     int *kup;
1070 
1071     slgd = &mycpu->gd_slab;
1072     z = ptr;
1073     kup = btokup(z);
1074     KKASSERT(*kup == -((int)mycpuid + 1));
1075     KKASSERT(z->z_RCount > 0);
1076     atomic_subtract_int(&z->z_RCount, 1);
1077 
1078     logmemory(free_rem_beg, z, NULL, 0L, 0);
1079     KKASSERT(z->z_Magic == ZALLOC_SLAB_MAGIC);
1080     KKASSERT(z->z_Cpu  == mycpu->gd_cpuid);
1081     nfree = z->z_NFree;
1082 
1083     /*
1084      * Indicate that we will no longer be off of the ZoneAry by
1085      * clearing RSignal.
1086      */
1087     if (z->z_RChunks)
1088 	z->z_RSignal = 0;
1089 
1090     /*
1091      * Atomically extract the bchunks list and then process it back
1092      * into the lchunks list.  We want to append our bchunks to the
1093      * lchunks list and not prepend since we likely do not have
1094      * cache mastership of the related data (not that it helps since
1095      * we are using c_Next).
1096      */
1097     clean_zone_rchunks(z);
1098     if (z->z_NFree && nfree == 0) {
1099 	z->z_Next = slgd->ZoneAry[z->z_ZoneIndex];
1100 	slgd->ZoneAry[z->z_ZoneIndex] = z;
1101     }
1102 
1103     /*
1104      * If the zone becomes totally free, and there are other zones we
1105      * can allocate from, move this zone to the FreeZones list.  Since
1106      * this code can be called from an IPI callback, do *NOT* try to mess
1107      * with kernel_map here.  Hysteresis will be performed at malloc() time.
1108      *
1109      * Do not move the zone if there is an IPI inflight, otherwise MP
1110      * races can result in our free_remote code accessing a destroyed
1111      * zone.
1112      */
1113     if (z->z_NFree == z->z_NMax &&
1114 	(z->z_Next || slgd->ZoneAry[z->z_ZoneIndex] != z) &&
1115 	z->z_RCount == 0
1116     ) {
1117 	SLZone **pz;
1118 	int *kup;
1119 
1120 	for (pz = &slgd->ZoneAry[z->z_ZoneIndex];
1121 	     z != *pz;
1122 	     pz = &(*pz)->z_Next) {
1123 	    ;
1124 	}
1125 	*pz = z->z_Next;
1126 	z->z_Magic = -1;
1127 	z->z_Next = slgd->FreeZones;
1128 	slgd->FreeZones = z;
1129 	++slgd->NFreeZones;
1130 	kup = btokup(z);
1131 	*kup = 0;
1132     }
1133     logmemory(free_rem_end, z, NULL, 0L, 0);
1134 }
1135 
1136 /*
1137  * free (SLAB ALLOCATOR)
1138  *
1139  * Free a memory block previously allocated by malloc.  Note that we do not
1140  * attempt to update ks_loosememuse as MP races could prevent us from
1141  * checking memory limits in malloc.
1142  *
1143  * MPSAFE
1144  */
1145 void
1146 kfree(void *ptr, struct malloc_type *type)
1147 {
1148     SLZone *z;
1149     SLChunk *chunk;
1150     SLGlobalData *slgd;
1151     struct globaldata *gd;
1152     int *kup;
1153     unsigned long size;
1154     SLChunk *bchunk;
1155     int rsignal;
1156 
1157     logmemory_quick(free_beg);
1158     gd = mycpu;
1159     slgd = &gd->gd_slab;
1160 
1161     if (ptr == NULL)
1162 	panic("trying to free NULL pointer");
1163 
1164     /*
1165      * Handle special 0-byte allocations
1166      */
1167     if (ptr == ZERO_LENGTH_PTR) {
1168 	logmemory(free_zero, ptr, type, -1UL, 0);
1169 	logmemory_quick(free_end);
1170 	return;
1171     }
1172 
1173     /*
1174      * Panic on bad malloc type
1175      */
1176     if (type->ks_magic != M_MAGIC)
1177 	panic("free: malloc type lacks magic");
1178 
1179     /*
1180      * Handle oversized allocations.  XXX we really should require that a
1181      * size be passed to free() instead of this nonsense.
1182      *
1183      * This code is never called via an ipi.
1184      */
1185     kup = btokup(ptr);
1186     if (*kup > 0) {
1187 	size = *kup << PAGE_SHIFT;
1188 	*kup = 0;
1189 #ifdef INVARIANTS
1190 	KKASSERT(sizeof(weirdary) <= size);
1191 	bcopy(weirdary, ptr, sizeof(weirdary));
1192 #endif
1193 	/*
1194 	 * NOTE: For oversized allocations we do not record the
1195 	 *	     originating cpu.  It gets freed on the cpu calling
1196 	 *	     kfree().  The statistics are in aggregate.
1197 	 *
1198 	 * note: XXX we have still inherited the interrupts-can't-block
1199 	 * assumption.  An interrupt thread does not bump
1200 	 * gd_intr_nesting_level so check TDF_INTTHREAD.  This is
1201 	 * primarily until we can fix softupdate's assumptions about free().
1202 	 */
1203 	crit_enter();
1204 	--type->ks_inuse[gd->gd_cpuid];
1205 	type->ks_memuse[gd->gd_cpuid] -= size;
1206 	if (mycpu->gd_intr_nesting_level ||
1207 	    (gd->gd_curthread->td_flags & TDF_INTTHREAD))
1208 	{
1209 	    logmemory(free_ovsz_delayed, ptr, type, size, 0);
1210 	    z = (SLZone *)ptr;
1211 	    z->z_Magic = ZALLOC_OVSZ_MAGIC;
1212 	    z->z_Next = slgd->FreeOvZones;
1213 	    z->z_ChunkSize = size;
1214 	    slgd->FreeOvZones = z;
1215 	    crit_exit();
1216 	} else {
1217 	    crit_exit();
1218 	    logmemory(free_ovsz, ptr, type, size, 0);
1219 	    kmem_slab_free(ptr, size);	/* may block */
1220 	    atomic_add_int(&ZoneBigAlloc, -(int)size / 1024);
1221 	}
1222 	logmemory_quick(free_end);
1223 	return;
1224     }
1225 
1226     /*
1227      * Zone case.  Figure out the zone based on the fact that it is
1228      * ZoneSize aligned.
1229      */
1230     z = (SLZone *)((uintptr_t)ptr & ZoneMask);
1231     kup = btokup(z);
1232     KKASSERT(*kup < 0);
1233     KKASSERT(z->z_Magic == ZALLOC_SLAB_MAGIC);
1234 
1235     /*
1236      * If we do not own the zone then use atomic ops to free to the
1237      * remote cpu linked list and notify the target zone using a
1238      * passive message.
1239      *
1240      * The target zone cannot be deallocated while we own a chunk of it,
1241      * so the zone header's storage is stable until the very moment
1242      * we adjust z_RChunks.  After that we cannot safely dereference (z).
1243      *
1244      * (no critical section needed)
1245      */
1246     if (z->z_CpuGd != gd) {
1247 	/*
1248 	 * Making these adjustments now allow us to avoid passing (type)
1249 	 * to the remote cpu.  Note that ks_inuse/ks_memuse is being
1250 	 * adjusted on OUR cpu, not the zone cpu, but it should all still
1251 	 * sum up properly and cancel out.
1252 	 */
1253 	crit_enter();
1254 	--type->ks_inuse[gd->gd_cpuid];
1255 	type->ks_memuse[gd->gd_cpuid] -= z->z_ChunkSize;
1256 	crit_exit();
1257 
1258 	/*
1259 	 * WARNING! This code competes with other cpus.  Once we
1260 	 *	    successfully link the chunk to RChunks the remote
1261 	 *	    cpu can rip z's storage out from under us.
1262 	 *
1263 	 *	    Bumping RCount prevents z's storage from getting
1264 	 *	    ripped out.
1265 	 */
1266 	rsignal = z->z_RSignal;
1267 	cpu_lfence();
1268 	if (rsignal)
1269 		atomic_add_int(&z->z_RCount, 1);
1270 
1271 	chunk = ptr;
1272 	for (;;) {
1273 	    bchunk = z->z_RChunks;
1274 	    cpu_ccfence();
1275 	    chunk->c_Next = bchunk;
1276 	    cpu_sfence();
1277 
1278 	    if (atomic_cmpset_ptr(&z->z_RChunks, bchunk, chunk))
1279 		break;
1280 	}
1281 
1282 	/*
1283 	 * We have to signal the remote cpu if our actions will cause
1284 	 * the remote zone to be placed back on ZoneAry so it can
1285 	 * move the zone back on.
1286 	 *
1287 	 * We only need to deal with NULL->non-NULL RChunk transitions
1288 	 * and only if z_RSignal is set.  We interlock by reading rsignal
1289 	 * before adding our chunk to RChunks.  This should result in
1290 	 * virtually no IPI traffic.
1291 	 *
1292 	 * We can use a passive IPI to reduce overhead even further.
1293 	 */
1294 	if (bchunk == NULL && rsignal) {
1295 		logmemory(free_request, ptr, type,
1296 			  (unsigned long)z->z_ChunkSize, 0);
1297 	    lwkt_send_ipiq_passive(z->z_CpuGd, kfree_remote, z);
1298 	    /* z can get ripped out from under us from this point on */
1299 	} else if (rsignal) {
1300 	    atomic_subtract_int(&z->z_RCount, 1);
1301 	    /* z can get ripped out from under us from this point on */
1302 	}
1303 	logmemory_quick(free_end);
1304 	return;
1305     }
1306 
1307     /*
1308      * kfree locally
1309      */
1310     logmemory(free_chunk, ptr, type, (unsigned long)z->z_ChunkSize, 0);
1311 
1312     crit_enter();
1313     chunk = ptr;
1314     chunk_mark_free(z, chunk);
1315 
1316     /*
1317      * Put weird data into the memory to detect modifications after freeing,
1318      * illegal pointer use after freeing (we should fault on the odd address),
1319      * and so forth.  XXX needs more work, see the old malloc code.
1320      */
1321 #ifdef INVARIANTS
1322     if (z->z_ChunkSize < sizeof(weirdary))
1323 	bcopy(weirdary, chunk, z->z_ChunkSize);
1324     else
1325 	bcopy(weirdary, chunk, sizeof(weirdary));
1326 #endif
1327 
1328     /*
1329      * Add this free non-zero'd chunk to a linked list for reuse.  Add
1330      * to the front of the linked list so it is more likely to be
1331      * reallocated, since it is already in our L1 cache.
1332      */
1333 #ifdef INVARIANTS
1334     if ((vm_offset_t)chunk < KvaStart || (vm_offset_t)chunk >= KvaEnd)
1335 	panic("BADFREE %p", chunk);
1336 #endif
1337     chunk->c_Next = z->z_LChunks;
1338     z->z_LChunks = chunk;
1339     if (chunk->c_Next == NULL)
1340 	    z->z_LChunksp = &chunk->c_Next;
1341 
1342 #ifdef INVARIANTS
1343     if (chunk->c_Next && (vm_offset_t)chunk->c_Next < KvaStart)
1344 	panic("BADFREE2");
1345 #endif
1346 
1347     /*
1348      * Bump the number of free chunks.  If it becomes non-zero the zone
1349      * must be added back onto the appropriate list.
1350      */
1351     if (z->z_NFree++ == 0) {
1352 	z->z_Next = slgd->ZoneAry[z->z_ZoneIndex];
1353 	slgd->ZoneAry[z->z_ZoneIndex] = z;
1354     }
1355 
1356     --type->ks_inuse[z->z_Cpu];
1357     type->ks_memuse[z->z_Cpu] -= z->z_ChunkSize;
1358 
1359     check_zone_free(slgd, z);
1360     logmemory_quick(free_end);
1361     crit_exit();
1362 }
1363 
1364 /*
1365  * Cleanup slabs which are hanging around due to RChunks.  Called once every
1366  * 10 seconds on all cpus.
1367  */
1368 void
1369 slab_cleanup(void)
1370 {
1371     SLGlobalData *slgd = &mycpu->gd_slab;
1372     SLZone *z;
1373     int i;
1374 
1375     crit_enter();
1376     for (i = 0; i < NZONES; ++i) {
1377 	if ((z = slgd->ZoneAry[i]) == NULL)
1378 		continue;
1379 	z = z->z_Next;
1380 
1381 	/*
1382 	 * Scan zones starting with the second zone in each list.
1383 	 */
1384 	while (z) {
1385 	    /*
1386 	     * Shift all RChunks to the end of the LChunks list.  This is
1387 	     * an O(1) operation.
1388 	     */
1389 	    clean_zone_rchunks(z);
1390 	    z = check_zone_free(slgd, z);
1391 	}
1392     }
1393     crit_exit();
1394 }
1395 
1396 #if defined(INVARIANTS)
1397 
1398 /*
1399  * Helper routines for sanity checks
1400  */
1401 static
1402 void
1403 chunk_mark_allocated(SLZone *z, void *chunk)
1404 {
1405     int bitdex = ((char *)chunk - (char *)z->z_BasePtr) / z->z_ChunkSize;
1406     __uint32_t *bitptr;
1407 
1408     KKASSERT((((intptr_t)chunk ^ (intptr_t)z) & ZoneMask) == 0);
1409     KASSERT(bitdex >= 0 && bitdex < z->z_NMax,
1410 	    ("memory chunk %p bit index %d is illegal", chunk, bitdex));
1411     bitptr = &z->z_Bitmap[bitdex >> 5];
1412     bitdex &= 31;
1413     KASSERT((*bitptr & (1 << bitdex)) == 0,
1414 	    ("memory chunk %p is already allocated!", chunk));
1415     *bitptr |= 1 << bitdex;
1416 }
1417 
1418 static
1419 void
1420 chunk_mark_free(SLZone *z, void *chunk)
1421 {
1422     int bitdex = ((char *)chunk - (char *)z->z_BasePtr) / z->z_ChunkSize;
1423     __uint32_t *bitptr;
1424 
1425     KKASSERT((((intptr_t)chunk ^ (intptr_t)z) & ZoneMask) == 0);
1426     KASSERT(bitdex >= 0 && bitdex < z->z_NMax,
1427 	    ("memory chunk %p bit index %d is illegal!", chunk, bitdex));
1428     bitptr = &z->z_Bitmap[bitdex >> 5];
1429     bitdex &= 31;
1430     KASSERT((*bitptr & (1 << bitdex)) != 0,
1431 	    ("memory chunk %p is already free!", chunk));
1432     *bitptr &= ~(1 << bitdex);
1433 }
1434 
1435 #endif
1436 
1437 /*
1438  * kmem_slab_alloc()
1439  *
1440  *	Directly allocate and wire kernel memory in PAGE_SIZE chunks with the
1441  *	specified alignment.  M_* flags are expected in the flags field.
1442  *
1443  *	Alignment must be a multiple of PAGE_SIZE.
1444  *
1445  *	NOTE! XXX For the moment we use vm_map_entry_reserve/release(),
1446  *	but when we move zalloc() over to use this function as its backend
1447  *	we will have to switch to kreserve/krelease and call reserve(0)
1448  *	after the new space is made available.
1449  *
1450  *	Interrupt code which has preempted other code is not allowed to
1451  *	use PQ_CACHE pages.  However, if an interrupt thread is run
1452  *	non-preemptively or blocks and then runs non-preemptively, then
1453  *	it is free to use PQ_CACHE pages.  <--- may not apply any longer XXX
1454  */
1455 static void *
1456 kmem_slab_alloc(vm_size_t size, vm_offset_t align, int flags)
1457 {
1458     vm_size_t i;
1459     vm_offset_t addr;
1460     int count, vmflags, base_vmflags;
1461     vm_page_t mbase = NULL;
1462     vm_page_t m;
1463     thread_t td;
1464 
1465     size = round_page(size);
1466     addr = vm_map_min(&kernel_map);
1467 
1468     count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
1469     crit_enter();
1470     vm_map_lock(&kernel_map);
1471     if (vm_map_findspace(&kernel_map, addr, size, align, 0, &addr)) {
1472 	vm_map_unlock(&kernel_map);
1473 	if ((flags & M_NULLOK) == 0)
1474 	    panic("kmem_slab_alloc(): kernel_map ran out of space!");
1475 	vm_map_entry_release(count);
1476 	crit_exit();
1477 	return(NULL);
1478     }
1479 
1480     /*
1481      * kernel_object maps 1:1 to kernel_map.
1482      */
1483     vm_object_hold(&kernel_object);
1484     vm_object_reference_locked(&kernel_object);
1485     vm_map_insert(&kernel_map, &count,
1486 		  &kernel_object, NULL,
1487 		  addr, addr, addr + size,
1488 		  VM_MAPTYPE_NORMAL,
1489 		  VM_PROT_ALL, VM_PROT_ALL,
1490 		  0);
1491     vm_object_drop(&kernel_object);
1492     vm_map_set_wired_quick(&kernel_map, addr, size, &count);
1493     vm_map_unlock(&kernel_map);
1494 
1495     td = curthread;
1496 
1497     base_vmflags = 0;
1498     if (flags & M_ZERO)
1499         base_vmflags |= VM_ALLOC_ZERO;
1500     if (flags & M_USE_RESERVE)
1501 	base_vmflags |= VM_ALLOC_SYSTEM;
1502     if (flags & M_USE_INTERRUPT_RESERVE)
1503         base_vmflags |= VM_ALLOC_INTERRUPT;
1504     if ((flags & (M_RNOWAIT|M_WAITOK)) == 0) {
1505 	panic("kmem_slab_alloc: bad flags %08x (%p)",
1506 	      flags, ((int **)&size)[-1]);
1507     }
1508 
1509     /*
1510      * Allocate the pages.  Do not mess with the PG_ZERO flag or map
1511      * them yet.  VM_ALLOC_NORMAL can only be set if we are not preempting.
1512      *
1513      * VM_ALLOC_SYSTEM is automatically set if we are preempting and
1514      * M_WAITOK was specified as an alternative (i.e. M_USE_RESERVE is
1515      * implied in this case), though I'm not sure if we really need to
1516      * do that.
1517      */
1518     vmflags = base_vmflags;
1519     if (flags & M_WAITOK) {
1520 	if (td->td_preempted)
1521 	    vmflags |= VM_ALLOC_SYSTEM;
1522 	else
1523 	    vmflags |= VM_ALLOC_NORMAL;
1524     }
1525 
1526     vm_object_hold(&kernel_object);
1527     for (i = 0; i < size; i += PAGE_SIZE) {
1528 	m = vm_page_alloc(&kernel_object, OFF_TO_IDX(addr + i), vmflags);
1529 	if (i == 0)
1530 		mbase = m;
1531 
1532 	/*
1533 	 * If the allocation failed we either return NULL or we retry.
1534 	 *
1535 	 * If M_WAITOK is specified we wait for more memory and retry.
1536 	 * If M_WAITOK is specified from a preemption we yield instead of
1537 	 * wait.  Livelock will not occur because the interrupt thread
1538 	 * will not be preempting anyone the second time around after the
1539 	 * yield.
1540 	 */
1541 	if (m == NULL) {
1542 	    if (flags & M_WAITOK) {
1543 		if (td->td_preempted) {
1544 		    lwkt_switch();
1545 		} else {
1546 		    vm_wait(0);
1547 		}
1548 		i -= PAGE_SIZE;	/* retry */
1549 		continue;
1550 	    }
1551 	    break;
1552 	}
1553     }
1554 
1555     /*
1556      * Check and deal with an allocation failure
1557      */
1558     if (i != size) {
1559 	while (i != 0) {
1560 	    i -= PAGE_SIZE;
1561 	    m = vm_page_lookup(&kernel_object, OFF_TO_IDX(addr + i));
1562 	    /* page should already be busy */
1563 	    vm_page_free(m);
1564 	}
1565 	vm_map_lock(&kernel_map);
1566 	vm_map_delete(&kernel_map, addr, addr + size, &count);
1567 	vm_map_unlock(&kernel_map);
1568 	vm_object_drop(&kernel_object);
1569 
1570 	vm_map_entry_release(count);
1571 	crit_exit();
1572 	return(NULL);
1573     }
1574 
1575     /*
1576      * Success!
1577      *
1578      * NOTE: The VM pages are still busied.  mbase points to the first one
1579      *	     but we have to iterate via vm_page_next()
1580      */
1581     vm_object_drop(&kernel_object);
1582     crit_exit();
1583 
1584     /*
1585      * Enter the pages into the pmap and deal with PG_ZERO and M_ZERO.
1586      */
1587     m = mbase;
1588     i = 0;
1589 
1590     while (i < size) {
1591 	/*
1592 	 * page should already be busy
1593 	 */
1594 	m->valid = VM_PAGE_BITS_ALL;
1595 	vm_page_wire(m);
1596 	pmap_enter(&kernel_pmap, addr + i, m, VM_PROT_ALL | VM_PROT_NOSYNC,
1597 		   1, NULL);
1598 	if ((m->flags & PG_ZERO) == 0 && (flags & M_ZERO))
1599 	    bzero((char *)addr + i, PAGE_SIZE);
1600 	vm_page_flag_clear(m, PG_ZERO);
1601 	KKASSERT(m->flags & (PG_WRITEABLE | PG_MAPPED));
1602 	vm_page_flag_set(m, PG_REFERENCED);
1603 	vm_page_wakeup(m);
1604 
1605 	i += PAGE_SIZE;
1606 	vm_object_hold(&kernel_object);
1607 	m = vm_page_next(m);
1608 	vm_object_drop(&kernel_object);
1609     }
1610     smp_invltlb();
1611     vm_map_entry_release(count);
1612     atomic_add_long(&SlabsAllocated, 1);
1613     return((void *)addr);
1614 }
1615 
1616 /*
1617  * kmem_slab_free()
1618  */
1619 static void
1620 kmem_slab_free(void *ptr, vm_size_t size)
1621 {
1622     crit_enter();
1623     vm_map_remove(&kernel_map, (vm_offset_t)ptr, (vm_offset_t)ptr + size);
1624     atomic_add_long(&SlabsFreed, 1);
1625     crit_exit();
1626 }
1627 
1628 void *
1629 kmalloc_cachealign(unsigned long size_alloc, struct malloc_type *type,
1630     int flags)
1631 {
1632 #if (__VM_CACHELINE_SIZE == 32)
1633 #define CAN_CACHEALIGN(sz)	((sz) >= 256)
1634 #elif (__VM_CACHELINE_SIZE == 64)
1635 #define CAN_CACHEALIGN(sz)	((sz) >= 512)
1636 #elif (__VM_CACHELINE_SIZE == 128)
1637 #define CAN_CACHEALIGN(sz)	((sz) >= 1024)
1638 #else
1639 #error "unsupported cacheline size"
1640 #endif
1641 
1642 	void *ret;
1643 
1644 	if (size_alloc < __VM_CACHELINE_SIZE)
1645 		size_alloc = __VM_CACHELINE_SIZE;
1646 	else if (!CAN_CACHEALIGN(size_alloc))
1647 		flags |= M_POWEROF2;
1648 
1649 	ret = kmalloc(size_alloc, type, flags);
1650 	KASSERT(((uintptr_t)ret & (__VM_CACHELINE_SIZE - 1)) == 0,
1651 	    ("%p(%lu) not cacheline %d aligned",
1652 	     ret, size_alloc, __VM_CACHELINE_SIZE));
1653 	return ret;
1654 
1655 #undef CAN_CACHEALIGN
1656 }
1657