All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible
@ 2021-07-29 13:20 Vlastimil Babka
  2021-07-29 13:20 ` [PATCH v3 01/35] mm, slub: don't call flush_all() from slab_debug_trace_open() Vlastimil Babka
                   ` (37 more replies)
  0 siblings, 38 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:20 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

Changes since v2 [5]:
* Rebase to 5.14-rc3
* A number of fixes to the RT parts, big thanks to Mike Galbraith for testing
  and debugging!
  * The largest fix is to protect kmem_cache_cpu->partial by local_lock instead
    of cmpxchg tricks, which are insufficient on RT. To avoid divergence
    between RT and !RT, just do it everywhere. Affected mainly patch 25 and a
    new patch 33. This also addresses a theoretical race raised earlier by Jann
    Horn.
* Smaller fixes reported by Sebastian Andrzej Siewior and Cyrill Gorcunov

Changes since RFC v1 [1]:
* Addressed feedback from Christoph and Mel, added their acks.
* Finished RT conversion, adopting 2 patches from the RT tree.
* The local_lock conversion has to sacrifice lockless fathpaths on PREEMPT_RT
* Added some more cleanup patches to the front.

This series was initially inspired by Mel's pcplist local_lock rewrite, and
also interest to better understand SLUB's locking and the new primitives and RT
variants and implications. It should make SLUB more preemption-friendly,
especially for RT, hopefully without noticeable regressions, as the fast paths
are not affected.

Series is based on 5.14-rc3 and also available as a git branch:
https://git.kernel.org/pub/scm/linux/kernel/git/vbabka/linux.git/log/?h=slub-local-lock-v3r1

The series should now be sufficiently tested in both RT and !RT configs, mainly
thanks to Mike. The RFC/v1 version also got basic performance screening by
Mel that didn't show major regressions. Mike's testing with hackbench of v2 on
!RT reported negligible differences [6]:

virgin(ish) tip
5.13.0.g60ab3ed-tip
          7,320.67 msec task-clock                #    7.792 CPUs utilized            ( +-  0.31% )
           221,215      context-switches          #    0.030 M/sec                    ( +-  3.97% )
            16,234      cpu-migrations            #    0.002 M/sec                    ( +-  4.07% )
            13,233      page-faults               #    0.002 M/sec                    ( +-  0.91% )
    27,592,205,252      cycles                    #    3.769 GHz                      ( +-  0.32% )
     8,309,495,040      instructions              #    0.30  insn per cycle           ( +-  0.37% )
     1,555,210,607      branches                  #  212.441 M/sec                    ( +-  0.42% )
         5,484,209      branch-misses             #    0.35% of all branches          ( +-  2.13% )

           0.93949 +- 0.00423 seconds time elapsed  ( +-  0.45% )
           0.94608 +- 0.00384 seconds time elapsed  ( +-  0.41% ) (repeat)
           0.94422 +- 0.00410 seconds time elapsed  ( +-  0.43% )

5.13.0.g60ab3ed-tip +slub-local-lock-v2r3
          7,343.57 msec task-clock                #    7.776 CPUs utilized            ( +-  0.44% )
           223,044      context-switches          #    0.030 M/sec                    ( +-  3.02% )
            16,057      cpu-migrations            #    0.002 M/sec                    ( +-  4.03% )
            13,164      page-faults               #    0.002 M/sec                    ( +-  0.97% )
    27,684,906,017      cycles                    #    3.770 GHz                      ( +-  0.45% )
     8,323,273,871      instructions              #    0.30  insn per cycle           ( +-  0.28% )
     1,556,106,680      branches                  #  211.901 M/sec                    ( +-  0.31% )
         5,463,468      branch-misses             #    0.35% of all branches          ( +-  1.33% )

           0.94440 +- 0.00352 seconds time elapsed  ( +-  0.37% )
           0.94830 +- 0.00228 seconds time elapsed  ( +-  0.24% ) (repeat)
           0.93813 +- 0.00440 seconds time elapsed  ( +-  0.47% ) (repeat)

RT configs showed some throughput regressions, but that's expected tradeoff for
the preemption improvements through the RT mutex. It didn't prevent the v2 to
be incorporated to the 5.13 RT tree [7], leading to testing exposure and
bugfixes.

Before the series, SLUB is lockless in both allocation and free fast paths, but
elsewhere, it's disabling irqs for considerable periods of time - especially in
allocation slowpath and the bulk allocation, where IRQs are re-enabled only
when a new page from the page allocator is needed, and the context allows
blocking. The irq disabled sections can then include deactivate_slab() which
walks a full freelist and frees the slab back to page allocator or
unfreeze_partials() going through a list of percpu partial slabs. The RT tree
currently has some patches mitigating these, but we can do much better in
mainline too.

Patches 1-6 are straightforward improvements or cleanups that could exist
outside of this series too, but are prerequsities.

Patches 7-10 are also preparatory code changes without functional changes, but
not so useful without the rest of the series.

Patch 11 simplifies the fast paths on systems with preemption, based on
(hopefully correct) observation that the current loops to verify tid are
unnecessary.

Patches 12-21 focus on reducing irq disabled scope in the allocation slowpath.

Patch 12 moves disabling of irqs into ___slab_alloc() from its callers, which
are the allocation slowpath, and bulk allocation. Instead these callers only
disable preemption to stabilize the cpu. The following patches then gradually
reduce the scope of disabled irqs in ___slab_alloc() and the functions called
from there. As of patch 15, the re-enabling of irqs based on gfp flags before
calling the page allocator is removed from allocate_slab(). As of patch 18,
it's possible to reach the page allocator (in case of existing slabs depleted)
without disabling and re-enabling irqs a single time.

Pathces 22-27 reduce the scope of disabled irqs in functions related to
unfreezing percpu partial slab.

Patch 28 is preparatory. Patch 29 is adopted from the RT tree and converts the
flushing of percpu slabs on all cpus from using IPI to workqueue, so that the
processing isn't happening with irqs disabled in the IPI handler. The flushing
is not performance critical so it should be acceptable.

Patch 30 also comes from RT tree and makes object_map_lock RT compatible.

Patches 31-32 make slab_lock irq-safe on RT where we cannot rely on having
irq disabled from the list_lock spin lock usage.

Patch 33 changes kmem_cache_cpu->partial handling in put_cpu_partial() from
cmpxchg loop to a short irq disabled section, which is used by all other code
modifying the field. This addresses a theoretical race scenario pointed out by
Jann, and makes the critical section safe wrt with RT local_lock semantics
after the conversion in patch 35.

Patch 34 changes preempt disable to migrate disable, so that the nested
list_lock spinlock is safe to take on RT. Because migrate_disable() is a
function call even on !RT, a small set of private wrappers is introduced
to keep using the cheaper preempt_disable() on !PREEMPT_RT configurations.

As of this patch, SLUB should be compatible with RT's lock semantics, to the
best of my knowledge.

Finally, patch 35 changes irq disabled sections that protect kmem_cache_cpu
fields in the slow paths, with a local lock. However on PREEMPT_RT it means the
lockless fast paths can now preempt slow paths which don't expect that, so the
local lock has to be taken also in the fast paths and they are no longer
lockless. It's up to RT folks to decide if this is a good tradeoff.
The patch also updates the locking documentation in the file's comment.

The main results of this series:

* irq disabling is only done for minimum amount of time needed to protect the
  kmem_cache_cpu data and as part of spin lock, local lock and bit lock
  operations to make them irq-safe

* SLUB should be fully PREEMPT_RT compatible

This should have obvious implications for better preemptibility, especially on RT.

Some details are different than how the previous SLUB RT tree patches were
implemented:

  mm: sl[au]b: Change list_lock to raw_spinlock_t [2] - the SLAB part can be
  dropped as a different patch restricts RT to SLUB anyway. And after this series
  the list_lock in SLUB is never used with irqs disabled before taking the lock
  so it doesn't have to be converted to raw_spinlock_t.

  mm: slub: Move discard_slab() invocations out of IRQ-off sections [3] should be
  unnecessary as this series does move these invocations outside irq disabled
  sections in a different way.

The remaining patches to upstream from the RT tree are small ones related to
KConfig. The patch that restricts PREEMPT_RT to SLUB (not SLAB or SLOB) makes
sense. The patch that disables CONFIG_SLUB_CPU_PARTIAL with PREEMPT_RT could
perhaps be re-evaluated as the series addresses some latency issues with it.

[1] [RFC 00/26] SLUB: use local_lock for kmem_cache_cpu protection and reduce disabling irqs
    https://lore.kernel.org/lkml/20210524233946.20352-1-vbabka@suse.cz/
[2] https://git.kernel.org/pub/scm/linux/kernel/git/rt/linux-rt-devel.git/tree/patches/0001-mm-sl-au-b-Change-list_lock-to-raw_spinlock_t.patch?h=linux-5.12.y-rt-patches
[3] https://git.kernel.org/pub/scm/linux/kernel/git/rt/linux-rt-devel.git/tree/patches/0004-mm-slub-Move-discard_slab-invocations-out-of-IRQ-off.patch?h=linux-5.12.y-rt-patches
[4] https://git.kernel.org/pub/scm/linux/kernel/git/rt/linux-rt-devel.git/tree/patches/0005-mm-slub-Move-flush_cpu_slab-invocations-__free_slab-.patch?h=linux-5.12.y-rt-patches
[5] https://lore.kernel.org/lkml/20210609113903.1421-1-vbabka@suse.cz/
[6] https://lore.kernel.org/lkml/891dc24e38106f8542f4c72831d52dc1a1863ae8.camel@gmx.de
[7] https://lore.kernel.org/linux-rt-users/87tul5p2fa.ffs@nanos.tec.linutronix.de/

Sebastian Andrzej Siewior (2):
  mm: slub: Move flush_cpu_slab() invocations __free_slab() invocations
    out of IRQ context
  mm: slub: Make object_map_lock a raw_spinlock_t

Vlastimil Babka (33):
  mm, slub: don't call flush_all() from slab_debug_trace_open()
  mm, slub: allocate private object map for debugfs listings
  mm, slub: allocate private object map for validate_slab_cache()
  mm, slub: don't disable irq for debug_check_no_locks_freed()
  mm, slub: remove redundant unfreeze_partials() from put_cpu_partial()
  mm, slub: unify cmpxchg_double_slab() and __cmpxchg_double_slab()
  mm, slub: extract get_partial() from new_slab_objects()
  mm, slub: dissolve new_slab_objects() into ___slab_alloc()
  mm, slub: return slab page from get_partial() and set c->page
    afterwards
  mm, slub: restructure new page checks in ___slab_alloc()
  mm, slub: simplify kmem_cache_cpu and tid setup
  mm, slub: move disabling/enabling irqs to ___slab_alloc()
  mm, slub: do initial checks in  ___slab_alloc() with irqs enabled
  mm, slub: move disabling irqs closer to get_partial() in
    ___slab_alloc()
  mm, slub: restore irqs around calling new_slab()
  mm, slub: validate slab from partial list or page allocator before
    making it cpu slab
  mm, slub: check new pages with restored irqs
  mm, slub: stop disabling irqs around get_partial()
  mm, slub: move reset of c->page and freelist out of deactivate_slab()
  mm, slub: make locking in deactivate_slab() irq-safe
  mm, slub: call deactivate_slab() without disabling irqs
  mm, slub: move irq control into unfreeze_partials()
  mm, slub: discard slabs in unfreeze_partials() without irqs disabled
  mm, slub: detach whole partial list at once in unfreeze_partials()
  mm, slub: separate detaching of partial list in unfreeze_partials()
    from unfreezing
  mm, slub: only disable irq with spin_lock in __unfreeze_partials()
  mm, slub: don't disable irqs in slub_cpu_dead()
  mm, slab: make flush_slab() possible to call with irqs enabled
  mm, slub: optionally save/restore irqs in slab_[un]lock()/
  mm, slub: make slab_lock() disable irqs with PREEMPT_RT
  mm, slub: protect put_cpu_partial() with disabled irqs instead of
    cmpxchg
  mm, slub: use migrate_disable() on PREEMPT_RT
  mm, slub: convert kmem_cpu_slab protection to local_lock

 include/linux/slub_def.h |   2 +
 mm/slub.c                | 808 +++++++++++++++++++++++++--------------
 2 files changed, 530 insertions(+), 280 deletions(-)

-- 
2.32.0


^ permalink raw reply	[flat|nested] 47+ messages in thread

* [PATCH v3 01/35] mm, slub: don't call flush_all() from slab_debug_trace_open()
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
@ 2021-07-29 13:20 ` Vlastimil Babka
  2021-07-29 13:20 ` [PATCH v3 02/35] mm, slub: allocate private object map for debugfs listings Vlastimil Babka
                   ` (36 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:20 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

slab_debug_trace_open() can only be called on caches with SLAB_STORE_USER flag
and as with all slub debugging flags, such caches avoid cpu or percpu partial
slabs altogether, so there's nothing to flush.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Acked-by: Christoph Lameter <cl@linux.com>
---
 mm/slub.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 090fa14628f9..422a61d7bf5f 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -5820,9 +5820,6 @@ static int slab_debug_trace_open(struct inode *inode, struct file *filep)
 	if (!alloc_loc_track(t, PAGE_SIZE / sizeof(struct location), GFP_KERNEL))
 		return -ENOMEM;
 
-	/* Push back cpu slabs */
-	flush_all(s);
-
 	for_each_kmem_cache_node(s, node, n) {
 		unsigned long flags;
 		struct page *page;
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 02/35] mm, slub: allocate private object map for debugfs listings
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
  2021-07-29 13:20 ` [PATCH v3 01/35] mm, slub: don't call flush_all() from slab_debug_trace_open() Vlastimil Babka
