Skip to contentSkip to conversation

SLUB: The unqueued slab allocator V5

PATCH In review Christoph Lameter 2 participants 6 messages Mar 11, 2007

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover SLUB: The unqueued slab allocator V5

[PATCH] SLUB The unqueued slab allocator v4

V4->V5:

- Single object slabs only for slabs > slub_max_order otherwise generate sufficient objects to avoid frequent use of the page allocator. This is necessary to compensate for fragmentation caused by frequent uses of the page allocator. We expect slabs of PAGE_SIZE from this rule since multi object slabs require uses of fields that are in use on i386 and x86_64. See the quicklist patchset for a way to fix that issue and a patch to get rid of the PAGE_SIZE special casing.

- Drop pass through to page allocator due to page allocator fragmenting memory. The buffering through large order allocations is done in SLUB. Infrequent larger order allocations cause less fragmentation than frequent small order allocations.

- We need to update object sizes when merging slabs otherwise kzalloc will not initialize the full object (this caused the failure on varios platforms).

- Padding checks before redzone checks so that we get messages about the corruption of whole slab and not about a single object.

Note that SLUB will warn on zero sized allocations. SLAB just allocates some memory. So some traces from the usb subsystem etc should be expected.

Note that the definition of the return type of ksize() is currently different between mm and Linus tree. Patch is conforming to mm.

