Skip to content

Commit c59def9

Browse files
Christoph LameterLinus Torvalds
authored andcommitted
Slab allocators: Drop support for destructors
There is no user of destructors left. There is no reason why we should keep checking for destructors calls in the slab allocators. The RFC for this patch was discussed at http://marc.info/?l=linux-kernel&m=117882364330705&w=2 Destructors were mainly used for list management which required them to take a spinlock. Taking a spinlock in a destructor is a bit risky since the slab allocators may run the destructors anytime they decide a slab is no longer needed. Patch drops destructor support. Any attempt to use a destructor will BUG(). Acked-by: Pekka Enberg <penberg@cs.helsinki.fi> Acked-by: Paul Mundt <lethal@linux-sh.org> Signed-off-by: Christoph Lameter <clameter@sgi.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent afc0ced commit c59def9

4 files changed

Lines changed: 16 additions & 62 deletions

File tree

‎include/linux/slub_def.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ struct kmem_cache {
4040
int objects; /* Number of objects in slab */
4141
int refcount; /* Refcount for slab cache destroy */
4242
void (*ctor)(void *, struct kmem_cache *, unsigned long);
43-
void (*dtor)(void *, struct kmem_cache *, unsigned long);
4443
int inuse; /* Offset to metadata */
4544
int align; /* Alignment */
4645
const char *name; /* Name (only for display!) */

‎mm/slab.c‎

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,6 @@ struct kmem_cache {
409409
/* constructor func */
410410
void (*ctor) (void *, struct kmem_cache *, unsigned long);
411411

412-
/* de-constructor func */
413-
void (*dtor) (void *, struct kmem_cache *, unsigned long);
414-
415412
/* 5) cache creation/removal */
416413
const char *name;
417414
struct list_head next;
@@ -1911,20 +1908,11 @@ static void slab_destroy_objs(struct kmem_cache *cachep, struct slab *slabp)
19111908
slab_error(cachep, "end of a freed object "
19121909
"was overwritten");
19131910
}
1914-
if (cachep->dtor && !(cachep->flags & SLAB_POISON))
1915-
(cachep->dtor) (objp + obj_offset(cachep), cachep, 0);
19161911
}
19171912
}
19181913
#else
19191914
static void slab_destroy_objs(struct kmem_cache *cachep, struct slab *slabp)
19201915
{
1921-
if (cachep->dtor) {
1922-
int i;
1923-
for (i = 0; i < cachep->num; i++) {
1924-
void *objp = index_to_obj(cachep, slabp, i);
1925-
(cachep->dtor) (objp, cachep, 0);
1926-
}
1927-
}
19281916
}
19291917
#endif
19301918

