[RFC PATCH 1/7] mm/slab: refactor freelist shuffle

Harry Yoo posted 7 patches 9 months, 3 weeks ago
[RFC PATCH 1/7] mm/slab: refactor freelist shuffle
Posted by Harry Yoo 9 months, 3 weeks ago
shuffle_freelist() function returns false when 1) it can't shuffle
the freelist due to lack of a random sequence or,
2) when there is a single object.

In a later patch, I'd like to return an error in shuffle_freelist() when
setup_object() fails. But with current code, it'll be hard to determine
whether it should initialize the objects anyway or let allocate_slab()
return an error.

To address this, decouple the shuffle eligibility checks into
should_shuffle_freelist() function and call it before shuffle_freelist().
Change the return type of shuffle_freelist() to void for now.

Signed-off-by: Harry Yoo <harry.yoo@oracle.com>
---
 mm/slub.c | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index dac149df1be1..95a9f04b5904 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2534,17 +2534,21 @@ static void *next_freelist_entry(struct kmem_cache *s,
 	return (char *)start + idx;
 }
 
+static bool should_shuffle_freelist(struct kmem_cache *s, struct slab *slab)
+{
+	if (slab->objects < 2 || !s->random_seq)
+		return false;
+	return true;
+}
+
 /* Shuffle the single linked freelist based on a random pre-computed sequence */
-static bool shuffle_freelist(struct kmem_cache *s, struct slab *slab)
+static void shuffle_freelist(struct kmem_cache *s, struct slab *slab)
 {
 	void *start;
 	void *cur;
 	void *next;
 	unsigned long idx, pos, page_limit, freelist_count;
 
-	if (slab->objects < 2 || !s->random_seq)
-		return false;
-
 	freelist_count = oo_objects(s->oo);
 	pos = get_random_u32_below(freelist_count);
 
@@ -2564,8 +2568,6 @@ static bool shuffle_freelist(struct kmem_cache *s, struct slab *slab)
 		cur = next;
 	}
 	set_freepointer(s, cur, NULL);
-
-	return true;
 }
 #else
 static inline int init_cache_random_seq(struct kmem_cache *s)
@@ -2573,10 +2575,13 @@ static inline int init_cache_random_seq(struct kmem_cache *s)
 	return 0;
 }
 static inline void init_freelist_randomization(void) { }
-static inline bool shuffle_freelist(struct kmem_cache *s, struct slab *slab)
+static inline bool should_shuffle_freelist(struct kmem_cache *s,
+					   struct slab *slab)
 {
 	return false;
 }
+static inline void shuffle_freelist(struct kmem_cache *s, struct slab *slab)
+{ }
 #endif /* CONFIG_SLAB_FREELIST_RANDOM */
 
 static __always_inline void account_slab(struct slab *slab, int order,
@@ -2606,7 +2611,6 @@ static struct slab *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
 	gfp_t alloc_gfp;
 	void *start, *p, *next;
 	int idx;
-	bool shuffle;
 
 	flags &= gfp_allowed_mask;
 
@@ -2648,9 +2652,9 @@ static struct slab *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
 
 	setup_slab_debug(s, slab, start);
 
-	shuffle = shuffle_freelist(s, slab);
-
-	if (!shuffle) {
+	if (should_shuffle_freelist(s, slab)) {
+		shuffle_freelist(s, slab);
+	} else {
 		start = fixup_red_left(s, start);
 		start = setup_object(s, start);
 		slab->freelist = start;
-- 
2.43.0