Skip to contentSkip to conversation

mm/slub, kunit: Make slub_kunit pass even when SLAB_RED_ZONE flag is set

PATCH In review Hyeonggon Yoo 2 participants 8 messages Mar 16, 2022 – Apr 6, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/slub, kunit: Make slub_kunit pass even when SLAB_RED_ZONE flag is set

Testcase test_next_pointer in slub_kunit fails when SLAB_RED_ZONE flag is globally set. This is because on_freelist() cuts corrupted freelist chain and does not update cut objects' redzone to SLUB_RED_ACTIVE.

When the test validates a slab that whose freelist is cut, it expects redzone of objects unreachable by freelist is set to SLUB_RED_ACTIVE. And it reports "Left Redzone overritten" error because the expectation failed.

This patch makes slub_kunit expect two more errors for reporting and fixing red overwritten error when SLAB_RED_ZONE flag is set.

The test passes on slub_debug and slub_debug=Z after this patch.

---
 lib/slub_kunit.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
lib/slub_kunit.c +17 -2
index 8662dc6cb509..7cf1fb5a7fde 100644
@@ -45,21 +45,36 @@ static void test_next_pointer(struct kunit *test)
 	 * Expecting three errors.
 	 * One for the corrupted freechain and the other one for the wrong
 	 * count of objects in use. The third error is fixing broken cache.
+	 *
+	 * When flag SLUB_RED_ZONE is set, we expect two more errors for reporting
+	 * and fixing overwritten redzone error. This two errors are detected
+	 * because SLUB cuts corrupted freelist in on_freelist(), but does not
+	 * update its redzone to SLUB_RED_ACTIVE.
 	 */
 	validate_slab_cache(s);
-	KUNIT_EXPECT_EQ(test, 3, slab_errors);
+
+	if (s->flags & SLAB_RED_ZONE)
+		KUNIT_EXPECT_EQ(test, 5, slab_errors);
+	else
+		KUNIT_EXPECT_EQ(test, 3, slab_errors);
 
 	/*
 	 * Try to repair corrupted freepointer.
 	 * Still expecting two errors. The first for the wrong count
 	 * of objects in use.
 	 * The second error is for fixing broken cache.
+	 *
+	 * When SLUB_RED_ZONE flag is set, we expect two more errors
+	 * for same reason as above.
 	 */
 	*ptr_addr = tmp;
 	slab_errors = 0;
 
 	validate_slab_cache(s);
-	KUNIT_EXPECT_EQ(test, 2, slab_errors);
+	if (s->flags & SLAB_RED_ZONE)
+		KUNIT_EXPECT_EQ(test, 4, slab_errors);
+	else
+		KUNIT_EXPECT_EQ(test, 2, slab_errors);
 
 	/*
 	 * Previous validation repaired the count of objects in use.
Signature · 1 line
2.33.1
#1 lore raw
Quoted 9 lines · Hyeonggon Yoo

Testcase test_next_pointer in slub_kunit fails when SLAB_RED_ZONE flag is globally set. This is because on_freelist() cuts corrupted freelist chain and does not update cut objects' redzone to SLUB_RED_ACTIVE.

When the test validates a slab that whose freelist is cut, it expects redzone of objects unreachable by freelist is set to SLUB_RED_ACTIVE. And it reports "Left Redzone overritten" error because the expectation failed.

Quoted 4 lines

This patch makes slub_kunit expect two more errors for reporting and fixing red overwritten error when SLAB_RED_ZONE flag is set.

The test passes on slub_debug and slub_debug=Z after this patch.

Hmm I think it's not optimal strategy for unit tests to adapt like this to external influence. It seems rather fragile. The test cases should be designed to test a specific condition and that's it. So maybe we could e.g. introduce a new SLAB_ flag passed to kmem_cache_create that tells it to ignore any globally specified slub debug flags?

Quoted 48 lines
---
 lib/slub_kunit.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
lib/slub_kunit.c +17 -2
index 8662dc6cb509..7cf1fb5a7fde 100644
@@ -45,21 +45,36 @@ static void test_next_pointer(struct kunit *test)
 	 * Expecting three errors.
 	 * One for the corrupted freechain and the other one for the wrong
 	 * count of objects in use. The third error is fixing broken cache.
+	 *
+	 * When flag SLUB_RED_ZONE is set, we expect two more errors for reporting
+	 * and fixing overwritten redzone error. This two errors are detected
+	 * because SLUB cuts corrupted freelist in on_freelist(), but does not
+	 * update its redzone to SLUB_RED_ACTIVE.
 	 */
 	validate_slab_cache(s);
