|
|
1.1 root 1: /*
2: * Copyright (c) 2011 Free Software Foundation.
3: *
4: * This program is free software; you can redistribute it and/or modify
5: * it under the terms of the GNU General Public License as published by
6: * the Free Software Foundation; either version 2 of the License, or
7: * (at your option) any later version.
8: *
9: * This program is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU General Public License for more details.
13: *
14: * You should have received a copy of the GNU General Public License along
15: * with this program; if not, write to the Free Software Foundation, Inc.,
16: * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17: */
18:
19: /*
20: * Copyright (c) 2010, 2011 Richard Braun.
21: * All rights reserved.
22: *
23: * Redistribution and use in source and binary forms, with or without
24: * modification, are permitted provided that the following conditions
25: * are met:
26: * 1. Redistributions of source code must retain the above copyright
27: * notice, this list of conditions and the following disclaimer.
28: * 2. Redistributions in binary form must reproduce the above copyright
29: * notice, this list of conditions and the following disclaimer in the
30: * documentation and/or other materials provided with the distribution.
31: *
32: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
33: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
35: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
36: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
41: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42: *
43: *
44: * Object caching memory allocator.
45: */
46:
47: #ifndef _KERN_SLAB_H
48: #define _KERN_SLAB_H
49:
1.1.1.2 ! root 50: #include <cache.h>
1.1 root 51: #include <kern/lock.h>
52: #include <kern/list.h>
53: #include <kern/rbtree.h>
54: #include <mach/machine/vm_types.h>
55: #include <sys/types.h>
56: #include <vm/vm_types.h>
57:
58: #if SLAB_USE_CPU_POOLS
59: /*
60: * L1 cache line size.
61: */
62: #define CPU_L1_SIZE (1 << CPU_L1_SHIFT)
63:
64: /*
65: * Per-processor cache of pre-constructed objects.
66: *
67: * The flags member is a read-only CPU-local copy of the parent cache flags.
68: */
69: struct kmem_cpu_pool {
70: simple_lock_data_t lock;
71: int flags;
72: int size;
73: int transfer_size;
74: int nr_objs;
75: void **array;
76: } __attribute__((aligned(CPU_L1_SIZE)));
77:
78: /*
79: * When a cache is created, its CPU pool type is determined from the buffer
80: * size. For small buffer sizes, many objects can be cached in a CPU pool.
81: * Conversely, for large buffer sizes, this would incur much overhead, so only
82: * a few objects are stored in a CPU pool.
83: */
84: struct kmem_cpu_pool_type {
85: size_t buf_size;
86: int array_size;
87: size_t array_align;
88: struct kmem_cache *array_cache;
89: };
90: #endif /* SLAB_USE_CPU_POOLS */
91:
92: /*
93: * Buffer descriptor.
94: *
95: * For normal caches (i.e. without SLAB_CF_VERIFY), bufctls are located at the
96: * end of (but inside) each buffer. If SLAB_CF_VERIFY is set, bufctls are
97: * located after each buffer.
98: *
99: * When an object is allocated to a client, its bufctl isn't used. This memory
100: * is instead used for redzoning if cache debugging is in effect.
101: */
102: union kmem_bufctl {
103: union kmem_bufctl *next;
104: unsigned long redzone;
105: };
106:
107: /*
108: * Buffer tag.
109: *
110: * This structure is only used for SLAB_CF_VERIFY caches. It is located after
111: * the bufctl and includes information about the state of the buffer it
112: * describes (allocated or not). It should be thought of as a debugging
113: * extension of the bufctl.
114: */
115: struct kmem_buftag {
116: unsigned long state;
117: };
118:
119: /*
120: * Page-aligned collection of unconstructed buffers.
121: */
122: struct kmem_slab {
123: struct list list_node;
124: struct rbtree_node tree_node;
125: unsigned long nr_refs;
126: union kmem_bufctl *first_free;
127: void *addr;
128: };
129:
130: /*
131: * Type for constructor functions.
132: *
133: * The pre-constructed state of an object is supposed to include only
134: * elements such as e.g. linked lists, locks, reference counters. Therefore
135: * constructors are expected to 1) never fail and 2) not need any
136: * user-provided data. The first constraint implies that object construction
137: * never performs dynamic resource allocation, which also means there is no
138: * need for destructors.
139: */
140: typedef void (*kmem_cache_ctor_t)(void *obj);
141:
142: /*
143: * Types for slab allocation/free functions.
144: *
145: * All addresses and sizes must be page-aligned.
146: */
147: typedef vm_offset_t (*kmem_slab_alloc_fn_t)(vm_size_t);
148: typedef void (*kmem_slab_free_fn_t)(vm_offset_t, vm_size_t);
149:
150: /*
1.1.1.2 ! root 151: * Cache name buffer size. The size is chosen so that struct
! 152: * kmem_cache fits into two cache lines. The size of a cache line on
! 153: * a typical CPU is 64 bytes.
1.1 root 154: */
1.1.1.2 ! root 155: #define KMEM_CACHE_NAME_SIZE 24
1.1 root 156:
157: /*
158: * Cache of objects.
159: *
160: * Locking order : cpu_pool -> cache. CPU pools locking is ordered by CPU ID.
1.1.1.2 ! root 161: *
! 162: * Currently, SLAB_USE_CPU_POOLS is not defined. KMEM_CACHE_NAME_SIZE
! 163: * is chosen so that the struct fits into two cache lines. The first
! 164: * cache line contains all hot fields.
1.1 root 165: */
166: struct kmem_cache {
167: #if SLAB_USE_CPU_POOLS
168: /* CPU pool layer */
169: struct kmem_cpu_pool cpu_pools[NCPUS];
170: struct kmem_cpu_pool_type *cpu_pool_type;
171: #endif /* SLAB_USE_CPU_POOLS */
172:
173: /* Slab layer */
174: simple_lock_data_t lock;
175: struct list node; /* Cache list linkage */
176: struct list partial_slabs;
177: struct list free_slabs;
178: struct rbtree active_slabs;
179: int flags;
1.1.1.2 ! root 180: size_t bufctl_dist; /* Distance from buffer to bufctl */
! 181: size_t slab_size;
! 182: unsigned long bufs_per_slab;
! 183: unsigned long nr_objs; /* Number of allocated objects */
! 184: unsigned long nr_free_slabs;
! 185: kmem_cache_ctor_t ctor;
! 186: /* All fields below are cold */
1.1 root 187: size_t obj_size; /* User-provided size */
1.1.1.2 ! root 188: /* Assuming ! SLAB_USE_CPU_POOLS, here is the cacheline boundary */
1.1 root 189: size_t align;
190: size_t buf_size; /* Aligned object size */
191: size_t color;
192: size_t color_max;
193: unsigned long nr_bufs; /* Total number of buffers */
194: unsigned long nr_slabs;
195: kmem_slab_alloc_fn_t slab_alloc_fn;
196: kmem_slab_free_fn_t slab_free_fn;
197: char name[KMEM_CACHE_NAME_SIZE];
198: size_t buftag_dist; /* Distance from buffer to buftag */
199: size_t redzone_pad; /* Bytes from end of object to redzone word */
1.1.1.2 ! root 200: } __cacheline_aligned;
1.1 root 201:
202: /*
203: * Mach-style declarations for struct kmem_cache.
204: */
205: typedef struct kmem_cache *kmem_cache_t;
206: #define KMEM_CACHE_NULL ((kmem_cache_t) 0)
207:
208: /*
209: * VM submap for slab allocations.
210: */
211: extern vm_map_t kmem_map;
212:
213: /*
214: * Cache initialization flags.
215: */
216: #define KMEM_CACHE_NOCPUPOOL 0x1 /* Don't use the per-cpu pools */
217: #define KMEM_CACHE_NOOFFSLAB 0x2 /* Don't allocate external slab data */
218: #define KMEM_CACHE_NORECLAIM 0x4 /* Never give slabs back to their source,
219: implies KMEM_CACHE_NOOFFSLAB */
220: #define KMEM_CACHE_VERIFY 0x8 /* Use debugging facilities */
221:
222: /*
223: * Initialize a cache.
224: */
225: void kmem_cache_init(struct kmem_cache *cache, const char *name,
226: size_t obj_size, size_t align, kmem_cache_ctor_t ctor,
227: kmem_slab_alloc_fn_t slab_alloc_fn,
228: kmem_slab_free_fn_t slab_free_fn, int flags);
229:
230: /*
231: * Allocate an object from a cache.
232: */
233: vm_offset_t kmem_cache_alloc(struct kmem_cache *cache);
234:
235: /*
236: * Release an object to its cache.
237: */
238: void kmem_cache_free(struct kmem_cache *cache, vm_offset_t obj);
239:
240: /*
241: * Initialize the memory allocator module.
242: */
243: void slab_bootstrap(void);
244: void slab_init(void);
245:
246: /*
247: * Release free slabs to the VM system.
248: */
249: void slab_collect(void);
250:
251: /*
252: * Display a summary of all kernel caches.
253: */
254: void slab_info(void);
255:
256: #endif /* _KERN_SLAB_H */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.