Skip to content

Commit a285909

Browse files
hygonitehcaster
authored andcommitted
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. Suggested-by: Vlastimil Babka <[email protected]> Signed-off-by: Hyeonggon Yoo <[email protected]> Signed-off-by: Vlastimil Babka <[email protected]> Link: https://lore.kernel.org/r/Yk0sY9yoJhFEXWOg@hyeyoo
1 parent 1e703d0 commit a285909

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

include/linux/slab.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@
112112
#define SLAB_KASAN 0
113113
#endif
114114

115+
/*
116+
* Ignore user specified debugging flags.
117+
* Intended for caches created for self-tests so they have only flags
118+
* specified in the code and other flags are ignored.
119+
*/
120+
#define SLAB_NO_USER_FLAGS ((slab_flags_t __force)0x10000000U)
121+
115122
/* The following flags affect the page allocator grouping pages by mobility */
116123
/* Objects are reclaimable */
117124
#define SLAB_RECLAIM_ACCOUNT ((slab_flags_t __force)0x00020000U)

lib/slub_kunit.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ static int slab_errors;
1212
static void test_clobber_zone(struct kunit *test)
1313
{
1414
struct kmem_cache *s = kmem_cache_create("TestSlub_RZ_alloc", 64, 0,
15-
SLAB_RED_ZONE, NULL);
15+
SLAB_RED_ZONE|SLAB_NO_USER_FLAGS, NULL);
1616
u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
1717

1818
kasan_disable_current();
@@ -30,7 +30,7 @@ static void test_clobber_zone(struct kunit *test)
3030
static void test_next_pointer(struct kunit *test)
3131
{
3232
struct kmem_cache *s = kmem_cache_create("TestSlub_next_ptr_free", 64, 0,
33-
SLAB_POISON, NULL);
33+
SLAB_POISON|SLAB_NO_USER_FLAGS, NULL);
3434
u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
3535
unsigned long tmp;
3636
unsigned long *ptr_addr;
@@ -75,7 +75,7 @@ static void test_next_pointer(struct kunit *test)
7575
static void test_first_word(struct kunit *test)
7676
{
7777
struct kmem_cache *s = kmem_cache_create("TestSlub_1th_word_free", 64, 0,
78-
SLAB_POISON, NULL);
78+
SLAB_POISON|SLAB_NO_USER_FLAGS, NULL);
7979
u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
8080

8181
kmem_cache_free(s, p);
@@ -90,7 +90,7 @@ static void test_first_word(struct kunit *test)
9090
static void test_clobber_50th_byte(struct kunit *test)
9191
{
9292
struct kmem_cache *s = kmem_cache_create("TestSlub_50th_word_free", 64, 0,
93-
SLAB_POISON, NULL);
93+
SLAB_POISON|SLAB_NO_USER_FLAGS, NULL);
9494
u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
9595

9696
kmem_cache_free(s, p);
@@ -106,7 +106,7 @@ static void test_clobber_50th_byte(struct kunit *test)
106106
static void test_clobber_redzone_free(struct kunit *test)
107107
{
108108
struct kmem_cache *s = kmem_cache_create("TestSlub_RZ_free", 64, 0,
109-
SLAB_RED_ZONE, NULL);
109+
SLAB_RED_ZONE|SLAB_NO_USER_FLAGS, NULL);
110110
u8 *p = kmem_cache_alloc(s, GFP_KERNEL);
111111

112112
kasan_disable_current();

mm/slab.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ static inline slab_flags_t kmem_cache_flags(unsigned int object_size,
331331
SLAB_ACCOUNT)
332332
#elif defined(CONFIG_SLUB)
333333
#define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \
334-
SLAB_TEMPORARY | SLAB_ACCOUNT)
334+
SLAB_TEMPORARY | SLAB_ACCOUNT | SLAB_NO_USER_FLAGS)
335335
#else
336336
#define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE)
337337
#endif
@@ -350,7 +350,8 @@ static inline slab_flags_t kmem_cache_flags(unsigned int object_size,
350350
SLAB_NOLEAKTRACE | \
351351
SLAB_RECLAIM_ACCOUNT | \
352352
SLAB_TEMPORARY | \
353-
SLAB_ACCOUNT)
353+
SLAB_ACCOUNT | \
354+
SLAB_NO_USER_FLAGS)
354355

355356
bool __kmem_cache_empty(struct kmem_cache *);
356357
int __kmem_cache_shutdown(struct kmem_cache *);

mm/slub.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,9 @@ slab_flags_t kmem_cache_flags(unsigned int object_size,
15841584
slab_flags_t block_flags;
15851585
slab_flags_t slub_debug_local = slub_debug;
15861586

1587+
if (flags & SLAB_NO_USER_FLAGS)
1588+
return flags;
1589+
15871590
/*
15881591
* If the slab cache is for debugging (e.g. kmemleak) then
15891592
* don't store user (stack trace) information by default,

0 commit comments

Comments
 (0)