@ 2021-07-29 13:20 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 03/35] mm, slub: allocate private object map for validate_slab_cache() Vlastimil Babka
                   ` (35 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:20 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

Slub has a static spinlock protected bitmap for marking which objects are on
freelist when it wants to list them, for situations where dynamically
allocating such map can lead to recursion or locking issues, and on-stack
bitmap would be too large.

The handlers of debugfs files alloc_traces and free_traces also currently use this
shared bitmap, but their syscall context makes it straightforward to allocate a
private map before entering locked sections, so switch these processing paths
to use a private bitmap.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Acked-by: Christoph Lameter <cl@linux.com>
Acked-by: Mel Gorman <mgorman@techsingularity.net>
---
 mm/slub.c | 44 +++++++++++++++++++++++++++++---------------
 1 file changed, 29 insertions(+), 15 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 422a61d7bf5f..66795aec6e10 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -454,6 +454,18 @@ static inline bool cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
 static unsigned long object_map[BITS_TO_LONGS(MAX_OBJS_PER_PAGE)];
 static DEFINE_SPINLOCK(object_map_lock);
 
+static void __fill_map(unsigned long *obj_map, struct kmem_cache *s,
+		       struct page *page)
+{
+	void *addr = page_address(page);
+	void *p;
+
+	bitmap_zero(obj_map, page->objects);
+
+	for (p = page->freelist; p; p = get_freepointer(s, p))
+		set_bit(__obj_to_index(s, addr, p), obj_map);
+}
+
 #if IS_ENABLED(CONFIG_KUNIT)
 static bool slab_add_kunit_errors(void)
 {
@@ -483,17 +495,11 @@ static inline bool slab_add_kunit_errors(void) { return false; }
 static unsigned long *get_map(struct kmem_cache *s, struct page *page)
 	__acquires(&object_map_lock)
 {
-	void *p;
-	void *addr = page_address(page);
-
 	VM_BUG_ON(!irqs_disabled());
 
 	spin_lock(&object_map_lock);
 
-	bitmap_zero(object_map, page->objects);
-
-	for (p = page->freelist; p; p = get_freepointer(s, p))
-		set_bit(__obj_to_index(s, addr, p), object_map);
+	__fill_map(object_map, s, page);
 
 	return object_map;
 }
@@ -4874,17 +4880,17 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 }
 
 static void process_slab(struct loc_track *t, struct kmem_cache *s,
-		struct page *page, enum track_item alloc)
+		struct page *page, enum track_item alloc,
+		unsigned long *obj_map)
 {
 	void *addr = page_address(page);
 	void *p;
-	unsigned long *map;
 
-	map = get_map(s, page);
+	__fill_map(obj_map, s, page);
+
 	for_each_object(p, s, addr, page->objects)
-		if (!test_bit(__obj_to_index(s, addr, p), map))
+		if (!test_bit(__obj_to_index(s, addr, p), obj_map))
 			add_location(t, s, get_track(s, p, alloc));
-	put_map(map);
 }
 #endif  /* CONFIG_DEBUG_FS   */
 #endif	/* CONFIG_SLUB_DEBUG */
@@ -5811,14 +5817,21 @@ static int slab_debug_trace_open(struct inode *inode, struct file *filep)
 	struct loc_track *t = __seq_open_private(filep, &slab_debugfs_sops,
 						sizeof(struct loc_track));
 	struct kmem_cache *s = file_inode(filep)->i_private;
+	unsigned long *obj_map;
+
+	obj_map = bitmap_alloc(oo_objects(s->oo), GFP_KERNEL);
+	if (!obj_map)
+		return -ENOMEM;
 
 	if (strcmp(filep->f_path.dentry->d_name.name, "alloc_traces") == 0)
 		alloc = TRACK_ALLOC;
 	else
 		alloc = TRACK_FREE;
 
-	if (!alloc_loc_track(t, PAGE_SIZE / sizeof(struct location), GFP_KERNEL))
+	if (!alloc_loc_track(t, PAGE_SIZE / sizeof(struct location), GFP_KERNEL)) {
+		bitmap_free(obj_map);
 		return -ENOMEM;
+	}
 
 	for_each_kmem_cache_node(s, node, n) {
 		unsigned long flags;
@@ -5829,12 +5842,13 @@ static int slab_debug_trace_open(struct inode *inode, struct file *filep)
 
 		spin_lock_irqsave(&n->list_lock, flags);
 		list_for_each_entry(page, &n->partial, slab_list)
-			process_slab(t, s, page, alloc);
+			process_slab(t, s, page, alloc, obj_map);
 		list_for_each_entry(page, &n->full, slab_list)
-			process_slab(t, s, page, alloc);
+			process_slab(t, s, page, alloc, obj_map);
 		spin_unlock_irqrestore(&n->list_lock, flags);
 	}
 
+	bitmap_free(obj_map);
 	return 0;
 }
 
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 03/35] mm, slub: allocate private object map for validate_slab_cache()
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
  2021-07-29 13:20 ` [PATCH v3 01/35] mm, slub: don't call flush_all() from slab_debug_trace_open() Vlastimil Babka
  2021-07-29 13:20 ` [PATCH v3 02/35] mm, slub: allocate private object map for debugfs listings Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 04/35] mm, slub: don't disable irq for debug_check_no_locks_freed() Vlastimil Babka
                   ` (34 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

validate_slab_cache() is called either to handle a sysfs write, or from a
self-test context. In both situations it's straightforward to preallocate a
private object bitmap instead of grabbing the shared static one meant for
critical sections, so let's do that.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Acked-by: Christoph Lameter <cl@linux.com>
Acked-by: Mel Gorman <mgorman@techsingularity.net>
---
 mm/slub.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 66795aec6e10..743c6b7f8bb1 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -4674,11 +4674,11 @@ static int count_total(struct page *page)
 #endif
 
 #ifdef CONFIG_SLUB_DEBUG
-static void validate_slab(struct kmem_cache *s, struct page *page)
+static void validate_slab(struct kmem_cache *s, struct page *page,
+			  unsigned long *obj_map)
 {
 	void *p;
 	void *addr = page_address(page);
-	unsigned long *map;
 
 	slab_lock(page);
 
@@ -4686,21 +4686,20 @@ static void validate_slab(struct kmem_cache *s, struct page *page)
 		goto unlock;
 
 	/* Now we know that a valid freelist exists */
-	map = get_map(s, page);
+	__fill_map(obj_map, s, page);
 	for_each_object(p, s, addr, page->objects) {
-		u8 val = test_bit(__obj_to_index(s, addr, p), map) ?
+		u8 val = test_bit(__obj_to_index(s, addr, p), obj_map) ?
 			 SLUB_RED_INACTIVE : SLUB_RED_ACTIVE;
 
 		if (!check_object(s, page, p, val))
 			break;
 	}
-	put_map(map);
 unlock:
 	slab_unlock(page);
 }
 
 static int validate_slab_node(struct kmem_cache *s,
-		struct kmem_cache_node *n)
+		struct kmem_cache_node *n, unsigned long *obj_map)
 {
 	unsigned long count = 0;
 	struct page *page;
@@ -4709,7 +4708,7 @@ static int validate_slab_node(struct kmem_cache *s,
 	spin_lock_irqsave(&n->list_lock, flags);
 
 	list_for_each_entry(page, &n->partial, slab_list) {
-		validate_slab(s, page);
+		validate_slab(s, page, obj_map);
 		count++;
 	}
 	if (count != n->nr_partial) {
@@ -4722,7 +4721,7 @@ static int validate_slab_node(struct kmem_cache *s,
 		goto out;
 
 	list_for_each_entry(page, &n->full, slab_list) {
-		validate_slab(s, page);
+		validate_slab(s, page, obj_map);
 		count++;
 	}
 	if (count != atomic_long_read(&n->nr_slabs)) {
@@ -4741,10 +4740,17 @@ long validate_slab_cache(struct kmem_cache *s)
 	int node;
 	unsigned long count = 0;
 	struct kmem_cache_node *n;
+	unsigned long *obj_map;
+
+	obj_map = bitmap_alloc(oo_objects(s->oo), GFP_KERNEL);
+	if (!obj_map)
+		return -ENOMEM;
 
 	flush_all(s);
 	for_each_kmem_cache_node(s, node, n)
-		count += validate_slab_node(s, n);
+		count += validate_slab_node(s, n, obj_map);
+
+	bitmap_free(obj_map);
 
 	return count;
 }
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 04/35] mm, slub: don't disable irq for debug_check_no_locks_freed()
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (2 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 03/35] mm, slub: allocate private object map for validate_slab_cache() Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 05/35] mm, slub: remove redundant unfreeze_partials() from put_cpu_partial() Vlastimil Babka
                   ` (33 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

In slab_free_hook() we disable irqs around the debug_check_no_locks_freed()
call, which is unnecessary, as irqs are already being disabled inside the call.
This seems to be leftover from the past where there were more calls inside the
irq disabled sections. Remove the irq disable/enable operations.

Mel noted:
> Looks like it was needed for kmemcheck which went away back in 4.15

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Acked-by: Mel Gorman <mgorman@techsingularity.net>
---
 mm/slub.c | 14 +-------------
 1 file changed, 1 insertion(+), 13 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 743c6b7f8bb1..0f08b64e2fd1 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -1588,20 +1588,8 @@ static __always_inline bool slab_free_hook(struct kmem_cache *s,
 {
 	kmemleak_free_recursive(x, s->flags);
 
-	/*
-	 * Trouble is that we may no longer disable interrupts in the fast path
-	 * So in order to make the debug calls that expect irqs to be
-	 * disabled we need to disable interrupts temporarily.
-	 */
-#ifdef CONFIG_LOCKDEP
-	{
-		unsigned long flags;
+	debug_check_no_locks_freed(x, s->object_size);
 
-		local_irq_save(flags);
-		debug_check_no_locks_freed(x, s->object_size);
-		local_irq_restore(flags);
-	}
-#endif
 	if (!(s->flags & SLAB_DEBUG_OBJECTS))
 		debug_check_no_obj_freed(x, s->object_size);
 
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 05/35] mm, slub: remove redundant unfreeze_partials() from put_cpu_partial()
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (3 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 04/35] mm, slub: don't disable irq for debug_check_no_locks_freed() Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 06/35] mm, slub: unify cmpxchg_double_slab() and __cmpxchg_double_slab() Vlastimil Babka
                   ` (32 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

Commit d6e0b7fa1186 ("slub: make dead caches discard free slabs immediately")
introduced cpu partial flushing for kmemcg caches, based on setting the target
cpu_partial to 0 and adding a flushing check in put_cpu_partial().
This code that sets cpu_partial to 0 was later moved by c9fc586403e7 ("slab:
introduce __kmemcg_cache_deactivate()") and ultimately removed by 9855609bde03
("mm: memcg/slab: use a single set of kmem_caches for all accounted
allocations"). However the check and flush in put_cpu_partial() was never
removed, although it's effectively a dead code. So this patch removes it.

Note that d6e0b7fa1186 also added preempt_disable()/enable() to
unfreeze_partials() which could be thus also considered unnecessary. But
further patches will rely on it, so keep it.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 7 -------
 1 file changed, 7 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 0f08b64e2fd1..15f01d2ca30f 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2463,13 +2463,6 @@ static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
 
 	} while (this_cpu_cmpxchg(s->cpu_slab->partial, oldpage, page)
 								!= oldpage);
-	if (unlikely(!slub_cpu_partial(s))) {
-		unsigned long flags;
-
-		local_irq_save(flags);
-		unfreeze_partials(s, this_cpu_ptr(s->cpu_slab));
-		local_irq_restore(flags);
-	}
 	preempt_enable();
 #endif	/* CONFIG_SLUB_CPU_PARTIAL */
 }
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 06/35] mm, slub: unify cmpxchg_double_slab() and __cmpxchg_double_slab()
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (4 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 05/35] mm, slub: remove redundant unfreeze_partials() from put_cpu_partial() Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 07/35] mm, slub: extract get_partial() from new_slab_objects() Vlastimil Babka
                   ` (31 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

These functions differ only in irq disabling in the slow path. We can create a
common function with an extra bool parameter to control the irq disabling.
As the functions are inline and the parameter compile-time constant, there
will be no runtime overhead due to this change.

Also change the DEBUG_VM based irqs disable assert to the more standard
lockdep_assert based one.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Acked-by: Christoph Lameter <cl@linux.com>
---
 mm/slub.c | 62 +++++++++++++++++++++----------------------------------
 1 file changed, 24 insertions(+), 38 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 15f01d2ca30f..5673bdbfc23d 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -371,13 +371,13 @@ static __always_inline void slab_unlock(struct page *page)
 	__bit_spin_unlock(PG_locked, &page->flags);
 }
 
-/* Interrupts must be disabled (for the fallback code to work right) */
-static inline bool __cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
+static inline bool ___cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
 		void *freelist_old, unsigned long counters_old,
 		void *freelist_new, unsigned long counters_new,
-		const char *n)
+		const char *n, bool disable_irqs)
 {
-	VM_BUG_ON(!irqs_disabled());
+	if (!disable_irqs)
+		lockdep_assert_irqs_disabled();
 #if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
     defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
 	if (s->flags & __CMPXCHG_DOUBLE) {
@@ -388,15 +388,23 @@ static inline bool __cmpxchg_double_slab(struct kmem_cache *s, struct page *page
 	} else
 #endif
 	{
+		unsigned long flags;
+
+		if (disable_irqs)
+			local_irq_save(flags);
 		slab_lock(page);
 		if (page->freelist == freelist_old &&
 					page->counters == counters_old) {
 			page->freelist = freelist_new;
 			page->counters = counters_new;
 			slab_unlock(page);
+			if (disable_irqs)
+				local_irq_restore(flags);
 			return true;
 		}
 		slab_unlock(page);
+		if (disable_irqs)
+			local_irq_restore(flags);
 	}
 
 	cpu_relax();
@@ -409,45 +417,23 @@ static inline bool __cmpxchg_double_slab(struct kmem_cache *s, struct page *page
 	return false;
 }
 
-static inline bool cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
+/* Interrupts must be disabled (for the fallback code to work right) */
+static inline bool __cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
 		void *freelist_old, unsigned long counters_old,
 		void *freelist_new, unsigned long counters_new,
 		const char *n)
 {
-#if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
-    defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
-	if (s->flags & __CMPXCHG_DOUBLE) {
-		if (cmpxchg_double(&page->freelist, &page->counters,
-				   freelist_old, counters_old,
-				   freelist_new, counters_new))
-			return true;
-	} else
-#endif
-	{
-		unsigned long flags;
-
-		local_irq_save(flags);
-		slab_lock(page);
-		if (page->freelist == freelist_old &&
-					page->counters == counters_old) {
-			page->freelist = freelist_new;
-			page->counters = counters_new;
-			slab_unlock(page);
-			local_irq_restore(flags);
-			return true;
-		}
-		slab_unlock(page);
-		local_irq_restore(flags);
-	}
-
-	cpu_relax();
-	stat(s, CMPXCHG_DOUBLE_FAIL);
-
-#ifdef SLUB_DEBUG_CMPXCHG
-	pr_info("%s %s: cmpxchg double redo ", n, s->name);
-#endif
+	return ___cmpxchg_double_slab(s, page, freelist_old, counters_old,
+				      freelist_new, counters_new, n, false);
+}
 
-	return false;
+static inline bool cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
+		void *freelist_old, unsigned long counters_old,
+		void *freelist_new, unsigned long counters_new,
+		const char *n)
+{
+	return ___cmpxchg_double_slab(s, page, freelist_old, counters_old,
+				      freelist_new, counters_new, n, true);
 }
 
 #ifdef CONFIG_SLUB_DEBUG
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 07/35] mm, slub: extract get_partial() from new_slab_objects()
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (5 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 06/35] mm, slub: unify cmpxchg_double_slab() and __cmpxchg_double_slab() Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 08/35] mm, slub: dissolve new_slab_objects() into ___slab_alloc() Vlastimil Babka
                   ` (30 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

The later patches will need more fine grained control over individual actions
in ___slab_alloc(), the only caller of new_slab_objects(), so this is a first
preparatory step with no functional change.

This adds a goto label that appears unnecessary at this point, but will be
useful for later changes.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Acked-by: Christoph Lameter <cl@linux.com>
---
 mm/slub.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 5673bdbfc23d..09fa967519c5 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2596,17 +2596,12 @@ slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)
 static inline void *new_slab_objects(struct kmem_cache *s, gfp_t flags,
 			int node, struct kmem_cache_cpu **pc)
 {
-	void *freelist;
+	void *freelist = NULL;
 	struct kmem_cache_cpu *c = *pc;
 	struct page *page;
 
 	WARN_ON_ONCE(s->ctor && (flags & __GFP_ZERO));
 
-	freelist = get_partial(s, flags, node, c);
-
-	if (freelist)
-		return freelist;
-
 	page = new_slab(s, flags, node);
 	if (page) {
 		c = raw_cpu_ptr(s->cpu_slab);
@@ -2770,6 +2765,10 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 		goto redo;
 	}
 
+	freelist = get_partial(s, gfpflags, node, c);
+	if (freelist)
+		goto check_new_page;
+
 	freelist = new_slab_objects(s, gfpflags, node, &c);
 
 	if (unlikely(!freelist)) {
@@ -2777,6 +2776,7 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 		return NULL;
 	}
 
+check_new_page:
 	page = c->page;
 	if (likely(!kmem_cache_debug(s) && pfmemalloc_match(page, gfpflags)))
 		goto load_freelist;
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 08/35] mm, slub: dissolve new_slab_objects() into ___slab_alloc()
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (6 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 07/35] mm, slub: extract get_partial() from new_slab_objects() Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 09/35] mm, slub: return slab page from get_partial() and set c->page afterwards Vlastimil Babka
                   ` (29 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

The later patches will need more fine grained control over individual actions
in ___slab_alloc(), the only caller of new_slab_objects(), so dissolve it
there. This is a preparatory step with no functional change.

The only minor change is moving WARN_ON_ONCE() for using a constructor together
with __GFP_ZERO to new_slab(), which makes it somewhat less frequent, but still
able to catch a development change introducing a systematic misuse.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Acked-by: Christoph Lameter <cl@linux.com>
Acked-by: Mel Gorman <mgorman@techsingularity.net>
---
 mm/slub.c | 50 ++++++++++++++++++--------------------------------
 1 file changed, 18 insertions(+), 32 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 09fa967519c5..7ccd03d553bc 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -1868,6 +1868,8 @@ static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
 	if (unlikely(flags & GFP_SLAB_BUG_MASK))
 		flags = kmalloc_fix_flags(flags);
 
+	WARN_ON_ONCE(s->ctor && (flags & __GFP_ZERO));
+
 	return allocate_slab(s,
 		flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
 }
@@ -2593,36 +2595,6 @@ slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)
 #endif
 }
 
-static inline void *new_slab_objects(struct kmem_cache *s, gfp_t flags,
-			int node, struct kmem_cache_cpu **pc)
-{
-	void *freelist = NULL;
-	struct kmem_cache_cpu *c = *pc;
-	struct page *page;
-
-	WARN_ON_ONCE(s->ctor && (flags & __GFP_ZERO));
-
-	page = new_slab(s, flags, node);
-	if (page) {
-		c = raw_cpu_ptr(s->cpu_slab);
-		if (c->page)
-			flush_slab(s, c);
-
-		/*
-		 * No other reference to the page yet so we can
-		 * muck around with it freely without cmpxchg
-		 */
-		freelist = page->freelist;
-		page->freelist = NULL;
-
-		stat(s, ALLOC_SLAB);
-		c->page = page;
-		*pc = c;
-	}
-
-	return freelist;
-}
-
 static inline bool pfmemalloc_match(struct page *page, gfp_t gfpflags)
 {
 	if (unlikely(PageSlabPfmemalloc(page)))
@@ -2769,13 +2741,27 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 	if (freelist)
 		goto check_new_page;
 
-	freelist = new_slab_objects(s, gfpflags, node, &c);
+	page = new_slab(s, gfpflags, node);
 
-	if (unlikely(!freelist)) {
+	if (unlikely(!page)) {
 		slab_out_of_memory(s, gfpflags, node);
 		return NULL;
 	}
 
+	c = raw_cpu_ptr(s->cpu_slab);
+	if (c->page)
+		flush_slab(s, c);
+
+	/*
+	 * No other reference to the page yet so we can
+	 * muck around with it freely without cmpxchg
+	 */
+	freelist = page->freelist;
+	page->freelist = NULL;
+
+	stat(s, ALLOC_SLAB);
+	c->page = page;
+
 check_new_page:
 	page = c->page;
 	if (likely(!kmem_cache_debug(s) && pfmemalloc_match(page, gfpflags)))
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 09/35] mm, slub: return slab page from get_partial() and set c->page afterwards
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (7 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 08/35] mm, slub: dissolve new_slab_objects() into ___slab_alloc() Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 10/35] mm, slub: restructure new page checks in ___slab_alloc() Vlastimil Babka
                   ` (28 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

The function get_partial() finds a suitable page on a partial list, acquires
and returns its freelist and assigns the page pointer to kmem_cache_cpu.
In later patch we will need more control over the kmem_cache_cpu.page
assignment, so instead of passing a kmem_cache_cpu pointer, pass a pointer to a
pointer to a page that get_partial() can fill and the caller can assign the
kmem_cache_cpu.page pointer. No functional change as all of this still happens
with disabled IRQs.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 7ccd03d553bc..92a866adce3d 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2000,7 +2000,7 @@ static inline bool pfmemalloc_match(struct page *page, gfp_t gfpflags);
  * Try to allocate a partial slab from a specific node.
  */
 static void *get_partial_node(struct kmem_cache *s, struct kmem_cache_node *n,
-				struct kmem_cache_cpu *c, gfp_t flags)
+			      struct page **ret_page, gfp_t flags)
 {
 	struct page *page, *page2;
 	void *object = NULL;
@@ -2029,7 +2029,7 @@ static void *get_partial_node(struct kmem_cache *s, struct kmem_cache_node *n,
 
 		available += objects;
 		if (!object) {
-			c->page = page;
+			*ret_page = page;
 			stat(s, ALLOC_FROM_PARTIAL);
 			object = t;
 		} else {
@@ -2049,7 +2049,7 @@ static void *get_partial_node(struct kmem_cache *s, struct kmem_cache_node *n,
  * Get a page from somewhere. Search in increasing NUMA distances.
  */
 static void *get_any_partial(struct kmem_cache *s, gfp_t flags,
-		struct kmem_cache_cpu *c)
+			     struct page **ret_page)
 {
 #ifdef CONFIG_NUMA
 	struct zonelist *zonelist;
@@ -2091,7 +2091,7 @@ static void *get_any_partial(struct kmem_cache *s, gfp_t flags,
 
 			if (n && cpuset_zone_allowed(zone, flags) &&
 					n->nr_partial > s->min_partial) {
-				object = get_partial_node(s, n, c, flags);
+				object = get_partial_node(s, n, ret_page, flags);
 				if (object) {
 					/*
 					 * Don't check read_mems_allowed_retry()
@@ -2113,7 +2113,7 @@ static void *get_any_partial(struct kmem_cache *s, gfp_t flags,
  * Get a partial page, lock it and return it.
  */
 static void *get_partial(struct kmem_cache *s, gfp_t flags, int node,
-		struct kmem_cache_cpu *c)
+			 struct page **ret_page)
 {
 	void *object;
 	int searchnode = node;
@@ -2121,11 +2121,11 @@ static void *get_partial(struct kmem_cache *s, gfp_t flags, int node,
 	if (node == NUMA_NO_NODE)
 		searchnode = numa_mem_id();
 
-	object = get_partial_node(s, get_node(s, searchnode), c, flags);
+	object = get_partial_node(s, get_node(s, searchnode), ret_page, flags);
 	if (object || node != NUMA_NO_NODE)
 		return object;
 
-	return get_any_partial(s, flags, c);
+	return get_any_partial(s, flags, ret_page);
 }
 
 #ifdef CONFIG_PREEMPTION
@@ -2737,9 +2737,11 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 		goto redo;
 	}
 
-	freelist = get_partial(s, gfpflags, node, c);
-	if (freelist)
+	freelist = get_partial(s, gfpflags, node, &page);
+	if (freelist) {
+		c->page = page;
 		goto check_new_page;
+	}
 
 	page = new_slab(s, gfpflags, node);
 
@@ -2763,7 +2765,6 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 	c->page = page;
 
 check_new_page:
-	page = c->page;
 	if (likely(!kmem_cache_debug(s) && pfmemalloc_match(page, gfpflags)))
 		goto load_freelist;
 
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 10/35] mm, slub: restructure new page checks in ___slab_alloc()
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (8 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 09/35] mm, slub: return slab page from get_partial() and set c->page afterwards Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 11/35] mm, slub: simplify kmem_cache_cpu and tid setup Vlastimil Babka
                   ` (27 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

When we allocate slab object from a newly acquired page (from node's partial
list or page allocator), we usually also retain the page as a new percpu slab.
There are two exceptions - when pfmemalloc status of the page doesn't match our
gfp flags, or when the cache has debugging enabled.

The current code for these decisions is not easy to follow, so restructure it
and add comments. The new structure will also help with the following changes.
No functional change.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Acked-by: Mel Gorman <mgorman@techsingularity.net>
---
 mm/slub.c | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 92a866adce3d..469aa8155663 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2765,13 +2765,29 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 	c->page = page;
 
 check_new_page:
-	if (likely(!kmem_cache_debug(s) && pfmemalloc_match(page, gfpflags)))
-		goto load_freelist;
 
-	/* Only entered in the debug case */
-	if (kmem_cache_debug(s) &&
-			!alloc_debug_processing(s, page, freelist, addr))
-		goto new_slab;	/* Slab failed checks. Next slab needed */
+	if (kmem_cache_debug(s)) {
+		if (!alloc_debug_processing(s, page, freelist, addr))
+			/* Slab failed checks. Next slab needed */
+			goto new_slab;
+		else
+			/*
+			 * For debug case, we don't load freelist so that all
+			 * allocations go through alloc_debug_processing()
+			 */
+			goto return_single;
+	}
+
+	if (unlikely(!pfmemalloc_match(page, gfpflags)))
+		/*
+		 * For !pfmemalloc_match() case we don't load freelist so that
+		 * we don't make further mismatched allocations easier.
+		 */
+		goto return_single;
+
+	goto load_freelist;
+
+return_single:
 
 	deactivate_slab(s, page, get_freepointer(s, freelist), c);
 	return freelist;
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 11/35] mm, slub: simplify kmem_cache_cpu and tid setup
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (9 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 10/35] mm, slub: restructure new page checks in ___slab_alloc() Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 12/35] mm, slub: move disabling/enabling irqs to ___slab_alloc() Vlastimil Babka
                   ` (26 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

In slab_alloc_node() and do_slab_free() fastpaths we need to guarantee that
our kmem_cache_cpu pointer is from the same cpu as the tid value. Currently
that's done by reading the tid first using this_cpu_read(), then the
kmem_cache_cpu pointer and verifying we read the same tid using the pointer and
plain READ_ONCE().

This can be simplified to just fetching kmem_cache_cpu pointer and then reading
tid using the pointer. That guarantees they are from the same cpu. We don't
need to read the tid using this_cpu_read() because the value will be validated
by this_cpu_cmpxchg_double(), making sure we are on the correct cpu and the
freelist didn't change by anyone preempting us since reading the tid.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Acked-by: Mel Gorman <mgorman@techsingularity.net>
---
 mm/slub.c | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 469aa8155663..0871bba8ecc2 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2865,15 +2865,14 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s,
 	 * reading from one cpu area. That does not matter as long
 	 * as we end up on the original cpu again when doing the cmpxchg.
 	 *
-	 * We should guarantee that tid and kmem_cache are retrieved on
-	 * the same cpu. It could be different if CONFIG_PREEMPTION so we need
-	 * to check if it is matched or not.
+	 * We must guarantee that tid and kmem_cache_cpu are retrieved on the
+	 * same cpu. We read first the kmem_cache_cpu pointer and use it to read
+	 * the tid. If we are preempted and switched to another cpu between the
+	 * two reads, it's OK as the two are still associated with the same cpu
+	 * and cmpxchg later will validate the cpu.
 	 */
-	do {
-		tid = this_cpu_read(s->cpu_slab->tid);
-		c = raw_cpu_ptr(s->cpu_slab);
-	} while (IS_ENABLED(CONFIG_PREEMPTION) &&
-		 unlikely(tid != READ_ONCE(c->tid)));
+	c = raw_cpu_ptr(s->cpu_slab);
+	tid = READ_ONCE(c->tid);
 
 	/*
 	 * Irqless object alloc/free algorithm used here depends on sequence
@@ -3147,11 +3146,8 @@ static __always_inline void do_slab_free(struct kmem_cache *s,
 	 * data is retrieved via this pointer. If we are on the same cpu
 	 * during the cmpxchg then the free will succeed.
 	 */
-	do {
-		tid = this_cpu_read(s->cpu_slab->tid);
-		c = raw_cpu_ptr(s->cpu_slab);
-	} while (IS_ENABLED(CONFIG_PREEMPTION) &&
-		 unlikely(tid != READ_ONCE(c->tid)));
+	c = raw_cpu_ptr(s->cpu_slab);
+	tid = READ_ONCE(c->tid);
 
 	/* Same with comment on barrier() in slab_alloc_node() */
 	barrier();
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 12/35] mm, slub: move disabling/enabling irqs to ___slab_alloc()
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (10 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 11/35] mm, slub: simplify kmem_cache_cpu and tid setup Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 13/35] mm, slub: do initial checks in ___slab_alloc() with irqs enabled Vlastimil Babka
                   ` (25 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

Currently __slab_alloc() disables irqs around the whole ___slab_alloc().  This
includes cases where this is not needed, such as when the allocation ends up in
the page allocator and has to awkwardly enable irqs back based on gfp flags.
Also the whole kmem_cache_alloc_bulk() is executed with irqs disabled even when
it hits the __slab_alloc() slow path, and long periods with disabled interrupts
are undesirable.

As a first step towards reducing irq disabled periods, move irq handling into
___slab_alloc(). Callers will instead prevent the s->cpu_slab percpu pointer
from becoming invalid via get_cpu_ptr(), thus preempt_disable(). This does not
protect against modification by an irq handler, which is still done by disabled
irq for most of ___slab_alloc(). As a small immediate benefit,
slab_out_of_memory() from ___slab_alloc() is now called with irqs enabled.

kmem_cache_alloc_bulk() disables irqs for its fastpath and then re-enables them
before calling ___slab_alloc(), which then disables them at its discretion. The
whole kmem_cache_alloc_bulk() operation also disables preemption.

When  ___slab_alloc() calls new_slab() to allocate a new page, re-enable
preemption, because new_slab() will re-enable interrupts in contexts that allow
blocking (this will be improved by later patches).

The patch itself will thus increase overhead a bit due to disabled preemption
(on configs where it matters) and increased disabling/enabling irqs in
kmem_cache_alloc_bulk(), but that will be gradually improved in the following
patches.

Note in __slab_alloc() we need to change the #ifdef CONFIG_PREEMPT guard to
CONFIG_PREEMPT_COUNT to make sure preempt disable/enable is properly paired in
all configurations. On configs without involuntary preemption and debugging
the re-read of kmem_cache_cpu pointer is still compiled out as it was before.

[ Mike Galbraith <efault@gmx.de>: Fix kmem_cache_alloc_bulk() error path ]
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 36 ++++++++++++++++++++++++------------
 1 file changed, 24 insertions(+), 12 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 0871bba8ecc2..71a5617b839a 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2653,7 +2653,7 @@ static inline void *get_freelist(struct kmem_cache *s, struct page *page)
  * we need to allocate a new slab. This is the slowest path since it involves
  * a call to the page allocator and the setup of a new slab.
  *
- * Version of __slab_alloc to use when we know that interrupts are
+ * Version of __slab_alloc to use when we know that preemption is
  * already disabled (which is the case for bulk allocation).
  */
 static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
@@ -2661,9 +2661,11 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 {
 	void *freelist;
 	struct page *page;
+	unsigned long flags;
 
 	stat(s, ALLOC_SLOWPATH);
 
+	local_irq_save(flags);
 	page = c->page;
 	if (!page) {
 		/*
@@ -2726,6 +2728,7 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 	VM_BUG_ON(!c->page->frozen);
 	c->freelist = get_freepointer(s, freelist);
 	c->tid = next_tid(c->tid);
+	local_irq_restore(flags);
 	return freelist;
 
 new_slab:
@@ -2743,14 +2746,16 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 		goto check_new_page;
 	}
 
+	put_cpu_ptr(s->cpu_slab);
 	page = new_slab(s, gfpflags, node);
+	c = get_cpu_ptr(s->cpu_slab);
 
 	if (unlikely(!page)) {
+		local_irq_restore(flags);
 		slab_out_of_memory(s, gfpflags, node);
 		return NULL;
 	}
 
-	c = raw_cpu_ptr(s->cpu_slab);
 	if (c->page)
 		flush_slab(s, c);
 
@@ -2790,31 +2795,33 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 return_single:
 
 	deactivate_slab(s, page, get_freepointer(s, freelist), c);
+	local_irq_restore(flags);
 	return freelist;
 }
 
 /*
- * Another one that disabled interrupt and compensates for possible
- * cpu changes by refetching the per cpu area pointer.
+ * A wrapper for ___slab_alloc() for contexts where preemption is not yet
+ * disabled. Compensates for possible cpu changes by refetching the per cpu area
+ * pointer.
  */
 static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 			  unsigned long addr, struct kmem_cache_cpu *c)
 {
 	void *p;
-	unsigned long flags;
 
-	local_irq_save(flags);
-#ifdef CONFIG_PREEMPTION
+#ifdef CONFIG_PREEMPT_COUNT
 	/*
 	 * We may have been preempted and rescheduled on a different
-	 * cpu before disabling interrupts. Need to reload cpu area
+	 * cpu before disabling preemption. Need to reload cpu area
 	 * pointer.
 	 */
-	c = this_cpu_ptr(s->cpu_slab);
+	c = get_cpu_ptr(s->cpu_slab);
 #endif
 
 	p = ___slab_alloc(s, gfpflags, node, addr, c);
-	local_irq_restore(flags);
+#ifdef CONFIG_PREEMPT_COUNT
+	put_cpu_ptr(s->cpu_slab);
+#endif
 	return p;
 }
 
@@ -3334,8 +3341,8 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
 	 * IRQs, which protects against PREEMPT and interrupts
 	 * handlers invoking normal fastpath.
 	 */
+	c = get_cpu_ptr(s->cpu_slab);
 	local_irq_disable();
-	c = this_cpu_ptr(s->cpu_slab);
 
 	for (i = 0; i < size; i++) {
 		void *object = kfence_alloc(s, s->object_size, flags);
@@ -3356,6 +3363,8 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
 			 */
 			c->tid = next_tid(c->tid);
 
+			local_irq_enable();
+
 			/*
 			 * Invoking slow path likely have side-effect
 			 * of re-populating per CPU c->freelist
@@ -3368,6 +3377,8 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
 			c = this_cpu_ptr(s->cpu_slab);
 			maybe_wipe_obj_freeptr(s, p[i]);
 
+			local_irq_disable();
+
 			continue; /* goto for-loop */
 		}
 		c->freelist = get_freepointer(s, object);
@@ -3376,6 +3387,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
 	}
 	c->tid = next_tid(c->tid);
 	local_irq_enable();
+	put_cpu_ptr(s->cpu_slab);
 
 	/*
 	 * memcg and kmem_cache debug support and memory initialization.
@@ -3385,7 +3397,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
 				slab_want_init_on_alloc(flags, s));
 	return i;
 error:
-	local_irq_enable();
+	put_cpu_ptr(s->cpu_slab);
 	slab_post_alloc_hook(s, objcg, flags, i, p, false);
 	__kmem_cache_free_bulk(s, i, p);
 	return 0;
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 13/35] mm, slub: do initial checks in  ___slab_alloc() with irqs enabled
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (11 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 12/35] mm, slub: move disabling/enabling irqs to ___slab_alloc() Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 14/35] mm, slub: move disabling irqs closer to get_partial() in ___slab_alloc() Vlastimil Babka
                   ` (24 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

As another step of shortening irq disabled sections in ___slab_alloc(), delay
disabling irqs until we pass the initial checks if there is a cached percpu
slab and it's suitable for our allocation.

Now we have to recheck c->page after actually disabling irqs as an allocation
in irq handler might have replaced it.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
Acked-by: Mel Gorman <mgorman@techsingularity.net>
---
 mm/slub.c | 41 ++++++++++++++++++++++++++++++++---------
 1 file changed, 32 insertions(+), 9 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 71a5617b839a..dd01af81dd77 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2665,8 +2665,9 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 
 	stat(s, ALLOC_SLOWPATH);
 
-	local_irq_save(flags);
-	page = c->page;
+reread_page:
+
+	page = READ_ONCE(c->page);
 	if (!page) {
 		/*
 		 * if the node is not online or has no normal memory, just
@@ -2675,6 +2676,11 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 		if (unlikely(node != NUMA_NO_NODE &&
 			     !node_isset(node, slab_nodes)))
 			node = NUMA_NO_NODE;
+		local_irq_save(flags);
+		if (unlikely(c->page)) {
+			local_irq_restore(flags);
+			goto reread_page;
+		}
 		goto new_slab;
 	}
 redo:
@@ -2689,8 +2695,7 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 			goto redo;
 		} else {
 			stat(s, ALLOC_NODE_MISMATCH);
-			deactivate_slab(s, page, c->freelist, c);
-			goto new_slab;
+			goto deactivate_slab;
 		}
 	}
 
@@ -2699,12 +2704,15 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 	 * PFMEMALLOC but right now, we are losing the pfmemalloc
 	 * information when the page leaves the per-cpu allocator
 	 */
-	if (unlikely(!pfmemalloc_match(page, gfpflags))) {
-		deactivate_slab(s, page, c->freelist, c);
-		goto new_slab;
-	}
+	if (unlikely(!pfmemalloc_match(page, gfpflags)))
+		goto deactivate_slab;
 
-	/* must check again c->freelist in case of cpu migration or IRQ */
+	/* must check again c->page in case IRQ handler changed it */
+	local_irq_save(flags);
+	if (unlikely(page != c->page)) {
+		local_irq_restore(flags);
+		goto reread_page;
+	}
 	freelist = c->freelist;
 	if (freelist)
 		goto load_freelist;
@@ -2720,6 +2728,9 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 	stat(s, ALLOC_REFILL);
 
 load_freelist:
+
+	lockdep_assert_irqs_disabled();
+
 	/*
 	 * freelist is pointing to the list of objects to be used.
 	 * page is pointing to the page from which the objects are obtained.
@@ -2731,11 +2742,23 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 	local_irq_restore(flags);
 	return freelist;
 
+deactivate_slab:
+
+	local_irq_save(flags);
+	if (page != c->page) {
+		local_irq_restore(flags);
+		goto reread_page;
+	}
+	deactivate_slab(s, page, c->freelist, c);
+
 new_slab:
 
+	lockdep_assert_irqs_disabled();
+
 	if (slub_percpu_partial(c)) {
 		page = c->page = slub_percpu_partial(c);
 		slub_set_percpu_partial(c, page);
+		local_irq_restore(flags);
 		stat(s, CPU_PARTIAL_ALLOC);
 		goto redo;
 	}
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 14/35] mm, slub: move disabling irqs closer to get_partial() in ___slab_alloc()
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (12 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 13/35] mm, slub: do initial checks in ___slab_alloc() with irqs enabled Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 15/35] mm, slub: restore irqs around calling new_slab() Vlastimil Babka
                   ` (23 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

Continue reducing the irq disabled scope. Check for per-cpu partial slabs with
first with irqs enabled and then recheck with irqs disabled before grabbing
the slab page. Mostly preparatory for the following patches.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 34 +++++++++++++++++++++++++---------
 1 file changed, 25 insertions(+), 9 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index dd01af81dd77..187424ebf1d8 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2676,11 +2676,6 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 		if (unlikely(node != NUMA_NO_NODE &&
 			     !node_isset(node, slab_nodes)))
 			node = NUMA_NO_NODE;
-		local_irq_save(flags);
-		if (unlikely(c->page)) {
-			local_irq_restore(flags);
-			goto reread_page;
-		}
 		goto new_slab;
 	}
 redo:
@@ -2721,6 +2716,7 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 
 	if (!freelist) {
 		c->page = NULL;
+		local_irq_restore(flags);
 		stat(s, DEACTIVATE_BYPASS);
 		goto new_slab;
 	}
@@ -2750,12 +2746,19 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 		goto reread_page;
 	}
 	deactivate_slab(s, page, c->freelist, c);
+	local_irq_restore(flags);
 
 new_slab:
 
-	lockdep_assert_irqs_disabled();
-
 	if (slub_percpu_partial(c)) {
+		local_irq_save(flags);
+		if (unlikely(c->page)) {
+			local_irq_restore(flags);
+			goto reread_page;
+		}
+		if (unlikely(!slub_percpu_partial(c)))
+			goto new_objects; /* stolen by an IRQ handler */
+
 		page = c->page = slub_percpu_partial(c);
 		slub_set_percpu_partial(c, page);
 		local_irq_restore(flags);
@@ -2763,6 +2766,16 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 		goto redo;
 	}
 
+	local_irq_save(flags);
+	if (unlikely(c->page)) {
+		local_irq_restore(flags);
+		goto reread_page;
+	}
+
+new_objects:
+
+	lockdep_assert_irqs_disabled();
+
 	freelist = get_partial(s, gfpflags, node, &page);
 	if (freelist) {
 		c->page = page;
@@ -2795,15 +2808,18 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 check_new_page:
 
 	if (kmem_cache_debug(s)) {
-		if (!alloc_debug_processing(s, page, freelist, addr))
+		if (!alloc_debug_processing(s, page, freelist, addr)) {
 			/* Slab failed checks. Next slab needed */
+			c->page = NULL;
+			local_irq_restore(flags);
 			goto new_slab;
-		else
+		} else {
 			/*
 			 * For debug case, we don't load freelist so that all
 			 * allocations go through alloc_debug_processing()
 			 */
 			goto return_single;
+		}
 	}
 
 	if (unlikely(!pfmemalloc_match(page, gfpflags)))
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 15/35] mm, slub: restore irqs around calling new_slab()
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (13 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 14/35] mm, slub: move disabling irqs closer to get_partial() in ___slab_alloc() Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 16/35] mm, slub: validate slab from partial list or page allocator before making it cpu slab Vlastimil Babka
                   ` (22 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

allocate_slab() currently re-enables irqs before calling to the page allocator.
It depends on gfpflags_allow_blocking() to determine if it's safe to do so.
Now we can instead simply restore irq before calling it through new_slab().
The other caller early_kmem_cache_node_alloc() is unaffected by this.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 187424ebf1d8..43f38fd47062 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -1792,9 +1792,6 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
 
 	flags &= gfp_allowed_mask;
 
-	if (gfpflags_allow_blocking(flags))
-		local_irq_enable();
-
 	flags |= s->allocflags;
 
 	/*
@@ -1853,8 +1850,6 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
 	page->frozen = 1;
 
 out:
-	if (gfpflags_allow_blocking(flags))
-		local_irq_disable();
 	if (!page)
 		return NULL;
 
@@ -2782,16 +2777,17 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 		goto check_new_page;
 	}
 
+	local_irq_restore(flags);
 	put_cpu_ptr(s->cpu_slab);
 	page = new_slab(s, gfpflags, node);
 	c = get_cpu_ptr(s->cpu_slab);
 
 	if (unlikely(!page)) {
-		local_irq_restore(flags);
 		slab_out_of_memory(s, gfpflags, node);
 		return NULL;
 	}
 
+	local_irq_save(flags);
 	if (c->page)
 		flush_slab(s, c);
 
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 16/35] mm, slub: validate slab from partial list or page allocator before making it cpu slab
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (14 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 15/35] mm, slub: restore irqs around calling new_slab() Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 17/35] mm, slub: check new pages with restored irqs Vlastimil Babka
                   ` (21 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

When we obtain a new slab page from node partial list or page allocator, we
assign it to kmem_cache_cpu, perform some checks, and if they fail, we undo
the assignment.

In order to allow doing the checks without irq disabled, restructure the code
so that the checks are done first, and kmem_cache_cpu.page assignment only
after they pass.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 43f38fd47062..c1a88ad4048b 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2772,10 +2772,8 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 	lockdep_assert_irqs_disabled();
 
 	freelist = get_partial(s, gfpflags, node, &page);
-	if (freelist) {
-		c->page = page;
+	if (freelist)
 		goto check_new_page;
-	}
 
 	local_irq_restore(flags);
 	put_cpu_ptr(s->cpu_slab);
@@ -2788,9 +2786,6 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 	}
 
 	local_irq_save(flags);
-	if (c->page)
-		flush_slab(s, c);
-
 	/*
 	 * No other reference to the page yet so we can
 	 * muck around with it freely without cmpxchg
@@ -2799,14 +2794,12 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 	page->freelist = NULL;
 
 	stat(s, ALLOC_SLAB);
-	c->page = page;
 
 check_new_page:
 
 	if (kmem_cache_debug(s)) {
 		if (!alloc_debug_processing(s, page, freelist, addr)) {
 			/* Slab failed checks. Next slab needed */
-			c->page = NULL;
 			local_irq_restore(flags);
 			goto new_slab;
 		} else {
@@ -2825,10 +2818,18 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 		 */
 		goto return_single;
 
+	if (unlikely(c->page))
+		flush_slab(s, c);
+	c->page = page;
+
 	goto load_freelist;
 
 return_single:
 
+	if (unlikely(c->page))
+		flush_slab(s, c);
+	c->page = page;
+
 	deactivate_slab(s, page, get_freepointer(s, freelist), c);
 	local_irq_restore(flags);
 	return freelist;
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 17/35] mm, slub: check new pages with restored irqs
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (15 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 16/35] mm, slub: validate slab from partial list or page allocator before making it cpu slab Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 18/35] mm, slub: stop disabling irqs around get_partial() Vlastimil Babka
                   ` (20 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

Building on top of the previous patch, re-enable irqs before checking new
pages. alloc_debug_processing() is now called with enabled irqs so we need to
remove VM_BUG_ON(!irqs_disabled()); in check_slab() - there doesn't seem to be
a need for it anyway.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index c1a88ad4048b..fe0594b60e93 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -995,8 +995,6 @@ static int check_slab(struct kmem_cache *s, struct page *page)
 {
 	int maxobj;
 
-	VM_BUG_ON(!irqs_disabled());
-
 	if (!PageSlab(page)) {
 		slab_err(s, page, "Not a valid slab page");
 		return 0;
@@ -2772,10 +2770,10 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 	lockdep_assert_irqs_disabled();
 
 	freelist = get_partial(s, gfpflags, node, &page);
+	local_irq_restore(flags);
 	if (freelist)
 		goto check_new_page;
 
-	local_irq_restore(flags);
 	put_cpu_ptr(s->cpu_slab);
 	page = new_slab(s, gfpflags, node);
 	c = get_cpu_ptr(s->cpu_slab);
@@ -2785,7 +2783,6 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 		return NULL;
 	}
 
-	local_irq_save(flags);
 	/*
 	 * No other reference to the page yet so we can
 	 * muck around with it freely without cmpxchg
@@ -2800,7 +2797,6 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 	if (kmem_cache_debug(s)) {
 		if (!alloc_debug_processing(s, page, freelist, addr)) {
 			/* Slab failed checks. Next slab needed */
-			local_irq_restore(flags);
 			goto new_slab;
 		} else {
 			/*
@@ -2818,6 +2814,7 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 		 */
 		goto return_single;
 
+	local_irq_save(flags);
 	if (unlikely(c->page))
 		flush_slab(s, c);
 	c->page = page;
@@ -2826,6 +2823,7 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 
 return_single:
 
+	local_irq_save(flags);
 	if (unlikely(c->page))
 		flush_slab(s, c);
 	c->page = page;
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 18/35] mm, slub: stop disabling irqs around get_partial()
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (16 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 17/35] mm, slub: check new pages with restored irqs Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 19/35] mm, slub: move reset of c->page and freelist out of deactivate_slab() Vlastimil Babka
                   ` (19 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

The function get_partial() does not need to have irqs disabled as a whole. It's
sufficient to convert spin_lock operations to their irq saving/restoring
versions.

As a result, it's now possible to reach the page allocator from the slab
allocator without disabling and re-enabling interrupts on the way.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index fe0594b60e93..50a4add8983d 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -1993,11 +1993,12 @@ static inline bool pfmemalloc_match(struct page *page, gfp_t gfpflags);
  * Try to allocate a partial slab from a specific node.
  */
 static void *get_partial_node(struct kmem_cache *s, struct kmem_cache_node *n,
-			      struct page **ret_page, gfp_t flags)
+			      struct page **ret_page, gfp_t gfpflags)
 {
 	struct page *page, *page2;
 	void *object = NULL;
 	unsigned int available = 0;
+	unsigned long flags;
 	int objects;
 
 	/*
@@ -2009,11 +2010,11 @@ static void *get_partial_node(struct kmem_cache *s, struct kmem_cache_node *n,
 	if (!n || !n->nr_partial)
 		return NULL;
 
-	spin_lock(&n->list_lock);
+	spin_lock_irqsave(&n->list_lock, flags);
 	list_for_each_entry_safe(page, page2, &n->partial, slab_list) {
 		void *t;
 
-		if (!pfmemalloc_match(page, flags))
+		if (!pfmemalloc_match(page, gfpflags))
 			continue;
 
 		t = acquire_slab(s, n, page, object == NULL, &objects);
@@ -2034,7 +2035,7 @@ static void *get_partial_node(struct kmem_cache *s, struct kmem_cache_node *n,
 			break;
 
 	}
-	spin_unlock(&n->list_lock);
+	spin_unlock_irqrestore(&n->list_lock, flags);
 	return object;
 }
 
@@ -2749,8 +2750,10 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 			local_irq_restore(flags);
 			goto reread_page;
 		}
-		if (unlikely(!slub_percpu_partial(c)))
+		if (unlikely(!slub_percpu_partial(c))) {
+			local_irq_restore(flags);
 			goto new_objects; /* stolen by an IRQ handler */
+		}
 
 		page = c->page = slub_percpu_partial(c);
 		slub_set_percpu_partial(c, page);
@@ -2759,18 +2762,9 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 		goto redo;
 	}
 