-	KUNIT_EXPECT_EQ(test, 3, slab_errors);
+
+	if (s->flags & SLAB_RED_ZONE)
+		KUNIT_EXPECT_EQ(test, 5, slab_errors);
+	else
+		KUNIT_EXPECT_EQ(test, 3, slab_errors);
 
 	/*
 	 * Try to repair corrupted freepointer.
 	 * Still expecting two errors. The first for the wrong count
 	 * of objects in use.
 	 * The second error is for fixing broken cache.
+	 *
+	 * When SLUB_RED_ZONE flag is set, we expect two more errors
+	 * for same reason as above.
 	 */
 	*ptr_addr = tmp;
 	slab_errors = 0;
 
 	validate_slab_cache(s);
-	KUNIT_EXPECT_EQ(test, 2, slab_errors);
+	if (s->flags & SLAB_RED_ZONE)
+		KUNIT_EXPECT_EQ(test, 4, slab_errors);
+	else
+		KUNIT_EXPECT_EQ(test, 2, slab_errors);
 
 	/*
 	 * Previous validation repaired the count of objects in use.
#2 lore raw
Quoted 13 lines · Vlastimil Babka
Quoted 12 lines · Hyeonggon Yoo

Testcase test_next_pointer in slub_kunit fails when SLAB_RED_ZONE flag is globally set. This is because on_freelist() cuts corrupted freelist chain and does not update cut objects' redzone to SLUB_RED_ACTIVE.

When the test validates a slab that whose freelist is cut, it expects redzone of objects unreachable by freelist is set to SLUB_RED_ACTIVE. And it reports "Left Redzone overritten" error because the expectation failed.

This patch makes slub_kunit expect two more errors for reporting and fixing red overwritten error when SLAB_RED_ZONE flag is set.

Quoted 7 lines
Quoted 1 line

The test passes on slub_debug and slub_debug=Z after this patch.

Hmm I think it's not optimal strategy for unit tests to adapt like this to external influence. It seems rather fragile. The test cases should be designed to test a specific condition and that's it. So maybe we could e.g. introduce a new SLAB_ flag passed to kmem_cache_create that tells it to ignore any globally specified slub debug flags?

Agree. It's so easy to be broken. I think your suggestion is good for unit tests. I'll send a patch.

BTW, The situation is that SLUB makes (by cutting freelist chain) objects invalid (invalid redzone) and reporting what caused by SLUB itself is quite ugly.

No simple solution comes into mind but yeah... it's not a big problem as we rarely call validate_slab_cache().

Quoted 49 lines
Quoted 48 lines
---
 lib/slub_kunit.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
lib/slub_kunit.c +17 -2
index 8662dc6cb509..7cf1fb5a7fde 100644
@@ -45,21 +45,36 @@ static void test_next_pointer(struct kunit *test)
 	 * Expecting three errors.
 	 * One for the corrupted freechain and the other one for the wrong
 	 * count of objects in use. The third error is fixing broken cache.
+	 *
+	 * When flag SLUB_RED_ZONE is set, we expect two more errors for reporting
+	 * and fixing overwritten redzone error. This two errors are detected
+	 * because SLUB cuts corrupted freelist in on_freelist(), but does not
+	 * update its redzone to SLUB_RED_ACTIVE.
 	 */
 	validate_slab_cache(s);
-	KUNIT_EXPECT_EQ(test, 3, slab_errors);
+
+	if (s->flags & SLAB_RED_ZONE)
+		KUNIT_EXPECT_EQ(test, 5, slab_errors);
+	else
+		KUNIT_EXPECT_EQ(test, 3, slab_errors);
 
 	/*
 	 * Try to repair corrupted freepointer.
 	 * Still expecting two errors. The first for the wrong count
 	 * of objects in use.
 	 * The second error is for fixing broken cache.
+	 *
+	 * When SLUB_RED_ZONE flag is set, we expect two more errors
+	 * for same reason as above.
 	 */
 	*ptr_addr = tmp;
 	slab_errors = 0;
 
 	validate_slab_cache(s);