V3->V4
- Rename /proc/slabinfo to /proc/slubinfo. We have a different format after all.
- More bug fixes and stabilization of diagnostic functions. This seems to be finally something that works wherever we test it.
- Serialize kmem_cache_create and kmem_cache_destroy via slub_lock (Adrian's idea)
- Add two new modifications (separate patches) to guarantee
a mininum number of objects per slab and to pass through large allocations.

V2->V3
- Debugging and diagnostic support. This is runtime enabled and not compile time enabled. Runtime debugging can be controlled via kernel boot options on an individual slab cache basis or globally.
- Slab Trace support (For individual slab caches).
- Resiliency support: If basic sanity checks are enabled (via F f.e.) (boot option) then SLUB will do the best to perform diagnostics and then continue (i.e. mark corrupted objects as used).
- Fix up numerous issues including clash of SLUBs use of page flags with i386 arch use for pmd and pgds (which are managed as slab caches, sigh).
- Dynamic per CPU array sizing.
- Explain SLUB slabcache flags

V1->V2
- Fix up various issues. Tested on i386 UP, X86_64 SMP, ia64 NUMA.
- Provide NUMA support by splitting partial lists per node.
- Better Slab cache merge support (now at around 50% of slabs)
- List slab cache aliases if slab caches are merged.
- Updated descriptions /proc/slabinfo output

This is a new slab allocator which was motivated by the complexity of the existing code in mm/slab.c. It attempts to address a variety of concerns with the existing implementation.

A. Management of object queues

A particular concern was the complex management of the numerous object queues in SLAB. SLUB has no such queues. Instead we dedicate a slab for each allocating CPU and use objects from a slab directly instead of queueing them up.

B. Storage overhead of object queues

SLAB Object queues exist per node, per CPU. The alien cache queue even has a queue array that contain a queue for each processor on each node. For very large systems the number of queues and the number of objects that may be caught in those queues grows exponentially. On our systems with 1k nodes / processors we have several gigabytes just tied up for storing references to objects for those queues This does not include the objects that could be on those queues. One fears that the whole memory of the machine could one day be consumed by those queues.

C. SLAB meta data overhead

SLAB has overhead at the beginning of each slab. This means that data cannot be naturally aligned at the beginning of a slab block. SLUB keeps all meta data in the corresponding page_struct. Objects can be naturally aligned in the slab. F.e. a 128 byte object will be aligned at 128 byte boundaries and can fit tightly into a 4k page with no bytes left over. SLAB cannot do this.

D. SLAB has a complex cache reaper

SLUB does not need a cache reaper for UP systems. On SMP systems the per CPU slab may be pushed back into partial list but that operation is simple and does not require an iteration over a list of objects. SLAB expires per CPU, shared and alien object queues during cache reaping which may cause strange hold offs.

E. SLAB has complex NUMA policy layer support

SLUB pushes NUMA policy handling into the page allocator. This means that allocation is coarser (SLUB does interleave on a page level) but that situation was also present before 2.6.13. SLABs application of policies to individual slab objects allocated in SLAB is certainly a performance concern due to the frequent references to memory policies which may lead a sequence of objects to come from one node after another. SLUB will get a slab full of objects from one node and then will switch to the next.

F. Reduction of the size of partial slab lists

SLAB has per node partial lists. This means that over time a large number of partial slabs may accumulate on those lists. These can only be reused if allocator occur on specific nodes. SLUB has a global pool of partial slabs and will consume slabs from that pool to decrease fragmentation.

G. Tunables

SLAB has sophisticated tuning abilities for each slab cache. One can manipulate the queue sizes in detail. However, filling the queues still requires the uses of the spin lock to check out slabs. SLUB has a global parameter (min_slab_order) for tuning. Increasing the minimum slab order can decrease the locking overhead. The bigger the slab order the less motions of pages between per CPU and partial lists occur and the better SLUB will be scaling.

G. Slab merging

We often have slab caches with similar parameters. SLUB detects those on boot up and merges them into the corresponding general caches. This leads to more effective memory use. About 50% of all caches can be eliminated through slab merging. This will also decrease slab fragmentation because partial allocated slabs can be filled up again. Slab merging can be switched off by specifying slub_nomerge on boot up.

Note that merging can expose heretofore unknown bugs in the kernel because corrupted objects may now be placed differently and corrupt differing neighboring objects. Enable sanity checks to find those.

H. Diagnostics

The current slab diagnostics are difficult to use and require a recompilation of the kernel. SLUB contains debugging code that is always available (but is kept out of the hot code paths). SLUB diagnostics can be enabled via the "slab_debug" option. Parameters can be specified to select a single or a group of slab caches for diagnostics. This means that the system is running with the usual performance and it is much more likely that race conditions can be reproduced.

I. Resiliency

If basic sanity checks are on then SLUB is capable of detecting common error conditions and recover as best as possible to allow the system to continue.

J. Tracing

Tracing can be enabled via the slab_debug=T,<slabcache> option during boot. SLUB will then protocol each action on that slabcache and dump the object contents on free.

K. On demand DMA cache creation.

Generally DMA caches are not needed. If a kmalloc is used with __GFP_DMA then just create this single slabcache that is needed. For systems that have no ZONE_DMA requirement the support is completely eliminated.

L. Performance increase

Some benchmarks have shown speed improvements on kernbench in the range of 5-10%. The locking overhead of slub is based on the underlying base allocation size. If we can reliably allocate larger order pages then it is possible to increase slub performance much further. The anti-fragmentation patches may enable further performance increases.

Tested on:
i386 UP + SMP, x86_64 UP + SMP + NUMA emulation, IA64 NUMA + Simulator

SLUB Boot options

slub_nomerge Disable merging of slabs
slub_min_order=x Require a minimum order for slab caches. This
increases the managed chunk size and therefore reduces meta data and locking overhead.
slub_min_objects=x Mininum objects per slab. Default is 8.
slub_max_order=x Avoid generating slab large than order specified.
slub_debug Enable all diagnostics for all caches
slub_debug=<options> Enable selective options for all caches
slub_debug=<o>,<cache> Enable selective options for a certain set of
caches

Available Debug options
F Double Free checking, sanity and resiliency
R Red zoning
P Object / padding poisoning
U Track last free / alloc
T Trace all allocs / frees (only use on individual slabs).

To use SLUB: Apply this patch and then select SLUB as the default slab allocator. The output of /proc/slabinfo will then change. Here is a sample (this is an UP/SMP format. The NUMA display will show on which nodes the slabs were allocated). Flags are

a Cpucache Align requested
A Hardware Align required
C Constructor
d DMA cache
D Destructor
F Double free checking/Sanity
p Panic on failure
P Poisoning
r Objects are reclaimable
R RCU destroy
S Memory Spreading
U User Tracking
T Tracing
Z Red Zone

Thanks to Adrian Drzewiecki <[email protected]> for many ideas and spotting a lot of bugs.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#1 lore raw

PATCH 1/3 SLUB core

- Single object slabs only for slabs > slub_max_order otherwise generate sufficient objects to avoid frequent use of the page allocator. This is necessary to compensate for fragmentation caused by frequent uses of the page allocator.

- Drop pass through to page allocator due to page allocator fragmenting memory. The buffering of large order allocations is done in SLUB.

- We need to update object sizes when merging slabs otherwise kzalloc will not initialize the full object.

- Padding checks before redzone checks so that we get messages when the whole slab is corrupted first.

fs/proc/proc_misc.c +18 -0
Index: linux-2.6.21-rc3/fs/proc/proc_misc.c
===================================================================
@@ -397,6 +397,21 @@ static const struct file_operations proc
 };
 #endif
 
