Skip to content

Commit b789ef5

Browse files
Christoph Lameterpenberg
authored andcommitted
slub: Add cmpxchg_double_slab()
Add a function that operates on the second doubleword in the page struct and manipulates the object counters, the freelist and the frozen attribute. Signed-off-by: Christoph Lameter <cl@linux.com> Signed-off-by: Pekka Enberg <penberg@kernel.org>
1 parent fc9bb8c commit b789ef5

2 files changed

Lines changed: 61 additions & 5 deletions

File tree

‎include/linux/slub_def.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ enum stat_item {
3333
DEACTIVATE_REMOTE_FREES,/* Slab contained remotely freed objects */
3434
ORDER_FALLBACK, /* Number of times fallback was necessary */
3535
CMPXCHG_DOUBLE_CPU_FAIL,/* Failure of this_cpu_cmpxchg_double */
36+
CMPXCHG_DOUBLE_FAIL, /* Number of times that cmpxchg double did not match */
3637
NR_SLUB_STAT_ITEMS };
3738

3839
struct kmem_cache_cpu {

‎mm/slub.c‎

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ static inline int kmem_cache_debug(struct kmem_cache *s)
131131
/* Enable to test recovery from slab corruption on boot */
132132
#undef SLUB_RESILIENCY_TEST
133133

134+
/* Enable to log cmpxchg failures */
135+
#undef SLUB_DEBUG_CMPXCHG
136+
134137
/*
135138
* Mininum number of partial slabs. These will be left on the partial
136139
* lists even if they are empty. kmem_cache_shrink may reclaim them.
@@ -170,6 +173,7 @@ static inline int kmem_cache_debug(struct kmem_cache *s)
170173

171174
/* Internal SLUB flags */
172175
#define __OBJECT_POISON 0x80000000UL /* Poison object */
176+
#define __CMPXCHG_DOUBLE 0x40000000UL /* Use cmpxchg_double */
173177

174178
static int kmem_size = sizeof(struct kmem_cache);
175179

@@ -338,6 +342,37 @@ static inline int oo_objects(struct kmem_cache_order_objects x)
338342
return x.x & OO_MASK;
339343
}
340344

345+
static inline bool cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
346+
void *freelist_old, unsigned long counters_old,
347+
void *freelist_new, unsigned long counters_new,
348+
const char *n)
349+
{
350+
#ifdef CONFIG_CMPXCHG_DOUBLE
351+
if (s->flags & __CMPXCHG_DOUBLE) {
352+
if (cmpxchg_double(&page->freelist,
353+
freelist_old, counters_old,
354+
freelist_new, counters_new))
355+
return 1;
356+
} else
357+
#endif
358+
{
359+
if (page->freelist == freelist_old && page->counters == counters_old) {
360+
page->freelist = freelist_new;
361+
page->counters = counters_new;
362+
return 1;
363+
}
364+
}
365+
366+
cpu_relax();
367+
stat(s, CMPXCHG_DOUBLE_FAIL);
368+
369+
#ifdef SLUB_DEBUG_CMPXCHG
370+
printk(KERN_INFO "%s %s: cmpxchg double redo ", n, s->name);
371+
#endif
372+
373+
return 0;
374+
}
375+
341376
#ifdef CONFIG_SLUB_DEBUG
342377
/*
343378
* Determine a map of object in use on a page.
@@ -2596,6 +2631,12 @@ static int kmem_cache_open(struct kmem_cache *s,
25962631
}
25972632
}
25982633

2634+
#ifdef CONFIG_CMPXCHG_DOUBLE
2635+
if (system_has_cmpxchg_double() && (s->flags & SLAB_DEBUG_FLAGS) == 0)
2636+
/* Enable fast mode */
2637+
s->flags |= __CMPXCHG_DOUBLE;
2638+
#endif
2639+
25992640
/*
26002641
* The larger the object size is, the more pages we want on the partial
26012642
* list to avoid pounding the page allocator excessively.
@@ -4248,8 +4289,10 @@ static ssize_t sanity_checks_store(struct kmem_cache *s,
42484289
const char *buf, size_t length)
42494290
{
42504291
s->flags &= ~SLAB_DEBUG_FREE;
4251-
if (buf[0] == '1')
4292+
if (buf[0] == '1') {
4293+
s->flags &= ~__CMPXCHG_DOUBLE;
42524294
s->flags |= SLAB_DEBUG_FREE;
4295+
}
42534296
return length;
42544297
}
42554298
SLAB_ATTR(sanity_checks);
@@ -4263,8 +4306,10 @@ static ssize_t trace_store(struct kmem_cache *s, const char *buf,
42634306
size_t length)
42644307
{
42654308
s->flags &= ~SLAB_TRACE;
4266-
if (buf[0] == '1')
4309+
if (buf[0] == '1') {
4310+
s->flags &= ~__CMPXCHG_DOUBLE;
42674311
s->flags |= SLAB_TRACE;
4312+
}
42684313
return length;
42694314
}
42704315
SLAB_ATTR(trace);
@@ -4281,8 +4326,10 @@ static ssize_t red_zone_store(struct kmem_cache *s,
42814326
return -EBUSY;
42824327

42834328
s->flags &= ~SLAB_RED_ZONE;
4284-
if (buf[0] == '1')
4329+
if (buf[0] == '1') {
4330+
s->flags &= ~__CMPXCHG_DOUBLE;
42854331
s->flags |= SLAB_RED_ZONE;
4332+
}
42864333
calculate_sizes(s, -1);
42874334
return length;
42884335
}
@@ -4300,8 +4347,10 @@ static ssize_t poison_store(struct kmem_cache *s,
43004347
return -EBUSY;
43014348

43024349
s->flags &= ~SLAB_POISON;
4303-
if (buf[0] == '1')
4350+
if (buf[0] == '1') {
4351+
s->flags &= ~__CMPXCHG_DOUBLE;
43044352
s->flags |= SLAB_POISON;
4353+
}
43054354
calculate_sizes(s, -1);
43064355
return length;
43074356
}
@@ -4319,8 +4368,10 @@ static ssize_t store_user_store(struct kmem_cache *s,
43194368
return -EBUSY;
43204369

43214370
s->flags &= ~SLAB_STORE_USER;
4322-
if (buf[0] == '1')
4371+
if (buf[0] == '1') {
4372+
s->flags &= ~__CMPXCHG_DOUBLE;
43234373
s->flags |= SLAB_STORE_USER;
4374+
}
43244375
calculate_sizes(s, -1);
43254376
return length;
43264377
}
@@ -4493,6 +4544,8 @@ STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
44934544
STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
44944545
STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
44954546
STAT_ATTR(ORDER_FALLBACK, order_fallback);
4547+
STAT_ATTR(CMPXCHG_DOUBLE_CPU_FAIL, cmpxchg_double_cpu_fail);
4548+
STAT_ATTR(CMPXCHG_DOUBLE_FAIL, cmpxchg_double_fail);
44964549
#endif
44974550

44984551
static struct attribute *slab_attrs[] = {
@@ -4550,6 +4603,8 @@ static struct attribute *slab_attrs[] = {
45504603
&deactivate_to_tail_attr.attr,
45514604
&deactivate_remote_frees_attr.attr,
45524605
&order_fallback_attr.attr,
4606+
&cmpxchg_double_fail_attr.attr,
4607+
&cmpxchg_double_cpu_fail_attr.attr,
45534608
#endif
45544609
#ifdef CONFIG_FAILSLAB
45554610
&failslab_attr.attr,

0 commit comments

Comments
 (0)