-	KUNIT_EXPECT_EQ(test, 2, slab_errors);
+	if (s->flags & SLAB_RED_ZONE)
+		KUNIT_EXPECT_EQ(test, 4, slab_errors);
+	else
+		KUNIT_EXPECT_EQ(test, 2, slab_errors);
 
 	/*
 	 * Previous validation repaired the count of objects in use.
Signature · 2 lines
Thank you, You are awesome!
Hyeonggon :-)
#3 lore raw

PATCH 1/1 mm/slub, kunit: Make slub_kunit unaffected by global slub debugging flags

In reply to Vlastimil Babka

slub_kunit does not expect other debugging flags to be set when running tests. When SLAB_RED_ZONE flag is set globally, test fails because the flag affects number of errors reported.

To make slub_kunit unaffected by global slub debugging flags, introduce SLAB_NO_GLOBAL_FLAGS to ignore them. It's still allowed to specify debugging flags by specifying cache name(s) in slub_debug parameter.

---
 include/linux/slab.h |  3 +++
 lib/slub_kunit.c     | 10 +++++-----
 mm/slab.h            |  5 +++--
 mm/slub.c            |  3 +++
 4 files changed, 14 insertions(+), 7 deletions(-)
include/linux/slab.h +3 -0
index 0381868e5118..11fe2c28422d 100644
@@ -112,6 +112,9 @@
 #define SLAB_KASAN		0
 #endif
 
+/* Ignore globally specified debugging flags */
+#define SLAB_NO_GLOBAL_FLAGS	((slab_flags_t __force)0x10000000U)
+
 /* The following flags affect the page allocator grouping pages by mobility */
 /* Objects are reclaimable */
 #define SLAB_RECLAIM_ACCOUNT	((slab_flags_t __force)0x00020000U)
lib/slub_kunit.c +5 -5
index 8662dc6cb509..acf061dc558d 100644
@@ -12,7 +12,7 @@ static int slab_errors;
 static void test_clobber_zone(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_RZ_alloc", 64, 0,
-				SLAB_RED_ZONE, NULL);
+				SLAB_RED_ZONE|SLAB_NO_GLOBAL_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 
 	kasan_disable_current();
@@ -30,7 +30,7 @@ static void test_clobber_zone(struct kunit *test)
 static void test_next_pointer(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_next_ptr_free", 64, 0,
-				SLAB_POISON, NULL);
+				SLAB_POISON|SLAB_NO_GLOBAL_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 	unsigned long tmp;
 	unsigned long *ptr_addr;
@@ -75,7 +75,7 @@ static void test_next_pointer(struct kunit *test)
 static void test_first_word(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_1th_word_free", 64, 0,
-				SLAB_POISON, NULL);
+				SLAB_POISON|SLAB_NO_GLOBAL_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 
 	kmem_cache_free(s, p);
@@ -90,7 +90,7 @@ static void test_first_word(struct kunit *test)
 static void test_clobber_50th_byte(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_50th_word_free", 64, 0,
-				SLAB_POISON, NULL);
+				SLAB_POISON|SLAB_NO_GLOBAL_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 
 	kmem_cache_free(s, p);
@@ -106,7 +106,7 @@ static void test_clobber_50th_byte(struct kunit *test)
 static void test_clobber_redzone_free(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_RZ_free", 64, 0,
-				SLAB_RED_ZONE, NULL);
+				SLAB_RED_ZONE|SLAB_NO_GLOBAL_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 
 	kasan_disable_current();
mm/slab.h +3 -2
index c7f2abc2b154..69946131208a 100644
@@ -330,7 +330,7 @@ static inline slab_flags_t kmem_cache_flags(unsigned int object_size,
 			  SLAB_ACCOUNT)
 #elif defined(CONFIG_SLUB)
 #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \
-			  SLAB_TEMPORARY | SLAB_ACCOUNT)
+			  SLAB_TEMPORARY | SLAB_ACCOUNT | SLAB_NO_GLOBAL_FLAGS)
 #else
 #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE)
 #endif