+#ifdef CONFIG_SLUB
+extern struct seq_operations slubinfo_op;
+static int slubinfo_open(struct inode *inode, struct file *file)
+{
+	return seq_open(file, &slubinfo_op);
+}
+static const struct file_operations proc_slubinfo_operations = {
+	.open		= slubinfo_open,
+	.read		= seq_read,
+	.llseek		= seq_lseek,
+	.release	= seq_release,
+};
+#endif
+
+
 #ifdef CONFIG_SLAB
 extern struct seq_operations slabinfo_op;
 extern ssize_t slabinfo_write(struct file *, const char __user *, size_t, loff_t *);
@@ -708,6 +723,9 @@ void __init proc_misc_init(void)
 #endif
 	create_seq_entry("stat", 0, &proc_stat_operations);
 	create_seq_entry("interrupts", 0, &proc_interrupts_operations);
+#ifdef CONFIG_SLUB
+	create_seq_entry("slubinfo",S_IWUSR|S_IRUGO,&proc_slubinfo_operations);
+#endif
 #ifdef CONFIG_SLAB
 	create_seq_entry("slabinfo",S_IWUSR|S_IRUGO,&proc_slabinfo_operations);
 #ifdef CONFIG_DEBUG_SLAB_LEAK
include/linux/mm_types.h +15 -2
Index: linux-2.6.21-rc3/include/linux/mm_types.h
===================================================================
@@ -19,10 +19,16 @@ struct page {
 	unsigned long flags;		/* Atomic flags, some possibly
 					 * updated asynchronously */
 	atomic_t _count;		/* Usage count, see below. */
-	atomic_t _mapcount;		/* Count of ptes mapped in mms,
+	union {
+		atomic_t _mapcount;	/* Count of ptes mapped in mms,
 					 * to show when page is mapped
 					 * & limit reverse map searches.
 					 */
+		struct {	/* SLUB uses */
+			short unsigned int inuse;
+			short unsigned int offset;
+		};
+	};
 	union {
 	    struct {
 		unsigned long private;		/* Mapping-private opaque data:
@@ -43,8 +49,15 @@ struct page {
 #if NR_CPUS >= CONFIG_SPLIT_PTLOCK_CPUS
 	    spinlock_t ptl;
 #endif
+	    struct {			/* SLUB uses */
+		struct page *first_page;	/* Compound pages */
+		struct kmem_cache *slab;	/* Pointer to slab */
+	    };
+	};
+	union {
+		pgoff_t index;		/* Our offset within mapping. */
+		void *freelist;		/* SLUB: pointer to free object */
 	};
-	pgoff_t index;			/* Our offset within mapping. */
 	struct list_head lru;		/* Pageout list, eg. active_list
 					 * protected by zone->lru_lock !
 					 */
