[PATCH 1/2] mm: printk: introduce new format %pGs for slab flags

From: Sukrit Bhatnagar
Date: Wed May 22 2024 - 03:53:08 EST


The slab pages have their own flags (apart from PG_slab set in struct page),
kept in the kmem_cache's flag field. These flags are visible to the users of
slab cache and are needed when creating one. It will be useful to be able to
print these slab flags, mainly for debugging purposes, if the folio tests true
for slab.

Add printk format specifier for kmem_cache flags.

Signed-off-by: Sukrit Bhatnagar <Sukrit.Bhatnagar@xxxxxxxx>
---
Documentation/core-api/printk-formats.rst | 2 +
include/linux/slab.h | 5 ++
include/trace/events/mmflags.h | 67 +++++++++++++++++++++++
lib/test_printf.c | 13 +++++
lib/vsprintf.c | 22 ++++++++
mm/debug.c | 5 ++
mm/internal.h | 1 +
7 files changed, 115 insertions(+)

diff --git a/Documentation/core-api/printk-formats.rst b/Documentation/core-api/printk-formats.rst
index 4451ef501936..060af5df7a2c 100644
--- a/Documentation/core-api/printk-formats.rst
+++ b/Documentation/core-api/printk-formats.rst
@@ -583,6 +583,7 @@ Flags bitfields such as page flags, page_type, gfp_flags

%pGp 0x17ffffc0002036(referenced|uptodate|lru|active|private|node=0|zone=2|lastcpupid=0x1fffff)
%pGt 0xffffff7f(buddy)
+ %pGs 0x10310(HWCACHE_ALIGN|PANIC|TYPESAFE_BY_RCU|CMPXCHG_DOUBLE)
%pGg GFP_USER|GFP_DMA32|GFP_NOWARN
%pGv read|exec|mayread|maywrite|mayexec|denywrite

@@ -592,6 +593,7 @@ character. Currently supported are:

- p - [p]age flags, expects value of type (``unsigned long *``)
- t - page [t]ype, expects value of type (``unsigned int *``)
+ - s - [s]lab flags, expects value of type (``slab_flags_t *``)
- v - [v]ma_flags, expects value of type (``unsigned long *``)
- g - [g]fp_flags, expects value of type (``gfp_t *``)

diff --git a/include/linux/slab.h b/include/linux/slab.h
index 7247e217e21b..b1ca372f5ee1 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -21,6 +21,10 @@
#include <linux/cleanup.h>
#include <linux/hash.h>