@@ -2124,7 +2112,7 @@ static int setup_cpu_cache(struct kmem_cache *cachep)
21242112
* @align: The required alignment for the objects.
21252113
* @flags: SLAB flags
21262114
* @ctor: A constructor for the objects.
2127-
* @dtor: A destructor for the objects.
2115+
* @dtor: A destructor for the objects (not implemented anymore).
21282116
*
21292117
* Returns a ptr to the cache on success, NULL on failure.
21302118
* Cannot be called within a int, but can be interrupted.
@@ -2159,7 +2147,7 @@ kmem_cache_create (const char *name, size_t size, size_t align,
21592147
* Sanity checks... these are all serious usage bugs.
21602148
*/
21612149
if (!name || in_interrupt() || (size < BYTES_PER_WORD) ||
2162-
(size > (1 << MAX_OBJ_ORDER) * PAGE_SIZE) || (dtor && !ctor)) {
2150+
(size > (1 << MAX_OBJ_ORDER) * PAGE_SIZE) || dtor) {
21632151
printk(KERN_ERR "%s: Early error in slab %s\n", __FUNCTION__,
21642152
name);
21652153
BUG();
@@ -2213,9 +2201,6 @@ kmem_cache_create (const char *name, size_t size, size_t align,
22132201
if (flags & SLAB_DESTROY_BY_RCU)
22142202
BUG_ON(flags & SLAB_POISON);
22152203
#endif
2216-
if (flags & SLAB_DESTROY_BY_RCU)
2217-
BUG_ON(dtor);
2218-
22192204
/*
22202205
* Always checks flags, a caller might be expecting debug support which
22212206
* isn't available.
@@ -2370,7 +2355,6 @@ kmem_cache_create (const char *name, size_t size, size_t align,
23702355
BUG_ON(!cachep->slabp_cache);
23712356
}
23722357
cachep->ctor = ctor;
2373-
cachep->dtor = dtor;
23742358
cachep->name = name;
23752359

23762360
if (setup_cpu_cache(cachep)) {
@@ -2835,7 +2819,6 @@ static int cache_grow(struct kmem_cache *cachep,
28352819
* Perform extra freeing checks:
28362820
* - detect bad pointers.
28372821
* - POISON/RED_ZONE checking
2838-
* - destructor calls, for caches with POISON+dtor
28392822
*/
28402823
static void kfree_debugcheck(const void *objp)
28412824
{
@@ -2894,12 +2877,6 @@ static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
28942877
BUG_ON(objnr >= cachep->num);
28952878
BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
28962879

2897-
if (cachep->flags & SLAB_POISON && cachep->dtor) {
2898-
/* we want to cache poison the object,
2899-
* call the destruction callback
2900-
*/
2901-
cachep->dtor(objp + obj_offset(cachep), cachep, 0);
2902-
}
29032880
#ifdef CONFIG_DEBUG_SLAB_LEAK
29042881
slab_bufctl(slabp)[objnr] = BUFCTL_FREE;
29052882
#endif

‎mm/slob.c‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,6 @@ struct kmem_cache {
280280
unsigned long flags;
281281
const char *name;
282282
void (*ctor)(void *, struct kmem_cache *, unsigned long);
283-
void (*dtor)(void *, struct kmem_cache *, unsigned long);
284283
};
285284

286285
struct kmem_cache *kmem_cache_create(const char *name, size_t size,
@@ -296,13 +295,11 @@ struct kmem_cache *kmem_cache_create(const char *name, size_t size,
296295
c->name = name;
297296
c->size = size;
298297
if (flags & SLAB_DESTROY_BY_RCU) {
299-
BUG_ON(dtor);
300298
/* leave room for rcu footer at the end of object */
301299
c->size += sizeof(struct slob_rcu);
302300
}
303301
c->flags = flags;
304302
c->ctor = ctor;
305-
c->dtor = dtor;
306303
/* ignore alignment unless it's forced */
307304
c->align = (flags & SLAB_HWCACHE_ALIGN) ? SLOB_ALIGN : 0;
308305
if (c->align < align)
@@ -371,8 +368,6 @@ void kmem_cache_free(struct kmem_cache *c, void *b)
371368
slob_rcu->size = c->size;
372369
call_rcu(&slob_rcu->head, kmem_rcu_free);
373370
} else {
374-
if (c->dtor)
375-
c->dtor(b, c, 0);
376371
__kmem_cache_free(b, c->size);
377372
}
378373
}

‎mm/slub.c‎

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -891,13 +891,13 @@ static void kmem_cache_open_debug_check(struct kmem_cache *s)
891891
* On 32 bit platforms the limit is 256k. On 64bit platforms
892892
* the limit is 512k.
893893
*
894-
* Debugging or ctor/dtors may create a need to move the free
894+
* Debugging or ctor may create a need to move the free
895895
* pointer. Fail if this happens.
896896
*/
897897
if (s->size >= 65535 * sizeof(void *)) {
898898
BUG_ON(s->flags & (SLAB_RED_ZONE | SLAB_POISON |
899899
SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
900-
BUG_ON(s->ctor || s->dtor);
900+
BUG_ON(s->ctor);
901901
}
902902
else
903903
/*
@@ -1030,15 +1030,12 @@ static void __free_slab(struct kmem_cache *s, struct page *page)
10301030
{
10311031
int pages = 1 << s->order;
10321032

1033-
if (unlikely(SlabDebug(page) || s->dtor)) {
1033+
if (unlikely(SlabDebug(page))) {
10341034
void *p;
10351035

10361036
slab_pad_check(s, page);
1037-
for_each_object(p, s, page_address(page)) {
1038-
if (s->dtor)
1039-
s->dtor(p, s, 0);
1037+
for_each_object(p, s, page_address(page))
10401038
check_object(s, page, p, 0);
1041-
}
10421039
}
10431040

10441041
mod_zone_page_state(page_zone(page),
@@ -1871,7 +1868,7 @@ static int calculate_sizes(struct kmem_cache *s)
18711868
* then we should never poison the object itself.
18721869
*/
18731870
if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
1874-
!s->ctor && !s->dtor)
1871+
!s->ctor)
18751872
s->flags |= __OBJECT_POISON;
18761873
else
18771874
s->flags &= ~__OBJECT_POISON;
@@ -1901,7 +1898,7 @@ static int calculate_sizes(struct kmem_cache *s)
19011898

19021899
#ifdef CONFIG_SLUB_DEBUG
19031900
if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
1904-
s->ctor || s->dtor)) {
1901+
s->ctor)) {
19051902
/*
19061903
* Relocate free pointer after the object if it is not
19071904
* permitted to overwrite the first word of the object on
@@ -1970,13 +1967,11 @@ static int calculate_sizes(struct kmem_cache *s)
19701967
static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
19711968
const char *name, size_t size,
19721969
size_t align, unsigned long flags,
1973-
void (*ctor)(void *, struct kmem_cache *, unsigned long),
1974-
void (*dtor)(void *, struct kmem_cache *, unsigned long))
1970+
void (*ctor)(void *, struct kmem_cache *, unsigned long))
19751971
{
19761972
memset(s, 0, kmem_size);
19771973
s->name = name;
19781974
s->ctor = ctor;
1979-
s->dtor = dtor;
19801975
s->objsize = size;
19811976
s->flags = flags;
19821977
s->align = align;
@@ -2161,7 +2156,7 @@ static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
21612156

21622157
down_write(&slub_lock);
21632158
if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
2164-
flags, NULL, NULL))
2159+
flags, NULL))
21652160
goto panic;
21662161

21672162
list_add(&s->list, &slab_caches);
@@ -2463,23 +2458,22 @@ static int slab_unmergeable(struct kmem_cache *s)
24632458
if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
24642459
return 1;
24652460

2466-
if (s->ctor || s->dtor)
2461+
if (s->ctor)
24672462
return 1;
24682463

24692464
return 0;
24702465
}
24712466

24722467
static struct kmem_cache *find_mergeable(size_t size,
24732468
size_t align, unsigned long flags,
2474-
void (*ctor)(void *, struct kmem_cache *, unsigned long),
2475-
void (*dtor)(void *, struct kmem_cache *, unsigned long))
2469+
void (*ctor)(void *, struct kmem_cache *, unsigned long))
24762470
{
24772471
struct list_head *h;
24782472

24792473
if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
24802474
return NULL;
24812475

2482-
if (ctor || dtor)
2476+
if (ctor)
24832477
return NULL;
24842478

24852479
size = ALIGN(size, sizeof(void *));
@@ -2521,8 +2515,9 @@ struct kmem_cache *kmem_cache_create(const char *name, size_t size,
25212515
{
25222516
struct kmem_cache *s;
25232517

2518+
BUG_ON(dtor);
25242519
down_write(&slub_lock);
2525-
s = find_mergeable(size, align, flags, ctor, dtor);
2520+
s = find_mergeable(size, align, flags, ctor);
25262521
if (s) {
25272522
s->refcount++;
25282523
/*
@@ -2536,7 +2531,7 @@ struct kmem_cache *kmem_cache_create(const char *name, size_t size,
25362531
} else {
25372532
s = kmalloc(kmem_size, GFP_KERNEL);
25382533
if (s && kmem_cache_open(s, GFP_KERNEL, name,
2539-
size, align, flags, ctor, dtor)) {
2534+
size, align, flags, ctor)) {
25402535
if (sysfs_slab_add(s)) {
25412536
kfree(s);
25422537
goto err;
@@ -3177,17 +3172,6 @@ static ssize_t ctor_show(struct kmem_cache *s, char *buf)
31773172
}
31783173
SLAB_ATTR_RO(ctor);
31793174

3180-
static ssize_t dtor_show(struct kmem_cache *s, char *buf)
3181-
{
3182-
if (s->dtor) {
3183-
int n = sprint_symbol(buf, (unsigned long)s->dtor);
3184-
3185-
return n + sprintf(buf + n, "\n");
3186-
}
3187-
return 0;
3188-
}
3189-
SLAB_ATTR_RO(dtor);
3190-
31913175
static ssize_t aliases_show(struct kmem_cache *s, char *buf)
31923176
{
31933177
return sprintf(buf, "%d\n", s->refcount - 1);
@@ -3419,7 +3403,6 @@ static struct attribute * slab_attrs[] = {
34193403
&partial_attr.attr,
34203404
&cpu_slabs_attr.attr,
34213405
&ctor_attr.attr,
3422-
&dtor_attr.attr,
34233406
&aliases_attr.attr,
34243407
&align_attr.attr,
34253408
&sanity_checks_attr.attr,

0 commit comments

Comments
 (0)