include/linux/slab.h +7 -1
Index: linux-2.6.21-rc3/include/linux/slab.h
===================================================================
@@ -32,6 +32,7 @@ typedef struct kmem_cache kmem_cache_t _
 #define SLAB_PANIC		0x00040000UL	/* Panic if kmem_cache_create() fails */
 #define SLAB_DESTROY_BY_RCU	0x00080000UL	/* Defer freeing slabs to RCU */
 #define SLAB_MEM_SPREAD		0x00100000UL	/* Spread some memory over cpuset */
+#define SLAB_TRACE		0x00200000UL	/* Trace allocations and frees */
 
 /* Flags passed to a constructor functions */
 #define SLAB_CTOR_CONSTRUCTOR	0x001UL		/* If not set, then deconstructor */
@@ -94,9 +95,14 @@ static inline void *kcalloc(size_t n, si
  * the appropriate general cache at compile time.
  */
 
-#ifdef CONFIG_SLAB
+#if defined(CONFIG_SLAB) || defined(CONFIG_SLUB)
+#ifdef CONFIG_SLUB
+#include <linux/slub_def.h>
+#else
 #include <linux/slab_def.h>
+#endif /* !CONFIG_SLUB */
 #else
+
 /*
  * Fallback definitions for an allocator not wanting to provide
  * its own optimized kmalloc definitions (like SLOB).
include/linux/slub_def.h new file +177 -0

177 lines not on this page · raw message

init/Kconfig +40 -13

71 lines not on this page · raw message

mm/Makefile +1 -0
Index: linux-2.6.21-rc3/mm/Makefile
===================================================================
@@ -25,6 +25,7 @@ obj-$(CONFIG_TMPFS_POSIX_ACL) += shmem_a
 obj-$(CONFIG_TINY_SHMEM) += tiny-shmem.o
 obj-$(CONFIG_SLOB) += slob.o
 obj-$(CONFIG_SLAB) += slab.o
+obj-$(CONFIG_SLUB) += slub.o
 obj-$(CONFIG_MEMORY_HOTPLUG) += memory_hotplug.o
 obj-$(CONFIG_FS_XIP) += filemap_xip.o
 obj-$(CONFIG_MIGRATION) += migrate.o
mm/slub.c new file +2293 -0

2,293 lines not on this page · raw message

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#2 lore raw

PATCH 2/3 Enable poisoning for RCU and constructors

In reply to Christoph Lameter

Enable poisoning / redzoning for slabs with constructors or SLAB_DEWSTROY_BY_RCU

We cannot poison the object itself but we can poison padding spaces and do the redzoning. For that we introduce another flag controlling object poisoning.

mm/slub.c +11 -12
Index: linux-2.6.21-rc3/mm/slub.c
===================================================================
@@ -80,6 +80,9 @@
 #define ARCH_SLAB_MINALIGN sizeof(void *)
 #endif
 
+/* Internal SLUB flags */
+#define __OBJECT_POISON 0x80000000	/* Poison object */
+
 static int kmem_size = sizeof(struct kmem_cache);
 
 #ifdef CONFIG_SMP
@@ -247,8 +250,8 @@
 	if (s->objects == 1)
 		return;
 
-	if (s->flags & SLAB_POISON) {
-		memset(p, POISON_FREE, s->objsize -1);
+	if (s->flags & __OBJECT_POISON) {
+		memset(p, POISON_FREE, s->objsize - 1);
 		p[s->objsize -1] = POISON_END;
 	}
 
@@ -388,7 +391,8 @@
 			object_err(s, page, p, "Alignment padding check fails");
 
 	if (s->flags & SLAB_POISON) {
-		if (!active && (!check_bytes(p, POISON_FREE, s->objsize - 1) ||
+		if (!active && (s->flags & __OBJECT_POISON)
+				&& (!check_bytes(p, POISON_FREE, s->objsize - 1) ||
 				p[s->objsize -1] != POISON_END)) {
 			object_err(s, page, p, "Poison");
 			return 0;
@@ -1371,14 +1375,9 @@
 		strncmp(slub_debug_slabs, name, strlen(slub_debug_slabs)) == 0))
 			flags |= slub_debug;
 
-	if ((flags & SLAB_POISON) &&((flags & SLAB_DESTROY_BY_RCU) ||
-			ctor || dtor)) {
-		if (!(slub_debug & SLAB_POISON))
-			printk(KERN_WARNING "SLUB %s: Clearing SLAB_POISON "
-				"because de/constructor exists.\n",
-				s->name);
-		flags &= ~SLAB_POISON;
-	}
+	if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
+			!ctor && !dtor)
+		flags |= __OBJECT_POISON;
 
 	tentative_size = ALIGN(size, calculate_alignment(align, flags));
 
@@ -1389,7 +1388,7 @@
 	 */
 	if (size == PAGE_SIZE)
 		flags &= ~(SLAB_RED_ZONE| SLAB_DEBUG_FREE | \
-			SLAB_STORE_USER | SLAB_POISON);
+			SLAB_STORE_USER | SLAB_POISON | __OBJECT_POISON);
 
 	s->name = name;
 	s->ctor = ctor;

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw

PATCH 3/3 Configurable slub_max_order

In reply to Christoph Lameter

Add slub_max_order

Avoid slabs getting to large. Do no longer enforce slub_min_objects if the slab gets bigger than slub_max_order.

I am not sure if we really want this. Maybe we should make the selection of the base page size depending on page allocator defrag behavior? I.e. try to restrict allocations to order 0 and order 2 so that can limit fragmentation?

mm/slub.c +15 -1
Index: linux-2.6.21-rc3/mm/slub.c
===================================================================
@@ -1211,6 +1211,7 @@ static __always_inline struct page *get_
  * take the list_lock.
  */
 static int slub_min_order = 0;
+static int slub_max_order = 4;
 
 /*
  * Minimum number of objects per slab. This is necessary in order to
@@ -1249,7 +1250,11 @@ static int calculate_order(int size)
 			order < MAX_ORDER; order++) {
 		unsigned long slab_size = PAGE_SIZE << order;
 
-		if (slab_size < slub_min_objects * size)
+		if (slub_max_order > order &&
+				slab_size < slub_min_objects * size)
+			continue;
+
+		if (slab_size < size)
 			continue;
 
 		rem = slab_size % size;
@@ -1637,6 +1642,15 @@ static int __init setup_slub_min_order(c
 
 __setup("slub_min_order=", setup_slub_min_order);
 
+static int __init setup_slub_max_order(char *str)
+{
+	get_option (&str, &slub_max_order);
+
+	return 1;
+}
+
+__setup("slub_max_order=", setup_slub_max_order);
+
 static int __init setup_slub_min_objects(char *str)
 {
 	get_option (&str, &slub_min_objects);

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#4 lore raw
In reply to Christoph Lameter

Is this safe to think about applying yet?

We lost the leak detector feature.

It might be nice to create synonyms for PageActive, PageReferenced and
PageError, to make things clearer in the slub core. At the expense of
making things less clear globally. Am unsure.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#5 lore raw
Quoted 1 line · Andrew Morton

Is this safe to think about applying yet?

Its safe. By default kernels will be build with SLAB. SLUB becomes only a selectable alternative. It should not become the primary slab until we know that its really superior overall and have thoroughly tested it in a variety of workloads.

Quoted 1 line

We lost the leak detector feature.

There will be numerous small things that will have to be addressed. There is also some minor work to be done for tracking callers better.

Quoted 3 lines

It might be nice to create synonyms for PageActive, PageReferenced and
PageError, to make things clearer in the slub core. At the expense of
making things less clear globally. Am unsure.

I have been back and forth on doing that. There are somewhat similar in what they mean for SLUB. But creating synonyms may be confusing to those checking how page flags are being used.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>