@@ -349,7 +349,8 @@ static inline slab_flags_t kmem_cache_flags(unsigned int object_size,
 			      SLAB_NOLEAKTRACE | \
 			      SLAB_RECLAIM_ACCOUNT | \
 			      SLAB_TEMPORARY | \
-			      SLAB_ACCOUNT)
+			      SLAB_ACCOUNT | \
+			      SLAB_NO_GLOBAL_FLAGS)
 
 bool __kmem_cache_empty(struct kmem_cache *);
 int __kmem_cache_shutdown(struct kmem_cache *);
mm/slub.c +3 -0
index 71e8663f6037..2a3cffd7b27f 100644
@@ -1592,6 +1592,9 @@ slab_flags_t kmem_cache_flags(unsigned int object_size,
 	if (flags & SLAB_NOLEAKTRACE)
 		slub_debug_local &= ~SLAB_STORE_USER;
 
+	if (flags & SLAB_NO_GLOBAL_FLAGS)
+		slub_debug_local = 0;
+
 	len = strlen(name);
 	next_block = slub_debug_string;
 	/* Go through all blocks of debug options, see if any matches our slab's name */
Signature · 1 line
2.33.1
#4 lore raw

Re: [PATCH] mm/slub, kunit: Make slub_kunit unaffected by global slub debugging flags

Quoted 7 lines · Hyeonggon Yoo

slub_kunit does not expect other debugging flags to be set when running tests. When SLAB_RED_ZONE flag is set globally, test fails because the flag affects number of errors reported.

To make slub_kunit unaffected by global slub debugging flags, introduce SLAB_NO_GLOBAL_FLAGS to ignore them. It's still allowed to specify debugging flags by specifying cache name(s) in slub_debug parameter.

Given how we support globbing, I think it would be safest to just ignore everything that comes from slub_debug parameter when the SLAB_NO_GLOBAL_FLAGS flag is specified, even if it involves a (partial) cache name match.
Maybe name it SLAB_NO_USER_FLAGS then?

Quoted 14 lines
---
 include/linux/slab.h |  3 +++
 lib/slub_kunit.c     | 10 +++++-----
 mm/slab.h            |  5 +++--
 mm/slub.c            |  3 +++
 4 files changed, 14 insertions(+), 7 deletions(-)
include/linux/slab.h +0 -0
index 0381868e5118..11fe2c28422d 100644

include/linux/slab.h line 115

Quoted 4 lines
include/linux/slab.h +1 -0
@@ -112,6 +112,9 @@
 #define SLAB_KASAN		0
 #endif
 
+/* Ignore globally specified debugging flags */

I'd add that this is intended for caches created for self-tests so they always have flags as specified in the code.

Quoted 91 lines
include/linux/slab.h +2 -0
+#define SLAB_NO_GLOBAL_FLAGS	((slab_flags_t __force)0x10000000U)
+
 /* The following flags affect the page allocator grouping pages by mobility */
 /* Objects are reclaimable */
 #define SLAB_RECLAIM_ACCOUNT	((slab_flags_t __force)0x00020000U)
lib/slub_kunit.c +5 -5
index 8662dc6cb509..acf061dc558d 100644
@@ -12,7 +12,7 @@ static int slab_errors;
 static void test_clobber_zone(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_RZ_alloc", 64, 0,
-				SLAB_RED_ZONE, NULL);
+				SLAB_RED_ZONE|SLAB_NO_GLOBAL_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 
 	kasan_disable_current();
@@ -30,7 +30,7 @@ static void test_clobber_zone(struct kunit *test)
 static void test_next_pointer(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_next_ptr_free", 64, 0,
-				SLAB_POISON, NULL);
+				SLAB_POISON|SLAB_NO_GLOBAL_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 	unsigned long tmp;
 	unsigned long *ptr_addr;
@@ -75,7 +75,7 @@ static void test_next_pointer(struct kunit *test)
 static void test_first_word(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_1th_word_free", 64, 0,
-				SLAB_POISON, NULL);
+				SLAB_POISON|SLAB_NO_GLOBAL_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 
 	kmem_cache_free(s, p);
@@ -90,7 +90,7 @@ static void test_first_word(struct kunit *test)
 static void test_clobber_50th_byte(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_50th_word_free", 64, 0,
-				SLAB_POISON, NULL);
+				SLAB_POISON|SLAB_NO_GLOBAL_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 
 	kmem_cache_free(s, p);
@@ -106,7 +106,7 @@ static void test_clobber_50th_byte(struct kunit *test)
 static void test_clobber_redzone_free(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_RZ_free", 64, 0,
-				SLAB_RED_ZONE, NULL);
+				SLAB_RED_ZONE|SLAB_NO_GLOBAL_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 
 	kasan_disable_current();
mm/slab.h +3 -2
index c7f2abc2b154..69946131208a 100644
@@ -330,7 +330,7 @@ static inline slab_flags_t kmem_cache_flags(unsigned int object_size,
 			  SLAB_ACCOUNT)
 #elif defined(CONFIG_SLUB)
 #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \
-			  SLAB_TEMPORARY | SLAB_ACCOUNT)
+			  SLAB_TEMPORARY | SLAB_ACCOUNT | SLAB_NO_GLOBAL_FLAGS)
 #else
 #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE)
 #endif
