Anotamy of Linux SLAB layer
What it for?
============
The Linux kernel provides the SLAB layer, called the slab allocator, to allocate
and free frequently used data structures.
Where is it?
============
The SLAB layer built on top of physical pages and offer serveices to kmalloc().
------------------------------------------------------------------------
kvmalloc();
------------------------------------------------------------------------
SLAB layer: struct kmem_cache
void *kmem_cache_alloc()
void kmem_cache_free()
-------------------------------------------------------------------------
Physical pages: struct page
struct page * alloc_pages()
unsigned long __get_free_pages()
void __free_pages()
void free_pages()
------------------------------------------------------------------------
The design
==========
The SLAB layer divides differrent objects into groups called 'caches', each of
which stores a different type of object. There is one cache per object type.
The caches are then deviced into 'slabs'. The slabs are composed of one or more
physically CONTIGUOUS pages. Each cache may consist of mutiple slabs.
Each slab contains some number of objects, which are the data structures being
cached.
The 'cache' organizes 'slab' in it in groups - FULL/PARTIAL/EMPTY.
SLAB interface:
(1)
/**
* kmem_cache_create - Create a cache.
* @name: A string which is used in /proc/slabinfo to identify this cache.
* @size: The size of each object to be created in this cache.
* @align: The required alignment for the objects.
* @flags: SLAB flags
* @ctor: A constructor for the objects.
struct kmem_cache * kmem_cache_create(
const char *name;
size_t size;
size_t align;
unsigned long flags;
void (*ctor)(void *));
The point to understand it is that the routine DOES NOT alloc memory for
objects to be stored in it - it just allocates memory to store the cache's
descriptor, kmem_cache.
Allocating memory for objects is done by kmem_cache_alloc(). See below.
(2)
/**
* kmem_cache_alloc - Allocate an object
* @cachep: The cache to allocate from.
* @flags: See kmalloc().
*
* Allocate an object from this cache. The flags are only relevant
* if the cache has no available objects.
*/
void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
(3)
/**
* kmem_cache_free - Deallocate an object
* @cachep: The cache the allocation was from.
* @objp: The previously allocated object.
*
* Free an object which was previously allocated from this
* cache.
*/
void kmem_cache_free(struct kmem_cache *cachep, void *objp)
(4)
/**
* kmem_cache_destroy - delete a cache
* @cachep: the cache to destroy
*
* Remove a &struct kmem_cache object from the slab cache.
*
* It is expected this function will be called by a module when it is
* unloaded. This will remove the cache completely, and avoid a duplicate
* cache being allocated each time a module is loaded and unloaded, if the
* module doesn't have persistent in-kernel storage across loads and unloads.
*
* The cache must be empty before calling this function.
*
* The caller must guarantee that noone will allocate memory from the cache
* during the kmem_cache_destroy().
*/
void kmem_cache_destroy(struct kmem_cache *cachep)
SLAB/SLOB/SLUB
===============
The concept of SLAB was first implemented in Sun microsystem's SunOS. The Linux
data structure caching system shared the design.
The other two siblings were introduced to Linux later, SLUB and SLOB.
SLOB: An embeded version of SLAB.
SLUB: An improved version of SLAB.
BTW, TCC8900 BSP uses SLUB.
User interface
===============
Users are able to check out caches status on system via /proc/slabinfo.
Below is an example:
/proc # cat slabinfo
slabinfo - version: 2.1
# name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> : tunables <limit> <batchcount> <sharedfactor> : slabdata <active_slabs> <num_slabs> <sharedavail>
rpc_inode_cache 0 0 416 19 2 : tunables 0 0 0 : slabdata 0 0 0
udf_inode_cache 0 0 360 11 1 : tunables 0 0 0 : slabdata 0 0 0
fuse_request 0 0 376 21 2 : tunables 0 0 0 : slabdata 0 0 0
fuse_inode 0 0 416 19 2 : tunables 0 0 0 : slabdata 0 0 0
nfs_direct_cache 0 0 72 56 1 : tunables 0 0 0 : slabdata 0 0 0
nfs_write_data 36 36 448 9 1 : tunables 0 0 0 : slabdata 4 4 0
nfs_inode_cache 0 0 592 13 2 : tunables 0 0 0 : slabdata 0 0 0
isofs_inode_cache 0 0 336 12 1 : tunables 0 0 0 : slabdata 0 0 0
fat_inode_cache 11 11 360 11 1 : tunables 0 0 0 : slabdata 1 1 0
fat_cache 0 0 24 170 1 : tunables 0 0 0 : slabdata 0 0 0
ext2_inode_cache 566 567 432 9 1 : tunables 0 0 0 : slabdata 63 63 0
jbd2_revoke_record 0 0 32 128 1 : tunables 0 0 0 : slabdata 0 0 0
journal_handle 0 0 24 170 1 : tunables 0 0 0 : slabdata 0 0 0
revoke_record 0 0 16 256 1 : tunables 0 0 0 : slabdata 0 0 0
ext4_inode_cache 0 0 536 15 2 : tunables 0 0 0 : slabdata 0 0 0
ext4_free_block_extents 0 0 40 102 1 : tunables 0 0 0 : slabdata 0 0 0
ext4_alloc_context 0 0 112 36 1 : tunables 0 0 0 : slabdata 0 0 0
ext4_prealloc_space 0 0 64 64 1 : tunables 0 0 0 : slabdata 0 0 0
ext3_inode_cache 0 0 440 9 1 : tunables 0 0 0 : slabdata 0 0 0
ext3_xattr 0 0 56 73 1 : tunables 0 0 0 : slabdata 0 0 0
kiocb 25 25 160 25 1 : tunables 0 0 0 : slabdata 1 1 0
shmem_inode_cache 10 10 400 10 1 : tunables 0 0 0 : slabdata 1 1 0
UDP 8 8 480 8 1 : tunables 0 0 0 : slabdata 1 1 0
TCP 0 0 1056 15 4 : tunables 0 0 0 : slabdata 0 0 0
eventpoll_pwq 102 102 40 102 1 : tunables 0 0 0 : slabdata 1 1 0
scsi_data_buffer 0 0 24 170 1 : tunables 0 0 0 : slabdata 0 0 0
scsi_io_context 0 0 104 39 1 : tunables 0 0 0 : slabdata 0 0 0
blkdev_queue 26 26 1248 13 4 : tunables 0 0 0 : slabdata 2 2 0
blkdev_requests 18 18 224 18 1 : tunables 0 0 0 : slabdata 1 1 0
biovec-256 10 10 3072 10 8 : tunables 0 0 0 : slabdata 1 1 0
biovec-128 10 10 1536 10 4 : tunables 0 0 0 : slabdata 1 1 0
sock_inode_cache 11 11 352 11 1 : tunables 0 0 0 : slabdata 1 1 0
skbuff_fclone_cache 0 0 352 11 1 : tunables 0 0 0 : slabdata 0 0 0
file_lock_cache 0 0 104 39 1 : tunables 0 0 0 : slabdata 0 0 0
proc_inode_cache 36 36 328 12 1 : tunables 0 0 0 : slabdata 3 3 0
sigqueue 28 28 144 28 1 : tunables 0 0 0 : slabdata 1 1 0
radix_tree_node 199 234 296 13 1 : tunables 0 0 0 : slabdata 18 18 0
bdev_cache 19 19 416 19 2 : tunables 0 0 0 : slabdata 1 1 0
sysfs_dir_cache 4250 4250 48 85 1 : tunables 0 0 0 : slabdata 50 50 0
inode_cache 702 702 304 13 1 : tunables 0 0 0 : slabdata 54 54 0
dentry 976 1024 128 32 1 : tunables 0 0 0 : slabdata 32 32 0
buffer_head 888 896 72 56 1 : tunables 0 0 0 : slabdata 16 16 0
mm_struct 10 10 384 10 1 : tunables 0 0 0 : slabdata 1 1 0
vm_area_struct 68 92 88 46 1 : tunables 0 0 0 : slabdata 2 2 0
signal_cache 74 76 416 19 2 : tunables 0 0 0 : slabdata 4 4 0
sighand_cache 34 36 1312 12 4 : tunables 0 0 0 : slabdata 3 3 0
task_struct 34 60 768 10 2 : tunables 0 0 0 : slabdata 6 6 0
anon_vma 256 256 16 256 1 : tunables 0 0 0 : slabdata 1 1 0
idr_layer_cache 128 130 152 26 1 : tunables 0 0 0 : slabdata 5 5 0
kmalloc-4096 8 8 4096 8 8 : tunables 0 0 0 : slabdata 1 1 0
kmalloc-2048 24 24 2048 8 4 : tunables 0 0 0 : slabdata 3 3 0
kmalloc-1024 34 48 1024 8 2 : tunables 0 0 0 : slabdata 6 6 0
kmalloc-512 213 216 512 8 1 : tunables 0 0 0 : slabdata 27 27 0
kmalloc-256 48 48 256 16 1 : tunables 0 0 0 : slabdata 3 3 0
kmalloc-128 302 352 128 32 1 : tunables 0 0 0 : slabdata 11 11 0
kmalloc-64 510 512 64 64 1 : tunables 0 0 0 : slabdata 8 8 0
kmalloc-32 379 384 32 128 1 : tunables 0 0 0 : slabdata 3 3 0
kmalloc-16 768 768 16 256 1 : tunables 0 0 0 : slabdata 3 3 0
kmalloc-8 1536 1536 8 512 1 : tunables 0 0 0 : slabdata 3 3 0
kmalloc-192 111 168 192 21 1 : tunables 0 0 0 : slabdata 8 8 0
kmalloc-96 400 420 96 42 1 : tunables 0 0 0 : slabdata 10 10 0
What it for?
============
The Linux kernel provides the SLAB layer, called the slab allocator, to allocate
and free frequently used data structures.
Where is it?
============
The SLAB layer built on top of physical pages and offer serveices to kmalloc().
------------------------------------------------------------------------
kvmalloc();
------------------------------------------------------------------------
SLAB layer: struct kmem_cache
void *kmem_cache_alloc()
void kmem_cache_free()
-------------------------------------------------------------------------
Physical pages: struct page
struct page * alloc_pages()
unsigned long __get_free_pages()
void __free_pages()
void free_pages()
------------------------------------------------------------------------
The design
==========
The SLAB layer divides differrent objects into groups called 'caches', each of
which stores a different type of object. There is one cache per object type.
The caches are then deviced into 'slabs'. The slabs are composed of one or more
physically CONTIGUOUS pages. Each cache may consist of mutiple slabs.
Each slab contains some number of objects, which are the data structures being
cached.
The 'cache' organizes 'slab' in it in groups - FULL/PARTIAL/EMPTY.
SLAB interface:
(1)
/**
* kmem_cache_create - Create a cache.
* @name: A string which is used in /proc/slabinfo to identify this cache.
* @size: The size of each object to be created in this cache.
* @align: The required alignment for the objects.
* @flags: SLAB flags
* @ctor: A constructor for the objects.
struct kmem_cache * kmem_cache_create(
const char *name;
size_t size;
size_t align;
unsigned long flags;
void (*ctor)(void *));
The point to understand it is that the routine DOES NOT alloc memory for
objects to be stored in it - it just allocates memory to store the cache's
descriptor, kmem_cache.
Allocating memory for objects is done by kmem_cache_alloc(). See below.
(2)
/**
* kmem_cache_alloc - Allocate an object
* @cachep: The cache to allocate from.
* @flags: See kmalloc().
*
* Allocate an object from this cache. The flags are only relevant
* if the cache has no available objects.
*/
void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
(3)
/**
* kmem_cache_free - Deallocate an object
* @cachep: The cache the allocation was from.
* @objp: The previously allocated object.
*
* Free an object which was previously allocated from this
* cache.
*/
void kmem_cache_free(struct kmem_cache *cachep, void *objp)
(4)
/**
* kmem_cache_destroy - delete a cache
* @cachep: the cache to destroy
*
* Remove a &struct kmem_cache object from the slab cache.
*
* It is expected this function will be called by a module when it is
* unloaded. This will remove the cache completely, and avoid a duplicate
* cache being allocated each time a module is loaded and unloaded, if the
* module doesn't have persistent in-kernel storage across loads and unloads.
*
* The cache must be empty before calling this function.
*
* The caller must guarantee that noone will allocate memory from the cache
* during the kmem_cache_destroy().
*/
void kmem_cache_destroy(struct kmem_cache *cachep)
SLAB/SLOB/SLUB
===============
The concept of SLAB was first implemented in Sun microsystem's SunOS. The Linux
data structure caching system shared the design.
The other two siblings were introduced to Linux later, SLUB and SLOB.
SLOB: An embeded version of SLAB.
SLUB: An improved version of SLAB.
BTW, TCC8900 BSP uses SLUB.
User interface
===============
Users are able to check out caches status on system via /proc/slabinfo.
Below is an example:
/proc # cat slabinfo
slabinfo - version: 2.1
# name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> : tunables <limit> <batchcount> <sharedfactor> : slabdata <active_slabs> <num_slabs> <sharedavail>
rpc_inode_cache 0 0 416 19 2 : tunables 0 0 0 : slabdata 0 0 0
udf_inode_cache 0 0 360 11 1 : tunables 0 0 0 : slabdata 0 0 0
fuse_request 0 0 376 21 2 : tunables 0 0 0 : slabdata 0 0 0
fuse_inode 0 0 416 19 2 : tunables 0 0 0 : slabdata 0 0 0
nfs_direct_cache 0 0 72 56 1 : tunables 0 0 0 : slabdata 0 0 0
nfs_write_data 36 36 448 9 1 : tunables 0 0 0 : slabdata 4 4 0
nfs_inode_cache 0 0 592 13 2 : tunables 0 0 0 : slabdata 0 0 0
isofs_inode_cache 0 0 336 12 1 : tunables 0 0 0 : slabdata 0 0 0
fat_inode_cache 11 11 360 11 1 : tunables 0 0 0 : slabdata 1 1 0
fat_cache 0 0 24 170 1 : tunables 0 0 0 : slabdata 0 0 0
ext2_inode_cache 566 567 432 9 1 : tunables 0 0 0 : slabdata 63 63 0
jbd2_revoke_record 0 0 32 128 1 : tunables 0 0 0 : slabdata 0 0 0
journal_handle 0 0 24 170 1 : tunables 0 0 0 : slabdata 0 0 0
revoke_record 0 0 16 256 1 : tunables 0 0 0 : slabdata 0 0 0
ext4_inode_cache 0 0 536 15 2 : tunables 0 0 0 : slabdata 0 0 0
ext4_free_block_extents 0 0 40 102 1 : tunables 0 0 0 : slabdata 0 0 0
ext4_alloc_context 0 0 112 36 1 : tunables 0 0 0 : slabdata 0 0 0
ext4_prealloc_space 0 0 64 64 1 : tunables 0 0 0 : slabdata 0 0 0
ext3_inode_cache 0 0 440 9 1 : tunables 0 0 0 : slabdata 0 0 0
ext3_xattr 0 0 56 73 1 : tunables 0 0 0 : slabdata 0 0 0
kiocb 25 25 160 25 1 : tunables 0 0 0 : slabdata 1 1 0
shmem_inode_cache 10 10 400 10 1 : tunables 0 0 0 : slabdata 1 1 0
UDP 8 8 480 8 1 : tunables 0 0 0 : slabdata 1 1 0
TCP 0 0 1056 15 4 : tunables 0 0 0 : slabdata 0 0 0
eventpoll_pwq 102 102 40 102 1 : tunables 0 0 0 : slabdata 1 1 0
scsi_data_buffer 0 0 24 170 1 : tunables 0 0 0 : slabdata 0 0 0
scsi_io_context 0 0 104 39 1 : tunables 0 0 0 : slabdata 0 0 0
blkdev_queue 26 26 1248 13 4 : tunables 0 0 0 : slabdata 2 2 0
blkdev_requests 18 18 224 18 1 : tunables 0 0 0 : slabdata 1 1 0
biovec-256 10 10 3072 10 8 : tunables 0 0 0 : slabdata 1 1 0
biovec-128 10 10 1536 10 4 : tunables 0 0 0 : slabdata 1 1 0
sock_inode_cache 11 11 352 11 1 : tunables 0 0 0 : slabdata 1 1 0
skbuff_fclone_cache 0 0 352 11 1 : tunables 0 0 0 : slabdata 0 0 0
file_lock_cache 0 0 104 39 1 : tunables 0 0 0 : slabdata 0 0 0
proc_inode_cache 36 36 328 12 1 : tunables 0 0 0 : slabdata 3 3 0
sigqueue 28 28 144 28 1 : tunables 0 0 0 : slabdata 1 1 0
radix_tree_node 199 234 296 13 1 : tunables 0 0 0 : slabdata 18 18 0
bdev_cache 19 19 416 19 2 : tunables 0 0 0 : slabdata 1 1 0
sysfs_dir_cache 4250 4250 48 85 1 : tunables 0 0 0 : slabdata 50 50 0
inode_cache 702 702 304 13 1 : tunables 0 0 0 : slabdata 54 54 0
dentry 976 1024 128 32 1 : tunables 0 0 0 : slabdata 32 32 0
buffer_head 888 896 72 56 1 : tunables 0 0 0 : slabdata 16 16 0
mm_struct 10 10 384 10 1 : tunables 0 0 0 : slabdata 1 1 0
vm_area_struct 68 92 88 46 1 : tunables 0 0 0 : slabdata 2 2 0
signal_cache 74 76 416 19 2 : tunables 0 0 0 : slabdata 4 4 0
sighand_cache 34 36 1312 12 4 : tunables 0 0 0 : slabdata 3 3 0
task_struct 34 60 768 10 2 : tunables 0 0 0 : slabdata 6 6 0
anon_vma 256 256 16 256 1 : tunables 0 0 0 : slabdata 1 1 0
idr_layer_cache 128 130 152 26 1 : tunables 0 0 0 : slabdata 5 5 0
kmalloc-4096 8 8 4096 8 8 : tunables 0 0 0 : slabdata 1 1 0
kmalloc-2048 24 24 2048 8 4 : tunables 0 0 0 : slabdata 3 3 0
kmalloc-1024 34 48 1024 8 2 : tunables 0 0 0 : slabdata 6 6 0
kmalloc-512 213 216 512 8 1 : tunables 0 0 0 : slabdata 27 27 0
kmalloc-256 48 48 256 16 1 : tunables 0 0 0 : slabdata 3 3 0
kmalloc-128 302 352 128 32 1 : tunables 0 0 0 : slabdata 11 11 0
kmalloc-64 510 512 64 64 1 : tunables 0 0 0 : slabdata 8 8 0
kmalloc-32 379 384 32 128 1 : tunables 0 0 0 : slabdata 3 3 0
kmalloc-16 768 768 16 256 1 : tunables 0 0 0 : slabdata 3 3 0
kmalloc-8 1536 1536 8 512 1 : tunables 0 0 0 : slabdata 3 3 0
kmalloc-192 111 168 192 21 1 : tunables 0 0 0 : slabdata 8 8 0
kmalloc-96 400 420 96 42 1 : tunables 0 0 0 : slabdata 10 10 0