Skip to content

Commit afc0ced

Browse files
Nick PigginLinus Torvalds
authored andcommitted
slob: implement RCU freeing
The SLOB allocator should implement SLAB_DESTROY_BY_RCU correctly, because even on UP, RCU freeing semantics are not equivalent to simply freeing immediately. This also allows SLOB to be used on SMP. Signed-off-by: Nick Piggin <npiggin@suse.de> Acked-by: Matt Mackall <mpm@selenic.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent b2cd641 commit afc0ced

2 files changed

Lines changed: 47 additions & 12 deletions

File tree

‎init/Kconfig‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -577,14 +577,11 @@ config SLUB
577577
and has enhanced diagnostics.
578578

579579
config SLOB
580-
#
581-
# SLOB does not support SMP because SLAB_DESTROY_BY_RCU is unsupported
582-
#
583-
depends on EMBEDDED && !SMP && !SPARSEMEM
580+
depends on EMBEDDED && !SPARSEMEM
584581
bool "SLOB (Simple Allocator)"
585582
help
586583
SLOB replaces the SLAB allocator with a drastically simpler
587-
allocator. SLOB is more space efficient that SLAB but does not
584+
allocator. SLOB is more space efficient than SLAB but does not
588585
scale well (single lock for all operations) and is also highly
589586
susceptible to fragmentation. SLUB can accomplish a higher object
590587
density. It is usually better to use SLUB instead of SLOB.

‎mm/slob.c‎

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <linux/init.h>
3636
#include <linux/module.h>
3737
#include <linux/timer.h>
38+
#include <linux/rcupdate.h>
3839

3940
struct slob_block {
4041
int units;
@@ -53,6 +54,16 @@ struct bigblock {
5354
};
5455
typedef struct bigblock bigblock_t;
5556

57+
/*
58+
* struct slob_rcu is inserted at the tail of allocated slob blocks, which
59+
* were created with a SLAB_DESTROY_BY_RCU slab. slob_rcu is used to free
60+
* the block using call_rcu.
61+
*/
62+
struct slob_rcu {
63+
struct rcu_head head;
64+
int size;
65+
};
66+
5667
static slob_t arena = { .next = &arena, .units = 1 };
5768
static slob_t *slobfree = &arena;
5869
static bigblock_t *bigblocks;
@@ -266,6 +277,7 @@ size_t ksize(const void *block)
266277

267278
struct kmem_cache {
268279
unsigned int size, align;
280+
unsigned long flags;
269281
const char *name;
270282
void (*ctor)(void *, struct kmem_cache *, unsigned long);
271283
void (*dtor)(void *, struct kmem_cache *, unsigned long);
@@ -283,6 +295,12 @@ struct kmem_cache *kmem_cache_create(const char *name, size_t size,
283295
if (c) {
284296
c->name = name;
285297
c->size = size;
298+
if (flags & SLAB_DESTROY_BY_RCU) {
299+
BUG_ON(dtor);
300+
/* leave room for rcu footer at the end of object */
301+
c->size += sizeof(struct slob_rcu);
302+
}
303+
c->flags = flags;
286304
c->ctor = ctor;
287305
c->dtor = dtor;
288306
/* ignore alignment unless it's forced */
@@ -328,15 +346,35 @@ void *kmem_cache_zalloc(struct kmem_cache *c, gfp_t flags)
328346
}
329347
EXPORT_SYMBOL(kmem_cache_zalloc);
330348

331-
void kmem_cache_free(struct kmem_cache *c, void *b)
349+
static void __kmem_cache_free(void *b, int size)
332350
{
333-
if (c->dtor)
334-
c->dtor(b, c, 0);
335-
336-
if (c->size < PAGE_SIZE)
337-
slob_free(b, c->size);
351+
if (size < PAGE_SIZE)
352+
slob_free(b, size);
338353
else
339-
free_pages((unsigned long)b, get_order(c->size));
354+
free_pages((unsigned long)b, get_order(size));
355+
}
356+
357+
static void kmem_rcu_free(struct rcu_head *head)
358+
{
359+
struct slob_rcu *slob_rcu = (struct slob_rcu *)head;
360+
void *b = (void *)slob_rcu - (slob_rcu->size - sizeof(struct slob_rcu));
361+
362+
__kmem_cache_free(b, slob_rcu->size);
363+
}
364+
365+
void kmem_cache_free(struct kmem_cache *c, void *b)
366+
{
367+
if (unlikely(c->flags & SLAB_DESTROY_BY_RCU)) {
368+
struct slob_rcu *slob_rcu;
369+
slob_rcu = b + (c->size - sizeof(struct slob_rcu));
370+
INIT_RCU_HEAD(&slob_rcu->head);
371+
slob_rcu->size = c->size;
372+
call_rcu(&slob_rcu->head, kmem_rcu_free);
373+
} else {
374+
if (c->dtor)
375+
c->dtor(b, c, 0);
376+
__kmem_cache_free(b, c->size);
377+
}
340378
}
341379
EXPORT_SYMBOL(kmem_cache_free);
342380

0 commit comments

Comments
 (0)