@@ -349,7 +349,8 @@ static inline slab_flags_t kmem_cache_flags(unsigned int object_size,
 			      SLAB_NOLEAKTRACE | \
 			      SLAB_RECLAIM_ACCOUNT | \
 			      SLAB_TEMPORARY | \
-			      SLAB_ACCOUNT)
+			      SLAB_ACCOUNT | \
+			      SLAB_NO_GLOBAL_FLAGS)
 
 bool __kmem_cache_empty(struct kmem_cache *);
 int __kmem_cache_shutdown(struct kmem_cache *);
mm/slub.c +3 -0
index 71e8663f6037..2a3cffd7b27f 100644
@@ -1592,6 +1592,9 @@ slab_flags_t kmem_cache_flags(unsigned int object_size,
 	if (flags & SLAB_NOLEAKTRACE)
 		slub_debug_local &= ~SLAB_STORE_USER;
 
+	if (flags & SLAB_NO_GLOBAL_FLAGS)
+		slub_debug_local = 0;
+
 	len = strlen(name);
 	next_block = slub_debug_string;
 	/* Go through all blocks of debug options, see if any matches our slab's name */
#5 lore raw

PATCH v2 1/1 mm/slub, kunit: Make slub_kunit unaffected by user specified flags

slub_kunit does not expect other debugging flags to be set when running tests. When SLAB_RED_ZONE flag is set globally, test fails because the flag affects number of errors reported.

To make slub_kunit unaffected by user specified debugging flags, introduce SLAB_NO_USER_FLAGS to ignore them. With this flag, only flags specified in the code are used and others are ignored.

---
 include/linux/slab.h |  7 +++++++
 lib/slub_kunit.c     | 10 +++++-----
 mm/slab.h            |  5 +++--
 mm/slub.c            |  3 +++
 4 files changed, 18 insertions(+), 7 deletions(-)
include/linux/slab.h +7 -0
index 373b3ef99f4e..11ceddcae9f4 100644
@@ -112,6 +112,13 @@
 #define SLAB_KASAN		0
 #endif
 
+/*
+ * Ignore user specified debugging flags.
+ * Intended for caches created for self-tests so they have only flags
+ * specified in the code and other flags are ignored.
+ */
+#define SLAB_NO_USER_FLAGS	((slab_flags_t __force)0x10000000U)
+
 /* The following flags affect the page allocator grouping pages by mobility */
 /* Objects are reclaimable */
 #define SLAB_RECLAIM_ACCOUNT	((slab_flags_t __force)0x00020000U)
lib/slub_kunit.c +5 -5
index 8662dc6cb509..7a0564d7cb7a 100644
@@ -12,7 +12,7 @@ static int slab_errors;
 static void test_clobber_zone(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_RZ_alloc", 64, 0,
-				SLAB_RED_ZONE, NULL);
+				SLAB_RED_ZONE|SLAB_NO_USER_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 
 	kasan_disable_current();
@@ -30,7 +30,7 @@ static void test_clobber_zone(struct kunit *test)
 static void test_next_pointer(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_next_ptr_free", 64, 0,
-				SLAB_POISON, NULL);
+				SLAB_POISON|SLAB_NO_USER_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 	unsigned long tmp;
 	unsigned long *ptr_addr;
@@ -75,7 +75,7 @@ static void test_next_pointer(struct kunit *test)
 static void test_first_word(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_1th_word_free", 64, 0,
-				SLAB_POISON, NULL);
+				SLAB_POISON|SLAB_NO_USER_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 
 	kmem_cache_free(s, p);
@@ -90,7 +90,7 @@ static void test_first_word(struct kunit *test)
 static void test_clobber_50th_byte(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_50th_word_free", 64, 0,
-				SLAB_POISON, NULL);
+				SLAB_POISON|SLAB_NO_USER_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 
 	kmem_cache_free(s, p);
@@ -106,7 +106,7 @@ static void test_clobber_50th_byte(struct kunit *test)
 static void test_clobber_redzone_free(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_RZ_free", 64, 0,
-				SLAB_RED_ZONE, NULL);
+				SLAB_RED_ZONE|SLAB_NO_USER_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 
 	kasan_disable_current();
mm/slab.h +3 -2
index fd7ae2024897..f7d018100994 100644
@@ -331,7 +331,7 @@ static inline slab_flags_t kmem_cache_flags(unsigned int object_size,
 			  SLAB_ACCOUNT)
 #elif defined(CONFIG_SLUB)
 #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \
-			  SLAB_TEMPORARY | SLAB_ACCOUNT)
+			  SLAB_TEMPORARY | SLAB_ACCOUNT | SLAB_NO_USER_FLAGS)
 #else
 #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE)
 #endif
