xref: /dragonfly/sys/sys/slaballoc.h (revision 9d4f17d1)
1a108bf71SMatthew Dillon /*
2a108bf71SMatthew Dillon  * KERN_SLABALLOC.H	- Kernel SLAB memory allocator
3a108bf71SMatthew Dillon  *
48c10bfcfSMatthew Dillon  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
58c10bfcfSMatthew Dillon  *
68c10bfcfSMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
78c10bfcfSMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
8a108bf71SMatthew Dillon  *
9a108bf71SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
10a108bf71SMatthew Dillon  * modification, are permitted provided that the following conditions
11a108bf71SMatthew Dillon  * are met:
128c10bfcfSMatthew Dillon  *
13a108bf71SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
14a108bf71SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
15a108bf71SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
168c10bfcfSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
178c10bfcfSMatthew Dillon  *    the documentation and/or other materials provided with the
188c10bfcfSMatthew Dillon  *    distribution.
198c10bfcfSMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
208c10bfcfSMatthew Dillon  *    contributors may be used to endorse or promote products derived
218c10bfcfSMatthew Dillon  *    from this software without specific, prior written permission.
22a108bf71SMatthew Dillon  *
238c10bfcfSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
248c10bfcfSMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
258c10bfcfSMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
268c10bfcfSMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
278c10bfcfSMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
288c10bfcfSMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
298c10bfcfSMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
308c10bfcfSMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
318c10bfcfSMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
328c10bfcfSMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
338c10bfcfSMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34a108bf71SMatthew Dillon  * SUCH DAMAGE.
35a108bf71SMatthew Dillon  */
36a108bf71SMatthew Dillon 
37a108bf71SMatthew Dillon #ifndef _SYS_SLABALLOC_H_
38a108bf71SMatthew Dillon #define _SYS_SLABALLOC_H_
39a108bf71SMatthew Dillon 
40a108bf71SMatthew Dillon #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
41a108bf71SMatthew Dillon 
42e2164e29Szrj #ifndef _SYS__MALLOC_H_
43e2164e29Szrj #include <sys/_malloc.h>
44a108bf71SMatthew Dillon #endif
45*9d4f17d1Szrj #include <machine/stdint.h>
46a108bf71SMatthew Dillon 
47a108bf71SMatthew Dillon /*
48a108bf71SMatthew Dillon  * Note that any allocations which are exact multiples of PAGE_SIZE, or
49a108bf71SMatthew Dillon  * which are >= ZALLOC_ZONE_LIMIT, will fall through to the kmem subsystem.
50a108bf71SMatthew Dillon  */
51a108bf71SMatthew Dillon #define ZALLOC_ZONE_LIMIT	(16 * 1024)	/* max slab-managed alloc */
52a108bf71SMatthew Dillon #define ZALLOC_MIN_ZONE_SIZE	(32 * 1024)	/* minimum zone size */
53a108bf71SMatthew Dillon #define ZALLOC_MAX_ZONE_SIZE	(128 * 1024)	/* maximum zone size */
54a108bf71SMatthew Dillon #define ZALLOC_SLAB_MAGIC	0x736c6162	/* magic sanity */
5546a3f46dSMatthew Dillon #define ZALLOC_OVSZ_MAGIC	0x736c6163	/* magic sanity */
561c5ca4f3SMatthew Dillon #define ZALLOC_SLAB_SLIDE	20
571c5ca4f3SMatthew Dillon 
58a108bf71SMatthew Dillon 
59a108bf71SMatthew Dillon #if ZALLOC_ZONE_LIMIT == 16384
60a108bf71SMatthew Dillon #define NZONES			72
61a108bf71SMatthew Dillon #elif ZALLOC_ZONE_LIMIT == 32768
62a108bf71SMatthew Dillon #define NZONES			80
63a108bf71SMatthew Dillon #else
64a108bf71SMatthew Dillon #error "I couldn't figure out NZONES"
65a108bf71SMatthew Dillon #endif
66a108bf71SMatthew Dillon 
67a108bf71SMatthew Dillon /*
68a108bf71SMatthew Dillon  * Chunk structure for free elements
69a108bf71SMatthew Dillon  */
70a108bf71SMatthew Dillon typedef struct SLChunk {
71a108bf71SMatthew Dillon     struct SLChunk *c_Next;
72a108bf71SMatthew Dillon } SLChunk;
73a108bf71SMatthew Dillon 
74bbb201fdSMatthew Dillon #if defined(SLAB_DEBUG)
75bbb201fdSMatthew Dillon /*
76bbb201fdSMatthew Dillon  * Only used for kernels compiled w/SLAB_DEBUG
77bbb201fdSMatthew Dillon  */
78bbb201fdSMatthew Dillon struct ZSources {
79bbb201fdSMatthew Dillon     const char *file;
80bbb201fdSMatthew Dillon     int line;
81bbb201fdSMatthew Dillon };
82bbb201fdSMatthew Dillon 
83bbb201fdSMatthew Dillon #endif
84bbb201fdSMatthew Dillon 
85a108bf71SMatthew Dillon /*
86a108bf71SMatthew Dillon  * The IN-BAND zone header is placed at the beginning of each zone.
875fee07e6SMatthew Dillon  *
885fee07e6SMatthew Dillon  * NOTE! All fields are cpu-local except z_RChunks.  Remote cpus free
895fee07e6SMatthew Dillon  *	 chunks using atomic ops to z_RChunks and then signal local
905fee07e6SMatthew Dillon  *	 cpus as necessary.
91a108bf71SMatthew Dillon  */
92a108bf71SMatthew Dillon typedef struct SLZone {
9305220613SMatthew Dillon     __int32_t	z_Magic;	/* magic number for sanity check */
94a108bf71SMatthew Dillon     int		z_Cpu;		/* which cpu owns this zone? */
952db3b277SMatthew Dillon     struct globaldata *z_CpuGd;	/* which cpu owns this zone? */
96c1b91053SMatthew Dillon     TAILQ_ENTRY(SLZone) z_Entry;/* ZoneAry[] if z_NFree!=0, else Free*Zones */
97c06ca5eeSMatthew Dillon     void	*z_UNused01;
985fee07e6SMatthew Dillon     int		z_NFree;	/* total free chunks / ualloc space in zone */
99a108bf71SMatthew Dillon     int		z_NMax;		/* maximum free chunks */
1001c5ca4f3SMatthew Dillon     char	*z_BasePtr;	/* pointer to start of chunk array */
1011c5ca4f3SMatthew Dillon     int		z_UIndex;	/* current initial allocation index */
1021c5ca4f3SMatthew Dillon     int		z_UEndIndex;	/* last (first) allocation index */
103a108bf71SMatthew Dillon     int		z_ChunkSize;	/* chunk size for validation */
104a108bf71SMatthew Dillon     int		z_ZoneIndex;
105c7158d94SMatthew Dillon     int		z_Flags;
1065fee07e6SMatthew Dillon     SLChunk	*z_LChunks;	/* linked list of chunks current cpu */
1075fee07e6SMatthew Dillon     SLChunk	**z_LChunksp;	/* tailp */
1085fee07e6SMatthew Dillon     SLChunk	*z_RChunks;	/* linked list of chunks remote cpu */
1095fee07e6SMatthew Dillon     int		z_RSignal;	/* signal interlock */
110df9daea8SMatthew Dillon     int		z_RCount;	/* prevent local destruction w/inflight ipis */
111bbb201fdSMatthew Dillon #if defined(SLAB_DEBUG)
112bbb201fdSMatthew Dillon #define SLAB_DEBUG_ENTRIES	32	/* must be power of 2 */
113bbb201fdSMatthew Dillon     struct ZSources z_Sources[SLAB_DEBUG_ENTRIES];
114bbb201fdSMatthew Dillon     struct ZSources z_AltSources[SLAB_DEBUG_ENTRIES];
115bbb201fdSMatthew Dillon #endif
11610cc6608SMatthew Dillon #if defined(INVARIANTS)
11710cc6608SMatthew Dillon     __uint32_t	z_Bitmap[];	/* bitmap of free chunks for sanity check */
11810cc6608SMatthew Dillon #endif
119a108bf71SMatthew Dillon } SLZone;
120a108bf71SMatthew Dillon 
121c7158d94SMatthew Dillon #define SLZF_UNOTZEROD		0x0001
122c7158d94SMatthew Dillon 
123c1b91053SMatthew Dillon TAILQ_HEAD(SLZoneList, SLZone);
124243dbb26SMatthew Dillon typedef struct SLZoneList SLZoneList;
125243dbb26SMatthew Dillon 
126a108bf71SMatthew Dillon typedef struct SLGlobalData {
127243dbb26SMatthew Dillon     SLZoneList	ZoneAry[NZONES];	/* linked list of zones NFree > 0 */
128243dbb26SMatthew Dillon     SLZoneList	FreeZones;		/* whole zones that have become free */
129243dbb26SMatthew Dillon     SLZoneList	FreeOvZones;		/* oversized zones */
130a108bf71SMatthew Dillon     int		NFreeZones;		/* free zone count */
1311c5ca4f3SMatthew Dillon     int		JunkIndex;
132a108bf71SMatthew Dillon     struct malloc_type ZoneInfo;	/* stats on meta-zones allocated */
133a108bf71SMatthew Dillon } SLGlobalData;
134a108bf71SMatthew Dillon 
135a108bf71SMatthew Dillon #endif	/* _KERNEL */
136a108bf71SMatthew Dillon 
13705220613SMatthew Dillon #endif	/* _SYS_SLABALLOC_H_ */
138a108bf71SMatthew Dillon 
139