xref: /freebsd/sys/vm/uma.h (revision d37ea99837e6ad50837fd9fe1771ddf1c3ba6002)
1 /*
2  * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
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 unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  *
28  */
29 
30 /*
31  * uma.h - External definitions for the Universal Memory Allocator
32  *
33 */
34 
35 #ifndef VM_UMA_H
36 #define VM_UMA_H
37 
38 #include <sys/param.h>		/* For NULL */
39 #include <sys/malloc.h>		/* For M_* */
40 
41 /* User visable parameters */
42 #define UMA_SMALLEST_UNIT       (PAGE_SIZE / 256) /* Smallest item allocated */
43 
44 /* Types and type defs */
45 
46 struct uma_zone;
47 /* Opaque type used as a handle to the zone */
48 typedef struct uma_zone * uma_zone_t;
49 
50 /*
51  * Item constructor
52  *
53  * Arguments:
54  *	item  A pointer to the memory which has been allocated.
55  *	arg   The arg field passed to uma_zalloc_arg
56  *	size  The size of the allocated item
57  *
58  * Returns:
59  *	Nothing
60  *
61  * Discussion:
62  *	The constructor is called just before the memory is returned
63  *	to the user. It may block if necessary.
64  */
65 typedef void (*uma_ctor)(void *mem, int size, void *arg);
66 
67 /*
68  * Item destructor
69  *
70  * Arguments:
71  *	item  A pointer to the memory which has been allocated.
72  *	size  The size of the item being destructed.
73  *	arg   Argument passed through uma_zfree_arg
74  *
75  * Returns:
76  *	Nothing
77  *
78  * Discussion:
79  *	The destructor may perform operations that differ from those performed
80  *	by the initializer, but it must leave the object in the same state.
81  *	This IS type stable storage.  This is called after EVERY zfree call.
82  */
83 typedef void (*uma_dtor)(void *mem, int size, void *arg);
84 
85 /*
86  * Item initializer
87  *
88  * Arguments:
89  *	item  A pointer to the memory which has been allocated.
90  *	size  The size of the item being initialized.
91  *
92  * Returns:
93  *	Nothing
94  *
95  * Discussion:
96  *	The initializer is called when the memory is cached in the uma zone.
97  *	this should be the same state that the destructor leaves the object in.
98  */
99 typedef void (*uma_init)(void *mem, int size);
100 
101 /*
102  * Item discard function
103  *
104  * Arguments:
105  * 	item  A pointer to memory which has been 'freed' but has not left the
106  *	      zone's cache.
107  *	size  The size of the item being discarded.
108  *
109  * Returns:
110  *	Nothing
111  *
112  * Discussion:
113  *	This routine is called when memory leaves a zone and is returned to the
114  *	system for other uses.  It is the counter part to the init function.
115  */
116 typedef void (*uma_fini)(void *mem, int size);
117 
118 /*
119  * What's the difference between initializing and constructing?
120  *
121  * The item is initialized when it is cached, and this is the state that the
122  * object should be in when returned to the allocator. The purpose of this is
123  * to remove some code which would otherwise be called on each allocation by
124  * utilizing a known, stable state.  This differs from the constructor which
125  * will be called on EVERY allocation.
126  *
127  * For example, in the initializer you may want to initialize embeded locks,
128  * NULL list pointers, set up initial states, magic numbers, etc.  This way if
129  * the object is held in the allocator and re-used it won't be necessary to
130  * re-initialize it.
131  *
132  * The constructor may be used to lock a data structure, link it on to lists,
133  * bump reference counts or total counts of outstanding structures, etc.
134  *
135  */
136 
137 
138 /* Function proto types */
139 
140 /*
141  * Create a new uma zone
142  *
143  * Arguments:
144  *	name  The text name of the zone for debugging and stats, this memory
145  *		should not be freed until the zone has been deallocated.
146  *	size  The size of the object that is being created.
147  *	ctor  The constructor that is called when the object is allocated
148  *	dtor  The destructor that is called when the object is freed.
149  *	init  An initializer that sets up the initial state of the memory.
150  *	fini  A discard function that undoes initialization done by init.
151  *		ctor/dtor/init/fini may all be null, see notes above.
152  *	align A bitmask that corisponds to the requested alignment
153  *		eg 4 would be 0x3
154  *	flags A set of parameters that control the behavior of the zone
155  *
156  * Returns:
157  *	A pointer to a structure which is intended to be opaque to users of
158  *	the interface.  The value may be null if the wait flag is not set.
159  */
160 uma_zone_t uma_zcreate(char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
161 			uma_init uminit, uma_fini fini, int align,
162 			u_int16_t flags);
163 
164 /*
165  * Create a secondary uma zone
166  *
167  * Arguments:
168  *	name  The text name of the zone for debugging and stats, this memory
169  *		should not be freed until the zone has been deallocated.
170  *	ctor  The constructor that is called when the object is allocated
171  *	dtor  The destructor that is called when the object is freed.
172  *	zinit  An initializer that sets up the initial state of the memory
173  *		as the object passes from the Keg's slab to the Zone's cache.
174  *	zfini  A discard function that undoes initialization done by init
175  *		as the object passes from the Zone's cache to the Keg's slab.
176  *
177  *		ctor/dtor/zinit/zfini may all be null, see notes above.
178  *		Note that the zinit and zfini specified here are NOT
179  *		exactly the same as the init/fini specified to uma_zcreate()
180  *		when creating a master zone.  These zinit/zfini are called
181  *		on the TRANSITION from keg to zone (and vice-versa). Once
182  *		these are set, the primary zone may alter its init/fini
183  *		(which are called when the object passes from VM to keg)
184  *		using uma_zone_set_init/fini()) as well as its own
185  *		zinit/zfini (unset by default for master zone) with
186  *		uma_zone_set_zinit/zfini() (note subtle 'z' prefix).
187  *
188  *	master  A reference to this zone's Master Zone (Primary Zone),
189  *		which contains the backing Keg for the Secondary Zone
190  *		being added.
191  *
192  * Returns:
193  *	A pointer to a structure which is intended to be opaque to users of
194  *	the interface.  The value may be null if the wait flag is not set.
195  */
196 uma_zone_t uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
197 		    uma_init zinit, uma_fini zfini, uma_zone_t master);
198 
199 /*
200  * Definitions for uma_zcreate flags
201  *
202  * These flags share space with UMA_ZFLAGs in uma_int.h.  Be careful not to
203  * overlap when adding new features.  0xf000 is in use by uma_int.h.
204  */
205 #define UMA_ZONE_PAGEABLE	0x0001	/* Return items not fully backed by
206 					   physical memory XXX Not yet */
207 #define UMA_ZONE_ZINIT		0x0002	/* Initialize with zeros */
208 #define UMA_ZONE_STATIC		0x0004	/* Staticly sized zone */
209 #define UMA_ZONE_OFFPAGE	0x0008	/* Force the slab structure allocation
210 					   off of the real memory */
211 #define UMA_ZONE_MALLOC		0x0010	/* For use by malloc(9) only! */
212 #define UMA_ZONE_NOFREE		0x0020	/* Do not free slabs of this type! */
213 #define UMA_ZONE_MTXCLASS	0x0040	/* Create a new lock class */
214 #define	UMA_ZONE_VM		0x0080	/*
215 					 * Used for internal vm datastructures
216 					 * only.
217 					 */
218 #define	UMA_ZONE_HASH		0x0100	/*
219 					 * Use a hash table instead of caching
220 					 * information in the vm_page.
221 					 */
222 #define	UMA_ZONE_SECONDARY	0x0200	/* Zone is a Secondary Zone */
223 #define	UMA_ZONE_REFCNT		0x0400	/* Allocate refcnts in slabs */
224 #define	UMA_ZONE_MAXBUCKET	0x0800	/* Use largest buckets */
225 
226 /* Definitions for align */
227 #define UMA_ALIGN_PTR	(sizeof(void *) - 1)	/* Alignment fit for ptr */
228 #define UMA_ALIGN_LONG	(sizeof(long) - 1)	/* "" long */
229 #define UMA_ALIGN_INT	(sizeof(int) - 1)	/* "" int */
230 #define UMA_ALIGN_SHORT	(sizeof(short) - 1)	/* "" short */
231 #define UMA_ALIGN_CHAR	(sizeof(char) - 1)	/* "" char */
232 #define UMA_ALIGN_CACHE	(16 - 1)		/* Cache line size align */
233 
234 /*
235  * Destroys an empty uma zone.  If the zone is not empty uma complains loudly.
236  *
237  * Arguments:
238  *	zone  The zone we want to destroy.
239  *
240  */
241 void uma_zdestroy(uma_zone_t zone);
242 
243 /*
244  * Allocates an item out of a zone
245  *
246  * Arguments:
247  *	zone  The zone we are allocating from
248  *	arg   This data is passed to the ctor function
249  *	flags See sys/malloc.h for available flags.
250  *
251  * Returns:
252  *	A non null pointer to an initialized element from the zone is
253  *	garanteed if the wait flag is M_WAITOK, otherwise a null pointer may be
254  *	returned if the zone is empty or the ctor failed.
255  */
256 
257 void *uma_zalloc_arg(uma_zone_t zone, void *arg, int flags);
258 
259 /*
260  * Allocates an item out of a zone without supplying an argument
261  *
262  * This is just a wrapper for uma_zalloc_arg for convenience.
263  *
264  */
265 static __inline void *uma_zalloc(uma_zone_t zone, int flags);
266 
267 static __inline void *
268 uma_zalloc(uma_zone_t zone, int flags)
269 {
270 	return uma_zalloc_arg(zone, NULL, flags);
271 }
272 
273 /*
274  * Frees an item back into the specified zone.
275  *
276  * Arguments:
277  *	zone  The zone the item was originally allocated out of.
278  *	item  The memory to be freed.
279  *	arg   Argument passed to the destructor
280  *
281  * Returns:
282  *	Nothing.
283  */
284 
285 void uma_zfree_arg(uma_zone_t zone, void *item, void *arg);
286 
287 /*
288  * Frees an item back to a zone without supplying an argument
289  *
290  * This is just a wrapper for uma_zfree_arg for convenience.
291  *
292  */
293 static __inline void uma_zfree(uma_zone_t zone, void *item);
294 
295 static __inline void
296 uma_zfree(uma_zone_t zone, void *item)
297 {
298 	uma_zfree_arg(zone, item, NULL);
299 }
300 
301 /*
302  * XXX The rest of the prototypes in this header are h0h0 magic for the VM.
303  * If you think you need to use it for a normal zone you're probably incorrect.
304  */
305 
306 /*
307  * Backend page supplier routines
308  *
309  * Arguments:
310  *	zone  The zone that is requesting pages
311  *	size  The number of bytes being requested
312  *	pflag Flags for these memory pages, see below.
313  *	wait  Indicates our willingness to block.
314  *
315  * Returns:
316  *	A pointer to the alloced memory or NULL on failure.
317  */
318 
319 typedef void *(*uma_alloc)(uma_zone_t zone, int size, u_int8_t *pflag, int wait);
320 
321 /*
322  * Backend page free routines
323  *
324  * Arguments:
325  *	item  A pointer to the previously allocated pages
326  *	size  The original size of the allocation
327  *	pflag The flags for the slab.  See UMA_SLAB_* below
328  *
329  * Returns:
330  *	None
331  */
332 typedef void (*uma_free)(void *item, int size, u_int8_t pflag);
333 
334 
335 
336 /*
337  * Sets up the uma allocator. (Called by vm_mem_init)
338  *
339  * Arguments:
340  *	bootmem  A pointer to memory used to bootstrap the system.
341  *
342  * Returns:
343  *	Nothing
344  *
345  * Discussion:
346  *	This memory is used for zones which allocate things before the
347  *	backend page supplier can give us pages.  It should be
348  *	UMA_SLAB_SIZE * UMA_BOOT_PAGES bytes. (see uma_int.h)
349  *
350  */
351 
352 void uma_startup(void *bootmem);
353 
354 /*
355  * Finishes starting up the allocator.  This should
356  * be called when kva is ready for normal allocs.
357  *
358  * Arguments:
359  *	None
360  *
361  * Returns:
362  *	Nothing
363  *
364  * Discussion:
365  *	uma_startup2 is called by kmeminit() to enable us of uma for malloc.
366  */
367 
368 void uma_startup2(void);
369 
370 /*
371  * Reclaims unused memory for all zones
372  *
373  * Arguments:
374  *	None
375  * Returns:
376  *	None
377  *
378  * This should only be called by the page out daemon.
379  */
380 
381 void uma_reclaim(void);
382 
383 /*
384  * Switches the backing object of a zone
385  *
386  * Arguments:
387  *	zone  The zone to update
388  *	obj   The obj to use for future allocations
389  *	size  The size of the object to allocate
390  *
391  * Returns:
392  *	0  if kva space can not be allocated
393  *	1  if successful
394  *
395  * Discussion:
396  *	A NULL object can be used and uma will allocate one for you.  Setting
397  *	the size will limit the amount of memory allocated to this zone.
398  *
399  */
400 struct vm_object;
401 int uma_zone_set_obj(uma_zone_t zone, struct vm_object *obj, int size);
402 
403 /*
404  * Sets a high limit on the number of items allowed in a zone
405  *
406  * Arguments:
407  *	zone  The zone to limit
408  *
409  * Returns:
410  *	Nothing
411  */
412 void uma_zone_set_max(uma_zone_t zone, int nitems);
413 
414 /*
415  * The following two routines (uma_zone_set_init/fini)
416  * are used to set the backend init/fini pair which acts on an
417  * object as it becomes allocated and is placed in a slab within
418  * the specified zone's backing keg.  These should probably not
419  * be changed once allocations have already begun and only
420  * immediately upon zone creation.
421  */
422 void uma_zone_set_init(uma_zone_t zone, uma_init uminit);
423 void uma_zone_set_fini(uma_zone_t zone, uma_fini fini);
424 
425 /*
426  * The following two routines (uma_zone_set_zinit/zfini) are
427  * used to set the zinit/zfini pair which acts on an object as
428  * it passes from the backing Keg's slab cache to the
429  * specified Zone's bucket cache.  These should probably not
430  * be changed once allocations have already begun and
431  * only immediately upon zone creation.
432  */
433 void uma_zone_set_zinit(uma_zone_t zone, uma_init zinit);
434 void uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini);
435 
436 /*
437  * Replaces the standard page_alloc or obj_alloc functions for this zone
438  *
439  * Arguments:
440  *	zone   The zone whos back end allocator is being changed.
441  *	allocf A pointer to the allocation function
442  *
443  * Returns:
444  *	Nothing
445  *
446  * Discussion:
447  *	This could be used to implement pageable allocation, or perhaps
448  *	even DMA allocators if used in conjunction with the OFFPAGE
449  *	zone flag.
450  */
451 
452 void uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf);
453 
454 /*
455  * Used for freeing memory provided by the allocf above
456  *
457  * Arguments:
458  *	zone  The zone that intends to use this free routine.
459  *	freef The page freeing routine.
460  *
461  * Returns:
462  *	Nothing
463  */
464 
465 void uma_zone_set_freef(uma_zone_t zone, uma_free freef);
466 
467 /*
468  * These flags are setable in the allocf and visable in the freef.
469  */
470 #define UMA_SLAB_BOOT	0x01		/* Slab alloced from boot pages */
471 #define UMA_SLAB_KMEM	0x02		/* Slab alloced from kmem_map */
472 #define UMA_SLAB_PRIV	0x08		/* Slab alloced from priv allocator */
473 #define UMA_SLAB_OFFP	0x10		/* Slab is managed separately  */
474 #define UMA_SLAB_MALLOC	0x20		/* Slab is a large malloc slab */
475 /* 0x40 and 0x80 are available */
476 
477 /*
478  * Used to pre-fill a zone with some number of items
479  *
480  * Arguments:
481  *	zone    The zone to fill
482  *	itemcnt The number of items to reserve
483  *
484  * Returns:
485  *	Nothing
486  *
487  * NOTE: This is blocking and should only be done at startup
488  */
489 void uma_prealloc(uma_zone_t zone, int itemcnt);
490 
491 /*
492  * Used to lookup the reference counter allocated for an item
493  * from a UMA_ZONE_REFCNT zone.  For UMA_ZONE_REFCNT zones,
494  * reference counters are allocated for items and stored in
495  * the underlying slab header.
496  *
497  * Arguments:
498  * 	zone  The UMA_ZONE_REFCNT zone to which the item belongs.
499  *	item  The address of the item for which we want a refcnt.
500  *
501  * Returns:
502  * 	A pointer to a u_int32_t reference counter.
503  */
504 u_int32_t *uma_find_refcnt(uma_zone_t zone, void *item);
505 
506 #endif
507