@@ -350,7 +350,8 @@ static inline slab_flags_t kmem_cache_flags(unsigned int object_size,
 			      SLAB_NOLEAKTRACE | \
 			      SLAB_RECLAIM_ACCOUNT | \
 			      SLAB_TEMPORARY | \
-			      SLAB_ACCOUNT)
+			      SLAB_ACCOUNT | \
+			      SLAB_NO_USER_FLAGS)
 
 bool __kmem_cache_empty(struct kmem_cache *);
 int __kmem_cache_shutdown(struct kmem_cache *);
mm/slub.c +3 -0
index 74d92aa4a3a2..4c78f5919356 100644
@@ -1584,6 +1584,9 @@ slab_flags_t kmem_cache_flags(unsigned int object_size,
 	slab_flags_t block_flags;
 	slab_flags_t slub_debug_local = slub_debug;
 
+	if (flags & SLAB_NO_USER_FLAGS)
+		return flags;
+
 	/*
 	 * If the slab cache is for debugging (e.g. kmemleak) then
 	 * don't store user (stack trace) information by default,
Signature · 1 line
2.32.0
#6 lore raw

Re: [PATCH v2] mm/slub, kunit: Make slub_kunit unaffected by user specified flags

Quoted 10 lines · Hyeonggon Yoo

slub_kunit does not expect other debugging flags to be set when running tests. When SLAB_RED_ZONE flag is set globally, test fails because the flag affects number of errors reported.

To make slub_kunit unaffected by user specified debugging flags, introduce SLAB_NO_USER_FLAGS to ignore them. With this flag, only flags specified in the code are used and others are ignored.

Thanks, applied.

Quoted 111 lines
---
 include/linux/slab.h |  7 +++++++
 lib/slub_kunit.c     | 10 +++++-----
 mm/slab.h            |  5 +++--
 mm/slub.c            |  3 +++
 4 files changed, 18 insertions(+), 7 deletions(-)
include/linux/slab.h +7 -0
index 373b3ef99f4e..11ceddcae9f4 100644
@@ -112,6 +112,13 @@
 #define SLAB_KASAN		0
 #endif
 
+/*
+ * Ignore user specified debugging flags.
+ * Intended for caches created for self-tests so they have only flags
+ * specified in the code and other flags are ignored.
+ */
+#define SLAB_NO_USER_FLAGS	((slab_flags_t __force)0x10000000U)
+
 /* The following flags affect the page allocator grouping pages by mobility */
 /* Objects are reclaimable */
 #define SLAB_RECLAIM_ACCOUNT	((slab_flags_t __force)0x00020000U)
lib/slub_kunit.c +5 -5
index 8662dc6cb509..7a0564d7cb7a 100644
@@ -12,7 +12,7 @@ static int slab_errors;
 static void test_clobber_zone(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_RZ_alloc", 64, 0,
-				SLAB_RED_ZONE, NULL);
+				SLAB_RED_ZONE|SLAB_NO_USER_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 
 	kasan_disable_current();
@@ -30,7 +30,7 @@ static void test_clobber_zone(struct kunit *test)
 static void test_next_pointer(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_next_ptr_free", 64, 0,
-				SLAB_POISON, NULL);
+				SLAB_POISON|SLAB_NO_USER_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 	unsigned long tmp;
 	unsigned long *ptr_addr;
@@ -75,7 +75,7 @@ static void test_next_pointer(struct kunit *test)
 static void test_first_word(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_1th_word_free", 64, 0,
-				SLAB_POISON, NULL);
+				SLAB_POISON|SLAB_NO_USER_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 
 	kmem_cache_free(s, p);
@@ -90,7 +90,7 @@ static void test_first_word(struct kunit *test)
 static void test_clobber_50th_byte(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_50th_word_free", 64, 0,
-				SLAB_POISON, NULL);
+				SLAB_POISON|SLAB_NO_USER_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 
 	kmem_cache_free(s, p);
@@ -106,7 +106,7 @@ static void test_clobber_50th_byte(struct kunit *test)
 static void test_clobber_redzone_free(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_RZ_free", 64, 0,
-				SLAB_RED_ZONE, NULL);
+				SLAB_RED_ZONE|SLAB_NO_USER_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 
 	kasan_disable_current();
mm/slab.h +3 -2
index fd7ae2024897..f7d018100994 100644
@@ -331,7 +331,7 @@ static inline slab_flags_t kmem_cache_flags(unsigned int object_size,
 			  SLAB_ACCOUNT)
 #elif defined(CONFIG_SLUB)
 #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \
-			  SLAB_TEMPORARY | SLAB_ACCOUNT)
+			  SLAB_TEMPORARY | SLAB_ACCOUNT | SLAB_NO_USER_FLAGS)
 #else
 #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE)
 #endif