+/*
+ * In case of any changes, please don't forget to update the flags
+ * in include/linux/events/mmflags.h
+ */
enum _slab_flag_bits {
_SLAB_CONSISTENCY_CHECKS,
_SLAB_RED_ZONE,
@@ -64,6 +68,7 @@ enum _slab_flag_bits {

#define __SLAB_FLAG_BIT(nr) ((slab_flags_t __force)(1U << (nr)))
#define __SLAB_FLAG_UNUSED ((slab_flags_t __force)(0U))
+#define SLAB_FLAG_MASK ((slab_flags_t __force)(1U << _SLAB_FLAGS_LAST_BIT) - 1)

/*
* Flags to pass to kmem_cache_create().
diff --git a/include/trace/events/mmflags.h b/include/trace/events/mmflags.h
index e46d6e82765e..1457bc23206f 100644
--- a/include/trace/events/mmflags.h
+++ b/include/trace/events/mmflags.h
@@ -141,6 +141,73 @@ IF_HAVE_PG_ARCH_X(arch_3)
DEF_PAGETYPE_NAME(table), \
DEF_PAGETYPE_NAME(buddy)

+#ifdef CONFIG_DEBUG_OBJECTS
+#define IF_HAVE_SLAB_DEBUG_OBJECTS(_name) {1UL << _SLAB_##_name, __stringify(_name)},
+#else
+#define IF_HAVE_SLAB_DEBUG_OBJECTS(_name)
+#endif
+
+#ifdef CONFIG_FAILSLAB
+#define IF_HAVE_SLAB_FAILSLAB(_name) {1U << _SLAB_##_name, __stringify(_name)},
+#else
+#define IF_HAVE_SLAB_FAILSLAB(_name)
+#endif
+
+#ifdef CONFIG_MEMCG_KMEM
+#define IF_HAVE_SLAB_MEMCG_KMEM(_name) {1U << _SLAB_##_name, __stringify(_name)},
+#else
+#define IF_HAVE_SLAB_MEMCG_KMEM(_name)
+#endif
+
+#ifdef CONFIG_KASAN_GENERIC
+#define IF_HAVE_SLAB_KASAN_GENERIC(_name) {1UL << _SLAB_##_name, __stringify(_name)},
+#else
+#define IF_HAVE_SLAB_KASAN_GENERIC(_name)
+#endif
+
+#ifdef CONFIG_KFENCE
+#define IF_HAVE_SLAB_KFENCE(_name) {1UL << _SLAB_##_name, __stringify(_name)},
+#else
+#define IF_HAVE_SLAB_KFENCE(_name)
+#endif
+
+#ifdef CONFIG_SLUB_TINY
+#define IF_HAVE_SLAB_SLUB_TINY(_name) {1UL << _SLAB_##_name, __stringify(_name)}
+#else
+#define IF_HAVE_SLAB_SLUB_TINY(_name)
+#endif
+
+#define DEF_SLABFLAG_NAME(_name) { 1UL << _SLAB_##_name, __stringify(_name) }
+
+#define __def_slabflag_names \
+ DEF_SLABFLAG_NAME(CONSISTENCY_CHECKS), \
+ DEF_SLABFLAG_NAME(RED_ZONE), \
+ DEF_SLABFLAG_NAME(POISON), \
+ DEF_SLABFLAG_NAME(KMALLOC), \
+ DEF_SLABFLAG_NAME(HWCACHE_ALIGN), \
+ DEF_SLABFLAG_NAME(CACHE_DMA), \
+ DEF_SLABFLAG_NAME(CACHE_DMA32), \
+ DEF_SLABFLAG_NAME(STORE_USER), \
+ DEF_SLABFLAG_NAME(PANIC), \
+ DEF_SLABFLAG_NAME(TYPESAFE_BY_RCU), \
+ DEF_SLABFLAG_NAME(TRACE), \
+IF_HAVE_SLAB_DEBUG_OBJECTS(DEBUG_OBJECTS) \
+ DEF_SLABFLAG_NAME(NOLEAKTRACE), \
+ DEF_SLABFLAG_NAME(NO_MERGE), \
+IF_HAVE_SLAB_FAILSLAB(FAILSLAB) \
+IF_HAVE_SLAB_MEMCG_KMEM(MEMCG_KMEM) \
+IF_HAVE_SLAB_KASAN_GENERIC(KASAN_GENERIC) \
+ DEF_SLABFLAG_NAME(NO_USER_FLAGS), \
+IF_HAVE_SLAB_KFENCE(KFENCE) \
+IF_HAVE_SLAB_SLUB_TINY(SLUB_TINY) \
+ DEF_SLABFLAG_NAME(OBJECT_POISON), \
+ DEF_SLABFLAG_NAME(CMPXCHG_DOUBLE)
+
+#define show_slab_flags(flags) \
+ (flags) ? __print_flags(flags, "|", \
+ __def_slabflag_names \
+ ) : "none"
+
#if defined(CONFIG_X86)
#define __VM_ARCH_SPECIFIC_1 {VM_PAT, "pat" }
#elif defined(CONFIG_PPC)
diff --git a/lib/test_printf.c b/lib/test_printf.c
index 69b6a5e177f2..37f3f837bcbf 100644
--- a/lib/test_printf.c
+++ b/lib/test_printf.c
@@ -681,6 +681,19 @@ flags(void)
flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
test("read|exec|mayread|maywrite|mayexec", "%pGv", &flags);

+ flags = 0;
+ scnprintf(cmp_buffer, BUF_SIZE, "%#x(%s)", (unsigned int) flags, "");
+ test(cmp_buffer, "%pGs", &flags);
+
+ flags = 1U << _SLAB_FLAGS_LAST_BIT;
+ scnprintf(cmp_buffer, BUF_SIZE, "%#x(%s)", (unsigned int) flags, "");
+ test(cmp_buffer, "%pGs", &flags);
+
+ flags = SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_NO_USER_FLAGS;
+ scnprintf(cmp_buffer, BUF_SIZE, "%#x(%s)", (unsigned int) flags,
+ "HWCACHE_ALIGN|PANIC|NO_USER_FLAGS");
+ test(cmp_buffer, "%pGs", &flags);
+
gfp = GFP_TRANSHUGE;
test("GFP_TRANSHUGE", "%pGg", &gfp);

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 552738f14275..67f3584db58c 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -2054,6 +2054,25 @@ char *format_page_flags(char *buf, char *end, unsigned long flags)
return buf;
}

+static
+char *format_slab_flags(char *buf, char *end, unsigned int flags)
+{
+ buf = number(buf, end, flags, default_flag_spec);
+ if (buf < end)
+ *buf = '(';
+ buf++;
+
+ flags &= SLAB_FLAG_MASK;
+ if (flags)
+ buf = format_flags(buf, end, (__force unsigned long)flags, slabflag_names);
+
+ if (buf < end)
+ *buf = ')';
+ buf++;
+
+ return buf;
+}
+
static
char *format_page_type(char *buf, char *end, unsigned int page_type)
{
@@ -2088,6 +2107,9 @@ char *flags_string(char *buf, char *end, void *flags_ptr,
return format_page_flags(buf, end, *(unsigned long *)flags_ptr);
case 't':
return format_page_type(buf, end, *(unsigned int *)flags_ptr);
+ case 's':
+ flags = (__force unsigned int)(*(slab_flags_t *)flags_ptr);
+ return format_slab_flags(buf, end, flags);
case 'v':
flags = *(unsigned long *)flags_ptr;
names = vmaflag_names;
diff --git a/mm/debug.c b/mm/debug.c
index 69e524c3e601..2ef516f310e8 100644
--- a/mm/debug.c
+++ b/mm/debug.c
@@ -41,6 +41,11 @@ const struct trace_print_flags pagetype_names[] = {
{0, NULL}
};

+const struct trace_print_flags slabflag_names[] = {
+ __def_slabflag_names,
+ {0, NULL}
+};
+
const struct trace_print_flags gfpflag_names[] = {
__def_gfpflag_names,
{0, NULL}
diff --git a/mm/internal.h b/mm/internal.h
index 2adabe369403..6a15f4937db8 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -1123,6 +1123,7 @@ static inline void flush_tlb_batched_pending(struct mm_struct *mm)

extern const struct trace_print_flags pageflag_names[];
extern const struct trace_print_flags pagetype_names[];
+extern const struct trace_print_flags slabflag_names[];
extern const struct trace_print_flags vmaflag_names[];
extern const struct trace_print_flags gfpflag_names[];

--
2.34.1