mm/slub, kunit: Make slub_kunit pass even when SLAB_RED_ZONE flag is set
Subsystems (3)
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
@@ -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
Quoted 9 lines · Hyeonggon Yoo
Testcase
test_next_pointerinslub_kunitfails whenSLAB_RED_ZONEflag is globally set. This is becauseon_freelist()cuts corrupted freelist chain and does not update cut objects' redzone toSLUB_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_kunitexpect two more errors for reporting and fixing red overwritten error whenSLAB_RED_ZONEflag is set.The test passes on
slub_debugand 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
Signed-off-by Hyeonggon Yoo <[email protected]>--- lib/slub_kunit.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)lib/slub_kunit.c +17
-2@@ -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.
Quoted 13 lines · Vlastimil Babka
Quoted 12 lines · Hyeonggon Yoo
Testcase
test_next_pointerinslub_kunitfails whenSLAB_RED_ZONEflag is globally set. This is becauseon_freelist()cuts corrupted freelist chain and does not update cut objects' redzone toSLUB_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_kunitexpect two more errors for reporting and fixing red overwritten error whenSLAB_RED_ZONEflag is set.
Quoted 7 lines
Quoted 1 line
The test passes on
slub_debugand 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 tokmem_cache_createthat 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
Signed-off-by Hyeonggon Yoo <[email protected]>--- lib/slub_kunit.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)lib/slub_kunit.c +17
-2@@ -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 :-)
PATCH 1/1 mm/slub, kunit: Make slub_kunit unaffected by global slub debugging flags
In reply to Vlastimil Babkaslub_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
@@ -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
@@ -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
@@ -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
@@ -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
Re: [PATCH] mm/slub, kunit: Make slub_kunit unaffected by global slub debugging flags
Quoted 7 lines · Hyeonggon Yoo
slub_kunitdoes not expect other debugging flags to be set when running tests. WhenSLAB_RED_ZONEflag is set globally, test fails because the flag affects number of errors reported.To make
slub_kunitunaffected by global slub debugging flags, introduceSLAB_NO_GLOBAL_FLAGSto ignore them. It's still allowed to specify debugging flags by specifying cache name(s) inslub_debugparameter.
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
Suggested-by Vlastimil Babka <[email protected]>Signed-off-by Hyeonggon Yoo <[email protected]>--- 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
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@@ -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@@ -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@@ -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 */
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
@@ -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
@@ -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
@@ -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
@@ -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
Vlastimil Babka
AppliedRe: [PATCH v2] mm/slub, kunit: Make slub_kunit unaffected by user specified flags
Quoted 10 lines · Hyeonggon Yoo
slub_kunitdoes not expect other debugging flags to be set when running tests. WhenSLAB_RED_ZONEflag is set globally, test fails because the flag affects number of errors reported.To make
slub_kunitunaffected by user specified debugging flags, introduceSLAB_NO_USER_FLAGSto ignore them. With this flag, only flags specified in the code are used and others are ignored.Suggested-by Vlastimil Babka <[email protected]>Signed-off-by Hyeonggon Yoo <[email protected]>
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@@ -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@@ -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@@ -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@@ -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,
Re: [PATCH] mm/slub, kunit: Make slub_kunit unaffected by global slub debugging flags
In reply to Vlastimil BabkaQuoted 9 lines · Vlastimil Babka
Quoted 7 lines · Hyeonggon Yoo
slub_kunitdoes not expect other debugging flags to be set when running tests. WhenSLAB_RED_ZONEflag is set globally, test fails because the flag affects number of errors reported.To make
slub_kunitunaffected by global slub debugging flags, introduceSLAB_NO_GLOBAL_FLAGSto ignore them. It's still allowed to specify debugging flags by specifying cache name(s) inslub_debugparameter.
Quoted 6 lines
Given how we support globbing, I think it would be safest to just ignore everything that comes from
slub_debugparameter when theSLAB_NO_GLOBAL_FLAGSflag is specified, even if it involves a (partial) cache name match.
Maybe name itSLAB_NO_USER_FLAGSthen?
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
Suggested-by Vlastimil Babka <[email protected]>Signed-off-by Hyeonggon Yoo <[email protected]>--- 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@@ -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@@ -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@@ -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@@ -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 */