@@ -350,7 +350,8 @@ static inline slab_flags_t kmem_cache_flags(unsigned int object_size,
 			      SLAB_NOLEAKTRACE | \
 			      SLAB_RECLAIM_ACCOUNT | \
 			      SLAB_TEMPORARY | \
-			      SLAB_ACCOUNT)
+			      SLAB_ACCOUNT | \
+			      SLAB_NO_USER_FLAGS)
 
 bool __kmem_cache_empty(struct kmem_cache *);
 int __kmem_cache_shutdown(struct kmem_cache *);
mm/slub.c +3 -0
index 74d92aa4a3a2..4c78f5919356 100644
@@ -1584,6 +1584,9 @@ slab_flags_t kmem_cache_flags(unsigned int object_size,
 	slab_flags_t block_flags;
 	slab_flags_t slub_debug_local = slub_debug;
 
+	if (flags & SLAB_NO_USER_FLAGS)
+		return flags;
+
 	/*
 	 * If the slab cache is for debugging (e.g. kmemleak) then
 	 * don't store user (stack trace) information by default,
#7 lore raw

Re: [PATCH] mm/slub, kunit: Make slub_kunit unaffected by global slub debugging flags

In reply to Vlastimil Babka
Quoted 9 lines · Vlastimil Babka
Quoted 7 lines · Hyeonggon Yoo

slub_kunit does not expect other debugging flags to be set when running tests. When SLAB_RED_ZONE flag is set globally, test fails because the flag affects number of errors reported.

To make slub_kunit unaffected by global slub debugging flags, introduce SLAB_NO_GLOBAL_FLAGS to ignore them. It's still allowed to specify debugging flags by specifying cache name(s) in slub_debug parameter.

Quoted 6 lines

Given how we support globbing, I think it would be safest to just ignore everything that comes from slub_debug parameter when the SLAB_NO_GLOBAL_FLAGS flag is specified, even if it involves a (partial) cache name match.
Maybe name it SLAB_NO_USER_FLAGS then?

Seems reasonable. letting your specify debugging flags for self-test cache is not useful for any case.

Let's make self-test caches completely isolated from user-specified flags.

I sent v2 with your comments in mind.

Quoted 17 lines
Quoted 17 lines
---
 include/linux/slab.h |  3 +++
 lib/slub_kunit.c     | 10 +++++-----
 mm/slab.h            |  5 +++--
 mm/slub.c            |  3 +++
 4 files changed, 14 insertions(+), 7 deletions(-)
include/linux/slab.h +0 -0
index 0381868e5118..11fe2c28422d 100644
@@ -112,6 +112,9 @@
 #define SLAB_KASAN		0
 #endif
 
Quoted 5 lines

include/linux/slab.h line 115

Quoted 1 line
include/linux/slab.h +1 -0
+/* Ignore globally specified debugging flags */

I'd add that this is intended for caches created for self-tests so they always have flags as specified in the code.

Did it in v2.

Thanks!
Hyeonggon

Quoted 92 lines
Quoted 91 lines
include/linux/slab.h +2 -0
+#define SLAB_NO_GLOBAL_FLAGS	((slab_flags_t __force)0x10000000U)
+
 /* The following flags affect the page allocator grouping pages by mobility */
 /* Objects are reclaimable */
 #define SLAB_RECLAIM_ACCOUNT	((slab_flags_t __force)0x00020000U)
lib/slub_kunit.c +5 -5
index 8662dc6cb509..acf061dc558d 100644
@@ -12,7 +12,7 @@ static int slab_errors;
 static void test_clobber_zone(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_RZ_alloc", 64, 0,
-				SLAB_RED_ZONE, NULL);
+				SLAB_RED_ZONE|SLAB_NO_GLOBAL_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 
 	kasan_disable_current();
@@ -30,7 +30,7 @@ static void test_clobber_zone(struct kunit *test)
 static void test_next_pointer(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_next_ptr_free", 64, 0,
-				SLAB_POISON, NULL);
+				SLAB_POISON|SLAB_NO_GLOBAL_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 	unsigned long tmp;
 	unsigned long *ptr_addr;
@@ -75,7 +75,7 @@ static void test_next_pointer(struct kunit *test)
 static void test_first_word(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_1th_word_free", 64, 0,
-				SLAB_POISON, NULL);
+				SLAB_POISON|SLAB_NO_GLOBAL_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 
 	kmem_cache_free(s, p);
@@ -90,7 +90,7 @@ static void test_first_word(struct kunit *test)
 static void test_clobber_50th_byte(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_50th_word_free", 64, 0,
-				SLAB_POISON, NULL);
+				SLAB_POISON|SLAB_NO_GLOBAL_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 
 	kmem_cache_free(s, p);
@@ -106,7 +106,7 @@ static void test_clobber_50th_byte(struct kunit *test)
 static void test_clobber_redzone_free(struct kunit *test)
 {
 	struct kmem_cache *s = kmem_cache_create("TestSlub_RZ_free", 64, 0,
-				SLAB_RED_ZONE, NULL);
+				SLAB_RED_ZONE|SLAB_NO_GLOBAL_FLAGS, NULL);
 	u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
 
 	kasan_disable_current();
mm/slab.h +3 -2
index c7f2abc2b154..69946131208a 100644
@@ -330,7 +330,7 @@ static inline slab_flags_t kmem_cache_flags(unsigned int object_size,
 			  SLAB_ACCOUNT)
 #elif defined(CONFIG_SLUB)
 #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \
-			  SLAB_TEMPORARY | SLAB_ACCOUNT)
+			  SLAB_TEMPORARY | SLAB_ACCOUNT | SLAB_NO_GLOBAL_FLAGS)
 #else
 #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE)
 #endif
@@ -349,7 +349,8 @@ static inline slab_flags_t kmem_cache_flags(unsigned int object_size,
 			      SLAB_NOLEAKTRACE | \
 			      SLAB_RECLAIM_ACCOUNT | \
 			      SLAB_TEMPORARY | \
-			      SLAB_ACCOUNT)
+			      SLAB_ACCOUNT | \
+			      SLAB_NO_GLOBAL_FLAGS)
 
 bool __kmem_cache_empty(struct kmem_cache *);
 int __kmem_cache_shutdown(struct kmem_cache *);
mm/slub.c +3 -0
index 71e8663f6037..2a3cffd7b27f 100644
@@ -1592,6 +1592,9 @@ slab_flags_t kmem_cache_flags(unsigned int object_size,
 	if (flags & SLAB_NOLEAKTRACE)
 		slub_debug_local &= ~SLAB_STORE_USER;
 
+	if (flags & SLAB_NO_GLOBAL_FLAGS)
+		slub_debug_local = 0;
+
 	len = strlen(name);
 	next_block = slub_debug_string;
 	/* Go through all blocks of debug options, see if any matches our slab's name */