-	local_irq_save(flags);
-	if (unlikely(c->page)) {
-		local_irq_restore(flags);
-		goto reread_page;
-	}
-
 new_objects:
 
-	lockdep_assert_irqs_disabled();
-
 	freelist = get_partial(s, gfpflags, node, &page);
-	local_irq_restore(flags);
 	if (freelist)
 		goto check_new_page;
 
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 19/35] mm, slub: move reset of c->page and freelist out of deactivate_slab()
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (17 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 18/35] mm, slub: stop disabling irqs around get_partial() Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 20/35] mm, slub: make locking in deactivate_slab() irq-safe Vlastimil Babka
                   ` (18 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

deactivate_slab() removes the cpu slab by merging the cpu freelist with slab's
freelist and putting the slab on the proper node's list. It also sets the
respective kmem_cache_cpu pointers to NULL.

By extracting the kmem_cache_cpu operations from the function, we can make it
not dependent on disabled irqs.

Also if we return a single free pointer from ___slab_alloc, we no longer have
to assign kmem_cache_cpu.page before deactivation or care if somebody preempted
us and assigned a different page to our kmem_cache_cpu in the process.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 50a4add8983d..7a8554eb3d96 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2192,10 +2192,13 @@ static void init_kmem_cache_cpus(struct kmem_cache *s)
 }
 
 /*
- * Remove the cpu slab
+ * Finishes removing the cpu slab. Merges cpu's freelist with page's freelist,
+ * unfreezes the slabs and puts it on the proper list.
+ * Assumes the slab has been already safely taken away from kmem_cache_cpu
+ * by the caller.
  */
 static void deactivate_slab(struct kmem_cache *s, struct page *page,
-				void *freelist, struct kmem_cache_cpu *c)
+			    void *freelist)
 {
 	enum slab_modes { M_NONE, M_PARTIAL, M_FULL, M_FREE };
 	struct kmem_cache_node *n = get_node(s, page_to_nid(page));
@@ -2324,9 +2327,6 @@ static void deactivate_slab(struct kmem_cache *s, struct page *page,
 		discard_slab(s, page);
 		stat(s, FREE_SLAB);
 	}
-
-	c->page = NULL;
-	c->freelist = NULL;
 }
 
 /*
@@ -2451,10 +2451,16 @@ static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
 
 static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
 {
-	stat(s, CPUSLAB_FLUSH);
-	deactivate_slab(s, c->page, c->freelist, c);
+	void *freelist = c->freelist;
+	struct page *page = c->page;
 
+	c->page = NULL;
+	c->freelist = NULL;
 	c->tid = next_tid(c->tid);
+
+	deactivate_slab(s, page, freelist);
+
+	stat(s, CPUSLAB_FLUSH);
 }
 
 /*
@@ -2739,7 +2745,10 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 		local_irq_restore(flags);
 		goto reread_page;
 	}
-	deactivate_slab(s, page, c->freelist, c);
+	freelist = c->freelist;
+	c->page = NULL;
+	c->freelist = NULL;
+	deactivate_slab(s, page, freelist);
 	local_irq_restore(flags);
 
 new_slab:
@@ -2818,11 +2827,7 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 return_single:
 
 	local_irq_save(flags);
-	if (unlikely(c->page))
-		flush_slab(s, c);
-	c->page = page;
-
-	deactivate_slab(s, page, get_freepointer(s, freelist), c);
+	deactivate_slab(s, page, get_freepointer(s, freelist));
 	local_irq_restore(flags);
 	return freelist;
 }
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 20/35] mm, slub: make locking in deactivate_slab() irq-safe
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (18 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 19/35] mm, slub: move reset of c->page and freelist out of deactivate_slab() Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 21/35] mm, slub: call deactivate_slab() without disabling irqs Vlastimil Babka
                   ` (17 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

dectivate_slab() now no longer touches the kmem_cache_cpu structure, so it will
be possible to call it with irqs enabled. Just convert the spin_lock calls to
their irq saving/restoring variants to make it irq-safe.

Note we now have to use cmpxchg_double_slab() for irq-safe slab_lock(), because
in some situations we don't take the list_lock, which would disable irqs.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 7a8554eb3d96..3d5a2f9371f8 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2206,6 +2206,7 @@ static void deactivate_slab(struct kmem_cache *s, struct page *page,
 	enum slab_modes l = M_NONE, m = M_NONE;
 	void *nextfree, *freelist_iter, *freelist_tail;
 	int tail = DEACTIVATE_TO_HEAD;
+	unsigned long flags = 0;
 	struct page new;
 	struct page old;
 
@@ -2281,7 +2282,7 @@ static void deactivate_slab(struct kmem_cache *s, struct page *page,
 			 * that acquire_slab() will see a slab page that
 			 * is frozen
 			 */
-			spin_lock(&n->list_lock);
+			spin_lock_irqsave(&n->list_lock, flags);
 		}
 	} else {
 		m = M_FULL;
@@ -2292,7 +2293,7 @@ static void deactivate_slab(struct kmem_cache *s, struct page *page,
 			 * slabs from diagnostic functions will not see
 			 * any frozen slabs.
 			 */
-			spin_lock(&n->list_lock);
+			spin_lock_irqsave(&n->list_lock, flags);
 		}
 	}
 
