History

slub: Replace cmpxchg_double()

Peter Zijlstra committed 3 years ago

Commit 6801be41 parent b1fe7f23 files · +137 −67Browse files

Unified · Split

3 files
  1. include/linux/slub_def.h +9 −3
  2. mm/slab.h +46 −7
  3. mm/slub.c +82 −57
include/linux/slub_def.h+9−3
@@ -39,7 +39,8 @@ enum stat_item { · 38 unmodified lines
3939 CPU_PARTIAL_FREE, /* Refill cpu partial on free */
4040 CPU_PARTIAL_NODE, /* Refill cpu partial from node partial */
4141 CPU_PARTIAL_DRAIN, /* Drain cpu partial to node partial */
42− NR_SLUB_STAT_ITEMS };
42+ NR_SLUB_STAT_ITEMS
43+};
4344
4445 #ifndef CONFIG_SLUB_TINY
4546 /*
@@ -47,8 +48,13 @@ enum stat_item { · 1 unmodified line
4748 * with this_cpu_cmpxchg_double() alignment requirements.
4849 */
4950 struct kmem_cache_cpu {
50− void **freelist; /* Pointer to next available object */
51− unsigned long tid; /* Globally unique transaction id */
51+ union {
52+ struct {
53+ void **freelist; /* Pointer to next available object */
54+ unsigned long tid; /* Globally unique transaction id */
55+ };
56+ freelist_aba_t freelist_tid;
57+ };
5258 struct slab *slab; /* The slab from which we are allocating */
5359 #ifdef CONFIG_SLUB_CPU_PARTIAL
5460 struct slab *partial; /* Partially allocated frozen slabs */
mm/slab.h+46−7
@@ -6,6 +6,38 @@ · 5 unmodified lines
66 */
77 void __init kmem_cache_init(void);
88
9+#ifdef CONFIG_64BIT
10+# ifdef system_has_cmpxchg128
11+# define system_has_freelist_aba() system_has_cmpxchg128()
12+# define try_cmpxchg_freelist try_cmpxchg128
13+# endif
14+#define this_cpu_try_cmpxchg_freelist this_cpu_try_cmpxchg128
15+typedef u128 freelist_full_t;
16+#else /* CONFIG_64BIT */
17+# ifdef system_has_cmpxchg64
18+# define system_has_freelist_aba() system_has_cmpxchg64()
19+# define try_cmpxchg_freelist try_cmpxchg64
20+# endif
21+#define this_cpu_try_cmpxchg_freelist this_cpu_try_cmpxchg64
22+typedef u64 freelist_full_t;
23+#endif /* CONFIG_64BIT */
24+
25+#if defined(system_has_freelist_aba) && !defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
26+#undef system_has_freelist_aba
27+#endif
28+
29+/*
30+ * Freelist pointer and counter to cmpxchg together, avoids the typical ABA
31+ * problems with cmpxchg of just a pointer.
32+ */
33+typedef union {
34+ struct {
35+ void *freelist;
36+ unsigned long counter;
37+ };
38+ freelist_full_t full;
39+} freelist_aba_t;
40+
941 /* Reuses the bits in struct page */
1042 struct slab {
1143 unsigned long __page_flags;
@@ -38,14 +70,21 @@ struct slab { · 26 unmodified lines
3870 #endif
3971 };
4072 /* Double-word boundary */
41− void *freelist; /* first free object */
4273 union {
43− unsigned long counters;
4474 struct {
45− unsigned inuse:16;
46− unsigned objects:15;
47− unsigned frozen:1;
75+ void *freelist; /* first free object */
76+ union {
77+ unsigned long counters;
78+ struct {
79+ unsigned inuse:16;
80+ unsigned objects:15;
81+ unsigned frozen:1;
82+ };
83+ };
4884 };
85+#ifdef system_has_freelist_aba
86+ freelist_aba_t freelist_counter;
87+#endif
4988 };
5089 };
5190 struct rcu_head rcu_head;
@@ -72,8 +111,8 @@ SLAB_MATCH(memcg_data, memcg_data); · 20 unmodified lines
72111 #endif
73112 #undef SLAB_MATCH
74113 static_assert(sizeof(struct slab) <= sizeof(struct page));
75−#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && defined(CONFIG_SLUB)
76−static_assert(IS_ALIGNED(offsetof(struct slab, freelist), 2*sizeof(void *)));
114+#if defined(system_has_freelist_aba) && defined(CONFIG_SLUB)
115+static_assert(IS_ALIGNED(offsetof(struct slab, freelist), sizeof(freelist_aba_t)));
77116 #endif
78117
79118 /**
mm/slub.c+82−57
@@ -292,7 +292,12 @@ static inline bool kmem_cache_has_cpu_partial(struct kmem_cache *s) · 291 unmodified lines
292292 /* Poison object */
293293 #define __OBJECT_POISON ((slab_flags_t __force)0x80000000U)
294294 /* Use cmpxchg_double */
295+
296+#ifdef system_has_freelist_aba
295297 #define __CMPXCHG_DOUBLE ((slab_flags_t __force)0x40000000U)
298+#else
299+#define __CMPXCHG_DOUBLE ((slab_flags_t __force)0U)
300+#endif
296301
297302 /*
298303 * Tracking user of a slab.
@@ -512,6 +517,40 @@ static __always_inline void slab_unlock(struct slab *slab) · 213 unmodified lines
512517 __bit_spin_unlock(PG_locked, &page->flags);
513518 }
514519
520+static inline bool
521+__update_freelist_fast(struct slab *slab,
522+ void *freelist_old, unsigned long counters_old,
523+ void *freelist_new, unsigned long counters_new)
524+{
525+#ifdef system_has_freelist_aba
526+ freelist_aba_t old = { .freelist = freelist_old, .counter = counters_old };
527+ freelist_aba_t new = { .freelist = freelist_new, .counter = counters_new };
528+
529+ return try_cmpxchg_freelist(&slab->freelist_counter.full, &old.full, new.full);
530+#else
531+ return false;
532+#endif
533+}
534+
535+static inline bool
536+__update_freelist_slow(struct slab *slab,
537+ void *freelist_old, unsigned long counters_old,
538+ void *freelist_new, unsigned long counters_new)
539+{
540+ bool ret = false;
541+
542+ slab_lock(slab);
543+ if (slab->freelist == freelist_old &&
544+ slab->counters == counters_old) {
545+ slab->freelist = freelist_new;
546+ slab->counters = counters_new;
547+ ret = true;
548+ }
549+ slab_unlock(slab);
550+
551+ return ret;
552+}
553+
515554 /*
516555 * Interrupts must be disabled (for the fallback code to work right), typically
517556 * by an _irqsave() lock variant. On PREEMPT_RT the preempt_disable(), which is
@@ -519,33 +558,25 @@ static __always_inline void slab_unlock(struct slab *slab) · 1 unmodified line
519558 * allocation/ free operation in hardirq context. Therefore nothing can
520559 * interrupt the operation.
521560 */
522−static inline bool __cmpxchg_double_slab(struct kmem_cache *s, struct slab *slab,
561+static inline bool __slab_update_freelist(struct kmem_cache *s, struct slab *slab,
523562 void *freelist_old, unsigned long counters_old,
524563 void *freelist_new, unsigned long counters_new,
525564 const char *n)
526565 {
566+ bool ret;
567+
527568 if (USE_LOCKLESS_FAST_PATH())
528569 lockdep_assert_irqs_disabled();
529−#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
530− defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
570+
531571 if (s->flags & __CMPXCHG_DOUBLE) {
532− if (cmpxchg_double(&slab->freelist, &slab->counters,
533− freelist_old, counters_old,
534− freelist_new, counters_new))
535− return true;
536− } else
537−#endif
538− {
539− slab_lock(slab);
540− if (slab->freelist == freelist_old &&
541− slab->counters == counters_old) {
542− slab->freelist = freelist_new;
543− slab->counters = counters_new;
544− slab_unlock(slab);
545− return true;
546− }
547− slab_unlock(slab);
572+ ret = __update_freelist_fast(slab, freelist_old, counters_old,
573+ freelist_new, counters_new);
574+ } else {
575+ ret = __update_freelist_slow(slab, freelist_old, counters_old,
576+ freelist_new, counters_new);
548577 }
578+ if (likely(ret))
579+ return true;
549580
550581 cpu_relax();
551582 stat(s, CMPXCHG_DOUBLE_FAIL);
@@ -557,36 +588,26 @@ static inline bool __cmpxchg_double_slab(struct kmem_cache *s, struct slab *slab · 5 unmodified lines
557588 return false;
558589 }
559590
560−static inline bool cmpxchg_double_slab(struct kmem_cache *s, struct slab *slab,
591+static inline bool slab_update_freelist(struct kmem_cache *s, struct slab *slab,
561592 void *freelist_old, unsigned long counters_old,
562593 void *freelist_new, unsigned long counters_new,
563594 const char *n)
564595 {
565−#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
566− defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
596+ bool ret;
597+
567598 if (s->flags & __CMPXCHG_DOUBLE) {
568− if (cmpxchg_double(&slab->freelist, &slab->counters,
569− freelist_old, counters_old,
570− freelist_new, counters_new))
571− return true;
572− } else
573−#endif
574− {
599+ ret = __update_freelist_fast(slab, freelist_old, counters_old,
600+ freelist_new, counters_new);
601+ } else {
575602 unsigned long flags;
576603
577604 local_irq_save(flags);
578− slab_lock(slab);
579− if (slab->freelist == freelist_old &&
580− slab->counters == counters_old) {
581− slab->freelist = freelist_new;
582− slab->counters = counters_new;
583− slab_unlock(slab);
584− local_irq_restore(flags);
585− return true;
586− }
587− slab_unlock(slab);
605+ ret = __update_freelist_slow(slab, freelist_old, counters_old,
606+ freelist_new, counters_new);
588607 local_irq_restore(flags);
589608 }
609+ if (likely(ret))
610+ return true;
590611
591612 cpu_relax();
592613 stat(s, CMPXCHG_DOUBLE_FAIL);
@@ -2228,7 +2249,7 @@ static inline void *acquire_slab(struct kmem_cache *s, · 1635 unmodified lines
22282249 VM_BUG_ON(new.frozen);
22292250 new.frozen = 1;
22302251
2231− if (!__cmpxchg_double_slab(s, slab,
2252+ if (!__slab_update_freelist(s, slab,
22322253 freelist, counters,
22332254 new.freelist, new.counters,
22342255 "acquire_slab"))
@@ -2554,7 +2575,7 @@ redo: · 319 unmodified lines
25542575 }
25552576
25562577
2557− if (!cmpxchg_double_slab(s, slab,
2578+ if (!slab_update_freelist(s, slab,
25582579 old.freelist, old.counters,
25592580 new.freelist, new.counters,
25602581 "unfreezing slab")) {
@@ -2611,7 +2632,7 @@ static void __unfreeze_partials(struct kmem_cache *s, struct slab *partial_slab) · 50 unmodified lines
26112632
26122633 new.frozen = 0;
26132634
2614− } while (!__cmpxchg_double_slab(s, slab,
2635+ } while (!__slab_update_freelist(s, slab,
26152636 old.freelist, old.counters,
26162637 new.freelist, new.counters,
26172638 "unfreezing slab"));
@@ -3008,6 +3029,18 @@ static inline bool pfmemalloc_match(struct slab *slab, gfp_t gfpflags) · 390 unmodified lines
30083029 }
30093030
30103031 #ifndef CONFIG_SLUB_TINY
3032+static inline bool
3033+__update_cpu_freelist_fast(struct kmem_cache *s,
3034+ void *freelist_old, void *freelist_new,
3035+ unsigned long tid)
3036+{
3037+ freelist_aba_t old = { .freelist = freelist_old, .counter = tid };
3038+ freelist_aba_t new = { .freelist = freelist_new, .counter = next_tid(tid) };
3039+
3040+ return this_cpu_try_cmpxchg_freelist(s->cpu_slab->freelist_tid.full,
3041+ &old.full, new.full);
3042+}
3043+
30113044 /*
30123045 * Check the slab->freelist and either transfer the freelist to the
30133046 * per cpu freelist or deactivate the slab.
@@ -3034,7 +3067,7 @@ static inline void *get_freelist(struct kmem_cache *s, struct slab *slab) · 20 unmodified lines
30343067 new.inuse = slab->objects;
30353068 new.frozen = freelist != NULL;
30363069
3037− } while (!__cmpxchg_double_slab(s, slab,
3070+ } while (!__slab_update_freelist(s, slab,
30383071 freelist, counters,
30393072 NULL, new.counters,
30403073 "get_freelist"));
@@ -3359,11 +3392,7 @@ redo: · 318 unmodified lines
33593392 * against code executing on this cpu *not* from access by
33603393 * other cpus.
33613394 */
3362− if (unlikely(!this_cpu_cmpxchg_double(
3363− s->cpu_slab->freelist, s->cpu_slab->tid,
3364− object, tid,
3365− next_object, next_tid(tid)))) {
3366−
3395+ if (unlikely(!__update_cpu_freelist_fast(s, object, next_object, tid))) {
33673396 note_cmpxchg_failure("slab_alloc", s, tid);
33683397 goto redo;
33693398 }
@@ -3631,7 +3660,7 @@ static void __slab_free(struct kmem_cache *s, struct slab *slab, · 261 unmodified lines
36313660 }
36323661 }
36333662
3634− } while (!cmpxchg_double_slab(s, slab,
3663+ } while (!slab_update_freelist(s, slab,
36353664 prior, counters,
36363665 head, new.counters,
36373666 "__slab_free"));
@@ -3736,11 +3765,7 @@ redo: · 98 unmodified lines
37363765
37373766 set_freepointer(s, tail_obj, freelist);
37383767
3739− if (unlikely(!this_cpu_cmpxchg_double(
3740− s->cpu_slab->freelist, s->cpu_slab->tid,
3741− freelist, tid,
3742− head, next_tid(tid)))) {
3743−
3768+ if (unlikely(!__update_cpu_freelist_fast(s, freelist, head, tid))) {
37443769 note_cmpxchg_failure("slab_free", s, tid);
37453770 goto redo;
37463771 }
@@ -4505,11 +4530,11 @@ static int kmem_cache_open(struct kmem_cache *s, slab_flags_t flags) · 758 unmodified lines
45054530 }
45064531 }
45074532
4508−#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
4509− defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
4510− if (system_has_cmpxchg_double() && (s->flags & SLAB_NO_CMPXCHG) == 0)
4533+#ifdef system_has_freelist_aba
4534+ if (system_has_freelist_aba() && !(s->flags & SLAB_NO_CMPXCHG)) {
45114535 /* Enable fast mode */
45124536 s->flags |= __CMPXCHG_DOUBLE;
4537+ }
45134538 #endif
45144539
45154540 /*

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t