[PATCH] PREEMPT_RT+SLUB_CPU_PARTIAL fix attempt

From: Vlastimil Babka
Date: Fri Jul 23 2021 - 17:17:18 EST


---
mm/slub.c | 34 +++++++++++++++++++++++++++++++---
1 file changed, 31 insertions(+), 3 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 581004a5aca9..d12a50b5ee6f 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2437,13 +2437,19 @@ static void unfreeze_partials(struct kmem_cache *s)
{
struct page *partial_page;

+#ifndef CONFIG_PREEMPT_RT
do {
partial_page = this_cpu_read(s->cpu_slab->partial);

} while (partial_page &&
this_cpu_cmpxchg(s->cpu_slab->partial, partial_page, NULL)
!= partial_page);
-
+#else
+ local_lock(&s->cpu_slab->lock);
+ partial_page = this_cpu_read(s->cpu_slab->partial);
+ this_cpu_write(s->cpu_slab->partial, NULL);
+ local_unlock(&s->cpu_slab->lock);
+#endif
if (partial_page)
__unfreeze_partials(s, partial_page);
}
@@ -2479,10 +2485,15 @@ static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
{
#ifdef CONFIG_SLUB_CPU_PARTIAL
struct page *oldpage;
+ struct page *page_to_unfreeze = NULL;
int pages;
int pobjects;

- slub_get_cpu_ptr(s->cpu_slab);
+ /*
+ * On !RT we just want to disable preemption, on RT we need the lock
+ * for real. This happens to match local_lock() semantics.
+ */
+ local_lock(&s->cpu_slab->lock);
do {
pages = 0;
pobjects = 0;
@@ -2496,7 +2507,16 @@ 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.
*/
+#ifndef CONFIG_PREEMPT_RT
unfreeze_partials(s);
+#else
+ /*
+ * Postpone unfreezing until we drop the local
+ * lock to avoid relocking.
+ */
+ page_to_unfreeze = oldpage;
+#endif
+
oldpage = NULL;
pobjects = 0;
pages = 0;
@@ -2511,9 +2531,17 @@ static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
page->pobjects = pobjects;
page->next = oldpage;

+#ifndef CONFIG_PREEMPT_RT
} while (this_cpu_cmpxchg(s->cpu_slab->partial, oldpage, page)
!= oldpage);
- slub_put_cpu_ptr(s->cpu_slab);
+#else
+ this_cpu_write(s->cpu_slab->partial, page);
+ } while (false);
+#endif
+
+ local_unlock(&s->cpu_slab->lock);
+ if (page_to_unfreeze)
+ __unfreeze_partials(s, page_to_unfreeze);
#endif /* CONFIG_SLUB_CPU_PARTIAL */
}

--
2.32.0