@@ -2309,14 +2310,14 @@ static void deactivate_slab(struct kmem_cache *s, struct page *page,
 	}
 
 	l = m;
-	if (!__cmpxchg_double_slab(s, page,
+	if (!cmpxchg_double_slab(s, page,
 				old.freelist, old.counters,
 				new.freelist, new.counters,
 				"unfreezing slab"))
 		goto redo;
 
 	if (lock)
-		spin_unlock(&n->list_lock);
+		spin_unlock_irqrestore(&n->list_lock, flags);
 
 	if (m == M_PARTIAL)
 		stat(s, tail);
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 21/35] mm, slub: call deactivate_slab() without disabling irqs
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (19 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 20/35] mm, slub: make locking in deactivate_slab() irq-safe Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 22/35] mm, slub: move irq control into unfreeze_partials() Vlastimil Babka
                   ` (16 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

The function is now safe to be called with irqs enabled, so move the calls
outside of irq disabled sections.

When called from ___slab_alloc() -> flush_slab() we have irqs disabled, so to
reenable them before deactivate_slab() we need to open-code flush_slab() in
___slab_alloc() and reenable irqs after modifying the kmem_cache_cpu fields.
But that means a IRQ handler meanwhile might have assigned a new page to
kmem_cache_cpu.page so we have to retry the whole check.

The remaining callers of flush_slab() are the IPI handler which has disabled
irqs anyway, and slub_cpu_dead() which will be dealt with in the following
patch.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 3d5a2f9371f8..1fdbc2ea8f67 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2749,8 +2749,8 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 	freelist = c->freelist;
 	c->page = NULL;
 	c->freelist = NULL;
-	deactivate_slab(s, page, freelist);
 	local_irq_restore(flags);
+	deactivate_slab(s, page, freelist);
 
 new_slab:
 
@@ -2818,18 +2818,32 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 		 */
 		goto return_single;
 
+retry_load_page:
+
 	local_irq_save(flags);
-	if (unlikely(c->page))
-		flush_slab(s, c);
+	if (unlikely(c->page)) {
+		void *flush_freelist = c->freelist;
+		struct page *flush_page = c->page;
+
+		c->page = NULL;
+		c->freelist = NULL;
+		c->tid = next_tid(c->tid);
+
+		local_irq_restore(flags);
+
+		deactivate_slab(s, flush_page, flush_freelist);
+
+		stat(s, CPUSLAB_FLUSH);
+
+		goto retry_load_page;
+	}
 	c->page = page;
 
 	goto load_freelist;
 
 return_single:
 
-	local_irq_save(flags);
 	deactivate_slab(s, page, get_freepointer(s, freelist));
-	local_irq_restore(flags);
 	return freelist;
 }
 
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 22/35] mm, slub: move irq control into unfreeze_partials()
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (20 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 21/35] mm, slub: call deactivate_slab() without disabling irqs Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 23/35] mm, slub: discard slabs in unfreeze_partials() without irqs disabled Vlastimil Babka
                   ` (15 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

unfreeze_partials() can be optimized so that it doesn't need irqs disabled for
the whole time. As the first step, move irq control into the function and
remove it from the put_cpu_partial() caller.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 1fdbc2ea8f67..0ff103ea73d2 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2333,9 +2333,8 @@ static void deactivate_slab(struct kmem_cache *s, struct page *page,
 /*
  * Unfreeze all the cpu partial slabs.
  *
- * This function must be called with interrupts disabled
- * for the cpu using c (or some other guarantee must be there
- * to guarantee no concurrent accesses).
+ * This function must be called with preemption or migration
+ * disabled with c local to the cpu.
  */
 static void unfreeze_partials(struct kmem_cache *s,
 		struct kmem_cache_cpu *c)
@@ -2343,6 +2342,9 @@ static void unfreeze_partials(struct kmem_cache *s,
 #ifdef CONFIG_SLUB_CPU_PARTIAL
 	struct kmem_cache_node *n = NULL, *n2 = NULL;
 	struct page *page, *discard_page = NULL;
+	unsigned long flags;
+
+	local_irq_save(flags);
 
 	while ((page = slub_percpu_partial(c))) {
 		struct page new;
@@ -2395,6 +2397,8 @@ static void unfreeze_partials(struct kmem_cache *s,
 		discard_slab(s, page);
 		stat(s, FREE_SLAB);
 	}
+
+	local_irq_restore(flags);
 #endif	/* CONFIG_SLUB_CPU_PARTIAL */
 }
 
@@ -2422,14 +2426,11 @@ static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
 			pobjects = oldpage->pobjects;
 			pages = oldpage->pages;
 			if (drain && pobjects > slub_cpu_partial(s)) {
-				unsigned long flags;
 				/*
 				 * partial array is full. Move the existing
 				 * set to the per node partial list.
 				 */
-				local_irq_save(flags);
 				unfreeze_partials(s, this_cpu_ptr(s->cpu_slab));
-				local_irq_restore(flags);
 				oldpage = NULL;
 				pobjects = 0;
 				pages = 0;
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 23/35] mm, slub: discard slabs in unfreeze_partials() without irqs disabled
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (21 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 22/35] mm, slub: move irq control into unfreeze_partials() Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 24/35] mm, slub: detach whole partial list at once in unfreeze_partials() Vlastimil Babka
                   ` (14 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

No need for disabled irqs when discarding slabs, so restore them before
discarding.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/mm/slub.c b/mm/slub.c
index 0ff103ea73d2..1bde537a13b0 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2389,6 +2389,8 @@ static void unfreeze_partials(struct kmem_cache *s,
 	if (n)
 		spin_unlock(&n->list_lock);
 
+	local_irq_restore(flags);
+
 	while (discard_page) {
 		page = discard_page;
 		discard_page = discard_page->next;
@@ -2398,7 +2400,6 @@ static void unfreeze_partials(struct kmem_cache *s,
 		stat(s, FREE_SLAB);
 	}
 
-	local_irq_restore(flags);
 #endif	/* CONFIG_SLUB_CPU_PARTIAL */
 }
 
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 24/35] mm, slub: detach whole partial list at once in unfreeze_partials()
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (22 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 23/35] mm, slub: discard slabs in unfreeze_partials() without irqs disabled Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 25/35] mm, slub: separate detaching of partial list in unfreeze_partials() from unfreezing Vlastimil Babka
                   ` (13 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

Instead of iterating through the live percpu partial list, detach it from the
kmem_cache_cpu at once. This is simpler and will allow further optimization.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 1bde537a13b0..ede93f61651a 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2341,16 +2341,20 @@ static void unfreeze_partials(struct kmem_cache *s,
 {
 #ifdef CONFIG_SLUB_CPU_PARTIAL
 	struct kmem_cache_node *n = NULL, *n2 = NULL;
-	struct page *page, *discard_page = NULL;
+	struct page *page, *partial_page, *discard_page = NULL;
 	unsigned long flags;
 
 	local_irq_save(flags);
 
-	while ((page = slub_percpu_partial(c))) {
+	partial_page = slub_percpu_partial(c);
+	c->partial = NULL;
+
+	while (partial_page) {
 		struct page new;
 		struct page old;
 
-		slub_set_percpu_partial(c, page);
+		page = partial_page;
+		partial_page = page->next;
 
 		n2 = get_node(s, page_to_nid(page));
 		if (n != n2) {
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 25/35] mm, slub: separate detaching of partial list in unfreeze_partials() from unfreezing
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (23 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 24/35] mm, slub: detach whole partial list at once in unfreeze_partials() Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 26/35] mm, slub: only disable irq with spin_lock in __unfreeze_partials() Vlastimil Babka
                   ` (12 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

Unfreezing partial list can be split to two phases - detaching the list from
struct kmem_cache_cpu, and processing the list. The whole operation does not
need to be protected by disabled irqs. Restructure the code to separate the
detaching (with disabled irqs) and unfreezing (with irq disabling to be reduced
in the next patch).

Also, unfreeze_partials() can be called from another cpu on behalf of a cpu
that is being offlined, where disabling irqs on the local cpu has no sense, so
restructure the code as follows:

- __unfreeze_partials() is the bulk of unfreeze_partials() that processes the
  detached percpu partial list
- unfreeze_partials() detaches list from current cpu with irqs disabled and
  calls __unfreeze_partials()
- unfreeze_partials_cpu() is to be called for the offlined cpu so it needs no
  irq disabling, and is called from __flush_cpu_slab()
- flush_cpu_slab() is for the local cpu thus it needs to call
  unfreeze_partials(). So it can't simply call
  __flush_cpu_slab(smp_processor_id()) anymore and we have to open-code the
  proper calls.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 73 ++++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 51 insertions(+), 22 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index ede93f61651a..dba13cd8ca1c 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2330,25 +2330,15 @@ static void deactivate_slab(struct kmem_cache *s, struct page *page,
 	}
 }
 
-/*
- * Unfreeze all the cpu partial slabs.
- *
- * This function must be called with preemption or migration
- * disabled with c local to the cpu.
- */
-static void unfreeze_partials(struct kmem_cache *s,
-		struct kmem_cache_cpu *c)
-{
 #ifdef CONFIG_SLUB_CPU_PARTIAL
+static void __unfreeze_partials(struct kmem_cache *s, struct page *partial_page)
+{
 	struct kmem_cache_node *n = NULL, *n2 = NULL;
-	struct page *page, *partial_page, *discard_page = NULL;
+	struct page *page, *discard_page = NULL;
 	unsigned long flags;
 
 	local_irq_save(flags);
 
-	partial_page = slub_percpu_partial(c);
-	c->partial = NULL;
-
 	while (partial_page) {
 		struct page new;
 		struct page old;
@@ -2403,10 +2393,45 @@ static void unfreeze_partials(struct kmem_cache *s,
 		discard_slab(s, page);
 		stat(s, FREE_SLAB);
 	}
+}
 
-#endif	/* CONFIG_SLUB_CPU_PARTIAL */
+/*
+ * Unfreeze all the cpu partial slabs.
+ */
+static void unfreeze_partials(struct kmem_cache *s)
+{
+	struct page *partial_page;
+	unsigned long flags;
+
+	local_irq_save(flags);
+	partial_page = this_cpu_read(s->cpu_slab->partial);
+	this_cpu_write(s->cpu_slab->partial, NULL);
+	local_irq_restore(flags);
+
+	if (partial_page)
+		__unfreeze_partials(s, partial_page);
 }
 
+static void unfreeze_partials_cpu(struct kmem_cache *s,
+				  struct kmem_cache_cpu *c)
+{
+	struct page *partial_page;
+
+	partial_page = slub_percpu_partial(c);
+	c->partial = NULL;
+
+	if (partial_page)
+		__unfreeze_partials(s, partial_page);
+}
+
+#else	/* CONFIG_SLUB_CPU_PARTIAL */
+
+static inline void unfreeze_partials(struct kmem_cache *s) { }
+static inline void unfreeze_partials_cpu(struct kmem_cache *s,
+				  struct kmem_cache_cpu *c) { }
+
+#endif	/* CONFIG_SLUB_CPU_PARTIAL */
+
 /*
  * Put a page that was just frozen (in __slab_free|get_partial_node) into a
  * partial page slot if available.
@@ -2435,7 +2460,7 @@ static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
 				 * partial array is full. Move the existing
 				 * set to the per node partial list.
 				 */
-				unfreeze_partials(s, this_cpu_ptr(s->cpu_slab));
+				unfreeze_partials(s);
 				oldpage = NULL;
 				pobjects = 0;
 				pages = 0;
@@ -2470,11 +2495,6 @@ static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
 	stat(s, CPUSLAB_FLUSH);
 }
 
-/*
- * Flush cpu slab.
- *
- * Called from IPI handler with interrupts disabled.
- */
 static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
 {
 	struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
@@ -2482,14 +2502,23 @@ static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
 	if (c->page)
 		flush_slab(s, c);
 
-	unfreeze_partials(s, c);
+	unfreeze_partials_cpu(s, c);
 }
 
+/*
+ * Flush cpu slab.
+ *
+ * Called from IPI handler with interrupts disabled.
+ */
 static void flush_cpu_slab(void *d)
 {
 	struct kmem_cache *s = d;
+	struct kmem_cache_cpu *c = this_cpu_ptr(s->cpu_slab);
 
-	__flush_cpu_slab(s, smp_processor_id());
+	if (c->page)
+		flush_slab(s, c);
+
+	unfreeze_partials(s);
 }
 
 static bool has_cpu_slab(int cpu, void *info)
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 26/35] mm, slub: only disable irq with spin_lock in __unfreeze_partials()
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (24 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 25/35] mm, slub: separate detaching of partial list in unfreeze_partials() from unfreezing Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 27/35] mm, slub: don't disable irqs in slub_cpu_dead() Vlastimil Babka
                   ` (11 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

__unfreeze_partials() no longer needs to have irqs disabled, except for making
the spin_lock operations irq-safe, so convert the spin_locks operations and
remove the separate irq handling.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index dba13cd8ca1c..2208be8af7a5 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2335,9 +2335,7 @@ static void __unfreeze_partials(struct kmem_cache *s, struct page *partial_page)
 {
 	struct kmem_cache_node *n = NULL, *n2 = NULL;
 	struct page *page, *discard_page = NULL;
-	unsigned long flags;
-
-	local_irq_save(flags);
+	unsigned long flags = 0;
 
 	while (partial_page) {
 		struct page new;
@@ -2349,10 +2347,10 @@ static void __unfreeze_partials(struct kmem_cache *s, struct page *partial_page)
 		n2 = get_node(s, page_to_nid(page));
 		if (n != n2) {
 			if (n)
-				spin_unlock(&n->list_lock);
+				spin_unlock_irqrestore(&n->list_lock, flags);
 
 			n = n2;
-			spin_lock(&n->list_lock);
+			spin_lock_irqsave(&n->list_lock, flags);
 		}
 
 		do {
@@ -2381,9 +2379,7 @@ static void __unfreeze_partials(struct kmem_cache *s, struct page *partial_page)
 	}
 
 	if (n)
-		spin_unlock(&n->list_lock);
-
-	local_irq_restore(flags);
+		spin_unlock_irqrestore(&n->list_lock, flags);
 
 	while (discard_page) {
 		page = discard_page;
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 27/35] mm, slub: don't disable irqs in slub_cpu_dead()
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (25 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 26/35] mm, slub: only disable irq with spin_lock in __unfreeze_partials() Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 28/35] mm, slab: make flush_slab() possible to call with irqs enabled Vlastimil Babka
                   ` (10 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

slub_cpu_dead() cleans up for an offlined cpu from another cpu and calls only
functions that are now irq safe, so we don't need to disable irqs anymore.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 2208be8af7a5..edda419ec260 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2537,14 +2537,10 @@ static void flush_all(struct kmem_cache *s)
 static int slub_cpu_dead(unsigned int cpu)
 {
 	struct kmem_cache *s;
-	unsigned long flags;
 
 	mutex_lock(&slab_mutex);
-	list_for_each_entry(s, &slab_caches, list) {
-		local_irq_save(flags);
+	list_for_each_entry(s, &slab_caches, list)
 		__flush_cpu_slab(s, cpu);
-		local_irq_restore(flags);
-	}
 	mutex_unlock(&slab_mutex);
 	return 0;
 }
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 28/35] mm, slab: make flush_slab() possible to call with irqs enabled
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (26 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 27/35] mm, slub: don't disable irqs in slub_cpu_dead() Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 29/35] mm: slub: Move flush_cpu_slab() invocations __free_slab() invocations out of IRQ context Vlastimil Babka
                   ` (9 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

Currently flush_slab() is always called with disabled IRQs if it's needed, but
the following patches will change that, so add a parameter to control IRQ
disabling within the function, which only protects the kmem_cache_cpu
manipulation and not the call to deactivate_slab() which doesn't need it.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index edda419ec260..9fbc5396f3e1 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2477,16 +2477,28 @@ static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
 #endif	/* CONFIG_SLUB_CPU_PARTIAL */
 }
 
-static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
+static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c,
+			      bool lock)
 {
-	void *freelist = c->freelist;
-	struct page *page = c->page;
+	unsigned long flags;
+	void *freelist;
+	struct page *page;
+
+	if (lock)
+		local_irq_save(flags);
+
+	freelist = c->freelist;
+	page = c->page;
 
 	c->page = NULL;
 	c->freelist = NULL;
 	c->tid = next_tid(c->tid);
 
-	deactivate_slab(s, page, freelist);
+	if (lock)
+		local_irq_restore(flags);
+
+	if (page)
+		deactivate_slab(s, page, freelist);
 
 	stat(s, CPUSLAB_FLUSH);
 }
@@ -2496,7 +2508,7 @@ static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
 	struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
 
 	if (c->page)
-		flush_slab(s, c);
+		flush_slab(s, c, false);
 
 	unfreeze_partials_cpu(s, c);
 }
@@ -2512,7 +2524,7 @@ static void flush_cpu_slab(void *d)
 	struct kmem_cache_cpu *c = this_cpu_ptr(s->cpu_slab);
 
 	if (c->page)
-		flush_slab(s, c);
+		flush_slab(s, c, false);
 
 	unfreeze_partials(s);
 }
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 29/35] mm: slub: Move flush_cpu_slab() invocations __free_slab() invocations out of IRQ context
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (27 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 28/35] mm, slab: make flush_slab() possible to call with irqs enabled Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 30/35] mm: slub: Make object_map_lock a raw_spinlock_t Vlastimil Babka
                   ` (8 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

From: Sebastian Andrzej Siewior <bigeasy@linutronix.de>

flush_all() flushes a specific SLAB cache on each CPU (where the cache
is present). The deactivate_slab()/__free_slab() invocation happens
within IPI handler and is problematic for PREEMPT_RT.

The flush operation is not a frequent operation or a hot path. The
per-CPU flush operation can be moved to within a workqueue.

[vbabka@suse.cz: adapt to new SLUB changes]
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 48 insertions(+), 8 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 9fbc5396f3e1..dbb74dbe1c1e 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2513,33 +2513,73 @@ static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
 	unfreeze_partials_cpu(s, c);
 }
 
+struct slub_flush_work {
+	struct work_struct work;
+	struct kmem_cache *s;
+	bool skip;
+};
+
 /*
  * Flush cpu slab.
  *
- * Called from IPI handler with interrupts disabled.
+ * Called from CPU work handler with migration disabled.
  */
-static void flush_cpu_slab(void *d)
+static void flush_cpu_slab(struct work_struct *w)
 {
-	struct kmem_cache *s = d;
-	struct kmem_cache_cpu *c = this_cpu_ptr(s->cpu_slab);
+	struct kmem_cache *s;
+	struct kmem_cache_cpu *c;
+	struct slub_flush_work *sfw;
+
+	sfw = container_of(w, struct slub_flush_work, work);
+
+	s = sfw->s;
+	c = this_cpu_ptr(s->cpu_slab);
 
 	if (c->page)
-		flush_slab(s, c, false);
+		flush_slab(s, c, true);
 
 	unfreeze_partials(s);
 }
 
-static bool has_cpu_slab(int cpu, void *info)
+static bool has_cpu_slab(int cpu, struct kmem_cache *s)
 {
-	struct kmem_cache *s = info;
 	struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
 
 	return c->page || slub_percpu_partial(c);
 }
 
+static DEFINE_MUTEX(flush_lock);
+static DEFINE_PER_CPU(struct slub_flush_work, slub_flush);
+
 static void flush_all(struct kmem_cache *s)
 {
-	on_each_cpu_cond(has_cpu_slab, flush_cpu_slab, s, 1);
+	struct slub_flush_work *sfw;
+	unsigned int cpu;
+
+	mutex_lock(&flush_lock);
+	cpus_read_lock();
+
+	for_each_online_cpu(cpu) {
+		sfw = &per_cpu(slub_flush, cpu);
+		if (!has_cpu_slab(cpu, s)) {
+			sfw->skip = true;
+			continue;
+		}
+		INIT_WORK(&sfw->work, flush_cpu_slab);
+		sfw->skip = false;
+		sfw->s = s;
+		schedule_work_on(cpu, &sfw->work);
+	}
+
+	for_each_online_cpu(cpu) {
+		sfw = &per_cpu(slub_flush, cpu);
+		if (sfw->skip)
+			continue;
+		flush_work(&sfw->work);
+	}
+
+	cpus_read_unlock();
+	mutex_unlock(&flush_lock);
 }
 
 /*
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 30/35] mm: slub: Make object_map_lock a raw_spinlock_t
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (28 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 29/35] mm: slub: Move flush_cpu_slab() invocations __free_slab() invocations out of IRQ context Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 31/35] mm, slub: optionally save/restore irqs in slab_[un]lock()/ Vlastimil Babka
                   ` (7 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

From: Sebastian Andrzej Siewior <bigeasy@linutronix.de>

The variable object_map is protected by object_map_lock. The lock is always
acquired in debug code and within already atomic context

Make object_map_lock a raw_spinlock_t.

Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index dbb74dbe1c1e..1ee3ef7a1d3b 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -438,7 +438,7 @@ static inline bool cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
 
 #ifdef CONFIG_SLUB_DEBUG
 static unsigned long object_map[BITS_TO_LONGS(MAX_OBJS_PER_PAGE)];
-static DEFINE_SPINLOCK(object_map_lock);
+static DEFINE_RAW_SPINLOCK(object_map_lock);
 
 static void __fill_map(unsigned long *obj_map, struct kmem_cache *s,
 		       struct page *page)
@@ -483,7 +483,7 @@ static unsigned long *get_map(struct kmem_cache *s, struct page *page)
 {
 	VM_BUG_ON(!irqs_disabled());
 
-	spin_lock(&object_map_lock);
+	raw_spin_lock(&object_map_lock);
 
 	__fill_map(object_map, s, page);
 
@@ -493,7 +493,7 @@ static unsigned long *get_map(struct kmem_cache *s, struct page *page)
 static void put_map(unsigned long *map) __releases(&object_map_lock)
 {
 	VM_BUG_ON(map != object_map);
-	spin_unlock(&object_map_lock);
+	raw_spin_unlock(&object_map_lock);
 }
 
 static inline unsigned int size_from_object(struct kmem_cache *s)
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 31/35] mm, slub: optionally save/restore irqs in slab_[un]lock()/
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (29 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 30/35] mm: slub: Make object_map_lock a raw_spinlock_t Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 15:43   ` Mel Gorman
  2021-07-29 13:21 ` [PATCH v3 32/35] mm, slub: make slab_lock() disable irqs with PREEMPT_RT Vlastimil Babka
                   ` (6 subsequent siblings)
  37 siblings, 1 reply; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

For PREEMPT_RT we will need to disable irqs for this bit spinlock. As a
preparation, add a flags parameter, and an internal version that takes
additional bool parameter to control irq saving/restoring (the flags
parameter is compile-time unused if the bool is a constant false).

Convert ___cmpxchg_double_slab(), which also comes with the same bool
parameter, to use the internal version.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 49 +++++++++++++++++++++++++++++++------------------
 1 file changed, 31 insertions(+), 18 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 1ee3ef7a1d3b..2496e0add6f2 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -359,16 +359,33 @@ static inline unsigned int oo_objects(struct kmem_cache_order_objects x)
 /*
  * Per slab locking using the pagelock
  */
-static __always_inline void slab_lock(struct page *page)
+static __always_inline void
+__slab_lock(struct page *page, unsigned long *flags, bool disable_irqs)
 {
 	VM_BUG_ON_PAGE(PageTail(page), page);
+	if (disable_irqs)
+		local_irq_save(*flags);
 	bit_spin_lock(PG_locked, &page->flags);
 }
 
-static __always_inline void slab_unlock(struct page *page)
+static __always_inline void
+__slab_unlock(struct page *page, unsigned long *flags, bool disable_irqs)
 {
 	VM_BUG_ON_PAGE(PageTail(page), page);
 	__bit_spin_unlock(PG_locked, &page->flags);
+	if (disable_irqs)
+		local_irq_restore(*flags);
+}
+
+static __always_inline void
+slab_lock(struct page *page, unsigned long *flags)
+{
+	__slab_lock(page, flags, false);
+}
+
+static __always_inline void slab_unlock(struct page *page, unsigned long *flags)
+{
+	__slab_unlock(page, flags, false);
 }
 
 static inline bool ___cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
@@ -390,21 +407,15 @@ static inline bool ___cmpxchg_double_slab(struct kmem_cache *s, struct page *pag
 	{
 		unsigned long flags;
 
-		if (disable_irqs)
-			local_irq_save(flags);
-		slab_lock(page);
+		__slab_lock(page, &flags, disable_irqs);
 		if (page->freelist == freelist_old &&
 					page->counters == counters_old) {
 			page->freelist = freelist_new;
 			page->counters = counters_new;
-			slab_unlock(page);
-			if (disable_irqs)
-				local_irq_restore(flags);
+			__slab_unlock(page, &flags, disable_irqs);
 			return true;
 		}
-		slab_unlock(page);
-		if (disable_irqs)
-			local_irq_restore(flags);
+		__slab_unlock(page, &flags, disable_irqs);
 	}
 
 	cpu_relax();
@@ -1255,11 +1266,11 @@ static noinline int free_debug_processing(
 	struct kmem_cache_node *n = get_node(s, page_to_nid(page));
 	void *object = head;
 	int cnt = 0;
-	unsigned long flags;
+	unsigned long flags, flags2;
 	int ret = 0;
 
 	spin_lock_irqsave(&n->list_lock, flags);
-	slab_lock(page);
+	slab_lock(page, &flags2);
 
 	if (s->flags & SLAB_CONSISTENCY_CHECKS) {
 		if (!check_slab(s, page))
@@ -1292,7 +1303,7 @@ static noinline int free_debug_processing(
 		slab_err(s, page, "Bulk freelist count(%d) invalid(%d)\n",
 			 bulk_cnt, cnt);
 
-	slab_unlock(page);
+	slab_unlock(page, &flags2);
 	spin_unlock_irqrestore(&n->list_lock, flags);
 	if (!ret)
 		slab_fix(s, "Object at 0x%p not freed", object);
@@ -4040,9 +4051,10 @@ static void list_slab_objects(struct kmem_cache *s, struct page *page,
 	void *addr = page_address(page);
 	unsigned long *map;
 	void *p;
+	unsigned long flags;
 
 	slab_err(s, page, text, s->name);
-	slab_lock(page);
+	slab_lock(page, &flags);
 
 	map = get_map(s, page);
 	for_each_object(p, s, addr, page->objects) {
@@ -4053,7 +4065,7 @@ static void list_slab_objects(struct kmem_cache *s, struct page *page,
 		}
 	}
 	put_map(map);
-	slab_unlock(page);
+	slab_unlock(page, &flags);
 #endif
 }
 
@@ -4784,8 +4796,9 @@ static void validate_slab(struct kmem_cache *s, struct page *page,
 {
 	void *p;
 	void *addr = page_address(page);
+	unsigned long flags;
 
-	slab_lock(page);
+	slab_lock(page, &flags);
 
 	if (!check_slab(s, page) || !on_freelist(s, page, NULL))
 		goto unlock;
@@ -4800,7 +4813,7 @@ static void validate_slab(struct kmem_cache *s, struct page *page,
 			break;
 	}
 unlock:
-	slab_unlock(page);
+	slab_unlock(page, &flags);
 }
 
 static int validate_slab_node(struct kmem_cache *s,
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 32/35] mm, slub: make slab_lock() disable irqs with PREEMPT_RT
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (30 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 31/35] mm, slub: optionally save/restore irqs in slab_[un]lock()/ Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 33/35] mm, slub: protect put_cpu_partial() with disabled irqs instead of cmpxchg Vlastimil Babka
                   ` (5 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

We need to disable irqs around slab_lock() (a bit spinlock) to make it
irq-safe. The calls to slab_lock() are nested under spin_lock_irqsave() which
doesn't disable irqs on PREEMPT_RT, so add explicit disabling with PREEMPT_RT.

We also distinguish cmpxchg_double_slab() where we do the disabling explicitly
and __cmpxchg_double_slab() for contexts with already disabled irqs.  However
these context are also typically spin_lock_irqsave() thus insufficient on
PREEMPT_RT. Thus, change __cmpxchg_double_slab() to be same as
cmpxchg_double_slab() on PREEMPT_RT.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 2496e0add6f2..4f7218797603 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -380,12 +380,12 @@ __slab_unlock(struct page *page, unsigned long *flags, bool disable_irqs)
 static __always_inline void
 slab_lock(struct page *page, unsigned long *flags)
 {
-	__slab_lock(page, flags, false);
+	__slab_lock(page, flags, IS_ENABLED(CONFIG_PREEMPT_RT));
 }
 
 static __always_inline void slab_unlock(struct page *page, unsigned long *flags)
 {
-	__slab_unlock(page, flags, false);
+	__slab_unlock(page, flags, IS_ENABLED(CONFIG_PREEMPT_RT));
 }
 
 static inline bool ___cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
@@ -428,14 +428,19 @@ static inline bool ___cmpxchg_double_slab(struct kmem_cache *s, struct page *pag
 	return false;
 }
 
-/* Interrupts must be disabled (for the fallback code to work right) */
+/*
+ * Interrupts must be disabled (for the fallback code to work right), typically
+ * by an _irqsave() lock variant. Except on PREEMPT_RT where locks are different
+ * so we disable interrupts explicitly here.
+ */
 static inline bool __cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
 		void *freelist_old, unsigned long counters_old,
 		void *freelist_new, unsigned long counters_new,
 		const char *n)
 {
 	return ___cmpxchg_double_slab(s, page, freelist_old, counters_old,
-				      freelist_new, counters_new, n, false);
+				      freelist_new, counters_new, n,
+				      IS_ENABLED(CONFIG_PREEMPT_RT));
 }
 
 static inline bool cmpxchg_double_slab(struct kmem_cache *s, struct page *page,
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 33/35] mm, slub: protect put_cpu_partial() with disabled irqs instead of cmpxchg
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (31 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 32/35] mm, slub: make slab_lock() disable irqs with PREEMPT_RT Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 15:42   ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 34/35] mm, slub: use migrate_disable() on PREEMPT_RT Vlastimil Babka
                   ` (4 subsequent siblings)
  37 siblings, 1 reply; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

Jann Horn reported [1] the following theoretically possible race:

  task A: put_cpu_partial() calls preempt_disable()
  task A: oldpage = this_cpu_read(s->cpu_slab->partial)
  interrupt: kfree() reaches unfreeze_partials() and discards the page
  task B (on another CPU): reallocates page as page cache
  task A: reads page->pages and page->pobjects, which are actually
  halves of the pointer page->lru.prev
  task B (on another CPU): frees page
  interrupt: allocates page as SLUB page and places it on the percpu partial list
  task A: this_cpu_cmpxchg() succeeds

  which would cause page->pages and page->pobjects to end up containing
  halves of pointers that would then influence when put_cpu_partial()
  happens and show up in root-only sysfs files. Maybe that's acceptable,
  I don't know. But there should probably at least be a comment for now
  to point out that we're reading union fields of a page that might be
  in a completely different state.

Additionally, the this_cpu_cmpxchg() approach in put_cpu_partial() is only safe
against s->cpu_slab->partial manipulation in ___slab_alloc() if the latter
disables irqs, otherwise a __slab_free() in an irq handler could call
put_cpu_partial() in the middle of ___slab_alloc() manipulating ->partial
and corrupt it. This becomes an issue on RT after a local_lock is introduced
in later patch. The fix means taking the local_lock also in put_cpu_partial()
on RT.

After debugging this issue, Mike Galbraith suggested [2] that to avoid
different locking schemes on RT and !RT, we can just protect put_cpu_partial()
with disabled irqs (to be converted to local_lock_irqsave() later) everywhere.
This should be acceptable as it's not a fast path, and moving the actual
partial unfreezing outside of the irq disabled section makes it short, and with
the retry loop gone the code can be also simplified. In addition, the race
reported by Jann should no longer be possible.

[1] https://lore.kernel.org/lkml/CAG48ez1mvUuXwg0YPH5ANzhQLpbphqk-ZS+jbRz+H66fvm4FcA@mail.gmail.com/
[2] https://lore.kernel.org/linux-rt-users/e3470ab357b48bccfbd1f5133b982178a7d2befb.camel@gmx.de/

Reported-by: Jann Horn <jannh@google.com>
Suggested-by: Mike Galbraith <efault@gmx.de>
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 81 ++++++++++++++++++++++++++++++-------------------------
 1 file changed, 44 insertions(+), 37 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 4f7218797603..0fd60d9ca27e 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2002,7 +2002,12 @@ static inline void *acquire_slab(struct kmem_cache *s,
 	return freelist;
 }
 
+#ifdef CONFIG_SLUB_CPU_PARTIAL
 static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain);
+#else
+static inline void put_cpu_partial(struct kmem_cache *s, struct page *page,
+				   int drain) { }
+#endif
 static inline bool pfmemalloc_match(struct page *page, gfp_t gfpflags);
 
 /*
@@ -2436,14 +2441,6 @@ static void unfreeze_partials_cpu(struct kmem_cache *s,
 		__unfreeze_partials(s, partial_page);
 }
 
-#else	/* CONFIG_SLUB_CPU_PARTIAL */
-
-static inline void unfreeze_partials(struct kmem_cache *s) { }
-static inline void unfreeze_partials_cpu(struct kmem_cache *s,
-				  struct kmem_cache_cpu *c) { }
-
-#endif	/* CONFIG_SLUB_CPU_PARTIAL */
-
 /*
  * Put a page that was just frozen (in __slab_free|get_partial_node) into a
  * partial page slot if available.
@@ -2453,46 +2450,56 @@ static inline void unfreeze_partials_cpu(struct kmem_cache *s,
  */
 static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
 {
-#ifdef CONFIG_SLUB_CPU_PARTIAL
 	struct page *oldpage;
-	int pages;
-	int pobjects;
+	struct page *page_to_unfreeze = NULL;
+	unsigned long flags;
+	int pages = 0;
+	int pobjects = 0;
 
-	preempt_disable();
-	do {
-		pages = 0;
-		pobjects = 0;
-		oldpage = this_cpu_read(s->cpu_slab->partial);
+	local_irq_save(flags);
+
+	oldpage = this_cpu_read(s->cpu_slab->partial);
 
-		if (oldpage) {
+	if (oldpage) {
+		if (drain && pobjects > slub_cpu_partial(s)) {
+			/*
+			 * Partial array is full. Move the existing set to the
+			 * per node partial list. Postpone the actual unfreezing
+			 * outside of the critical section.
+			 */
+			page_to_unfreeze = oldpage;
+			oldpage = NULL;
+		} else {
 			pobjects = oldpage->pobjects;
 			pages = oldpage->pages;
-			if (drain && pobjects > slub_cpu_partial(s)) {
-				/*
-				 * partial array is full. Move the existing
-				 * set to the per node partial list.
-				 */
-				unfreeze_partials(s);
-				oldpage = NULL;
-				pobjects = 0;
-				pages = 0;
-				stat(s, CPU_PARTIAL_DRAIN);
-			}
 		}
+	}
 
-		pages++;
-		pobjects += page->objects - page->inuse;
+	pages++;
+	pobjects += page->objects - page->inuse;
 
-		page->pages = pages;
-		page->pobjects = pobjects;
-		page->next = oldpage;
+	page->pages = pages;
+	page->pobjects = pobjects;
+	page->next = oldpage;
 
-	} while (this_cpu_cmpxchg(s->cpu_slab->partial, oldpage, page)
-								!= oldpage);
-	preempt_enable();
-#endif	/* CONFIG_SLUB_CPU_PARTIAL */
+	this_cpu_write(s->cpu_slab->partial, page);
+
+	local_irq_restore(flags);
+
+	if (page_to_unfreeze) {
+		__unfreeze_partials(s, page_to_unfreeze);
+		stat(s, CPU_PARTIAL_DRAIN);
+	}
 }
 
+#else	/* CONFIG_SLUB_CPU_PARTIAL */
+
+static inline void unfreeze_partials(struct kmem_cache *s) { }
+static inline void unfreeze_partials_cpu(struct kmem_cache *s,
+				  struct kmem_cache_cpu *c) { }
+
+#endif	/* CONFIG_SLUB_CPU_PARTIAL */
+
 static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c,
 			      bool lock)
 {
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 34/35] mm, slub: use migrate_disable() on PREEMPT_RT
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (32 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 33/35] mm, slub: protect put_cpu_partial() with disabled irqs instead of cmpxchg Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 13:21 ` [PATCH v3 35/35] mm, slub: convert kmem_cpu_slab protection to local_lock Vlastimil Babka
                   ` (3 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

We currently use preempt_disable() (directly or via get_cpu_ptr()) to stabilize
the pointer to kmem_cache_cpu. On PREEMPT_RT this would be incompatible with
the list_lock spinlock. We can use migrate_disable() instead, but that
increases overhead on !PREEMPT_RT as it's an unconditional function call.

In order to get the best available mechanism on both PREEMPT_RT and
!PREEMPT_RT, introduce private slub_get_cpu_ptr() and slub_put_cpu_ptr()
wrappers and use them.

Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 39 ++++++++++++++++++++++++++++++---------
 1 file changed, 30 insertions(+), 9 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 0fd60d9ca27e..91e04e20cf60 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -118,6 +118,26 @@
  * 			the fast path and disables lockless freelists.
  */
 
+/*
+ * We could simply use migrate_disable()/enable() but as long as it's a
+ * function call even on !PREEMPT_RT, use inline preempt_disable() there.
+ */
+#ifndef CONFIG_PREEMPT_RT
+#define slub_get_cpu_ptr(var)	get_cpu_ptr(var)
+#define slub_put_cpu_ptr(var)	put_cpu_ptr(var)
+#else
+#define slub_get_cpu_ptr(var)		\
+({					\
+	migrate_disable();		\
+	this_cpu_ptr(var);		\
+})
+#define slub_put_cpu_ptr(var)		\
+do {					\
+	(void)(var);			\
+	migrate_enable();		\
+} while (0)
+#endif
+
 #ifdef CONFIG_SLUB_DEBUG
 #ifdef CONFIG_SLUB_DEBUG_ON
 DEFINE_STATIC_KEY_TRUE(slub_debug_enabled);
@@ -2805,7 +2825,7 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 	if (unlikely(!pfmemalloc_match(page, gfpflags)))
 		goto deactivate_slab;
 
-	/* must check again c->page in case IRQ handler changed it */
+	/* must check again c->page in case we got preempted and it changed */
 	local_irq_save(flags);
 	if (unlikely(page != c->page)) {
 		local_irq_restore(flags);
@@ -2864,7 +2884,8 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 		}
 		if (unlikely(!slub_percpu_partial(c))) {
 			local_irq_restore(flags);
-			goto new_objects; /* stolen by an IRQ handler */
+			/* we were preempted and partial list got empty */
+			goto new_objects;
 		}
 
 		page = c->page = slub_percpu_partial(c);
@@ -2880,9 +2901,9 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 	if (freelist)
 		goto check_new_page;
 
-	put_cpu_ptr(s->cpu_slab);
+	slub_put_cpu_ptr(s->cpu_slab);
 	page = new_slab(s, gfpflags, node);
-	c = get_cpu_ptr(s->cpu_slab);
+	c = slub_get_cpu_ptr(s->cpu_slab);
 
 	if (unlikely(!page)) {
 		slab_out_of_memory(s, gfpflags, node);
@@ -2965,12 +2986,12 @@ static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 	 * cpu before disabling preemption. Need to reload cpu area
 	 * pointer.
 	 */
-	c = get_cpu_ptr(s->cpu_slab);
+	c = slub_get_cpu_ptr(s->cpu_slab);
 #endif
 
 	p = ___slab_alloc(s, gfpflags, node, addr, c);
 #ifdef CONFIG_PREEMPT_COUNT
-	put_cpu_ptr(s->cpu_slab);
+	slub_put_cpu_ptr(s->cpu_slab);
 #endif
 	return p;
 }
@@ -3491,7 +3512,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
 	 * IRQs, which protects against PREEMPT and interrupts
 	 * handlers invoking normal fastpath.
 	 */
-	c = get_cpu_ptr(s->cpu_slab);
+	c = slub_get_cpu_ptr(s->cpu_slab);
 	local_irq_disable();
 
 	for (i = 0; i < size; i++) {
@@ -3537,7 +3558,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
 	}
 	c->tid = next_tid(c->tid);
 	local_irq_enable();
-	put_cpu_ptr(s->cpu_slab);
+	slub_put_cpu_ptr(s->cpu_slab);
 
 	/*
 	 * memcg and kmem_cache debug support and memory initialization.
@@ -3547,7 +3568,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
 				slab_want_init_on_alloc(flags, s));
 	return i;
 error:
-	put_cpu_ptr(s->cpu_slab);
+	slub_put_cpu_ptr(s->cpu_slab);
 	slab_post_alloc_hook(s, objcg, flags, i, p, false);
 	__kmem_cache_free_bulk(s, i, p);
 	return 0;
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* [PATCH v3 35/35] mm, slub: convert kmem_cpu_slab protection to local_lock
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (33 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 34/35] mm, slub: use migrate_disable() on PREEMPT_RT Vlastimil Babka
@ 2021-07-29 13:21 ` Vlastimil Babka
  2021-07-29 15:24 ` [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Sebastian Andrzej Siewior
                   ` (2 subsequent siblings)
  37 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 13:21 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn, Vlastimil Babka

Embed local_lock into struct kmem_cpu_slab and use the irq-safe versions of
local_lock instead of plain local_irq_save/restore. On !PREEMPT_RT that's
equivalent, with better lockdep visibility. On PREEMPT_RT that means better
preemption.

However, the cost on PREEMPT_RT is the loss of lockless fast paths which only
work with cpu freelist. Those are designed to detect and recover from being
preempted by other conflicting operations (both fast or slow path), but the
slow path operations assume they cannot be preempted by a fast path operation,
which is guaranteed naturally with disabled irqs. With local locks on
PREEMPT_RT, the fast paths now also need to take the local lock to avoid races.

In the allocation fastpath slab_alloc_node() we can just defer to the slowpath
__slab_alloc() which also works with cpu freelist, but under the local lock.
In the free fastpath do_slab_free() we have to add a new local lock protected
version of freeing to the cpu freelist, as the existing slowpath only works
with the page freelist.

Also update the comment about locking scheme in SLUB to reflect changes done
by this series.

[ Mike Galbraith <efault@gmx.de>: use local_lock() without irq in PREEMPT_RT
  scope; debugging of RT crashes resulting in put_cpu_partial() locking changes ]
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 include/linux/slub_def.h |   2 +
 mm/slub.c                | 146 ++++++++++++++++++++++++++++++---------
 2 files changed, 115 insertions(+), 33 deletions(-)

diff --git a/include/linux/slub_def.h b/include/linux/slub_def.h
index dcde82a4434c..b5bcac29b979 100644
--- a/include/linux/slub_def.h
+++ b/include/linux/slub_def.h
@@ -10,6 +10,7 @@
 #include <linux/kfence.h>
 #include <linux/kobject.h>
 #include <linux/reciprocal_div.h>
+#include <linux/local_lock.h>
 
 enum stat_item {
 	ALLOC_FASTPATH,		/* Allocation from cpu slab */
@@ -41,6 +42,7 @@ enum stat_item {
 	NR_SLUB_STAT_ITEMS };
 
 struct kmem_cache_cpu {
+	local_lock_t lock;	/* Protects the fields below except stat */
 	void **freelist;	/* Pointer to next available object */
 	unsigned long tid;	/* Globally unique transaction id */
 	struct page *page;	/* The slab from which we are allocating */
diff --git a/mm/slub.c b/mm/slub.c
index 91e04e20cf60..695ffaf28c25 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -46,13 +46,21 @@
 /*
  * Lock order:
  *   1. slab_mutex (Global Mutex)
- *   2. node->list_lock
- *   3. slab_lock(page) (Only on some arches and for debugging)
+ *   2. node->list_lock (Spinlock)
+ *   3. kmem_cache->cpu_slab->lock (Local lock)
+ *   4. slab_lock(page) (Only on some arches or for debugging)
+ *   5. object_map_lock (Only for debugging)
  *
  *   slab_mutex
  *
  *   The role of the slab_mutex is to protect the list of all the slabs
  *   and to synchronize major metadata changes to slab cache structures.
+ *   Also synchronizes memory hotplug callbacks.
+ *
+ *   slab_lock
+ *
+ *   The slab_lock is a wrapper around the page lock, thus it is a bit
+ *   spinlock.
  *
  *   The slab_lock is only used for debugging and on arches that do not
  *   have the ability to do a cmpxchg_double. It only protects:
@@ -61,6 +69,8 @@
  *	C. page->objects	-> Number of objects in page
  *	D. page->frozen		-> frozen state
  *
+ *   Frozen slabs
+ *
  *   If a slab is frozen then it is exempt from list management. It is not
  *   on any list except per cpu partial list. The processor that froze the
  *   slab is the one who can perform list operations on the page. Other
@@ -68,6 +78,8 @@
  *   froze the slab is the only one that can retrieve the objects from the
  *   page's freelist.
  *
+ *   list_lock
+ *
  *   The list_lock protects the partial and full list on each node and
  *   the partial slab counter. If taken then no new slabs may be added or
  *   removed from the lists nor make the number of partial slabs be modified.
@@ -79,10 +91,36 @@
  *   slabs, operations can continue without any centralized lock. F.e.
  *   allocating a long series of objects that fill up slabs does not require
  *   the list lock.
- *   Interrupts are disabled during allocation and deallocation in order to
- *   make the slab allocator safe to use in the context of an irq. In addition
- *   interrupts are disabled to ensure that the processor does not change
- *   while handling per_cpu slabs, due to kernel preemption.
+ *
+ *   cpu_slab->lock local lock
+ *
+ *   This locks protect slowpath manipulation of all kmem_cache_cpu fields
+ *   except the stat counters. This is a percpu structure manipulated only by
+ *   the local cpu, so the lock protects against being preempted or interrupted
+ *   by an irq. Fast path operations rely on lockless operations instead.
+ *   On PREEMPT_RT, the local lock does not actually disable irqs (and thus
+ *   prevent the lockless operations), so fastpath operations also need to take
+ *   the lock and are no longer lockless.
+ *
+ *   lockless fastpaths
+ *
+ *   The fast path allocation (slab_alloc_node()) and freeing (do_slab_free())
+ *   are fully lockless when satisfied from the percpu slab (and when
+ *   cmpxchg_double is possible to use, otherwise slab_lock is taken).
+ *   They also don't disable preemption or migration or irqs. They rely on
+ *   the transaction id (tid) field to detect being preempted or moved to
+ *   another cpu.
+ *
+ *   irq, preemption, migration considerations
+ *
+ *   Interrupts are disabled as part of list_lock or local_lock operations, or
+ *   around the slab_lock operation, in order to make the slab allocator safe
+ *   to use in the context of an irq.
+ *
+ *   In addition, preemption (or migration on PREEMPT_RT) is disabled in the
+ *   allocation slowpath, bulk allocation, and put_cpu_partial(), so that the
+ *   local cpu doesn't change in the process and e.g. the kmem_cache_cpu pointer
+ *   doesn't have to be revalidated in each section protected by the local lock.
  *
  * SLUB assigns one slab for allocation to each processor.
  * Allocations only occur from these slabs called cpu slabs.
@@ -2227,9 +2265,13 @@ static inline void note_cmpxchg_failure(const char *n,
 static void init_kmem_cache_cpus(struct kmem_cache *s)
 {
 	int cpu;
+	struct kmem_cache_cpu *c;
 
-	for_each_possible_cpu(cpu)
-		per_cpu_ptr(s->cpu_slab, cpu)->tid = init_tid(cpu);
+	for_each_possible_cpu(cpu) {
+		c = per_cpu_ptr(s->cpu_slab, cpu);
+		local_lock_init(&c->lock);
+		c->tid = init_tid(cpu);
+	}
 }
 
 /*
@@ -2440,10 +2482,10 @@ static void unfreeze_partials(struct kmem_cache *s)
 	struct page *partial_page;
 	unsigned long flags;
 
-	local_irq_save(flags);
+	local_lock_irqsave(&s->cpu_slab->lock, flags);
 	partial_page = this_cpu_read(s->cpu_slab->partial);
 	this_cpu_write(s->cpu_slab->partial, NULL);
-	local_irq_restore(flags);
+	local_unlock_irqrestore(&s->cpu_slab->lock, flags);
 
 	if (partial_page)
 		__unfreeze_partials(s, partial_page);
@@ -2476,7 +2518,7 @@ static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
 	int pages = 0;
 	int pobjects = 0;
 
-	local_irq_save(flags);
+	local_lock_irqsave(&s->cpu_slab->lock, flags);
 
 	oldpage = this_cpu_read(s->cpu_slab->partial);
 
@@ -2504,7 +2546,7 @@ static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
 
 	this_cpu_write(s->cpu_slab->partial, page);
 
-	local_irq_restore(flags);
+	local_unlock_irqrestore(&s->cpu_slab->lock, flags);
 
 	if (page_to_unfreeze) {
 		__unfreeze_partials(s, page_to_unfreeze);
@@ -2528,7 +2570,7 @@ static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c,
 	struct page *page;
 
 	if (lock)
-		local_irq_save(flags);
+		local_lock_irqsave(&s->cpu_slab->lock, flags);
 
 	freelist = c->freelist;
 	page = c->page;
@@ -2538,7 +2580,7 @@ static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c,
 	c->tid = next_tid(c->tid);
 
 	if (lock)
-		local_irq_restore(flags);
+		local_unlock_irqrestore(&s->cpu_slab->lock, flags);
 
 	if (page)
 		deactivate_slab(s, page, freelist);
@@ -2826,9 +2868,9 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 		goto deactivate_slab;
 
 	/* must check again c->page in case we got preempted and it changed */
-	local_irq_save(flags);
+	local_lock_irqsave(&s->cpu_slab->lock, flags);
 	if (unlikely(page != c->page)) {
-		local_irq_restore(flags);
+		local_unlock_irqrestore(&s->cpu_slab->lock, flags);
 		goto reread_page;
 	}
 	freelist = c->freelist;
@@ -2839,7 +2881,7 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 
 	if (!freelist) {
 		c->page = NULL;
-		local_irq_restore(flags);
+		local_unlock_irqrestore(&s->cpu_slab->lock, flags);
 		stat(s, DEACTIVATE_BYPASS);
 		goto new_slab;
 	}
@@ -2848,7 +2890,11 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 
 load_freelist:
 
-	lockdep_assert_irqs_disabled();
+#ifdef CONFIG_PREEMPT_RT
+	lockdep_assert_held(this_cpu_ptr(&s->cpu_slab->lock.lock));
+#else
+	lockdep_assert_held(this_cpu_ptr(&s->cpu_slab->lock));
+#endif
 
 	/*
 	 * freelist is pointing to the list of objects to be used.
@@ -2858,39 +2904,39 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 	VM_BUG_ON(!c->page->frozen);
 	c->freelist = get_freepointer(s, freelist);
 	c->tid = next_tid(c->tid);
-	local_irq_restore(flags);
+	local_unlock_irqrestore(&s->cpu_slab->lock, flags);
 	return freelist;
 
 deactivate_slab:
 
-	local_irq_save(flags);
+	local_lock_irqsave(&s->cpu_slab->lock, flags);
 	if (page != c->page) {
-		local_irq_restore(flags);
+		local_unlock_irqrestore(&s->cpu_slab->lock, flags);
 		goto reread_page;
 	}
 	freelist = c->freelist;
 	c->page = NULL;
 	c->freelist = NULL;
-	local_irq_restore(flags);
+	local_unlock_irqrestore(&s->cpu_slab->lock, flags);
 	deactivate_slab(s, page, freelist);
 
 new_slab:
 
 	if (slub_percpu_partial(c)) {
-		local_irq_save(flags);
+		local_lock_irqsave(&s->cpu_slab->lock, flags);
 		if (unlikely(c->page)) {
-			local_irq_restore(flags);
+			local_unlock_irqrestore(&s->cpu_slab->lock, flags);
 			goto reread_page;
 		}
 		if (unlikely(!slub_percpu_partial(c))) {
-			local_irq_restore(flags);
+			local_unlock_irqrestore(&s->cpu_slab->lock, flags);
 			/* we were preempted and partial list got empty */
 			goto new_objects;
 		}
 
 		page = c->page = slub_percpu_partial(c);
 		slub_set_percpu_partial(c, page);
-		local_irq_restore(flags);
+		local_unlock_irqrestore(&s->cpu_slab->lock, flags);
 		stat(s, CPU_PARTIAL_ALLOC);
 		goto redo;
 	}
@@ -2943,7 +2989,7 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 
 retry_load_page:
 
-	local_irq_save(flags);
+	local_lock_irqsave(&s->cpu_slab->lock, flags);
 	if (unlikely(c->page)) {
 		void *flush_freelist = c->freelist;
 		struct page *flush_page = c->page;
@@ -2952,7 +2998,7 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 		c->freelist = NULL;
 		c->tid = next_tid(c->tid);
 
-		local_irq_restore(flags);
+		local_unlock_irqrestore(&s->cpu_slab->lock, flags);
 
 		deactivate_slab(s, flush_page, flush_freelist);
 
@@ -3071,7 +3117,15 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s,
 
 	object = c->freelist;
 	page = c->page;
-	if (unlikely(!object || !page || !node_match(page, node))) {
+	/*
+	 * We cannot use the lockless fastpath on PREEMPT_RT because if a
+	 * slowpath has taken the local_lock_irqsave(), it is not protected
+	 * against a fast path operation in an irq handler. So we need to take
+	 * the slow path which uses local_lock. It is still relatively fast if
+	 * there is a suitable cpu freelist.
+	 */
+	if (IS_ENABLED(CONFIG_PREEMPT_RT) ||
+	    unlikely(!object || !page || !node_match(page, node))) {
 		object = __slab_alloc(s, gfpflags, node, addr, c);
 	} else {
 		void *next_object = get_freepointer_safe(s, object);
@@ -3331,6 +3385,7 @@ static __always_inline void do_slab_free(struct kmem_cache *s,
 	barrier();
 
 	if (likely(page == c->page)) {
+#ifndef CONFIG_PREEMPT_RT
 		void **freelist = READ_ONCE(c->freelist);
 
 		set_freepointer(s, tail_obj, freelist);
@@ -3343,6 +3398,31 @@ static __always_inline void do_slab_free(struct kmem_cache *s,
 			note_cmpxchg_failure("slab_free", s, tid);
 			goto redo;
 		}
+#else /* CONFIG_PREEMPT_RT */
+		/*
+		 * We cannot use the lockless fastpath on PREEMPT_RT because if
+		 * a slowpath has taken the local_lock_irqsave(), it is not
+		 * protected against a fast path operation in an irq handler. So
+		 * we need to take the local_lock. We shouldn't simply defer to
+		 * __slab_free() as that wouldn't use the cpu freelist at all.
+		 */
+		void **freelist;
+
+		local_lock(&s->cpu_slab->lock);
+		c = this_cpu_ptr(s->cpu_slab);
+		if (unlikely(page != c->page)) {
+			local_unlock(&s->cpu_slab->lock);
+			goto redo;
+		}
+		tid = c->tid;
+		freelist = c->freelist;
+
+		set_freepointer(s, tail_obj, freelist);
+		c->freelist = head;
+		c->tid = next_tid(tid);
+
+		local_unlock(&s->cpu_slab->lock);
+#endif
 		stat(s, FREE_FASTPATH);
 	} else
 		__slab_free(s, page, head, tail_obj, cnt, addr);
@@ -3513,7 +3593,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
 	 * handlers invoking normal fastpath.
 	 */
 	c = slub_get_cpu_ptr(s->cpu_slab);
-	local_irq_disable();
+	local_lock_irq(&s->cpu_slab->lock);
 
 	for (i = 0; i < size; i++) {
 		void *object = kfence_alloc(s, s->object_size, flags);
@@ -3534,7 +3614,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
 			 */
 			c->tid = next_tid(c->tid);
 
-			local_irq_enable();
+			local_unlock_irq(&s->cpu_slab->lock);
 
 			/*
 			 * Invoking slow path likely have side-effect
@@ -3548,7 +3628,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
 			c = this_cpu_ptr(s->cpu_slab);
 			maybe_wipe_obj_freeptr(s, p[i]);
 
-			local_irq_disable();
+			local_lock_irq(&s->cpu_slab->lock);
 
 			continue; /* goto for-loop */
 		}
@@ -3557,7 +3637,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
 		maybe_wipe_obj_freeptr(s, p[i]);
 	}
 	c->tid = next_tid(c->tid);
-	local_irq_enable();
+	local_unlock_irq(&s->cpu_slab->lock);
 	slub_put_cpu_ptr(s->cpu_slab);
 
 	/*
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* Re: [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (34 preceding siblings ...)
  2021-07-29 13:21 ` [PATCH v3 35/35] mm, slub: convert kmem_cpu_slab protection to local_lock Vlastimil Babka
@ 2021-07-29 15:24 ` Sebastian Andrzej Siewior
  2021-07-29 15:27   ` Vlastimil Babka
  2021-07-29 15:47 ` Vlastimil Babka
  2021-08-04 12:05 ` Mel Gorman
  37 siblings, 1 reply; 47+ messages in thread
From: Sebastian Andrzej Siewior @ 2021-07-29 15:24 UTC (permalink / raw)
  To: Vlastimil Babka
  Cc: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim, Mike Galbraith, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn

On 2021-07-29 15:20:57 [+0200], Vlastimil Babka wrote:
> Changes since v2 [5]:

With PARTIAL enabled on top of -rc3:

| root@debpg:~# grep ^kmalloc-512 /proc/slabinfo
| kmalloc-512         3552   3552    512   32    4 : tunables    0    0    0 : slabdata    111    111      0
| root@debpg:~# hackbench -g80
| Running in process mode with 80 groups using 40 file descriptors each (== 3200 tasks)
| Each sender will pass 100 messages of 100 bytes
| Time: 0.643
| root@debpg:~# grep ^kmalloc-512 /proc/slabinfo
| kmalloc-512       954080 954080    512   32    4 : tunables    0    0    0 : slabdata  29815  29815      0
| root@debpg:~# hackbench -g80
| Running in process mode with 80 groups using 40 file descriptors each (== 3200 tasks)
| Each sender will pass 100 messages of 100 bytes
| Time: 0.604
| root@debpg:~# grep ^kmalloc-512 /proc/slabinfo
| kmalloc-512       1647904 1647904    512   32    4 : tunables    0    0    0 : slabdata  51497  51497      0
| root@debpg:~# echo 1 > /sys/kernel/slab/kmalloc-512/shrink 
| root@debpg:~# grep ^kmalloc-512 /proc/slabinfo
| kmalloc-512          640   1120    512   32    4 : tunables    0    0    0 : slabdata     35     35      0

otherwise a few more hackbench invocations without manual shirnk lead to
OOM-killer:
| oom-kill:constraint=CONSTRAINT_NONE,nodemask=(null),cpuset=/,mems_allowed=0,task=systemd-logind,pid=1713,uid=0
| Out of memory: Killed process 1713 (systemd-logind) total-vm:15720kB, anon-rss:956kB, file-rss:0kB, shmem-rss:0kB, UID:0 pgtables:72kB oom_score_adj:0
| Mem-Info:
| active_anon:56 inactive_anon:24782 isolated_anon:0
|  active_file:13 inactive_file:45 isolated_file:0
|  unevictable:0 dirty:0 writeback:0
|  slab_reclaimable:8749 slab_unreclaimable:894017
|  mapped:68 shmem:118 pagetables:28612 bounce:0
|  free:8407 free_pcp:36 free_cma:0
| Node 0 active_anon:224kB inactive_anon:99128kB active_file:260kB inactive_file:712kB unevictable:0kB isolated(anon):0kB isolated(file):0kB mapped:764kB dirty:0kB writebaco
| Node 0 DMA free:15360kB min:28kB low:40kB high:52kB reserved_highatomic:0KB active_anon:0kB inactive_anon:0kB active_file:0kB inactive_file:0kB unevictable:0kB writependiB
| lowmem_reserve[]: 0 1939 3915 3915
| Node 0 DMA32 free:11696kB min:3960kB low:5944kB high:7928kB reserved_highatomic:0KB active_anon:0kB inactive_anon:40740kB active_file:0kB inactive_file:4kB unevictable:0kB
| lowmem_reserve[]: 0 0 1975 1975
| Node 0 Normal free:5692kB min:4032kB low:6052kB high:8072kB reserved_highatomic:0KB active_anon:224kB inactive_anon:58440kB active_file:440kB inactive_file:100kB unevictaB
| lowmem_reserve[]: 0 0 0 0
| Node 0 DMA: 0*4kB 0*8kB 0*16kB 0*32kB 0*64kB 0*128kB 0*256kB 0*512kB 1*1024kB (U) 1*2048kB (M) 3*4096kB (M) = 15360kB
| Node 0 DMA32: 11*4kB (UM) 15*8kB (M) 20*16kB (UME) 12*32kB (UME) 7*64kB (ME) 5*128kB (UME) 4*256kB (UM) 6*512kB (ME) 4*1024kB (M) 1*2048kB (M) 0*4096kB = 12196kB
| Node 0 Normal: 324*4kB (UME) 221*8kB (UME) 60*16kB (UM) 24*32kB (UME) 5*64kB (UM) 2*128kB (U) 0*256kB 0*512kB 0*1024kB 0*2048kB 0*4096kB = 5368kB
| Node 0 hugepages_total=0 hugepages_free=0 hugepages_surp=0 hugepages_size=2048kB
| 189 total pagecache pages
| 0 pages in swap cache
| Swap cache stats: add 0, delete 0, find 0/0
| Free swap  = 0kB
| Total swap = 0kB
| 1048432 pages RAM
| 0 pages HighMem/MovableOnly
| 41108 pages reserved
| Unreclaimable slab info:
…
| kmalloc-512          2144352KB    2144352KB

This does not happen if I disable SLUB_CPU_PARTIAL.

Sebastian

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible
  2021-07-29 15:24 ` [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Sebastian Andrzej Siewior
@ 2021-07-29 15:27   ` Vlastimil Babka
  2021-07-29 15:29     ` Sebastian Andrzej Siewior
  0 siblings, 1 reply; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 15:27 UTC (permalink / raw)
  To: Sebastian Andrzej Siewior
  Cc: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim, Mike Galbraith, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn

On 7/29/21 5:24 PM, Sebastian Andrzej Siewior wrote:
> On 2021-07-29 15:20:57 [+0200], Vlastimil Babka wrote:
>> Changes since v2 [5]:
> 
> With PARTIAL enabled on top of -rc3:

Is that also PREEMPT_RT? Interesting...

> | root@debpg:~# grep ^kmalloc-512 /proc/slabinfo
> | kmalloc-512         3552   3552    512   32    4 : tunables    0    0    0 : slabdata    111    111      0
> | root@debpg:~# hackbench -g80
> | Running in process mode with 80 groups using 40 file descriptors each (== 3200 tasks)
> | Each sender will pass 100 messages of 100 bytes
> | Time: 0.643
> | root@debpg:~# grep ^kmalloc-512 /proc/slabinfo
> | kmalloc-512       954080 954080    512   32    4 : tunables    0    0    0 : slabdata  29815  29815      0
> | root@debpg:~# hackbench -g80
> | Running in process mode with 80 groups using 40 file descriptors each (== 3200 tasks)
> | Each sender will pass 100 messages of 100 bytes
> | Time: 0.604
> | root@debpg:~# grep ^kmalloc-512 /proc/slabinfo
> | kmalloc-512       1647904 1647904    512   32    4 : tunables    0    0    0 : slabdata  51497  51497      0
> | root@debpg:~# echo 1 > /sys/kernel/slab/kmalloc-512/shrink 
> | root@debpg:~# grep ^kmalloc-512 /proc/slabinfo
> | kmalloc-512          640   1120    512   32    4 : tunables    0    0    0 : slabdata     35     35      0
> 
> otherwise a few more hackbench invocations without manual shirnk lead to
> OOM-killer:
> | oom-kill:constraint=CONSTRAINT_NONE,nodemask=(null),cpuset=/,mems_allowed=0,task=systemd-logind,pid=1713,uid=0
> | Out of memory: Killed process 1713 (systemd-logind) total-vm:15720kB, anon-rss:956kB, file-rss:0kB, shmem-rss:0kB, UID:0 pgtables:72kB oom_score_adj:0
> | Mem-Info:
> | active_anon:56 inactive_anon:24782 isolated_anon:0
> |  active_file:13 inactive_file:45 isolated_file:0
> |  unevictable:0 dirty:0 writeback:0
> |  slab_reclaimable:8749 slab_unreclaimable:894017
> |  mapped:68 shmem:118 pagetables:28612 bounce:0
> |  free:8407 free_pcp:36 free_cma:0
> | Node 0 active_anon:224kB inactive_anon:99128kB active_file:260kB inactive_file:712kB unevictable:0kB isolated(anon):0kB isolated(file):0kB mapped:764kB dirty:0kB writebaco
> | Node 0 DMA free:15360kB min:28kB low:40kB high:52kB reserved_highatomic:0KB active_anon:0kB inactive_anon:0kB active_file:0kB inactive_file:0kB unevictable:0kB writependiB
> | lowmem_reserve[]: 0 1939 3915 3915
> | Node 0 DMA32 free:11696kB min:3960kB low:5944kB high:7928kB reserved_highatomic:0KB active_anon:0kB inactive_anon:40740kB active_file:0kB inactive_file:4kB unevictable:0kB
> | lowmem_reserve[]: 0 0 1975 1975
> | Node 0 Normal free:5692kB min:4032kB low:6052kB high:8072kB reserved_highatomic:0KB active_anon:224kB inactive_anon:58440kB active_file:440kB inactive_file:100kB unevictaB
> | lowmem_reserve[]: 0 0 0 0
> | Node 0 DMA: 0*4kB 0*8kB 0*16kB 0*32kB 0*64kB 0*128kB 0*256kB 0*512kB 1*1024kB (U) 1*2048kB (M) 3*4096kB (M) = 15360kB
> | Node 0 DMA32: 11*4kB (UM) 15*8kB (M) 20*16kB (UME) 12*32kB (UME) 7*64kB (ME) 5*128kB (UME) 4*256kB (UM) 6*512kB (ME) 4*1024kB (M) 1*2048kB (M) 0*4096kB = 12196kB
> | Node 0 Normal: 324*4kB (UME) 221*8kB (UME) 60*16kB (UM) 24*32kB (UME) 5*64kB (UM) 2*128kB (U) 0*256kB 0*512kB 0*1024kB 0*2048kB 0*4096kB = 5368kB
> | Node 0 hugepages_total=0 hugepages_free=0 hugepages_surp=0 hugepages_size=2048kB
> | 189 total pagecache pages
> | 0 pages in swap cache
> | Swap cache stats: add 0, delete 0, find 0/0
> | Free swap  = 0kB
> | Total swap = 0kB
> | 1048432 pages RAM
> | 0 pages HighMem/MovableOnly
> | 41108 pages reserved
> | Unreclaimable slab info:
> …
> | kmalloc-512          2144352KB    2144352KB
> 
> This does not happen if I disable SLUB_CPU_PARTIAL.
> 
> Sebastian
> 


^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible
  2021-07-29 15:27   ` Vlastimil Babka
@ 2021-07-29 15:29     ` Sebastian Andrzej Siewior
  2021-07-29 15:30       ` Sebastian Andrzej Siewior
  2021-07-29 15:31       ` Vlastimil Babka
  0 siblings, 2 replies; 47+ messages in thread
From: Sebastian Andrzej Siewior @ 2021-07-29 15:29 UTC (permalink / raw)
  To: Vlastimil Babka
  Cc: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim, Mike Galbraith, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn

On 2021-07-29 17:27:18 [+0200], Vlastimil Babka wrote:
> On 7/29/21 5:24 PM, Sebastian Andrzej Siewior wrote:
> > On 2021-07-29 15:20:57 [+0200], Vlastimil Babka wrote:
> >> Changes since v2 [5]:
> > 
> > With PARTIAL enabled on top of -rc3:
> 
> Is that also PREEMPT_RT? Interesting...

No, plain -rc3.

Sebastian

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible
  2021-07-29 15:29     ` Sebastian Andrzej Siewior
@ 2021-07-29 15:30       ` Sebastian Andrzej Siewior
  2021-07-29 15:31       ` Vlastimil Babka
  1 sibling, 0 replies; 47+ messages in thread
From: Sebastian Andrzej Siewior @ 2021-07-29 15:30 UTC (permalink / raw)
  To: Vlastimil Babka
  Cc: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim, Mike Galbraith, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn

On 2021-07-29 17:29:02 [+0200], To Vlastimil Babka wrote:
> On 2021-07-29 17:27:18 [+0200], Vlastimil Babka wrote:
> > On 7/29/21 5:24 PM, Sebastian Andrzej Siewior wrote:
> > > On 2021-07-29 15:20:57 [+0200], Vlastimil Babka wrote:
> > >> Changes since v2 [5]:
> > > 
> > > With PARTIAL enabled on top of -rc3:
> > 
> > Is that also PREEMPT_RT? Interesting...
> 
> No, plain -rc3.

but it also happens with PREEMPT_RT. Just wanted to make sure that it
happens without RT before I report :)

Sebastian

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible
  2021-07-29 15:29     ` Sebastian Andrzej Siewior
  2021-07-29 15:30       ` Sebastian Andrzej Siewior
@ 2021-07-29 15:31       ` Vlastimil Babka
  1 sibling, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 15:31 UTC (permalink / raw)
  To: Sebastian Andrzej Siewior
  Cc: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim, Mike Galbraith, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn

On 7/29/21 5:29 PM, Sebastian Andrzej Siewior wrote:
> On 2021-07-29 17:27:18 [+0200], Vlastimil Babka wrote:
>> On 7/29/21 5:24 PM, Sebastian Andrzej Siewior wrote:
>> > On 2021-07-29 15:20:57 [+0200], Vlastimil Babka wrote:
>> >> Changes since v2 [5]:
>> > 
>> > With PARTIAL enabled on top of -rc3:
>> 
>> Is that also PREEMPT_RT? Interesting...
> 
> No, plain -rc3.

Thanks, probably screwed up put_cpu_partial() with my cleanups, will check.

> Sebastian
> 


^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH v3 33/35] mm, slub: protect put_cpu_partial() with disabled irqs instead of cmpxchg
  2021-07-29 13:21 ` [PATCH v3 33/35] mm, slub: protect put_cpu_partial() with disabled irqs instead of cmpxchg Vlastimil Babka
@ 2021-07-29 15:42   ` Vlastimil Babka
  0 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 15:42 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn


On 7/29/21 3:21 PM, Vlastimil Babka wrote:
> Reported-by: Jann Horn <jannh@google.com>
> Suggested-by: Mike Galbraith <efault@gmx.de>
> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>

Screwed up with the cleanups resulting in memory leak spotted by Sebastian,
fixed version below, thanks.

----8<----
From 180d8ff44285eadc0f309e98afbb61132db34f1c Mon Sep 17 00:00:00 2001
From: Vlastimil Babka <vbabka@suse.cz>
Date: Wed, 28 Jul 2021 12:26:27 +0200
Subject: [PATCH v3 33/35] mm, slub: protect put_cpu_partial() with disabled
 irqs instead of cmpxchg

Jann Horn reported [1] the following theoretically possible race:

  task A: put_cpu_partial() calls preempt_disable()
  task A: oldpage = this_cpu_read(s->cpu_slab->partial)
  interrupt: kfree() reaches unfreeze_partials() and discards the page
  task B (on another CPU): reallocates page as page cache
  task A: reads page->pages and page->pobjects, which are actually
  halves of the pointer page->lru.prev
  task B (on another CPU): frees page
  interrupt: allocates page as SLUB page and places it on the percpu partial list
  task A: this_cpu_cmpxchg() succeeds

  which would cause page->pages and page->pobjects to end up containing
  halves of pointers that would then influence when put_cpu_partial()
  happens and show up in root-only sysfs files. Maybe that's acceptable,
  I don't know. But there should probably at least be a comment for now
  to point out that we're reading union fields of a page that might be
  in a completely different state.

Additionally, the this_cpu_cmpxchg() approach in put_cpu_partial() is only safe
against s->cpu_slab->partial manipulation in ___slab_alloc() if the latter
disables irqs, otherwise a __slab_free() in an irq handler could call
put_cpu_partial() in the middle of ___slab_alloc() manipulating ->partial
and corrupt it. This becomes an issue on RT after a local_lock is introduced
in later patch. The fix means taking the local_lock also in put_cpu_partial()
on RT.

After debugging this issue, Mike Galbraith suggested [2] that to avoid
different locking schemes on RT and !RT, we can just protect put_cpu_partial()
with disabled irqs (to be converted to local_lock_irqsave() later) everywhere.
This should be acceptable as it's not a fast path, and moving the actual
partial unfreezing outside of the irq disabled section makes it short, and with
the retry loop gone the code can be also simplified. In addition, the race
reported by Jann should no longer be possible.

[1] https://lore.kernel.org/lkml/CAG48ez1mvUuXwg0YPH5ANzhQLpbphqk-ZS+jbRz+H66fvm4FcA@mail.gmail.com/
[2] https://lore.kernel.org/linux-rt-users/e3470ab357b48bccfbd1f5133b982178a7d2befb.camel@gmx.de/

Reported-by: Jann Horn <jannh@google.com>
Suggested-by: Mike Galbraith <efault@gmx.de>
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
---
 mm/slub.c | 81 ++++++++++++++++++++++++++++++-------------------------
 1 file changed, 44 insertions(+), 37 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 4f7218797603..e33a83e07c15 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2002,7 +2002,12 @@ static inline void *acquire_slab(struct kmem_cache *s,
 	return freelist;
 }
 
+#ifdef CONFIG_SLUB_CPU_PARTIAL
 static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain);
+#else
+static inline void put_cpu_partial(struct kmem_cache *s, struct page *page,
+				   int drain) { }
+#endif
 static inline bool pfmemalloc_match(struct page *page, gfp_t gfpflags);
 
 /*
@@ -2436,14 +2441,6 @@ static void unfreeze_partials_cpu(struct kmem_cache *s,
 		__unfreeze_partials(s, partial_page);
 }
 
-#else	/* CONFIG_SLUB_CPU_PARTIAL */
-
-static inline void unfreeze_partials(struct kmem_cache *s) { }
-static inline void unfreeze_partials_cpu(struct kmem_cache *s,
-				  struct kmem_cache_cpu *c) { }
-
-#endif	/* CONFIG_SLUB_CPU_PARTIAL */
-
 /*
  * Put a page that was just frozen (in __slab_free|get_partial_node) into a
  * partial page slot if available.
@@ -2453,46 +2450,56 @@ static inline void unfreeze_partials_cpu(struct kmem_cache *s,
  */
 static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
 {
-#ifdef CONFIG_SLUB_CPU_PARTIAL
 	struct page *oldpage;
-	int pages;
-	int pobjects;
+	struct page *page_to_unfreeze = NULL;
+	unsigned long flags;
+	int pages = 0;
+	int pobjects = 0;
 
-	preempt_disable();
-	do {
-		pages = 0;
-		pobjects = 0;
-		oldpage = this_cpu_read(s->cpu_slab->partial);
+	local_irq_save(flags);
+
+	oldpage = this_cpu_read(s->cpu_slab->partial);
 
-		if (oldpage) {
+	if (oldpage) {
+		if (drain && oldpage->pobjects > slub_cpu_partial(s)) {
+			/*
+			 * Partial array is full. Move the existing set to the
+			 * per node partial list. Postpone the actual unfreezing
+			 * outside of the critical section.
+			 */
+			page_to_unfreeze = oldpage;
+			oldpage = NULL;
+		} else {
 			pobjects = oldpage->pobjects;
 			pages = oldpage->pages;
-			if (drain && pobjects > slub_cpu_partial(s)) {
-				/*
-				 * partial array is full. Move the existing
-				 * set to the per node partial list.
-				 */
-				unfreeze_partials(s);
-				oldpage = NULL;
-				pobjects = 0;
-				pages = 0;
-				stat(s, CPU_PARTIAL_DRAIN);
-			}
 		}
+	}
 
-		pages++;
-		pobjects += page->objects - page->inuse;
+	pages++;
+	pobjects += page->objects - page->inuse;
 
-		page->pages = pages;
-		page->pobjects = pobjects;
-		page->next = oldpage;
+	page->pages = pages;
+	page->pobjects = pobjects;
+	page->next = oldpage;
 
-	} while (this_cpu_cmpxchg(s->cpu_slab->partial, oldpage, page)
-								!= oldpage);
-	preempt_enable();
-#endif	/* CONFIG_SLUB_CPU_PARTIAL */
+	this_cpu_write(s->cpu_slab->partial, page);
+
+	local_irq_restore(flags);
+
+	if (page_to_unfreeze) {
+		__unfreeze_partials(s, page_to_unfreeze);
+		stat(s, CPU_PARTIAL_DRAIN);
+	}
 }
 
+#else	/* CONFIG_SLUB_CPU_PARTIAL */
+
+static inline void unfreeze_partials(struct kmem_cache *s) { }
+static inline void unfreeze_partials_cpu(struct kmem_cache *s,
+				  struct kmem_cache_cpu *c) { }
+
+#endif	/* CONFIG_SLUB_CPU_PARTIAL */
+
 static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c,
 			      bool lock)
 {
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 47+ messages in thread

* Re: [PATCH v3 31/35] mm, slub: optionally save/restore irqs in slab_[un]lock()/
  2021-07-29 13:21 ` [PATCH v3 31/35] mm, slub: optionally save/restore irqs in slab_[un]lock()/ Vlastimil Babka
@ 2021-07-29 15:43   ` Mel Gorman
  0 siblings, 0 replies; 47+ messages in thread
From: Mel Gorman @ 2021-07-29 15:43 UTC (permalink / raw)
  To: Vlastimil Babka
  Cc: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim, Mike Galbraith,
	Sebastian Andrzej Siewior, Thomas Gleixner,
	Jesper Dangaard Brouer, Jann Horn

On Thu, Jul 29, 2021 at 03:21:28PM +0200, Vlastimil Babka wrote:
> For PREEMPT_RT we will need to disable irqs for this bit spinlock. As a
> preparation, add a flags parameter, and an internal version that takes
> additional bool parameter to control irq saving/restoring (the flags
> parameter is compile-time unused if the bool is a constant false).
> 
> Convert ___cmpxchg_double_slab(), which also comes with the same bool
> parameter, to use the internal version.
> 
> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>

On arm64 allmodconfig, the following build warning was new

 In file included from ./include/linux/spinlock.h:54,
                  from ./include/linux/mmzone.h:8,
                  from ./include/linux/gfp.h:6,
                  from ./include/linux/mm.h:10,
                  from mm/slub.c:13:
 mm/slub.c: In function '___cmpxchg_double_slab.isra.0':
 ./include/linux/irqflags.h:177:3: warning: 'flags' may be used uninitialized in this function [-Wmaybe-uninitialized]
   177 |   arch_local_irq_restore(flags);  \
       |   ^~~~~~~~~~~~~~~~~~~~~~
 mm/slub.c:408:17: note: 'flags' was declared here
   408 |   unsigned long flags;
       |                 ^~~~~
 In file included from ./include/linux/string.h:262,
                  from ./include/linux/bitmap.h:10,
                  from ./include/linux/cpumask.h:12,

No idea what's special about arm allmodconfig that confuses the
compiler.

-- 
Mel Gorman
SUSE Labs

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (35 preceding siblings ...)
  2021-07-29 15:24 ` [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Sebastian Andrzej Siewior
@ 2021-07-29 15:47 ` Vlastimil Babka
  2021-07-30  9:19   ` Sebastian Andrzej Siewior
  2021-08-04 12:05 ` Mel Gorman
  37 siblings, 1 reply; 47+ messages in thread
From: Vlastimil Babka @ 2021-07-29 15:47 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim
  Cc: Mike Galbraith, Sebastian Andrzej Siewior, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn

On 7/29/21 3:20 PM, Vlastimil Babka wrote:
> Changes since v2 [5]:
> * Rebase to 5.14-rc3
> * A number of fixes to the RT parts, big thanks to Mike Galbraith for testing
>   and debugging!
>   * The largest fix is to protect kmem_cache_cpu->partial by local_lock instead
>     of cmpxchg tricks, which are insufficient on RT. To avoid divergence
>     between RT and !RT, just do it everywhere. Affected mainly patch 25 and a
>     new patch 33. This also addresses a theoretical race raised earlier by Jann
>     Horn.
> * Smaller fixes reported by Sebastian Andrzej Siewior and Cyrill Gorcunov
> 
> Changes since RFC v1 [1]:
> * Addressed feedback from Christoph and Mel, added their acks.
> * Finished RT conversion, adopting 2 patches from the RT tree.
> * The local_lock conversion has to sacrifice lockless fathpaths on PREEMPT_RT
> * Added some more cleanup patches to the front.
> 
> This series was initially inspired by Mel's pcplist local_lock rewrite, and
> also interest to better understand SLUB's locking and the new primitives and RT
> variants and implications. It should make SLUB more preemption-friendly,
> especially for RT, hopefully without noticeable regressions, as the fast paths
> are not affected.
> 
> Series is based on 5.14-rc3 and also available as a git branch:
> https://git.kernel.org/pub/scm/linux/kernel/git/vbabka/linux.git/log/?h=slub-local-lock-v3r1

Branch with fixed memory leak in patch 33:

https://git.kernel.org/pub/scm/linux/kernel/git/vbabka/linux.git/log/?h=slub-local-lock-v3r2

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible
  2021-07-29 15:47 ` Vlastimil Babka
@ 2021-07-30  9:19   ` Sebastian Andrzej Siewior
  0 siblings, 0 replies; 47+ messages in thread
From: Sebastian Andrzej Siewior @ 2021-07-30  9:19 UTC (permalink / raw)
  To: Vlastimil Babka
  Cc: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim, Mike Galbraith, Thomas Gleixner,
	Mel Gorman, Jesper Dangaard Brouer, Jann Horn

On 2021-07-29 17:47:20 [+0200], Vlastimil Babka wrote:
> Branch with fixed memory leak in patch 33:
> 
> https://git.kernel.org/pub/scm/linux/kernel/git/vbabka/linux.git/log/?h=slub-local-lock-v3r2

This looks stable, nothing happen during the night.

Sebastian

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible
  2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
                   ` (36 preceding siblings ...)
  2021-07-29 15:47 ` Vlastimil Babka
@ 2021-08-04 12:05 ` Mel Gorman
  2021-08-04 12:14   ` Vlastimil Babka
  37 siblings, 1 reply; 47+ messages in thread
From: Mel Gorman @ 2021-08-04 12:05 UTC (permalink / raw)
  To: Vlastimil Babka
  Cc: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim, Mike Galbraith,
	Sebastian Andrzej Siewior, Thomas Gleixner,
	Jesper Dangaard Brouer, Jann Horn

On Thu, Jul 29, 2021 at 03:20:57PM +0200, Vlastimil Babka wrote:
> Changes since v2 [5]:
> * Rebase to 5.14-rc3
> * A number of fixes to the RT parts, big thanks to Mike Galbraith for testing
>   and debugging!
>   * The largest fix is to protect kmem_cache_cpu->partial by local_lock instead
>     of cmpxchg tricks, which are insufficient on RT. To avoid divergence
>     between RT and !RT, just do it everywhere. Affected mainly patch 25 and a
>     new patch 33. This also addresses a theoretical race raised earlier by Jann
>     Horn.
> * Smaller fixes reported by Sebastian Andrzej Siewior and Cyrill Gorcunov
> 
> Changes since RFC v1 [1]:
> * Addressed feedback from Christoph and Mel, added their acks.
> * Finished RT conversion, adopting 2 patches from the RT tree.
> * The local_lock conversion has to sacrifice lockless fathpaths on PREEMPT_RT
> * Added some more cleanup patches to the front.
> 
> This series was initially inspired by Mel's pcplist local_lock rewrite, and
> also interest to better understand SLUB's locking and the new primitives and RT
> variants and implications. It should make SLUB more preemption-friendly,
> especially for RT, hopefully without noticeable regressions, as the fast paths
> are not affected.
> 
> Series is based on 5.14-rc3 and also available as a git branch:
> https://git.kernel.org/pub/scm/linux/kernel/git/vbabka/linux.git/log/?h=slub-local-lock-v3r1
> 

FWIW, I ran a corrected version of this series through a few tests. Some
small gains, no major regressions in terms of performance on a !PREEMPT_RT
configuration across 6 different machines.

-- 
Mel Gorman
SUSE Labs

^ permalink raw reply	[flat|nested] 47+ messages in thread

* Re: [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible
  2021-08-04 12:05 ` Mel Gorman
@ 2021-08-04 12:14   ` Vlastimil Babka
  0 siblings, 0 replies; 47+ messages in thread
From: Vlastimil Babka @ 2021-08-04 12:14 UTC (permalink / raw)
  To: Mel Gorman
  Cc: linux-mm, linux-kernel, Christoph Lameter, David Rientjes,
	Pekka Enberg, Joonsoo Kim, Mike Galbraith,
	Sebastian Andrzej Siewior, Thomas Gleixner,
	Jesper Dangaard Brouer, Jann Horn

On 8/4/21 2:05 PM, Mel Gorman wrote:
> On Thu, Jul 29, 2021 at 03:20:57PM +0200, Vlastimil Babka wrote:
>> Series is based on 5.14-rc3 and also available as a git branch:
>> https://git.kernel.org/pub/scm/linux/kernel/git/vbabka/linux.git/log/?h=slub-local-lock-v3r1
>> 
> 
> FWIW, I ran a corrected version of this series through a few tests. Some
> small gains, no major regressions in terms of performance on a !PREEMPT_RT
> configuration across 6 different machines.

Thanks a lot, Mel!



^ permalink raw reply	[flat|nested] 47+ messages in thread

end of thread, other threads:[~2021-08-04 12:14 UTC | newest]

Thread overview: 47+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-29 13:20 [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Vlastimil Babka
2021-07-29 13:20 ` [PATCH v3 01/35] mm, slub: don't call flush_all() from slab_debug_trace_open() Vlastimil Babka
2021-07-29 13:20 ` [PATCH v3 02/35] mm, slub: allocate private object map for debugfs listings Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 03/35] mm, slub: allocate private object map for validate_slab_cache() Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 04/35] mm, slub: don't disable irq for debug_check_no_locks_freed() Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 05/35] mm, slub: remove redundant unfreeze_partials() from put_cpu_partial() Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 06/35] mm, slub: unify cmpxchg_double_slab() and __cmpxchg_double_slab() Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 07/35] mm, slub: extract get_partial() from new_slab_objects() Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 08/35] mm, slub: dissolve new_slab_objects() into ___slab_alloc() Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 09/35] mm, slub: return slab page from get_partial() and set c->page afterwards Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 10/35] mm, slub: restructure new page checks in ___slab_alloc() Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 11/35] mm, slub: simplify kmem_cache_cpu and tid setup Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 12/35] mm, slub: move disabling/enabling irqs to ___slab_alloc() Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 13/35] mm, slub: do initial checks in ___slab_alloc() with irqs enabled Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 14/35] mm, slub: move disabling irqs closer to get_partial() in ___slab_alloc() Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 15/35] mm, slub: restore irqs around calling new_slab() Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 16/35] mm, slub: validate slab from partial list or page allocator before making it cpu slab Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 17/35] mm, slub: check new pages with restored irqs Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 18/35] mm, slub: stop disabling irqs around get_partial() Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 19/35] mm, slub: move reset of c->page and freelist out of deactivate_slab() Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 20/35] mm, slub: make locking in deactivate_slab() irq-safe Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 21/35] mm, slub: call deactivate_slab() without disabling irqs Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 22/35] mm, slub: move irq control into unfreeze_partials() Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 23/35] mm, slub: discard slabs in unfreeze_partials() without irqs disabled Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 24/35] mm, slub: detach whole partial list at once in unfreeze_partials() Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 25/35] mm, slub: separate detaching of partial list in unfreeze_partials() from unfreezing Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 26/35] mm, slub: only disable irq with spin_lock in __unfreeze_partials() Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 27/35] mm, slub: don't disable irqs in slub_cpu_dead() Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 28/35] mm, slab: make flush_slab() possible to call with irqs enabled Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 29/35] mm: slub: Move flush_cpu_slab() invocations __free_slab() invocations out of IRQ context Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 30/35] mm: slub: Make object_map_lock a raw_spinlock_t Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 31/35] mm, slub: optionally save/restore irqs in slab_[un]lock()/ Vlastimil Babka
2021-07-29 15:43   ` Mel Gorman
2021-07-29 13:21 ` [PATCH v3 32/35] mm, slub: make slab_lock() disable irqs with PREEMPT_RT Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 33/35] mm, slub: protect put_cpu_partial() with disabled irqs instead of cmpxchg Vlastimil Babka
2021-07-29 15:42   ` Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 34/35] mm, slub: use migrate_disable() on PREEMPT_RT Vlastimil Babka
2021-07-29 13:21 ` [PATCH v3 35/35] mm, slub: convert kmem_cpu_slab protection to local_lock Vlastimil Babka
2021-07-29 15:24 ` [PATCH v3 00/35] SLUB: reduce irq disabled scope and make it RT compatible Sebastian Andrzej Siewior
2021-07-29 15:27   ` Vlastimil Babka
2021-07-29 15:29     ` Sebastian Andrzej Siewior
2021-07-29 15:30       ` Sebastian Andrzej Siewior
2021-07-29 15:31       ` Vlastimil Babka
2021-07-29 15:47 ` Vlastimil Babka
2021-07-30  9:19   ` Sebastian Andrzej Siewior
2021-08-04 12:05 ` Mel Gorman
2021-08-04 12:14   ` Vlastimil Babka

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.