Skip to contentSkip to conversation

SLAB support for KASAN

PATCH Superseded Alexander Potapenko 6 participants 30 messages Feb 26, 2016 – Mar 11, 2016

Subsystems (21)

v4 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Alexander Potapenko

author reviewer
#0 lore raw

v4 cover SLAB support for KASAN

This patch set implements SLAB support for KASAN

Unlike SLUB, SLAB doesn't store allocation/deallocation stacks for heap objects, therefore we reimplement this feature in mm/kasan/stackdepot.c. The intention is to ultimately switch SLUB to use this implementation as well, which will save a lot of memory (right now SLUB bloats each object by 256 bytes to store the allocation/deallocation stacks).

Also neither SLUB nor SLAB delay the reuse of freed memory chunks, which is necessary for better detection of use-after-free errors. We introduce memory quarantine (mm/kasan/quarantine.c), which allows delayed reuse of deallocated memory.

Alexander Potapenko (7):
kasan: Modify kmalloc_large_oob_right(), add
kmalloc_pagealloc_oob_right()
mm, kasan: SLAB support
mm, kasan: Added GFP flags to KASAN API
arch, ftrace: For KASAN put hard/soft IRQ entries into separate
sections
mm, kasan: Stackdepot implementation. Enable stackdepot for SLAB
kasan: Test fix: Warn if the UAF could not be detected in kmalloc_uaf2
mm: kasan: Initial memory quarantine implementation
---
v2: - merged two patches that touched kmalloc_large_oob_right
- moved stackdepot implementation to lib/
- moved IRQ definitions to include/linux/interrupt.h

v3: - minor description changes
- store deallocation info in the "mm, kasan: SLAB support" patch

v4: - fix kbuild error reports

---
 Documentation/kasan.txt              |   5 +-
 arch/arm/kernel/vmlinux.lds.S        |   1 +
 arch/arm64/kernel/vmlinux.lds.S      |   1 +
 arch/blackfin/kernel/vmlinux.lds.S   |   1 +
 arch/c6x/kernel/vmlinux.lds.S        |   1 +
 arch/metag/kernel/vmlinux.lds.S      |   1 +
 arch/microblaze/kernel/vmlinux.lds.S |   1 +
 arch/mips/kernel/vmlinux.lds.S       |   1 +
 arch/nios2/kernel/vmlinux.lds.S      |   1 +
 arch/openrisc/kernel/vmlinux.lds.S   |   1 +
 arch/parisc/kernel/vmlinux.lds.S     |   1 +
 arch/powerpc/kernel/vmlinux.lds.S    |   1 +
 arch/s390/kernel/vmlinux.lds.S       |   1 +
 arch/sh/kernel/vmlinux.lds.S         |   1 +
 arch/sparc/kernel/vmlinux.lds.S      |   1 +
 arch/tile/kernel/vmlinux.lds.S       |   1 +
 arch/x86/kernel/Makefile             |   1 +
 arch/x86/kernel/vmlinux.lds.S        |   1 +
 include/asm-generic/vmlinux.lds.h    |  12 +-
 include/linux/ftrace.h               |  11 --
 include/linux/interrupt.h            |  20 +++
 include/linux/kasan.h                |  63 ++++++--
 include/linux/slab.h                 |  10 +-
 include/linux/slab_def.h             |  14 ++
 include/linux/slub_def.h             |  11 ++
 include/linux/stackdepot.h           |  32 ++++
 kernel/softirq.c                     |   2 +-
 kernel/trace/trace_functions_graph.c |   1 +
 lib/Kconfig.kasan                    |   4 +-
 lib/Makefile                         |   7 +
 lib/stackdepot.c                     | 274 +++++++++++++++++++++++++++++++
 lib/test_kasan.c                     |  59 ++++++-
 mm/Makefile                          |   1 +
 mm/kasan/Makefile                    |   4 +
 mm/kasan/kasan.c                     | 221 +++++++++++++++++++++++--
 mm/kasan/kasan.h                     |  45 ++++++
 mm/kasan/quarantine.c                | 306 +++++++++++++++++++++++++++++++++++
 mm/kasan/report.c                    |  71 ++++++--
 mm/mempool.c                         |  23 +--
 mm/page_alloc.c                      |   2 +-
 mm/slab.c                            |  58 ++++++-
 mm/slab.h                            |   2 +
 mm/slab_common.c                     |   8 +-
 mm/slub.c                            |  21 +--
 44 files changed, 1214 insertions(+), 90 deletions(-)
 create mode 100644 include/linux/stackdepot.h
 create mode 100644 lib/stackdepot.c
 create mode 100644 mm/kasan/quarantine.c
Signature · 7 lines
2.7.0.rc3.207.g0ac5344

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
author reviewer
#1 lore raw

PATCH v4 1/7 kasan: Modify kmalloc_large_oob_right(), add kmalloc_pagealloc_oob_right()

Rename kmalloc_large_oob_right() to kmalloc_pagealloc_oob_right(), as the test only checks the page allocator functionality.
Also reimplement kmalloc_large_oob_right() so that the test allocates a large enough chunk of memory that still does not trigger the page allocator fallback.

---
v2: - Merged "kasan: Change the behavior of kmalloc_large_oob_right" and
"kasan: Changed kmalloc_large_oob_right, added kmalloc_pagealloc_oob_right" from v1

v3: - Minor description changes

---
 lib/test_kasan.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
lib/test_kasan.c +27 -1
index c32f3b0..90ad74f 100644
@@ -65,11 +65,34 @@ static noinline void __init kmalloc_node_oob_right(void)
 	kfree(ptr);
 }
 
-static noinline void __init kmalloc_large_oob_right(void)
+#ifdef CONFIG_SLUB
+static noinline void __init kmalloc_pagealloc_oob_right(void)
 {
 	char *ptr;
 	size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
 
+	/* Allocate a chunk that does not fit into a SLUB cache to trigger
+	 * the page allocator fallback.
+	 */
+	pr_info("kmalloc pagealloc allocation: out-of-bounds to right\n");
+	ptr = kmalloc(size, GFP_KERNEL);
+	if (!ptr) {
+		pr_err("Allocation failed\n");
+		return;
+	}
+
+	ptr[size] = 0;
+	kfree(ptr);
+}
+#endif
+
+static noinline void __init kmalloc_large_oob_right(void)
+{
+	char *ptr;
+	size_t size = KMALLOC_MAX_CACHE_SIZE - 256;
+	/* Allocate a chunk that is large enough, but still fits into a slab
+	 * and does not trigger the page allocator fallback in SLUB.
+	 */
 	pr_info("kmalloc large allocation: out-of-bounds to right\n");
 	ptr = kmalloc(size, GFP_KERNEL);
 	if (!ptr) {
@@ -324,6 +347,9 @@ static int __init kmalloc_tests_init(void)
 	kmalloc_oob_right();
 	kmalloc_oob_left();
 	kmalloc_node_oob_right();
+#ifdef CONFIG_SLUB
+	kmalloc_pagealloc_oob_right();
+#endif
 	kmalloc_large_oob_right();
 	kmalloc_oob_krealloc_more();
 	kmalloc_oob_krealloc_less();
Signature · 7 lines
2.7.0.rc3.207.g0ac5344

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
author reviewer
#2 lore raw

PATCH v4 2/7 mm, kasan: SLAB support

In reply to Alexander Potapenko

Add KASAN hooks to SLAB allocator.

This patch is based on the "mm: kasan: unified support for SLUB and SLAB allocators" patch originally prepared by Dmitry Chernenkov.

---
v3: - minor description changes
- store deallocation info in kasan_slab_free()

v4: - fix kbuild compile-time warnings in print_track()

---
 Documentation/kasan.txt  |   5 +--
 include/linux/kasan.h    |  12 ++++++
 include/linux/slab.h     |   6 +++
 include/linux/slab_def.h |  14 +++++++
 include/linux/slub_def.h |  11 +++++
 lib/Kconfig.kasan        |   4 +-
 mm/Makefile              |   1 +
 mm/kasan/kasan.c         | 102 +++++++++++++++++++++++++++++++++++++++++++++++
 mm/kasan/kasan.h         |  34 ++++++++++++++++
 mm/kasan/report.c        |  61 +++++++++++++++++++++++-----
 mm/slab.c                |  46 ++++++++++++++++++---
 mm/slab_common.c         |   2 +-
 12 files changed, 277 insertions(+), 21 deletions(-)
Documentation/kasan.txt +2 -3
index aa1e0c9..7dd95b3 100644
@@ -12,8 +12,7 @@ KASAN uses compile-time instrumentation for checking every memory access,
 therefore you will need a GCC version 4.9.2 or later. GCC 5.0 or later is
 required for detection of out-of-bounds accesses to stack or global variables.
 
-Currently KASAN is supported only for x86_64 architecture and requires the
-kernel to be built with the SLUB allocator.
+Currently KASAN is supported only for x86_64 architecture.
 
 1. Usage
 ========
@@ -27,7 +26,7 @@ inline are compiler instrumentation types. The former produces smaller binary
 the latter is 1.1 - 2 times faster. Inline instrumentation requires a GCC
 version 5.0 or later.
 
-Currently KASAN works only with the SLUB memory allocator.
+KASAN works with both SLUB and SLAB memory allocators.
 For better bug detection and nicer reporting, enable CONFIG_STACKTRACE.
 
 To disable instrumentation for specific files or directories, add a line
include/linux/kasan.h +12 -0
index 4b9f85c..4405a35 100644
@@ -46,6 +46,9 @@ void kasan_unpoison_shadow(const void *address, size_t size);
 void kasan_alloc_pages(struct page *page, unsigned int order);
 void kasan_free_pages(struct page *page, unsigned int order);
 
+void kasan_cache_create(struct kmem_cache *cache, size_t *size,
+			unsigned long *flags);
+
 void kasan_poison_slab(struct page *page);
 void kasan_unpoison_object_data(struct kmem_cache *cache, void *object);
 void kasan_poison_object_data(struct kmem_cache *cache, void *object);
@@ -59,6 +62,11 @@ void kasan_krealloc(const void *object, size_t new_size);
 void kasan_slab_alloc(struct kmem_cache *s, void *object);
 void kasan_slab_free(struct kmem_cache *s, void *object);
 
+struct kasan_cache {
+	int alloc_meta_offset;
+	int free_meta_offset;
+};
+
 int kasan_module_alloc(void *addr, size_t size);
 void kasan_free_shadow(const struct vm_struct *vm);
 
@@ -72,6 +80,10 @@ static inline void kasan_disable_current(void) {}
 static inline void kasan_alloc_pages(struct page *page, unsigned int order) {}
 static inline void kasan_free_pages(struct page *page, unsigned int order) {}
 
+static inline void kasan_cache_create(struct kmem_cache *cache,
+				      size_t *size,
+				      unsigned long *flags) {}
+
 static inline void kasan_poison_slab(struct page *page) {}
 static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
 					void *object) {}
include/linux/slab.h +6 -0
index 3627d5c..840e652 100644
@@ -92,6 +92,12 @@
 # define SLAB_ACCOUNT		0x00000000UL
 #endif
 
+#ifdef CONFIG_KASAN
+#define SLAB_KASAN		0x08000000UL
+#else
+#define SLAB_KASAN		0x00000000UL
+#endif
+
 /* The following flags affect the page allocator grouping pages by mobility */
 #define SLAB_RECLAIM_ACCOUNT	0x00020000UL		/* Objects are reclaimable */
 #define SLAB_TEMPORARY		SLAB_RECLAIM_ACCOUNT	/* Objects are short-lived */
include/linux/slab_def.h +14 -0
index cf139d3..f57232c 100644
@@ -73,8 +73,22 @@ struct kmem_cache {
 #ifdef CONFIG_MEMCG
 	struct memcg_cache_params memcg_params;
 #endif
+#ifdef CONFIG_KASAN
+	struct kasan_cache kasan_info;
+#endif
 
 	struct kmem_cache_node *node[MAX_NUMNODES];
 };
 
+static inline void *nearest_obj(struct kmem_cache *cache, struct page *page,
+				void *x) {
+	void *object = x - (x - page->s_mem) % cache->size;
+	void *last_object = page->s_mem + (cache->num - 1) * cache->size;
+
+	if (unlikely(object > last_object))
+		return last_object;
+	else
+		return object;
+}
+
 #endif	/* _LINUX_SLAB_DEF_H */
include/linux/slub_def.h +11 -0
index b7e57927..c6970a0 100644
@@ -129,4 +129,15 @@ static inline void *virt_to_obj(struct kmem_cache *s,
 void object_err(struct kmem_cache *s, struct page *page,
 		u8 *object, char *reason);
 
+static inline void *nearest_obj(struct kmem_cache *cache, struct page *page,
+				void *x) {
+	void *object = x - (x - page_address(page)) % cache->size;
+	void *last_object = page_address(page) +
+		(page->objects - 1) * cache->size;
+	if (unlikely(object > last_object))
+		return last_object;
+	else
+		return object;
+}
+
 #endif /* _LINUX_SLUB_DEF_H */
lib/Kconfig.kasan +3 -1
index 0fee5ac..0e4d2b3 100644
@@ -5,7 +5,7 @@ if HAVE_ARCH_KASAN
 
 config KASAN
 	bool "KASan: runtime memory debugger"
-	depends on SLUB_DEBUG
+	depends on SLUB_DEBUG || (SLAB && !DEBUG_SLAB)
 	select CONSTRUCTORS
 	help
 	  Enables kernel address sanitizer - runtime memory debugger,
@@ -16,6 +16,8 @@ config KASAN
 	  This feature consumes about 1/8 of available memory and brings about
 	  ~x3 performance slowdown.
 	  For better error detection enable CONFIG_STACKTRACE.
+	  Currently CONFIG_KASAN doesn't work with CONFIG_DEBUG_SLAB
+	  (the resulting kernel does not boot).
 
 choice
 	prompt "Instrumentation type"
mm/Makefile +1 -0
index 2ed4319..d675b37 100644
@@ -3,6 +3,7 @@
 #
 
 KASAN_SANITIZE_slab_common.o := n
+KASAN_SANITIZE_slab.o := n
 KASAN_SANITIZE_slub.o := n
 
 mmu-y			:= nommu.o
mm/kasan/kasan.c +102 -0

128 lines not on this page · raw message

mm/kasan/kasan.h +34 -0
index 4f6c62e..7b9e4ab9 100644
@@ -54,6 +54,40 @@ struct kasan_global {
 #endif
 };
 
+/**
+ * Structures to keep alloc and free tracks *
+ */
+
+enum kasan_state {
+	KASAN_STATE_INIT,
+	KASAN_STATE_ALLOC,
+	KASAN_STATE_FREE
+};
+
+struct kasan_track {
+	u64 cpu : 6;			/* for NR_CPUS = 64 */
+	u64 pid : 16;			/* 65536 processes */
+	u64 when : 42;			/* ~140 years */
+};
+
+struct kasan_alloc_meta {
+	u32 state : 2;	/* enum kasan_state */
+	u32 alloc_size : 30;
+	struct kasan_track track;
+};
+
+struct kasan_free_meta {
+	/* Allocator freelist pointer, unused by KASAN. */
+	void **freelist;
+	struct kasan_track track;
+};
+
+struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache,
+					const void *object);
+struct kasan_free_meta *get_free_info(struct kmem_cache *cache,
+					const void *object);
+
+
 static inline const void *kasan_shadow_to_mem(const void *shadow_addr)
 {
 	return (void *)(((unsigned long)shadow_addr - KASAN_SHADOW_OFFSET)
mm/kasan/report.c +50 -11

87 lines not on this page · raw message

mm/slab.c +41 -5

127 lines not on this page · raw message

mm/slab_common.c +1 -1
index 065b7bd..bf04ec7 100644
@@ -35,7 +35,7 @@ struct kmem_cache *kmem_cache;
  */
 #define SLAB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
 		SLAB_TRACE | SLAB_DESTROY_BY_RCU | SLAB_NOLEAKTRACE | \
-		SLAB_FAILSLAB)
+		SLAB_FAILSLAB | SLAB_KASAN)
 
 #define SLAB_MERGE_SAME (SLAB_RECLAIM_ACCOUNT | SLAB_CACHE_DMA | \
 			 SLAB_NOTRACK | SLAB_ACCOUNT)
Signature · 7 lines
2.7.0.rc3.207.g0ac5344

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#3 lore raw

Re: [PATCH v4 2/7] mm, kasan: SLAB support

Quoted 186 lines · Alexander Potapenko

Add KASAN hooks to SLAB allocator.

This patch is based on the "mm: kasan: unified support for SLUB and SLAB allocators" patch originally prepared by Dmitry Chernenkov.

---
v3: - minor description changes
- store deallocation info in kasan_slab_free()

v4: - fix kbuild compile-time warnings in print_track()
---

mm/kasan/kasan.c +53 -0
index bc0a8d8..d26ffb4 100644
@@ -314,6 +314,59 @@ void kasan_free_pages(struct page *page, unsigned int order)
 				KASAN_FREE_PAGE);
 }
 
+#ifdef CONFIG_SLAB
+/*
+ * Adaptive redzone policy taken from the userspace AddressSanitizer runtime.
+ * For larger allocations larger redzones are used.
+ */
+static size_t optimal_redzone(size_t object_size)
+{
+	int rz =
+		object_size <= 64        - 16   ? 16 :
+		object_size <= 128       - 32   ? 32 :
+		object_size <= 512       - 64   ? 64 :
+		object_size <= 4096      - 128  ? 128 :
+		object_size <= (1 << 14) - 256  ? 256 :
+		object_size <= (1 << 15) - 512  ? 512 :
+		object_size <= (1 << 16) - 1024 ? 1024 : 2048;
+	return rz;
+}
+
+void kasan_cache_create(struct kmem_cache *cache, size_t *size,
+			unsigned long *flags)
+{
+	int redzone_adjust;
+	/* Make sure the adjusted size is still less than
+	 * KMALLOC_MAX_CACHE_SIZE.
+	 * TODO: this check is only useful for SLAB, but not SLUB. We'll need
+	 * to skip it for SLUB when it starts using kasan_cache_create().
+	 */
+	if (*size > KMALLOC_MAX_CACHE_SIZE -
+	    sizeof(struct kasan_alloc_meta) -
+	    sizeof(struct kasan_free_meta))
+		return;
+	*flags |= SLAB_KASAN;
+	/* Add alloc meta. */
+	cache->kasan_info.alloc_meta_offset = *size;
+	*size += sizeof(struct kasan_alloc_meta);
+
+	/* Add free meta. */
+	if (cache->flags & SLAB_DESTROY_BY_RCU || cache->ctor ||
+	    cache->object_size < sizeof(struct kasan_free_meta)) {
+		cache->kasan_info.free_meta_offset = *size;
+		*size += sizeof(struct kasan_free_meta);
+	}
+	redzone_adjust = optimal_redzone(cache->object_size) -
+		(*size - cache->object_size);
+	if (redzone_adjust > 0)
+		*size += redzone_adjust;
+	*size = min(KMALLOC_MAX_CACHE_SIZE,
+		    max(*size,
+			cache->object_size +
+			optimal_redzone(cache->object_size)));
+}
+#endif
+
 
 
 

void kasan_poison_slab(struct page *page)
{

mm/kasan/kasan.c +49 -0
 	kasan_poison_shadow(page_address(page),
@@ -331,8 +384,36 @@ void kasan_poison_object_data(struct kmem_cache *cache, void *object)
 	kasan_poison_shadow(object,
 			round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE),
 			KASAN_KMALLOC_REDZONE);
+#ifdef CONFIG_SLAB
+	if (cache->flags & SLAB_KASAN) {
+		struct kasan_alloc_meta *alloc_info =
+			get_alloc_info(cache, object);
+		alloc_info->state = KASAN_STATE_INIT;
+	}
+#endif
+}
+
+static inline void set_track(struct kasan_track *track)
+{
+	track->cpu = raw_smp_processor_id();
+	track->pid = current->pid;
+	track->when = jiffies;
 }
 
+#ifdef CONFIG_SLAB
+struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache,
+					const void *object)
+{
+	return (void *)object + cache->kasan_info.alloc_meta_offset;
+}
+
+struct kasan_free_meta *get_free_info(struct kmem_cache *cache,
+				      const void *object)
+{
+	return (void *)object + cache->kasan_info.free_meta_offset;
+}
+#endif
+
 void kasan_slab_alloc(struct kmem_cache *cache, void *object)
 {
 	kasan_kmalloc(cache, object, cache->object_size);
@@ -347,6 +428,17 @@ void kasan_slab_free(struct kmem_cache *cache, void *object)
 	if (unlikely(cache->flags & SLAB_DESTROY_BY_RCU))
 		return;
 
+#ifdef CONFIG_SLAB
+	if (cache->flags & SLAB_KASAN) {
+		struct kasan_free_meta *free_info =
+			get_free_info(cache, object);
+		struct kasan_alloc_meta *alloc_info =
+			get_alloc_info(cache, object);
+		alloc_info->state = KASAN_STATE_FREE;
+		set_track(&free_info->track);
+	}
+#endif
+
 	kasan_poison_shadow(object, rounded_up_size, KASAN_KMALLOC_FREE);
 }
 
@@ -366,6 +458,16 @@ void kasan_kmalloc(struct kmem_cache *cache, const void *object, size_t size)
 	kasan_unpoison_shadow(object, size);
 	kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
 		KASAN_KMALLOC_REDZONE);
+#ifdef CONFIG_SLAB
+	if (cache->flags & SLAB_KASAN) {
+		struct kasan_alloc_meta *alloc_info =
+			get_alloc_info(cache, object);
+
+		alloc_info->state = KASAN_STATE_ALLOC;
+		alloc_info->alloc_size = size;
+		set_track(&alloc_info->track);
+	}
+#endif
 }
 EXPORT_SYMBOL(kasan_kmalloc);
 
mm/kasan/kasan.h +24 -0
index 4f6c62e..7b9e4ab9 100644
@@ -54,6 +54,40 @@ struct kasan_global {
 #endif
 };
 
+/**
+ * Structures to keep alloc and free tracks *
+ */
+
+enum kasan_state {
+	KASAN_STATE_INIT,
+	KASAN_STATE_ALLOC,
+	KASAN_STATE_FREE
+};
+
+struct kasan_track {
+	u64 cpu : 6;			/* for NR_CPUS = 64 */
+	u64 pid : 16;			/* 65536 processes */
+	u64 when : 42;			/* ~140 years */
+};
+
+struct kasan_alloc_meta {
+	u32 state : 2;	/* enum kasan_state */
+	u32 alloc_size : 30;
+	struct kasan_track track;
+};
+
+struct kasan_free_meta {
+	/* Allocator freelist pointer, unused by KASAN. */

mm/kasan/kasan.h struct kasan_global { · line 90

Quoted 10 lines
mm/kasan/kasan.h +10 -0
+	void **freelist;
+	struct kasan_track track;
+};
+
+struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache,
+					const void *object);
+struct kasan_free_meta *get_free_info(struct kmem_cache *cache,
+					const void *object);
+
+

Basically, all this big pile of code above is implementation of yet another SLAB_STORE_USER and SLAB_RED_ZONE exclusively for KASAN. It would be so much better to alter existing code to satisfy all you needs.

Quoted 17 lines
mm/kasan/kasan.h +0 -0
 static inline const void *kasan_shadow_to_mem(const void *shadow_addr)
 {
 	return (void *)(((unsigned long)shadow_addr - KASAN_SHADOW_OFFSET)
mm/kasan/report.c +6 -0
index 12f222d..2c1407f 100644
@@ -115,6 +115,44 @@ static inline bool init_task_stack_addr(const void *addr)
 			sizeof(init_thread_union.stack));
 }
 
+#ifdef CONFIG_SLAB
+static void print_track(struct kasan_track *track)
+{
+	pr_err("PID = %u, CPU = %u, timestamp = %lu\n", track->pid,
+	       track->cpu, (unsigned long)track->when);
+}

mm/kasan/report.c static inline bool init_task_stack_addr(const void *addr) · line 133

Quoted 10 lines
mm/kasan/report.c +10 -0
+
+static void print_object(struct kmem_cache *cache, void *object)
+{
+	struct kasan_alloc_meta *alloc_info = get_alloc_info(cache, object);
+	struct kasan_free_meta *free_info;
+
+	pr_err("Object at %p, in cache %s\n", object, cache->name);
+	if (!(cache->flags & SLAB_KASAN))
+		return;
+	switch (alloc_info->state) {

'->state' seems useless. It's used only here, but object's state could be determined by shadow value.

Quoted 33 lines
mm/kasan/report.c +22 -4
+	case KASAN_STATE_INIT:
+		pr_err("Object not allocated yet\n");
+		break;
+	case KASAN_STATE_ALLOC:
+		pr_err("Object allocated with size %u bytes.\n",
+		       alloc_info->alloc_size);
+		pr_err("Allocation:\n");
+		print_track(&alloc_info->track);
+		break;
+	case KASAN_STATE_FREE:
+		pr_err("Object freed, allocated with size %u bytes\n",
+		       alloc_info->alloc_size);
+		free_info = get_free_info(cache, object);
+		pr_err("Allocation:\n");
+		print_track(&alloc_info->track);
+		pr_err("Deallocation:\n");
+		print_track(&free_info->track);
+		break;
+	}
+}
+#endif
+
 static void print_address_description(struct kasan_access_info *info)
 {
 	const void *addr = info->access_addr;
@@ -126,17 +164,14 @@ static void print_address_description(struct kasan_access_info *info)
 		if (PageSlab(page)) {
 			void *object;
 			struct kmem_cache *cache = page->slab_cache;
-			void *last_object;
-
-			object = virt_to_obj(cache, page_address(page), addr);
-			last_object = page_address(page) +

mm/kasan/report.c static void print_address_description(struct kasan_access_info *info) · line 171

Quoted 10 lines
mm/kasan/report.c +5 -5
-				page->objects * cache->size;
-
-			if (unlikely(object > last_object))
-				object = last_object; /* we hit into padding */
-
+			object = nearest_obj(cache, page,
+						(void *)info->access_addr);
+#ifdef CONFIG_SLAB
+			print_object(cache, object);
+#else

Instead of these ifdefs, please, make universal API for printing object's information.

mm/kasan/report.c static void print_address_description(struct kasan_access_info *info) · line 184

Quoted 13 lines
mm/kasan/report.c +3 -2
 			object_err(cache, page, object,
-				"kasan: bad access detected");
+					"kasan: bad access detected");
+#endif
 			return;
 		}
 		dump_page(page, "kasan: bad access detected");
@@ -146,8 +181,9 @@ static void print_address_description(struct kasan_access_info *info)
 		if (!init_task_stack_addr(addr))
 			pr_err("Address belongs to variable %pS\n", addr);
 	}
-
+#ifdef CONFIG_SLUB

???

mm/kasan/report.c static void kasan_report_error(struct kasan_access_info *info) · line 272

Quoted 10 lines
mm/kasan/report.c +2 -0
 	dump_stack();
+#endif
 }
 
 static bool row_is_guilty(const void *row, const void *guilty)
@@ -233,6 +269,9 @@ static void kasan_report_error(struct kasan_access_info *info)
 		dump_stack();
 	} else {
 		print_error_description(info);
+#ifdef CONFIG_SLAB

I'm lost here. What's the point of reordering dump_stack() for CONFIG_SLAB=y?

Quoted 13 lines
mm/kasan/report.c +2 -0
+		dump_stack();
+#endif
 		print_address_description(info);
 		print_shadow_for_address(info->first_bad_addr);
 	}
mm/slab.c +0 -0
index 621fbcb..805b39b 100644
 
 
 
 
 	if (gfpflags_allow_blocking(local_flags))

mm/slab.c free_done: · line 3380

Quoted 8 lines
mm/slab.c +4 -1
@@ -3364,7 +3374,10 @@ free_done:
 static inline void __cache_free(struct kmem_cache *cachep, void *objp,
 				unsigned long caller)
 {
-	struct array_cache *ac = cpu_cache_get(cachep);
+	struct array_cache *ac;
+
+	kasan_slab_free(cachep, objp);
+	ac = cpu_cache_get(cachep);

Why cpu_cache_get() was moved? Looks like unnecessary change.

mm/slab.c static inline void __cache_free(struct kmem_cache *cachep, void *objp, · line 3419

Quoted 8 lines
mm/slab.c +1 -0
 	check_irq_off();
 	kmemleak_free_recursive(objp, cachep->flags);
@@ -3403,6 +3416,8 @@ static inline void __cache_free(struct kmem_cache *cachep, void *objp,
 void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
 {
 	void *ret = slab_alloc(cachep, flags, _RET_IP_);
+	if (ret)

kasan_slab_alloc() should deal fine with ret == NULL.

mm/slab.c static inline void __cache_free(struct kmem_cache *cachep, void *objp, · line 3423

Quoted 4 lines
mm/slab.c +1 -0
+		kasan_slab_alloc(cachep, ret);
 
 	trace_kmem_cache_alloc(_RET_IP_, ret,
 			       cachep->object_size, cachep->size, flags);

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author reviewer
#4 lore raw

Re: [PATCH v4 2/7] mm, kasan: SLAB support

Quoted 194 lines · Andrey Ryabinin
Quoted 191 lines · Alexander Potapenko

Add KASAN hooks to SLAB allocator.

This patch is based on the "mm: kasan: unified support for SLUB and SLAB allocators" patch originally prepared by Dmitry Chernenkov.

---
v3: - minor description changes
- store deallocation info in kasan_slab_free()

v4: - fix kbuild compile-time warnings in print_track()
---

mm/kasan/kasan.c +53 -0
index bc0a8d8..d26ffb4 100644
@@ -314,6 +314,59 @@ void kasan_free_pages(struct page *page, unsigned int order)
                              KASAN_FREE_PAGE);
 }
 
+#ifdef CONFIG_SLAB
+/*
+ * Adaptive redzone policy taken from the userspace AddressSanitizer runtime.
+ * For larger allocations larger redzones are used.
+ */
+static size_t optimal_redzone(size_t object_size)
+{
+     int rz =
+             object_size <= 64        - 16   ? 16 :
+             object_size <= 128       - 32   ? 32 :
+             object_size <= 512       - 64   ? 64 :
+             object_size <= 4096      - 128  ? 128 :
+             object_size <= (1 << 14) - 256  ? 256 :
+             object_size <= (1 << 15) - 512  ? 512 :
+             object_size <= (1 << 16) - 1024 ? 1024 : 2048;
+     return rz;
+}
+
+void kasan_cache_create(struct kmem_cache *cache, size_t *size,
+                     unsigned long *flags)
+{
+     int redzone_adjust;
+     /* Make sure the adjusted size is still less than
+      * KMALLOC_MAX_CACHE_SIZE.
+      * TODO: this check is only useful for SLAB, but not SLUB. We'll need
+      * to skip it for SLUB when it starts using kasan_cache_create().
+      */
+     if (*size > KMALLOC_MAX_CACHE_SIZE -
+         sizeof(struct kasan_alloc_meta) -
+         sizeof(struct kasan_free_meta))
+             return;
+     *flags |= SLAB_KASAN;
+     /* Add alloc meta. */
+     cache->kasan_info.alloc_meta_offset = *size;
+     *size += sizeof(struct kasan_alloc_meta);
+
+     /* Add free meta. */
+     if (cache->flags & SLAB_DESTROY_BY_RCU || cache->ctor ||
+         cache->object_size < sizeof(struct kasan_free_meta)) {
+             cache->kasan_info.free_meta_offset = *size;
+             *size += sizeof(struct kasan_free_meta);
+     }
+     redzone_adjust = optimal_redzone(cache->object_size) -
+             (*size - cache->object_size);
+     if (redzone_adjust > 0)
+             *size += redzone_adjust;
+     *size = min(KMALLOC_MAX_CACHE_SIZE,
+                 max(*size,
+                     cache->object_size +
+                     optimal_redzone(cache->object_size)));
+}
+#endif
+
 
 
 

void kasan_poison_slab(struct page *page)
{
kasan_poison_shadow(page_address(page),

mm/kasan/kasan.c +49 -0
@@ -331,8 +384,36 @@ void kasan_poison_object_data(struct kmem_cache *cache, void *object)
      kasan_poison_shadow(object,
                      round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE),
                      KASAN_KMALLOC_REDZONE);
+#ifdef CONFIG_SLAB
+     if (cache->flags & SLAB_KASAN) {
+             struct kasan_alloc_meta *alloc_info =
+                     get_alloc_info(cache, object);
+             alloc_info->state = KASAN_STATE_INIT;
+     }
+#endif
+}
+
+static inline void set_track(struct kasan_track *track)
+{
+     track->cpu = raw_smp_processor_id();
+     track->pid = current->pid;
+     track->when = jiffies;
 }
 
+#ifdef CONFIG_SLAB
+struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache,
+                                     const void *object)
+{
+     return (void *)object + cache->kasan_info.alloc_meta_offset;
+}
+
+struct kasan_free_meta *get_free_info(struct kmem_cache *cache,
+                                   const void *object)
+{
+     return (void *)object + cache->kasan_info.free_meta_offset;
+}
+#endif
+
 void kasan_slab_alloc(struct kmem_cache *cache, void *object)
 {
      kasan_kmalloc(cache, object, cache->object_size);
@@ -347,6 +428,17 @@ void kasan_slab_free(struct kmem_cache *cache, void *object)
      if (unlikely(cache->flags & SLAB_DESTROY_BY_RCU))
              return;
 
+#ifdef CONFIG_SLAB
+     if (cache->flags & SLAB_KASAN) {
+             struct kasan_free_meta *free_info =
+                     get_free_info(cache, object);
+             struct kasan_alloc_meta *alloc_info =
+                     get_alloc_info(cache, object);
+             alloc_info->state = KASAN_STATE_FREE;
+             set_track(&free_info->track);
+     }
+#endif
+
      kasan_poison_shadow(object, rounded_up_size, KASAN_KMALLOC_FREE);
 }
 
@@ -366,6 +458,16 @@ void kasan_kmalloc(struct kmem_cache *cache, const void *object, size_t size)
      kasan_unpoison_shadow(object, size);
      kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
              KASAN_KMALLOC_REDZONE);
+#ifdef CONFIG_SLAB
+     if (cache->flags & SLAB_KASAN) {
+             struct kasan_alloc_meta *alloc_info =
+                     get_alloc_info(cache, object);
+
+             alloc_info->state = KASAN_STATE_ALLOC;
+             alloc_info->alloc_size = size;
+             set_track(&alloc_info->track);
+     }
+#endif
 }
 EXPORT_SYMBOL(kasan_kmalloc);
 
mm/kasan/kasan.h +29 -0
index 4f6c62e..7b9e4ab9 100644
@@ -54,6 +54,40 @@ struct kasan_global {
 #endif
 };
 
+/**
+ * Structures to keep alloc and free tracks *
+ */
+
+enum kasan_state {
+     KASAN_STATE_INIT,
+     KASAN_STATE_ALLOC,
+     KASAN_STATE_FREE
+};
+
+struct kasan_track {
+     u64 cpu : 6;                    /* for NR_CPUS = 64 */
+     u64 pid : 16;                   /* 65536 processes */
+     u64 when : 42;                  /* ~140 years */
+};
+
+struct kasan_alloc_meta {
+     u32 state : 2;  /* enum kasan_state */
+     u32 alloc_size : 30;
+     struct kasan_track track;
+};
+
+struct kasan_free_meta {
+     /* Allocator freelist pointer, unused by KASAN. */
+     void **freelist;
+     struct kasan_track track;
+};
+
+struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache,
Quoted 8 lines

mm/kasan/kasan.h struct kasan_global { · line 90

Quoted 5 lines
mm/kasan/kasan.h +5 -0
+                                     const void *object);
+struct kasan_free_meta *get_free_info(struct kmem_cache *cache,
+                                     const void *object);
+
+

Basically, all this big pile of code above is implementation of yet another SLAB_STORE_USER and SLAB_RED_ZONE exclusively for KASAN. It would be so much better to alter existing code to satisfy all you needs.

Thanks for the suggestion, I will take a look.

Quoted 67 lines
Quoted 17 lines
mm/kasan/kasan.h +0 -0
 static inline const void *kasan_shadow_to_mem(const void *shadow_addr)
 {
      return (void *)(((unsigned long)shadow_addr - KASAN_SHADOW_OFFSET)
mm/kasan/report.c +6 -0
index 12f222d..2c1407f 100644
@@ -115,6 +115,44 @@ static inline bool init_task_stack_addr(const void *addr)
                      sizeof(init_thread_union.stack));
 }
 
+#ifdef CONFIG_SLAB
+static void print_track(struct kasan_track *track)
+{
+     pr_err("PID = %u, CPU = %u, timestamp = %lu\n", track->pid,
+            track->cpu, (unsigned long)track->when);
+}

mm/kasan/report.c static inline bool init_task_stack_addr(const void *addr) · line 133

Quoted 10 lines
mm/kasan/report.c +10 -0
+
+static void print_object(struct kmem_cache *cache, void *object)
+{
+     struct kasan_alloc_meta *alloc_info = get_alloc_info(cache, object);
+     struct kasan_free_meta *free_info;
+
+     pr_err("Object at %p, in cache %s\n", object, cache->name);
+     if (!(cache->flags & SLAB_KASAN))
+             return;
+     switch (alloc_info->state) {

'->state' seems useless. It's used only here, but object's state could be determined by shadow value.

Quoted 37 lines
mm/kasan/report.c +22 -8
+     case KASAN_STATE_INIT:
+             pr_err("Object not allocated yet\n");
+             break;
+     case KASAN_STATE_ALLOC:
+             pr_err("Object allocated with size %u bytes.\n",
+                    alloc_info->alloc_size);
+             pr_err("Allocation:\n");
+             print_track(&alloc_info->track);
+             break;
+     case KASAN_STATE_FREE:
+             pr_err("Object freed, allocated with size %u bytes\n",
+                    alloc_info->alloc_size);
+             free_info = get_free_info(cache, object);
+             pr_err("Allocation:\n");
+             print_track(&alloc_info->track);
+             pr_err("Deallocation:\n");
+             print_track(&free_info->track);
+             break;
+     }
+}
+#endif
+
 static void print_address_description(struct kasan_access_info *info)
 {
      const void *addr = info->access_addr;
@@ -126,17 +164,14 @@ static void print_address_description(struct kasan_access_info *info)
              if (PageSlab(page)) {
                      void *object;
                      struct kmem_cache *cache = page->slab_cache;
-                     void *last_object;
-
-                     object = virt_to_obj(cache, page_address(page), addr);
-                     last_object = page_address(page) +
-                             page->objects * cache->size;
-
-                     if (unlikely(object > last_object))
-                             object = last_object; /* we hit into padding */
Quoted 8 lines

mm/kasan/report.c static void print_address_description(struct kasan_access_info *info) · line 171

Quoted 6 lines
mm/kasan/report.c +5 -1
-
+                     object = nearest_obj(cache, page,
+                                             (void *)info->access_addr);
+#ifdef CONFIG_SLAB
+                     print_object(cache, object);
+#else

Instead of these ifdefs, please, make universal API for printing object's information.

My intention here was to touch the SLUB functionality as little as possible to avoid the mess and feature regressions.
I'll be happy to refactor the code in the upcoming patches once this one is landed.

Quoted 7 lines
Quoted 7 lines
mm/kasan/report.c +2 -1
                      object_err(cache, page, object,
-                             "kasan: bad access detected");
+                                     "kasan: bad access detected");
+#endif
                      return;
              }
              dump_page(page, "kasan: bad access detected");
Quoted 8 lines

mm/kasan/report.c static void print_address_description(struct kasan_access_info *info) · line 184

Quoted 6 lines
mm/kasan/report.c +1 -1
@@ -146,8 +181,9 @@ static void print_address_description(struct kasan_access_info *info)
              if (!init_task_stack_addr(addr))
                      pr_err("Address belongs to variable %pS\n", addr);
      }
-
+#ifdef CONFIG_SLUB

???

Not sure what did you mean here, assuming this comment is related to the next one.

Quoted 5 lines
Quoted 5 lines
mm/kasan/report.c +1 -0
      dump_stack();
+#endif
 }
 
Quoted 8 lines

mm/kasan/report.c static void kasan_report_error(struct kasan_access_info *info) · line 272

Quoted 6 lines
mm/kasan/report.c +1 -0
 static bool row_is_guilty(const void *row, const void *guilty)
@@ -233,6 +269,9 @@ static void kasan_report_error(struct kasan_access_info *info)
              dump_stack();
      } else {
              print_error_description(info);
+#ifdef CONFIG_SLAB

I'm lost here. What's the point of reordering dump_stack() for CONFIG_SLAB=y?

I should have documented this in the patch description properly. My intention is to make the KASAN reports look more like those in the userspace AddressSanitizer, so I'm moving the memory access stack to the top of the report.
Having seen hundreds and hundreds of ASan reports, we believe that important information must go at the beginning of the error report. First, people usually do not need to read further once they see the access stack.
Second, the whole report may simply not make it to the log (e.g. in the case of a premature shutdown or remote log collection). As said before, I wasn't going to touch the SLUB output format in this patch set, but that also needs to be fixed (I'd also remove some unnecessary info, e.g. the memory dump).

Quoted 11 lines
Quoted 11 lines
mm/kasan/report.c +2 -0
+             dump_stack();
+#endif
              print_address_description(info);
              print_shadow_for_address(info->first_bad_addr);
      }
mm/slab.c +0 -0
index 621fbcb..805b39b 100644
Quoted 12 lines

mm/slab.c free_done: · line 3380

Quoted 10 lines

if (gfpflags_allow_blocking(local_flags))

mm/slab.c +4 -1
@@ -3364,7 +3374,10 @@ free_done:
 static inline void __cache_free(struct kmem_cache *cachep, void *objp,
                              unsigned long caller)
 {
-     struct array_cache *ac = cpu_cache_get(cachep);
+     struct array_cache *ac;
+
+     kasan_slab_free(cachep, objp);
+     ac = cpu_cache_get(cachep);

Why cpu_cache_get() was moved? Looks like unnecessary change.

Agreed.

Quoted 10 lines

mm/slab.c static inline void __cache_free(struct kmem_cache *cachep, void *objp, · line 3419

Quoted 8 lines
mm/slab.c +1 -0
      check_irq_off();
      kmemleak_free_recursive(objp, cachep->flags);
@@ -3403,6 +3416,8 @@ static inline void __cache_free(struct kmem_cache *cachep, void *objp,
 void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
 {
      void *ret = slab_alloc(cachep, flags, _RET_IP_);
+     if (ret)

kasan_slab_alloc() should deal fine with ret == NULL.

And it actually does. I'll remove this code in the updated patch set.

Quoted 5 lines
Quoted 5 lines
mm/slab.c +1 -0
+             kasan_slab_alloc(cachep, ret);
 
      trace_kmem_cache_alloc(_RET_IP_, ret,
                             cachep->object_size, cachep->size, flags);
Signature · 22 lines
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Diese E-Mail ist vertraulich. Wenn Sie nicht der richtige Adressat sind,
leiten Sie diese bitte nicht weiter, informieren Sie den
Absender und löschen Sie die E-Mail und alle Anhänge. Vielen Dank.
This e-mail is confidential. If you are not the right addressee please
do not forward it, please inform the sender, and please erase this
e-mail including any attachments. Thanks.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
author reviewer
#5 lore raw

Re: [PATCH v4 2/7] mm, kasan: SLAB support

Quoted 203 lines · Alexander Potapenko
Quoted 194 lines · Andrey Ryabinin
Quoted 191 lines · Alexander Potapenko

Add KASAN hooks to SLAB allocator.

This patch is based on the "mm: kasan: unified support for SLUB and SLAB allocators" patch originally prepared by Dmitry Chernenkov.

---
v3: - minor description changes
- store deallocation info in kasan_slab_free()

v4: - fix kbuild compile-time warnings in print_track()
---

mm/kasan/kasan.c +53 -0
index bc0a8d8..d26ffb4 100644
@@ -314,6 +314,59 @@ void kasan_free_pages(struct page *page, unsigned int order)
                              KASAN_FREE_PAGE);
 }
 
+#ifdef CONFIG_SLAB
+/*
+ * Adaptive redzone policy taken from the userspace AddressSanitizer runtime.
+ * For larger allocations larger redzones are used.
+ */
+static size_t optimal_redzone(size_t object_size)
+{
+     int rz =
+             object_size <= 64        - 16   ? 16 :
+             object_size <= 128       - 32   ? 32 :
+             object_size <= 512       - 64   ? 64 :
+             object_size <= 4096      - 128  ? 128 :
+             object_size <= (1 << 14) - 256  ? 256 :
+             object_size <= (1 << 15) - 512  ? 512 :
+             object_size <= (1 << 16) - 1024 ? 1024 : 2048;
+     return rz;
+}
+
+void kasan_cache_create(struct kmem_cache *cache, size_t *size,
+                     unsigned long *flags)
+{
+     int redzone_adjust;
+     /* Make sure the adjusted size is still less than
+      * KMALLOC_MAX_CACHE_SIZE.
+      * TODO: this check is only useful for SLAB, but not SLUB. We'll need
+      * to skip it for SLUB when it starts using kasan_cache_create().
+      */
+     if (*size > KMALLOC_MAX_CACHE_SIZE -
+         sizeof(struct kasan_alloc_meta) -
+         sizeof(struct kasan_free_meta))
+             return;
+     *flags |= SLAB_KASAN;
+     /* Add alloc meta. */
+     cache->kasan_info.alloc_meta_offset = *size;
+     *size += sizeof(struct kasan_alloc_meta);
+
+     /* Add free meta. */
+     if (cache->flags & SLAB_DESTROY_BY_RCU || cache->ctor ||
+         cache->object_size < sizeof(struct kasan_free_meta)) {
+             cache->kasan_info.free_meta_offset = *size;
+             *size += sizeof(struct kasan_free_meta);
+     }
+     redzone_adjust = optimal_redzone(cache->object_size) -
+             (*size - cache->object_size);
+     if (redzone_adjust > 0)
+             *size += redzone_adjust;
+     *size = min(KMALLOC_MAX_CACHE_SIZE,
+                 max(*size,
+                     cache->object_size +
+                     optimal_redzone(cache->object_size)));
+}
+#endif
+
 
 
 

void kasan_poison_slab(struct page *page)
{
kasan_poison_shadow(page_address(page),

mm/kasan/kasan.c +49 -0
@@ -331,8 +384,36 @@ void kasan_poison_object_data(struct kmem_cache *cache, void *object)
      kasan_poison_shadow(object,
                      round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE),
                      KASAN_KMALLOC_REDZONE);
+#ifdef CONFIG_SLAB
+     if (cache->flags & SLAB_KASAN) {
+             struct kasan_alloc_meta *alloc_info =
+                     get_alloc_info(cache, object);
+             alloc_info->state = KASAN_STATE_INIT;
+     }
+#endif
+}
+
+static inline void set_track(struct kasan_track *track)
+{
+     track->cpu = raw_smp_processor_id();
+     track->pid = current->pid;
+     track->when = jiffies;
 }
 
+#ifdef CONFIG_SLAB
+struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache,
+                                     const void *object)
+{
+     return (void *)object + cache->kasan_info.alloc_meta_offset;
+}
+
+struct kasan_free_meta *get_free_info(struct kmem_cache *cache,
+                                   const void *object)
+{
+     return (void *)object + cache->kasan_info.free_meta_offset;
+}
+#endif
+
 void kasan_slab_alloc(struct kmem_cache *cache, void *object)
 {
      kasan_kmalloc(cache, object, cache->object_size);
@@ -347,6 +428,17 @@ void kasan_slab_free(struct kmem_cache *cache, void *object)
      if (unlikely(cache->flags & SLAB_DESTROY_BY_RCU))
              return;
 
+#ifdef CONFIG_SLAB
+     if (cache->flags & SLAB_KASAN) {
+             struct kasan_free_meta *free_info =
+                     get_free_info(cache, object);
+             struct kasan_alloc_meta *alloc_info =
+                     get_alloc_info(cache, object);
+             alloc_info->state = KASAN_STATE_FREE;
+             set_track(&free_info->track);
+     }
+#endif
+
      kasan_poison_shadow(object, rounded_up_size, KASAN_KMALLOC_FREE);
 }
 
@@ -366,6 +458,16 @@ void kasan_kmalloc(struct kmem_cache *cache, const void *object, size_t size)
      kasan_unpoison_shadow(object, size);
      kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
              KASAN_KMALLOC_REDZONE);
+#ifdef CONFIG_SLAB
+     if (cache->flags & SLAB_KASAN) {
+             struct kasan_alloc_meta *alloc_info =
+                     get_alloc_info(cache, object);
+
+             alloc_info->state = KASAN_STATE_ALLOC;
+             alloc_info->alloc_size = size;
+             set_track(&alloc_info->track);
+     }
+#endif
 }
 EXPORT_SYMBOL(kasan_kmalloc);
 
mm/kasan/kasan.h +29 -0
index 4f6c62e..7b9e4ab9 100644
@@ -54,6 +54,40 @@ struct kasan_global {
 #endif
 };
 
+/**
+ * Structures to keep alloc and free tracks *
+ */
+
+enum kasan_state {
+     KASAN_STATE_INIT,
+     KASAN_STATE_ALLOC,
+     KASAN_STATE_FREE
+};
+
+struct kasan_track {
+     u64 cpu : 6;                    /* for NR_CPUS = 64 */
+     u64 pid : 16;                   /* 65536 processes */
+     u64 when : 42;                  /* ~140 years */
+};
+
+struct kasan_alloc_meta {
+     u32 state : 2;  /* enum kasan_state */
+     u32 alloc_size : 30;
+     struct kasan_track track;
+};
+
+struct kasan_free_meta {
+     /* Allocator freelist pointer, unused by KASAN. */
+     void **freelist;
+     struct kasan_track track;
+};
+
+struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache,
Quoted 8 lines

mm/kasan/kasan.h struct kasan_global { · line 90

Quoted 5 lines
mm/kasan/kasan.h +5 -0
+                                     const void *object);
+struct kasan_free_meta *get_free_info(struct kmem_cache *cache,
+                                     const void *object);
+
+

Basically, all this big pile of code above is implementation of yet another SLAB_STORE_USER and SLAB_RED_ZONE exclusively for KASAN. It would be so much better to alter existing code to satisfy all you needs.

Quoted 32 lines

Thanks for the suggestion, I will take a look.

Quoted 29 lines
Quoted 17 lines
mm/kasan/kasan.h +0 -0
 static inline const void *kasan_shadow_to_mem(const void *shadow_addr)
 {
      return (void *)(((unsigned long)shadow_addr - KASAN_SHADOW_OFFSET)
mm/kasan/report.c +6 -0
index 12f222d..2c1407f 100644
@@ -115,6 +115,44 @@ static inline bool init_task_stack_addr(const void *addr)
                      sizeof(init_thread_union.stack));
 }
 
+#ifdef CONFIG_SLAB
+static void print_track(struct kasan_track *track)
+{
+     pr_err("PID = %u, CPU = %u, timestamp = %lu\n", track->pid,
+            track->cpu, (unsigned long)track->when);
+}

mm/kasan/report.c static inline bool init_task_stack_addr(const void *addr) · line 133

Quoted 10 lines
mm/kasan/report.c +10 -0
+
+static void print_object(struct kmem_cache *cache, void *object)
+{
+     struct kasan_alloc_meta *alloc_info = get_alloc_info(cache, object);
+     struct kasan_free_meta *free_info;
+
+     pr_err("Object at %p, in cache %s\n", object, cache->name);
+     if (!(cache->flags & SLAB_KASAN))
+             return;
+     switch (alloc_info->state) {

'->state' seems useless. It's used only here, but object's state could be determined by shadow value.

Don't think it's a good idea to rely on the shadow values here, the state is a different conception.
For example, shadow values don't distinguish between the freed memory and memory in the quarantine.
In the userspace tool it's also possible for the user to manually poison and unpoison parts of the memory (and I can imagine having the same mechanism in KASAN), but this does not necessarily make them change the state.

Quoted 156 lines
Quoted 37 lines
Quoted 37 lines
mm/kasan/report.c +22 -8
+     case KASAN_STATE_INIT:
+             pr_err("Object not allocated yet\n");
+             break;
+     case KASAN_STATE_ALLOC:
+             pr_err("Object allocated with size %u bytes.\n",
+                    alloc_info->alloc_size);
+             pr_err("Allocation:\n");
+             print_track(&alloc_info->track);
+             break;
+     case KASAN_STATE_FREE:
+             pr_err("Object freed, allocated with size %u bytes\n",
+                    alloc_info->alloc_size);
+             free_info = get_free_info(cache, object);
+             pr_err("Allocation:\n");
+             print_track(&alloc_info->track);
+             pr_err("Deallocation:\n");
+             print_track(&free_info->track);
+             break;
+     }
+}
+#endif
+
 static void print_address_description(struct kasan_access_info *info)
 {
      const void *addr = info->access_addr;
@@ -126,17 +164,14 @@ static void print_address_description(struct kasan_access_info *info)
              if (PageSlab(page)) {
                      void *object;
                      struct kmem_cache *cache = page->slab_cache;
-                     void *last_object;
-
-                     object = virt_to_obj(cache, page_address(page), addr);
-                     last_object = page_address(page) +
-                             page->objects * cache->size;
-
-                     if (unlikely(object > last_object))
-                             object = last_object; /* we hit into padding */
Quoted 8 lines

mm/kasan/report.c static void print_address_description(struct kasan_access_info *info) · line 171

Quoted 6 lines
mm/kasan/report.c +5 -1
-
+                     object = nearest_obj(cache, page,
+                                             (void *)info->access_addr);
+#ifdef CONFIG_SLAB
+                     print_object(cache, object);
+#else

Instead of these ifdefs, please, make universal API for printing object's information.

My intention here was to touch the SLUB functionality as little as possible to avoid the mess and feature regressions.
I'll be happy to refactor the code in the upcoming patches once this one is landed.

Quoted 7 lines
Quoted 7 lines
mm/kasan/report.c +2 -1
                      object_err(cache, page, object,
-                             "kasan: bad access detected");
+                                     "kasan: bad access detected");
+#endif
                      return;
              }
              dump_page(page, "kasan: bad access detected");
Quoted 8 lines

mm/kasan/report.c static void print_address_description(struct kasan_access_info *info) · line 184

Quoted 6 lines
mm/kasan/report.c +1 -1
@@ -146,8 +181,9 @@ static void print_address_description(struct kasan_access_info *info)
              if (!init_task_stack_addr(addr))
                      pr_err("Address belongs to variable %pS\n", addr);
      }
-
+#ifdef CONFIG_SLUB

???

Not sure what did you mean here, assuming this comment is related to the next one.

Quoted 5 lines
Quoted 5 lines
mm/kasan/report.c +1 -0
      dump_stack();
+#endif
 }
 
Quoted 8 lines

mm/kasan/report.c static void kasan_report_error(struct kasan_access_info *info) · line 272

Quoted 6 lines
mm/kasan/report.c +1 -0
 static bool row_is_guilty(const void *row, const void *guilty)
@@ -233,6 +269,9 @@ static void kasan_report_error(struct kasan_access_info *info)
              dump_stack();
      } else {
              print_error_description(info);
+#ifdef CONFIG_SLAB

I'm lost here. What's the point of reordering dump_stack() for CONFIG_SLAB=y?

I should have documented this in the patch description properly. My intention is to make the KASAN reports look more like those in the userspace AddressSanitizer, so I'm moving the memory access stack to the top of the report.
Having seen hundreds and hundreds of ASan reports, we believe that important information must go at the beginning of the error report. First, people usually do not need to read further once they see the access stack.
Second, the whole report may simply not make it to the log (e.g. in the case of a premature shutdown or remote log collection). As said before, I wasn't going to touch the SLUB output format in this patch set, but that also needs to be fixed (I'd also remove some unnecessary info, e.g. the memory dump).

Quoted 11 lines
Quoted 11 lines
mm/kasan/report.c +2 -0
+             dump_stack();
+#endif
              print_address_description(info);
              print_shadow_for_address(info->first_bad_addr);
      }
mm/slab.c +0 -0
index 621fbcb..805b39b 100644
Quoted 12 lines

mm/slab.c free_done: · line 3380

Quoted 10 lines

if (gfpflags_allow_blocking(local_flags))

mm/slab.c +4 -1
@@ -3364,7 +3374,10 @@ free_done:
 static inline void __cache_free(struct kmem_cache *cachep, void *objp,
                              unsigned long caller)
 {
-     struct array_cache *ac = cpu_cache_get(cachep);
+     struct array_cache *ac;
+
+     kasan_slab_free(cachep, objp);
+     ac = cpu_cache_get(cachep);

Why cpu_cache_get() was moved? Looks like unnecessary change.

Agreed.

Quoted 10 lines

mm/slab.c static inline void __cache_free(struct kmem_cache *cachep, void *objp, · line 3419

Quoted 8 lines
mm/slab.c +1 -0
      check_irq_off();
      kmemleak_free_recursive(objp, cachep->flags);
@@ -3403,6 +3416,8 @@ static inline void __cache_free(struct kmem_cache *cachep, void *objp,
 void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
 {
      void *ret = slab_alloc(cachep, flags, _RET_IP_);
+     if (ret)

kasan_slab_alloc() should deal fine with ret == NULL.

And it actually does. I'll remove this code in the updated patch set.

Quoted 5 lines

mm/slab.c static inline void __cache_free(struct kmem_cache *cachep, void *objp, · line 3423

Quoted 5 lines
mm/slab.c +1 -0
+             kasan_slab_alloc(cachep, ret);
 
      trace_kmem_cache_alloc(_RET_IP_, ret,
                             cachep->object_size, cachep->size, flags);

--
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Diese E-Mail ist vertraulich. Wenn Sie nicht der richtige Adressat sind, leiten Sie diese bitte nicht weiter, informieren Sie den Absender und löschen Sie die E-Mail und alle Anhänge. Vielen Dank. This e-mail is confidential. If you are not the right addressee please do not forward it, please inform the sender, and please erase this e-mail including any attachments. Thanks.

Signature · 22 lines
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Diese E-Mail ist vertraulich. Wenn Sie nicht der richtige Adressat sind,
leiten Sie diese bitte nicht weiter, informieren Sie den
Absender und löschen Sie die E-Mail und alle Anhänge. Vielen Dank.
This e-mail is confidential. If you are not the right addressee please
do not forward it, please inform the sender, and please erase this
e-mail including any attachments. Thanks.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#6 lore raw

Re: [PATCH v4 2/7] mm, kasan: SLAB support

In reply to Alexander Potapenko
Quoted 23 lines · Alexander Potapenko
Quoted 15 lines
Quoted 15 lines

static void print_address_description(struct kasan_access_info *info)

 {
      const void *addr = info->access_addr;
mm/kasan/report.c +0 -8
@@ -126,17 +164,14 @@ static void print_address_description(struct kasan_access_info *info)
              if (PageSlab(page)) {
                      void *object;
                      struct kmem_cache *cache = page->slab_cache;
-                     void *last_object;
-
-                     object = virt_to_obj(cache, page_address(page), addr);
-                     last_object = page_address(page) +
-                             page->objects * cache->size;
-
-                     if (unlikely(object > last_object))
-                             object = last_object; /* we hit into padding */
Quoted 8 lines

mm/kasan/report.c static void print_address_description(struct kasan_access_info *info) · line 171

Quoted 6 lines
mm/kasan/report.c +5 -1
-
+                     object = nearest_obj(cache, page,
+                                             (void *)info->access_addr);
+#ifdef CONFIG_SLAB
+                     print_object(cache, object);
+#else

Instead of these ifdefs, please, make universal API for printing object's information.

Quoted 5 lines

My intention here was to touch the SLUB functionality as little as possible to avoid the mess and feature regressions.
I'll be happy to refactor the code in the upcoming patches once this one is landed.

Avoid mess? You create one.
Although I don't understand that don't touch slub thing, but you can just have object_err(cache, page, str) for slab without touching slub.

Quoted 41 lines
Quoted 7 lines
Quoted 7 lines
mm/kasan/report.c +2 -1
                      object_err(cache, page, object,
-                             "kasan: bad access detected");
+                                     "kasan: bad access detected");
+#endif
                      return;
              }
              dump_page(page, "kasan: bad access detected");
Quoted 8 lines

mm/kasan/report.c static void print_address_description(struct kasan_access_info *info) · line 184

Quoted 6 lines
mm/kasan/report.c +1 -1
@@ -146,8 +181,9 @@ static void print_address_description(struct kasan_access_info *info)
              if (!init_task_stack_addr(addr))
                      pr_err("Address belongs to variable %pS\n", addr);
      }
-
+#ifdef CONFIG_SLUB

???

Not sure what did you mean here, assuming this comment is related to the next one.

Quoted 5 lines
Quoted 5 lines
mm/kasan/report.c +1 -0
      dump_stack();
+#endif
 }
 
Quoted 8 lines

mm/kasan/report.c static void kasan_report_error(struct kasan_access_info *info) · line 272

Quoted 6 lines
mm/kasan/report.c +1 -0
 static bool row_is_guilty(const void *row, const void *guilty)
@@ -233,6 +269,9 @@ static void kasan_report_error(struct kasan_access_info *info)
              dump_stack();
      } else {
              print_error_description(info);
+#ifdef CONFIG_SLAB

I'm lost here. What's the point of reordering dump_stack() for CONFIG_SLAB=y?

I should have documented this in the patch description properly. My intention is to make the KASAN reports look more like those in the userspace AddressSanitizer, so I'm moving the memory access stack to the top of the report.
Having seen hundreds and hundreds of ASan reports, we believe that important information must go at the beginning of the error report. First, people usually do not need to read further once they see the access stack.
Second, the whole report may simply not make it to the log (e.g. in the case of a premature shutdown or remote log collection).

Quoted 4 lines

As said before, I wasn't going to touch the SLUB output format in this patch set, but that also needs to be fixed (I'd also remove some unnecessary info, e.g. the memory dump).

That's all sounds fine, but this doesn't explain:
a) How this change related to this patch? (the answer is - it doesn't).
b) Why the output of non sl[a,u]b bugs depends on CONFIG_SL[A,U]B ?

So, in SLAB's print_objects() you can print stacks in whatever order you like. That's it, don't change anything else here.

If you are not satisfied with current format output, change it, but in separate patch[es], with reasoning described in changelog and without weird config dependencies.

Quoted 8 lines
Quoted 8 lines

mm/slab.c

Quoted 7 lines
mm/kasan/report.c +2 -0
+             dump_stack();
+#endif
              print_address_description(info);
              print_shadow_for_address(info->first_bad_addr);
      }
mm/slab.c +0 -0
index 621fbcb..805b39b 100644

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author reviewer
#7 lore raw

PATCH v4 3/7 mm, kasan: Added GFP flags to KASAN API

In reply to Alexander Potapenko

Add GFP flags to KASAN hooks for future patches to use.

This patch is based on the "mm: kasan: unified support for SLUB and SLAB allocators" patch originally prepared by Dmitry Chernenkov.

---
v4: - fix kbuild compilation error (missing parameter for kasan_kmalloc())

---
 include/linux/kasan.h | 19 +++++++++++--------
 include/linux/slab.h  |  4 ++--
 mm/kasan/kasan.c      | 15 ++++++++-------
 mm/mempool.c          | 16 ++++++++--------
 mm/slab.c             | 14 +++++++-------
 mm/slab_common.c      |  4 ++--
 mm/slub.c             | 17 +++++++++--------
 7 files changed, 47 insertions(+), 42 deletions(-)
include/linux/kasan.h +11 -8
index 4405a35..bf71ab0 100644
@@ -53,13 +53,14 @@ void kasan_poison_slab(struct page *page);
 void kasan_unpoison_object_data(struct kmem_cache *cache, void *object);
 void kasan_poison_object_data(struct kmem_cache *cache, void *object);
 
-void kasan_kmalloc_large(const void *ptr, size_t size);
+void kasan_kmalloc_large(const void *ptr, size_t size, gfp_t flags);
 void kasan_kfree_large(const void *ptr);
 void kasan_kfree(void *ptr);
-void kasan_kmalloc(struct kmem_cache *s, const void *object, size_t size);
-void kasan_krealloc(const void *object, size_t new_size);
+void kasan_kmalloc(struct kmem_cache *s, const void *object, size_t size,
+		  gfp_t flags);
+void kasan_krealloc(const void *object, size_t new_size, gfp_t flags);
 
-void kasan_slab_alloc(struct kmem_cache *s, void *object);
+void kasan_slab_alloc(struct kmem_cache *s, void *object, gfp_t flags);
 void kasan_slab_free(struct kmem_cache *s, void *object);
 
 struct kasan_cache {
@@ -90,14 +91,16 @@ static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
 static inline void kasan_poison_object_data(struct kmem_cache *cache,
 					void *object) {}
 
-static inline void kasan_kmalloc_large(void *ptr, size_t size) {}
+static inline void kasan_kmalloc_large(void *ptr, size_t size, gfp_t flags) {}
 static inline void kasan_kfree_large(const void *ptr) {}
 static inline void kasan_kfree(void *ptr) {}
 static inline void kasan_kmalloc(struct kmem_cache *s, const void *object,
-				size_t size) {}
-static inline void kasan_krealloc(const void *object, size_t new_size) {}
+				size_t size, gfp_t flags) {}
+static inline void kasan_krealloc(const void *object, size_t new_size,
+				 gfp_t flags) {}
 
-static inline void kasan_slab_alloc(struct kmem_cache *s, void *object) {}
+static inline void kasan_slab_alloc(struct kmem_cache *s, void *object,
+				   gfp_t flags) {}
 static inline void kasan_slab_free(struct kmem_cache *s, void *object) {}
 
 static inline int kasan_module_alloc(void *addr, size_t size) { return 0; }
include/linux/slab.h +2 -2
index 840e652..b873343 100644
@@ -367,7 +367,7 @@ static __always_inline void *kmem_cache_alloc_trace(struct kmem_cache *s,
 {
 	void *ret = kmem_cache_alloc(s, flags);
 
-	kasan_kmalloc(s, ret, size);
+	kasan_kmalloc(s, ret, size, flags);
 	return ret;
 }
 
@@ -378,7 +378,7 @@ kmem_cache_alloc_node_trace(struct kmem_cache *s,
 {
 	void *ret = kmem_cache_alloc_node(s, gfpflags, node);
 
-	kasan_kmalloc(s, ret, size);
+	kasan_kmalloc(s, ret, size, gfpflags);
 	return ret;
 }
 #endif /* CONFIG_TRACING */
mm/kasan/kasan.c +8 -7
index d26ffb4..95b2267 100644
@@ -414,9 +414,9 @@ struct kasan_free_meta *get_free_info(struct kmem_cache *cache,
 }
 #endif
 
-void kasan_slab_alloc(struct kmem_cache *cache, void *object)
+void kasan_slab_alloc(struct kmem_cache *cache, void *object, gfp_t flags)
 {
-	kasan_kmalloc(cache, object, cache->object_size);
+	kasan_kmalloc(cache, object, cache->object_size, flags);
 }
 
 void kasan_slab_free(struct kmem_cache *cache, void *object)
@@ -442,7 +442,8 @@ void kasan_slab_free(struct kmem_cache *cache, void *object)
 	kasan_poison_shadow(object, rounded_up_size, KASAN_KMALLOC_FREE);
 }
 
-void kasan_kmalloc(struct kmem_cache *cache, const void *object, size_t size)
+void kasan_kmalloc(struct kmem_cache *cache, const void *object, size_t size,
+		   gfp_t flags)
 {
 	unsigned long redzone_start;
 	unsigned long redzone_end;
@@ -471,7 +472,7 @@ void kasan_kmalloc(struct kmem_cache *cache, const void *object, size_t size)
 }
 EXPORT_SYMBOL(kasan_kmalloc);
 
-void kasan_kmalloc_large(const void *ptr, size_t size)
+void kasan_kmalloc_large(const void *ptr, size_t size, gfp_t flags)
 {
 	struct page *page;
 	unsigned long redzone_start;
@@ -490,7 +491,7 @@ void kasan_kmalloc_large(const void *ptr, size_t size)
 		KASAN_PAGE_REDZONE);
 }
 
-void kasan_krealloc(const void *object, size_t size)
+void kasan_krealloc(const void *object, size_t size, gfp_t flags)
 {
 	struct page *page;
 
@@ -500,9 +501,9 @@ void kasan_krealloc(const void *object, size_t size)
 	page = virt_to_head_page(object);
 
 	if (unlikely(!PageSlab(page)))
-		kasan_kmalloc_large(object, size);
+		kasan_kmalloc_large(object, size, flags);
 	else
-		kasan_kmalloc(page->slab_cache, object, size);
+		kasan_kmalloc(page->slab_cache, object, size, flags);
 }
 
 void kasan_kfree(void *ptr)
mm/mempool.c +8 -8
index 004d42b..b47c8a7 100644
@@ -112,12 +112,12 @@ static void kasan_poison_element(mempool_t *pool, void *element)
 		kasan_free_pages(element, (unsigned long)pool->pool_data);
 }
 
-static void kasan_unpoison_element(mempool_t *pool, void *element)
+static void kasan_unpoison_element(mempool_t *pool, void *element, gfp_t flags)
 {
 	if (pool->alloc == mempool_alloc_slab)
-		kasan_slab_alloc(pool->pool_data, element);
+		kasan_slab_alloc(pool->pool_data, element, flags);
 	if (pool->alloc == mempool_kmalloc)
-		kasan_krealloc(element, (size_t)pool->pool_data);
+		kasan_krealloc(element, (size_t)pool->pool_data, flags);
 	if (pool->alloc == mempool_alloc_pages)
 		kasan_alloc_pages(element, (unsigned long)pool->pool_data);
 }
@@ -130,13 +130,13 @@ static void add_element(mempool_t *pool, void *element)
 	pool->elements[pool->curr_nr++] = element;
 }
 
-static void *remove_element(mempool_t *pool)
+static void *remove_element(mempool_t *pool, gfp_t flags)
 {
 	void *element = pool->elements[--pool->curr_nr];
 
 	BUG_ON(pool->curr_nr < 0);
 	check_element(pool, element);
-	kasan_unpoison_element(pool, element);
+	kasan_unpoison_element(pool, element, flags);
 	return element;
 }
 
@@ -154,7 +154,7 @@ void mempool_destroy(mempool_t *pool)
 		return;
 
 	while (pool->curr_nr) {
-		void *element = remove_element(pool);
+		void *element = remove_element(pool, GFP_KERNEL);
 		pool->free(element, pool->pool_data);
 	}
 	kfree(pool->elements);
@@ -250,7 +250,7 @@ int mempool_resize(mempool_t *pool, int new_min_nr)
 	spin_lock_irqsave(&pool->lock, flags);
 	if (new_min_nr <= pool->min_nr) {
 		while (new_min_nr < pool->curr_nr) {
-			element = remove_element(pool);
+			element = remove_element(pool, GFP_KERNEL);
 			spin_unlock_irqrestore(&pool->lock, flags);
 			pool->free(element, pool->pool_data);
 			spin_lock_irqsave(&pool->lock, flags);
@@ -336,7 +336,7 @@ repeat_alloc:
 
 	spin_lock_irqsave(&pool->lock, flags);
 	if (likely(pool->curr_nr)) {
-		element = remove_element(pool);
+		element = remove_element(pool, gfp_temp);
 		spin_unlock_irqrestore(&pool->lock, flags);
 		/* paired with rmb in mempool_free(), read comment there */
 		smp_wmb();
mm/slab.c +7 -7
index 805b39b..52a7a8d 100644
@@ -3417,7 +3417,7 @@ void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
 {
 	void *ret = slab_alloc(cachep, flags, _RET_IP_);
 	if (ret)
-		kasan_slab_alloc(cachep, ret);
+		kasan_slab_alloc(cachep, ret, flags);
 
 	trace_kmem_cache_alloc(_RET_IP_, ret,
 			       cachep->object_size, cachep->size, flags);
@@ -3448,7 +3448,7 @@ kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
 	ret = slab_alloc(cachep, flags, _RET_IP_);
 
 	if (ret)
-		kasan_kmalloc(cachep, ret, size);
+		kasan_kmalloc(cachep, ret, size, flags);
 	trace_kmalloc(_RET_IP_, ret,
 		      size, cachep->size, flags);
 	return ret;
@@ -3473,7 +3473,7 @@ void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
 	void *ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
 
 	if (ret)
-		kasan_slab_alloc(cachep, ret);
+		kasan_slab_alloc(cachep, ret, flags);
 	trace_kmem_cache_alloc_node(_RET_IP_, ret,
 				    cachep->object_size, cachep->size,
 				    flags, nodeid);
@@ -3493,7 +3493,7 @@ void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
 	ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
 
 	if (ret)
-		kasan_kmalloc(cachep, ret, size);
+		kasan_kmalloc(cachep, ret, size, flags);
 	trace_kmalloc_node(_RET_IP_, ret,
 			   size, cachep->size,
 			   flags, nodeid);
@@ -3513,7 +3513,7 @@ __do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller)
 		return cachep;
 	ret = kmem_cache_alloc_node_trace(cachep, flags, node, size);
 	if (ret)
-		kasan_kmalloc(cachep, ret, size);
+		kasan_kmalloc(cachep, ret, size, flags);
 
 	return ret;
 }
@@ -3550,7 +3550,7 @@ static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
 	ret = slab_alloc(cachep, flags, caller);
 
 	if (ret)
-		kasan_kmalloc(cachep, ret, size);
+		kasan_kmalloc(cachep, ret, size, flags);
 	trace_kmalloc(caller, ret,
 		      size, cachep->size, flags);
 
@@ -4278,7 +4278,7 @@ size_t ksize(const void *objp)
 	/* We assume that ksize callers could use whole allocated area,
 	 * so we need to unpoison this area.
 	 */
-	kasan_krealloc(objp, size);
+	kasan_krealloc(objp, size, GFP_NOWAIT);
 
 	return size;
 }
mm/slab_common.c +2 -2
index bf04ec7..538f616 100644
@@ -1009,7 +1009,7 @@ void *kmalloc_order(size_t size, gfp_t flags, unsigned int order)
 	page = alloc_kmem_pages(flags, order);
 	ret = page ? page_address(page) : NULL;
 	kmemleak_alloc(ret, size, 1, flags);
-	kasan_kmalloc_large(ret, size);
+	kasan_kmalloc_large(ret, size, flags);
 	return ret;
 }
 EXPORT_SYMBOL(kmalloc_order);
@@ -1190,7 +1190,7 @@ static __always_inline void *__do_krealloc(const void *p, size_t new_size,
 		ks = ksize(p);
 
 	if (ks >= new_size) {
-		kasan_krealloc((void *)p, new_size);
+		kasan_krealloc((void *)p, new_size, flags);
 		return (void *)p;
 	}
 
mm/slub.c +9 -8
index d8fbd4a..2978695 100644
@@ -1272,7 +1272,7 @@ static inline void dec_slabs_node(struct kmem_cache *s, int node,
 static inline void kmalloc_large_node_hook(void *ptr, size_t size, gfp_t flags)
 {
 	kmemleak_alloc(ptr, size, 1, flags);
-	kasan_kmalloc_large(ptr, size);
+	kasan_kmalloc_large(ptr, size, flags);
 }
 
 static inline void kfree_hook(const void *x)
@@ -1306,7 +1306,7 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags,
 		kmemcheck_slab_alloc(s, flags, object, slab_ksize(s));
 		kmemleak_alloc_recursive(object, s->object_size, 1,
 					 s->flags, flags);
-		kasan_slab_alloc(s, object);
+		kasan_slab_alloc(s, object, flags);
 	}
 	memcg_kmem_put_cache(s);
 }
@@ -2584,7 +2584,7 @@ void *kmem_cache_alloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size)
 {
 	void *ret = slab_alloc(s, gfpflags, _RET_IP_);
 	trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags);
-	kasan_kmalloc(s, ret, size);
+	kasan_kmalloc(s, ret, size, gfpflags);
 	return ret;
 }
 EXPORT_SYMBOL(kmem_cache_alloc_trace);
@@ -2612,7 +2612,7 @@ void *kmem_cache_alloc_node_trace(struct kmem_cache *s,
 	trace_kmalloc_node(_RET_IP_, ret,
 			   size, s->size, gfpflags, node);
 
-	kasan_kmalloc(s, ret, size);
+	kasan_kmalloc(s, ret, size, gfpflags);
 	return ret;
 }
 EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
@@ -3156,7 +3156,8 @@ static void early_kmem_cache_node_alloc(int node)
 	init_object(kmem_cache_node, n, SLUB_RED_ACTIVE);
 	init_tracking(kmem_cache_node, n);
 #endif
-	kasan_kmalloc(kmem_cache_node, n, sizeof(struct kmem_cache_node));
+	kasan_kmalloc(kmem_cache_node, n, sizeof(struct kmem_cache_node),
+		      GFP_KERNEL);
 	init_kmem_cache_node(n);
 	inc_slabs_node(kmem_cache_node, node, page->objects);
 
@@ -3531,7 +3532,7 @@ void *__kmalloc(size_t size, gfp_t flags)
 
 	trace_kmalloc(_RET_IP_, ret, size, s->size, flags);
 
-	kasan_kmalloc(s, ret, size);
+	kasan_kmalloc(s, ret, size, flags);
 
 	return ret;
 }
@@ -3576,7 +3577,7 @@ void *__kmalloc_node(size_t size, gfp_t flags, int node)
 
 	trace_kmalloc_node(_RET_IP_, ret, size, s->size, flags, node);
 
-	kasan_kmalloc(s, ret, size);
+	kasan_kmalloc(s, ret, size, flags);
 
 	return ret;
 }
@@ -3605,7 +3606,7 @@ size_t ksize(const void *object)
 	size_t size = __ksize(object);
 	/* We assume that ksize callers could use whole allocated area,
 	   so we need unpoison this area. */
-	kasan_krealloc(object, size);
+	kasan_krealloc(object, size, GFP_NOWAIT);
 	return size;
 }
 EXPORT_SYMBOL(ksize);
Signature · 7 lines
2.7.0.rc3.207.g0ac5344

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
author reviewer
#8 lore raw

PATCH v4 4/7 arch, ftrace: For KASAN put hard/soft IRQ entries into separate sections

In reply to Alexander Potapenko

KASAN needs to know whether the allocation happens in an IRQ handler. This lets us strip everything below the IRQ entry point to reduce the number of unique stack traces needed to be stored.

Move the definition of __irq_entry to <linux/interrupt.h> so that the users don't need to pull in <linux/ftrace.h>. Also introduce the __softirq_entry macro which is similar to __irq_entry, but puts the corresponding functions to the .softirqentry.text section.

---
v2: - per request from Steven Rostedt, moved the declarations of __softirq_entry and __irq_entry to <linux/interrupt.h>

v3: - minor description changes

---
 arch/arm/kernel/vmlinux.lds.S        |  1 +
 arch/arm64/kernel/vmlinux.lds.S      |  1 +
 arch/blackfin/kernel/vmlinux.lds.S   |  1 +
 arch/c6x/kernel/vmlinux.lds.S        |  1 +
 arch/metag/kernel/vmlinux.lds.S      |  1 +
 arch/microblaze/kernel/vmlinux.lds.S |  1 +
 arch/mips/kernel/vmlinux.lds.S       |  1 +
 arch/nios2/kernel/vmlinux.lds.S      |  1 +
 arch/openrisc/kernel/vmlinux.lds.S   |  1 +
 arch/parisc/kernel/vmlinux.lds.S     |  1 +
 arch/powerpc/kernel/vmlinux.lds.S    |  1 +
 arch/s390/kernel/vmlinux.lds.S       |  1 +
 arch/sh/kernel/vmlinux.lds.S         |  1 +
 arch/sparc/kernel/vmlinux.lds.S      |  1 +
 arch/tile/kernel/vmlinux.lds.S       |  1 +
 arch/x86/kernel/vmlinux.lds.S        |  1 +
 include/asm-generic/vmlinux.lds.h    | 12 +++++++++++-
 include/linux/ftrace.h               | 11 -----------
 include/linux/interrupt.h            | 20 ++++++++++++++++++++
 kernel/softirq.c                     |  2 +-
 kernel/trace/trace_functions_graph.c |  1 +
 21 files changed, 49 insertions(+), 13 deletions(-)
arch/arm/kernel/vmlinux.lds.S +1 -0
index 8b60fde..28b690fc 100644
@@ -105,6 +105,7 @@ SECTIONS
 			*(.exception.text)
 			__exception_text_end = .;
 			IRQENTRY_TEXT
+			SOFTIRQENTRY_TEXT
 			TEXT_TEXT
 			SCHED_TEXT
 			LOCK_TEXT
arch/arm64/kernel/vmlinux.lds.S +1 -0
index e3928f5..b9242b7 100644
@@ -102,6 +102,7 @@ SECTIONS
 			*(.exception.text)
 			__exception_text_end = .;
 			IRQENTRY_TEXT
+			SOFTIRQENTRY_TEXT
 			TEXT_TEXT
 			SCHED_TEXT
 			LOCK_TEXT
arch/blackfin/kernel/vmlinux.lds.S +1 -0
index c9eec84..d920b95 100644
@@ -35,6 +35,7 @@ SECTIONS
 #endif
 		LOCK_TEXT
 		IRQENTRY_TEXT
+		SOFTIRQENTRY_TEXT
 		KPROBES_TEXT
 #ifdef CONFIG_ROMKERNEL
 		__sinittext = .;
arch/c6x/kernel/vmlinux.lds.S +1 -0
index 5a6e141..50bc10f 100644
@@ -72,6 +72,7 @@ SECTIONS
 		SCHED_TEXT
 		LOCK_TEXT
 		IRQENTRY_TEXT
+		SOFTIRQENTRY_TEXT
 		KPROBES_TEXT
 		*(.fixup)
 		*(.gnu.warning)
arch/metag/kernel/vmlinux.lds.S +1 -0
index e12055e..150ace9 100644
@@ -24,6 +24,7 @@ SECTIONS
 	LOCK_TEXT
 	KPROBES_TEXT
 	IRQENTRY_TEXT
+	SOFTIRQENTRY_TEXT
 	*(.text.*)
 	*(.gnu.warning)
 	}
arch/microblaze/kernel/vmlinux.lds.S +1 -0
index be9488d..0a47f04 100644
@@ -36,6 +36,7 @@ SECTIONS {
 		LOCK_TEXT
 		KPROBES_TEXT
 		IRQENTRY_TEXT
+		SOFTIRQENTRY_TEXT
 		. = ALIGN (4) ;
 		_etext = . ;
 	}
arch/mips/kernel/vmlinux.lds.S +1 -0
index 0a93e83..54d653e 100644
@@ -58,6 +58,7 @@ SECTIONS
 		LOCK_TEXT
 		KPROBES_TEXT
 		IRQENTRY_TEXT
+		SOFTIRQENTRY_TEXT
 		*(.text.*)
 		*(.fixup)
 		*(.gnu.warning)
arch/nios2/kernel/vmlinux.lds.S +1 -0
index 326fab4..e23e895 100644
@@ -39,6 +39,7 @@ SECTIONS
 		SCHED_TEXT
 		LOCK_TEXT
 		IRQENTRY_TEXT
+		SOFTIRQENTRY_TEXT
 		KPROBES_TEXT
 	} =0
 	_etext = .;
arch/openrisc/kernel/vmlinux.lds.S +1 -0
index 2d69a85..d936de4 100644
@@ -50,6 +50,7 @@ SECTIONS
 	  LOCK_TEXT
 	  KPROBES_TEXT
 	  IRQENTRY_TEXT
+	  SOFTIRQENTRY_TEXT
 	  *(.fixup)
 	  *(.text.__*)
 	  _etext = .;
arch/parisc/kernel/vmlinux.lds.S +1 -0
index 308f290..f3ead0b 100644
@@ -72,6 +72,7 @@ SECTIONS
 		LOCK_TEXT
 		KPROBES_TEXT
 		IRQENTRY_TEXT
+		SOFTIRQENTRY_TEXT
 		*(.text.do_softirq)
 		*(.text.sys_exit)
 		*(.text.do_sigaltstack)
arch/powerpc/kernel/vmlinux.lds.S +1 -0
index d41fd0a..2dd91f7 100644
@@ -55,6 +55,7 @@ SECTIONS
 		LOCK_TEXT
 		KPROBES_TEXT
 		IRQENTRY_TEXT
+		SOFTIRQENTRY_TEXT
 
 #ifdef CONFIG_PPC32
 		*(.got1)
arch/s390/kernel/vmlinux.lds.S +1 -0
index 445657f..0f41a82 100644
@@ -28,6 +28,7 @@ SECTIONS
 		LOCK_TEXT
 		KPROBES_TEXT
 		IRQENTRY_TEXT
+		SOFTIRQENTRY_TEXT
 		*(.fixup)
 		*(.gnu.warning)
 	} :text = 0x0700
arch/sh/kernel/vmlinux.lds.S +1 -0
index db88cbf..235a410 100644
@@ -39,6 +39,7 @@ SECTIONS
 		LOCK_TEXT
 		KPROBES_TEXT
 		IRQENTRY_TEXT
+		SOFTIRQENTRY_TEXT
 		*(.fixup)
 		*(.gnu.warning)
 		_etext = .;		/* End of text section */
arch/sparc/kernel/vmlinux.lds.S +1 -0
index f1a2f68..aadd321 100644
@@ -48,6 +48,7 @@ SECTIONS
 		LOCK_TEXT
 		KPROBES_TEXT
 		IRQENTRY_TEXT
+		SOFTIRQENTRY_TEXT
 		*(.gnu.warning)
 	} = 0
 	_etext = .;
arch/tile/kernel/vmlinux.lds.S +1 -0
index 0e059a0..378f5d8 100644
@@ -45,6 +45,7 @@ SECTIONS
     LOCK_TEXT
     KPROBES_TEXT
     IRQENTRY_TEXT
+    SOFTIRQENTRY_TEXT
     __fix_text_end = .;   /* tile-cpack won't rearrange before this */
     ALIGN_FUNCTION();
     *(.hottext*)
arch/x86/kernel/vmlinux.lds.S +1 -0
index 74e4bf1..056a97a 100644
@@ -102,6 +102,7 @@ SECTIONS
 		KPROBES_TEXT
 		ENTRY_TEXT
 		IRQENTRY_TEXT
+		SOFTIRQENTRY_TEXT
 		*(.fixup)
 		*(.gnu.warning)
 		/* End of text section */
include/asm-generic/vmlinux.lds.h +11 -1
index c4bd0e2..b470421 100644
@@ -456,7 +456,7 @@
 		*(.entry.text)						\
 		VMLINUX_SYMBOL(__entry_text_end) = .;
 
-#ifdef CONFIG_FUNCTION_GRAPH_TRACER
+#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
 #define IRQENTRY_TEXT							\
 		ALIGN_FUNCTION();					\
 		VMLINUX_SYMBOL(__irqentry_text_start) = .;		\
@@ -466,6 +466,16 @@
 #define IRQENTRY_TEXT
 #endif
 
+#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
+#define SOFTIRQENTRY_TEXT						\
+		ALIGN_FUNCTION();					\
+		VMLINUX_SYMBOL(__softirqentry_text_start) = .;		\
+		*(.softirqentry.text)					\
+		VMLINUX_SYMBOL(__softirqentry_text_end) = .;
+#else
+#define SOFTIRQENTRY_TEXT
+#endif
+
 /* Section used for early init (in .S files) */
 #define HEAD_TEXT  *(.head.text)
 
include/linux/ftrace.h +0 -11
index c2b340e..4da848d 100644
@@ -799,16 +799,6 @@ ftrace_push_return_trace(unsigned long ret, unsigned long func, int *depth,
  */
 #define __notrace_funcgraph		notrace
 
-/*
- * We want to which function is an entrypoint of a hardirq.
- * That will help us to put a signal on output.
- */
-#define __irq_entry		 __attribute__((__section__(".irqentry.text")))
-
-/* Limits of hardirq entrypoints */
-extern char __irqentry_text_start[];
-extern char __irqentry_text_end[];
-
 #define FTRACE_NOTRACE_DEPTH 65536
 #define FTRACE_RETFUNC_DEPTH 50
 #define FTRACE_RETSTACK_ALLOC_SIZE 32
@@ -845,7 +835,6 @@ static inline void unpause_graph_tracing(void)
 #else /* !CONFIG_FUNCTION_GRAPH_TRACER */
 
 #define __notrace_funcgraph
-#define __irq_entry
 #define INIT_FTRACE_GRAPH
 
 static inline void ftrace_graph_init_task(struct task_struct *t) { }
include/linux/interrupt.h +20 -0
index 0e95fcc..1dcecaf 100644
@@ -673,4 +673,24 @@ extern int early_irq_init(void);
 extern int arch_probe_nr_irqs(void);
 extern int arch_early_irq_init(void);
 
+#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
+/*
+ * We want to know which function is an entrypoint of a hardirq or a softirq.
+ */
+#define __irq_entry		 __attribute__((__section__(".irqentry.text")))
+#define __softirq_entry  \
+	__attribute__((__section__(".softirqentry.text")))
+
+/* Limits of hardirq entrypoints */
+extern char __irqentry_text_start[];
+extern char __irqentry_text_end[];
+/* Limits of softirq entrypoints */
+extern char __softirqentry_text_start[];
+extern char __softirqentry_text_end[];
+
+#else
+#define __irq_entry
+#define __softirq_entry
+#endif
+
 #endif
kernel/softirq.c +1 -1
index 479e443..359be4f 100644
@@ -227,7 +227,7 @@ static inline bool lockdep_softirq_start(void) { return false; }
 static inline void lockdep_softirq_end(bool in_hardirq) { }
 #endif
 
-asmlinkage __visible void __do_softirq(void)
+asmlinkage __visible void __softirq_entry __do_softirq(void)
 {
 	unsigned long end = jiffies + MAX_SOFTIRQ_TIME;
 	unsigned long old_flags = current->flags;
kernel/trace/trace_functions_graph.c +1 -0
index a663cbb..3e6f7d4 100644
@@ -8,6 +8,7 @@
  */
 #include <linux/uaccess.h>
 #include <linux/ftrace.h>
+#include <linux/interrupt.h>
 #include <linux/slab.h>
 #include <linux/fs.h>
 
Signature · 7 lines
2.7.0.rc3.207.g0ac5344

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#9 lore raw

Re: [PATCH v4 4/7] arch, ftrace: For KASAN put hard/soft IRQ entries into separate sections

reproduce: wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # save the attached .config to linux build tree…

Hi Alexander,

[auto build test ERROR on tip/x86/core]
[also build test ERROR on v4.5-rc5]
[cannot apply to next-20160226]
[if your patch is applied to the wrong git tree, please drop us a note to help improving the system]

url:    https://github.com/0day-ci/linux/commits/Alexander-Potapenko/SLAB-support-for-KASAN/20160227-005318
config: arm-pxa_defconfig (attached as .config)

reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=arm

All errors (new ones prefixed by >>):

>> arch/arm/kernel/traps.c:476:39: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'handle_fiq_as_nmi'

asmlinkage void __exception_irq_entry handle_fiq_as_nmi(struct pt_regs *regs)
^

vim +476 arch/arm/kernel/traps.c

c0e7f7ee Daniel Thompson 2014-09-17  470   * circumstances where non-maskability improves robustness, such as
c0e7f7ee Daniel Thompson 2014-09-17  471   * watchdog or debug logic.
c0e7f7ee Daniel Thompson 2014-09-17  472   *
c0e7f7ee Daniel Thompson 2014-09-17  473   * This handler is not appropriate for general purpose use in drivers
c0e7f7ee Daniel Thompson 2014-09-17  474   * platform code and can be overrideen using set_fiq_handler.
c0e7f7ee Daniel Thompson 2014-09-17  475   */
c0e7f7ee Daniel Thompson 2014-09-17 @476  asmlinkage void __exception_irq_entry handle_fiq_as_nmi(struct pt_regs *regs)
c0e7f7ee Daniel Thompson 2014-09-17  477  {
c0e7f7ee Daniel Thompson 2014-09-17  478  	struct pt_regs *old_regs = set_irq_regs(regs);

c0e7f7ee Daniel Thompson 2014-09-17 479

:::::: The code at line 476 was first introduced by commit
:::::: c0e7f7ee717e2b4c5791e7422424c96b5008c39e ARM: 8150/3: fiq: Replace default FIQ handler

:::::: TO: Daniel Thompson <[email protected]> :::::: CC: Russell King <[email protected]>

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

maintainer Acked-by on v4 4/7
#10 lore raw

Re: [PATCH v4 4/7] arch, ftrace: For KASAN put hard/soft IRQ entries into separate sections

In reply to Alexander Potapenko
Quoted 10 lines · On Fri, 26 Feb 2016 17:48:44 +0100 Alexander Potapenko <[email protected]>…

KASAN needs to know whether the allocation happens in an IRQ handler. This lets us strip everything below the IRQ entry point to reduce the number of unique stack traces needed to be stored.

Move the definition of __irq_entry to <linux/interrupt.h> so that the users don't need to pull in <linux/ftrace.h>. Also introduce the __softirq_entry macro which is similar to __irq_entry, but puts the corresponding functions to the .softirqentry.text section.

-- Steve

Quoted 20 lines

---
v2: - per request from Steven Rostedt, moved the declarations of __softirq_entry and __irq_entry to <linux/interrupt.h>

v3: - minor description changes

---
 arch/arm/kernel/vmlinux.lds.S        |  1 +
 arch/arm64/kernel/vmlinux.lds.S      |  1 +
 arch/blackfin/kernel/vmlinux.lds.S   |  1 +
 arch/c6x/kernel/vmlinux.lds.S        |  1 +
 arch/metag/kernel/vmlinux.lds.S      |  1 +
 arch/microblaze/kernel/vmlinux.lds.S |  1 +
 arch/mips/kernel/vmlinux.lds.S       |  1 +
 arch/nios2/kernel/vmlinux.lds.S      |  1 +
 arch/openrisc/kernel/vmlinux.lds.S   |  1 +
 arch/parisc/kernel/vmlinux.lds.S     |  1 +
 arch/powerpc/kernel/vmlinux.lds.S    |  1 +
 arch/s390/kernel/vmlinux.lds.S       |  1 +
 arch/sh/kernel/vmlinux.lds.S         |  1 +
 arch/sparc/kernel/vmlinux.lds.S      |  1 +
Quoted 10 lines
 arch/tile/kernel/vmlinux.lds.S       |  1 +
 arch/x86/kernel/vmlinux.lds.S        |  1 +
 include/asm-generic/vmlinux.lds.h    | 12 +++++++++++-
 include/linux/ftrace.h               | 11 -----------
 include/linux/interrupt.h            | 20 ++++++++++++++++++++
 kernel/softirq.c                     |  2 +-
 kernel/trace/trace_functions_graph.c |  1 +
 21 files changed, 49 insertions(+), 13 deletions(-)

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author reviewer
#11 lore raw

PATCH v4 5/7 mm, kasan: Stackdepot implementation. Enable stackdepot for SLAB

In reply to Alexander Potapenko

Stack depot will allow KASAN store allocation/deallocation stack traces for memory chunks. The stack traces are stored in a hash table and referenced by handles which reside in the kasan_alloc_meta and kasan_free_meta structures in the allocated memory chunks.

IRQ stack traces are cut below the IRQ entry point to avoid unnecessary duplication.

Right now stackdepot support is only enabled in SLAB allocator. Once KASAN features in SLAB are on par with those in SLUB we can switch SLUB to stackdepot as well, thus removing the dependency on SLUB stack bookkeeping, which wastes a lot of memory.

This patch is based on the "mm: kasan: stack depots" patch originally prepared by Dmitry Chernenkov.

---
v2: - per request from Joonsoo Kim, moved the stackdepot implementation to lib/, as there's a plan to use it for page owner
- added copyright comments
- added comments about smp_load_acquire()/smp_store_release()

v3: - minor description changes

---
 arch/x86/kernel/Makefile   |   1 +
 include/linux/stackdepot.h |  32 ++++++
 lib/Makefile               |   7 ++
 lib/stackdepot.c           | 274 +++++++++++++++++++++++++++++++++++++++++++++
 mm/kasan/Makefile          |   1 +
 mm/kasan/kasan.c           |  51 ++++++++-
 mm/kasan/kasan.h           |   4 +
 mm/kasan/report.c          |   9 ++
 8 files changed, 376 insertions(+), 3 deletions(-)
arch/x86/kernel/Makefile +1 -0
index b1b78ff..500584d 100644
@@ -19,6 +19,7 @@ endif
 KASAN_SANITIZE_head$(BITS).o := n
 KASAN_SANITIZE_dumpstack.o := n
 KASAN_SANITIZE_dumpstack_$(BITS).o := n
+KASAN_SANITIZE_stacktrace.o := n
 
 CFLAGS_irq.o := -I$(src)/../include/asm/trace
 
include/linux/stackdepot.h new file +32 -0
new file mode 100644
index 0000000..b6cbe05
@@ -0,0 +1,32 @@
+/*
+ * A generic stack depot implementation
+ *
+ * Author: Alexander Potapenko <[email protected]>
+ * Copyright (C) 2016 Google, Inc.
+ *
+ * Based on code by Dmitry Chernenkov.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#ifndef _LINUX_STACKDEPOT_H
+#define _LINUX_STACKDEPOT_H
+
+typedef u32 depot_stack_handle;
+
+struct stack_trace;
+
+depot_stack_handle depot_save_stack(struct stack_trace *trace, gfp_t flags);
+
+void depot_fetch_stack(depot_stack_handle handle, struct stack_trace *trace);
+
+#endif
lib/Makefile +7 -0
index a7c26a4..10a4ae3 100644
@@ -167,6 +167,13 @@ obj-$(CONFIG_SG_SPLIT) += sg_split.o
 obj-$(CONFIG_STMP_DEVICE) += stmp_device.o
 obj-$(CONFIG_IRQ_POLL) += irq_poll.o
 
+ifeq ($(CONFIG_KASAN),y)
+ifeq ($(CONFIG_SLAB),y)
+	obj-y	+= stackdepot.o
+	KASAN_SANITIZE_slub.o := n
+endif
+endif
+
 libfdt_files = fdt.o fdt_ro.o fdt_wip.o fdt_rw.o fdt_sw.o fdt_strerror.o \
 	       fdt_empty_tree.o
 $(foreach file, $(libfdt_files), \
lib/stackdepot.c new file +274 -0

274 lines not on this page · raw message

mm/kasan/Makefile +1 -0
index a61460d..32bd73a 100644
@@ -7,3 +7,4 @@ CFLAGS_REMOVE_kasan.o = -pg
 CFLAGS_kasan.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector)
 
 obj-y := kasan.o report.o kasan_init.o
+
mm/kasan/kasan.c +48 -3

92 lines not on this page · raw message

mm/kasan/kasan.h +4 -0
index 7b9e4ab9..b4e5942 100644
@@ -2,6 +2,7 @@
 #define __MM_KASAN_KASAN_H
 
 #include <linux/kasan.h>
+#include <linux/stackdepot.h>
 
 #define KASAN_SHADOW_SCALE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT)
 #define KASAN_SHADOW_MASK       (KASAN_SHADOW_SCALE_SIZE - 1)
@@ -64,10 +65,13 @@ enum kasan_state {
 	KASAN_STATE_FREE
 };
 
+#define KASAN_STACK_DEPTH 64
+
 struct kasan_track {
 	u64 cpu : 6;			/* for NR_CPUS = 64 */
 	u64 pid : 16;			/* 65536 processes */
 	u64 when : 42;			/* ~140 years */
+	depot_stack_handle stack : sizeof(depot_stack_handle);
 };
 
 struct kasan_alloc_meta {
mm/kasan/report.c +9 -0
index 2c1407f..82d8858 100644
@@ -18,6 +18,7 @@
 #include <linux/printk.h>
 #include <linux/sched.h>
 #include <linux/slab.h>
+#include <linux/stackdepot.h>
 #include <linux/stacktrace.h>
 #include <linux/string.h>
 #include <linux/types.h>
@@ -120,6 +121,14 @@ static void print_track(struct kasan_track *track)
 {
 	pr_err("PID = %u, CPU = %u, timestamp = %lu\n", track->pid,
 	       track->cpu, (unsigned long)track->when);
+	if (track->stack) {
+		struct stack_trace trace;
+
+		depot_fetch_stack(track->stack, &trace);
+		print_stack_trace(&trace, 0);
+	} else {
+		pr_err("(stack is not available)\n");
+	}
 }
 
 static void print_object(struct kmem_cache *cache, void *object)
Signature · 7 lines
2.7.0.rc3.207.g0ac5344

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#12 lore raw

Re: [PATCH v4 5/7] mm, kasan: Stackdepot implementation. Enable stackdepot for SLAB

Quoted 33 lines · Alexander Potapenko

Stack depot will allow KASAN store allocation/deallocation stack traces for memory chunks. The stack traces are stored in a hash table and referenced by handles which reside in the kasan_alloc_meta and kasan_free_meta structures in the allocated memory chunks.

IRQ stack traces are cut below the IRQ entry point to avoid unnecessary duplication.

Right now stackdepot support is only enabled in SLAB allocator. Once KASAN features in SLAB are on par with those in SLUB we can switch SLUB to stackdepot as well, thus removing the dependency on SLUB stack bookkeeping, which wastes a lot of memory.

This patch is based on the "mm: kasan: stack depots" patch originally prepared by Dmitry Chernenkov.

---
v2: - per request from Joonsoo Kim, moved the stackdepot implementation to lib/, as there's a plan to use it for page owner
- added copyright comments
- added comments about smp_load_acquire()/smp_store_release()

v3: - minor description changes
---

lib/Makefile +0 -0
index a7c26a4..10a4ae3 100644

lib/Makefile obj-$(CONFIG_SG_SPLIT) += sg_split.o · line 171

Quoted 5 lines
lib/Makefile +2 -0
@@ -167,6 +167,13 @@ obj-$(CONFIG_SG_SPLIT) += sg_split.o
 obj-$(CONFIG_STMP_DEVICE) += stmp_device.o
 obj-$(CONFIG_IRQ_POLL) += irq_poll.o
 
+ifeq ($(CONFIG_KASAN),y)
+ifeq ($(CONFIG_SLAB),y)

Just try to imagine that another subsystem wants to use stackdepot. How this gonna look like?

We have Kconfig to describe dependencies. So, this should be under CONFIG_STACKDEPOT. So any user of this feature can just do 'select STACKDEPOT' in Kconfig.

Quoted 17 lines
lib/Makefile +5 -0
+	obj-y	+= stackdepot.o
+	KASAN_SANITIZE_slub.o := n
+endif
+endif
+
 libfdt_files = fdt.o fdt_ro.o fdt_wip.o fdt_rw.o fdt_sw.o fdt_strerror.o \
 	       fdt_empty_tree.o
 $(foreach file, $(libfdt_files), \
lib/stackdepot.c new file +2 -0
new file mode 100644
index 0000000..f09b0da
 
 
+/* Allocation of a new stack in raw storage */
+static struct stack_record *depot_alloc_stack(unsigned long *entries, int size,

lib/stackdepot.c line 135

Quoted 10 lines
lib/stackdepot.c new file +8 -0
+		u32 hash, void **prealloc, gfp_t alloc_flags)
+{
 
 
+
+	stack->hash = hash;
+	stack->size = size;
+	stack->handle.slabindex = depot_index;
+	stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
+	__memcpy(stack->entries, entries, size * sizeof(unsigned long));

s/__memcpy/memcpy

Quoted 43 lines
lib/stackdepot.c new file +41 -0
+	depot_offset += required_size;
+
+	return stack;
+}
+
 
 
+/*
+ * depot_save_stack - save stack in a stack depot.
+ * @trace - the stacktrace to save.
+ * @alloc_flags - flags for allocating additional memory if required.
+ *
+ * Returns the handle of the stack struct stored in depot.
+ */
+depot_stack_handle depot_save_stack(struct stack_trace *trace,
+				    gfp_t alloc_flags)
+{
+	u32 hash;
+	depot_stack_handle retval = 0;
+	struct stack_record *found = NULL, **bucket;
+	unsigned long flags;
+	struct page *page = NULL;
+	void *prealloc = NULL;
+
+	if (unlikely(trace->nr_entries == 0))
+		goto exit;
+
+	hash = hash_stack(trace->entries, trace->nr_entries);
+	/* Bad luck, we won't store this stack. */
+	if (hash == 0)
+		goto exit;
+
+	bucket = &stack_table[hash & STACK_HASH_MASK];
+
+	/* Fast path: look the stack trace up without locking.
+	 *
+	 * The smp_load_acquire() here pairs with smp_store_release() to
+	 * |bucket| below.
+	 */
+	found = find_stack(smp_load_acquire(bucket), trace->entries,
+			   trace->nr_entries, hash);
+	if (found)
+		goto exit;

lib/stackdepot.c line 233

Quoted 10 lines
lib/stackdepot.c new file +10 -0
+
+	/* Check if the current or the next stack slab need to be initialized.
+	 * If so, allocate the memory - we won't be able to do that under the
+	 * lock.
+	 *
+	 * The smp_load_acquire() here pairs with smp_store_release() to
+	 * |next_slab_inited| in depot_alloc_stack() and init_stack_slab().
+	 */
+	if (unlikely(!smp_load_acquire(&next_slab_inited))) {
+		if (!preempt_count() && !in_irq()) {

If you trying to detect atomic context here, than this doesn't work. E.g. you can't know about held spinlocks in non-preemptible kernel.
And I'm not sure why need this. You know gfp flags here, so allocation in atomic context shouldn't be problem.

lib/stackdepot.c line 236

Quoted 3 lines
lib/stackdepot.c new file +3 -0
+			alloc_flags &= (__GFP_RECLAIM | __GFP_IO | __GFP_FS |
+				__GFP_NOWARN | __GFP_NORETRY |
+				__GFP_NOMEMALLOC | __GFP_DIRECT_RECLAIM);

I think blacklist approach would be better here.

lib/stackdepot.c line 237

Quoted 1 line
lib/stackdepot.c new file +1 -0
+			page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);

STACK_ALLOC_ORDER = 4 - that's a lot. Do you really need that much?

mm/kasan/Makefile CFLAGS_REMOVE_kasan.o = -pg · line 10

Quoted 9 lines
mm/kasan/Makefile +1 -0
index a61460d..32bd73a 100644
@@ -7,3 +7,4 @@ CFLAGS_REMOVE_kasan.o = -pg
 CFLAGS_kasan.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector)
 
 obj-y := kasan.o report.o kasan_init.o
+

Extra newline.

Quoted 13 lines
mm/kasan/kasan.h +1 -0
index 7b9e4ab9..b4e5942 100644
@@ -2,6 +2,7 @@
 #define __MM_KASAN_KASAN_H
 
 #include <linux/kasan.h>
+#include <linux/stackdepot.h>
 
 #define KASAN_SHADOW_SCALE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT)
 #define KASAN_SHADOW_MASK       (KASAN_SHADOW_SCALE_SIZE - 1)

mm/kasan/kasan.h enum kasan_state { · line 68

Quoted 4 lines
mm/kasan/kasan.h +1 -0
@@ -64,10 +65,13 @@ enum kasan_state {
 	KASAN_STATE_FREE
 };
 
+#define KASAN_STACK_DEPTH 64

I think, you can reduce this (32 perhaps?). Kernel stacks are not so deep usually.

mm/kasan/kasan.h enum kasan_state { · line 76

Quoted 8 lines
mm/kasan/kasan.h +2 -0
+
 struct kasan_track {
 	u64 cpu : 6;			/* for NR_CPUS = 64 */
 	u64 pid : 16;			/* 65536 processes */
 	u64 when : 42;			/* ~140 years */
+	depot_stack_handle stack : sizeof(depot_stack_handle);
 };
 

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

reviewer
#13 lore raw

Re: [PATCH v4 5/7] mm, kasan: Stackdepot implementation. Enable stackdepot for SLAB

Quoted 68 lines · Andrey Ryabinin
Quoted 33 lines · Alexander Potapenko

Stack depot will allow KASAN store allocation/deallocation stack traces for memory chunks. The stack traces are stored in a hash table and referenced by handles which reside in the kasan_alloc_meta and kasan_free_meta structures in the allocated memory chunks.

IRQ stack traces are cut below the IRQ entry point to avoid unnecessary duplication.

Right now stackdepot support is only enabled in SLAB allocator. Once KASAN features in SLAB are on par with those in SLUB we can switch SLUB to stackdepot as well, thus removing the dependency on SLUB stack bookkeeping, which wastes a lot of memory.

This patch is based on the "mm: kasan: stack depots" patch originally prepared by Dmitry Chernenkov.

---
v2: - per request from Joonsoo Kim, moved the stackdepot implementation to lib/, as there's a plan to use it for page owner
- added copyright comments
- added comments about smp_load_acquire()/smp_store_release()

v3: - minor description changes
---

lib/Makefile +0 -0
index a7c26a4..10a4ae3 100644

lib/Makefile obj-$(CONFIG_SG_SPLIT) += sg_split.o · line 171

Quoted 5 lines
lib/Makefile +2 -0
@@ -167,6 +167,13 @@ obj-$(CONFIG_SG_SPLIT) += sg_split.o
 obj-$(CONFIG_STMP_DEVICE) += stmp_device.o
 obj-$(CONFIG_IRQ_POLL) += irq_poll.o
 
+ifeq ($(CONFIG_KASAN),y)
+ifeq ($(CONFIG_SLAB),y)

Just try to imagine that another subsystem wants to use stackdepot. How this gonna look like?

We have Kconfig to describe dependencies. So, this should be under CONFIG_STACKDEPOT. So any user of this feature can just do 'select STACKDEPOT' in Kconfig.

Quoted 19 lines
lib/Makefile +5 -0
+     obj-y   += stackdepot.o
+     KASAN_SANITIZE_slub.o := n
+endif
+endif
+
 libfdt_files = fdt.o fdt_ro.o fdt_wip.o fdt_rw.o fdt_sw.o fdt_strerror.o \
             fdt_empty_tree.o
 $(foreach file, $(libfdt_files), \
lib/stackdepot.c new file +4 -0
new file mode 100644
index 0000000..f09b0da
 
 
+/* Allocation of a new stack in raw storage */
+static struct stack_record *depot_alloc_stack(unsigned long *entries, int size,
+             u32 hash, void **prealloc, gfp_t alloc_flags)
+{
Quoted 8 lines

lib/stackdepot.c line 135

Quoted 6 lines
lib/stackdepot.c new file +6 -0
+
+     stack->hash = hash;
+     stack->size = size;
+     stack->handle.slabindex = depot_index;
+     stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
+     __memcpy(stack->entries, entries, size * sizeof(unsigned long));

s/__memcpy/memcpy/

memcpy should be instrumented by asan/tsan, and we would like to avoid that instrumentation here.
It's plain unnecessary and for tsan it can create recursion issues (double entry into runtime).

Quoted 49 lines
Quoted 49 lines
lib/stackdepot.c new file +47 -0
+     depot_offset += required_size;
+
+     return stack;
+}
+
 
 
+/*
+ * depot_save_stack - save stack in a stack depot.
+ * @trace - the stacktrace to save.
+ * @alloc_flags - flags for allocating additional memory if required.
+ *
+ * Returns the handle of the stack struct stored in depot.
+ */
+depot_stack_handle depot_save_stack(struct stack_trace *trace,
+                                 gfp_t alloc_flags)
+{
+     u32 hash;
+     depot_stack_handle retval = 0;
+     struct stack_record *found = NULL, **bucket;
+     unsigned long flags;
+     struct page *page = NULL;
+     void *prealloc = NULL;
+
+     if (unlikely(trace->nr_entries == 0))
+             goto exit;
+
+     hash = hash_stack(trace->entries, trace->nr_entries);
+     /* Bad luck, we won't store this stack. */
+     if (hash == 0)
+             goto exit;
+
+     bucket = &stack_table[hash & STACK_HASH_MASK];
+
+     /* Fast path: look the stack trace up without locking.
+      *
+      * The smp_load_acquire() here pairs with smp_store_release() to
+      * |bucket| below.
+      */
+     found = find_stack(smp_load_acquire(bucket), trace->entries,
+                        trace->nr_entries, hash);
+     if (found)
+             goto exit;
+
+     /* Check if the current or the next stack slab need to be initialized.
+      * If so, allocate the memory - we won't be able to do that under the
+      * lock.
+      *
+      * The smp_load_acquire() here pairs with smp_store_release() to
Quoted 8 lines

lib/stackdepot.c line 233

Quoted 4 lines
lib/stackdepot.c new file +4 -0
+      * |next_slab_inited| in depot_alloc_stack() and init_stack_slab().
+      */
+     if (unlikely(!smp_load_acquire(&next_slab_inited))) {
+             if (!preempt_count() && !in_irq()) {

If you trying to detect atomic context here, than this doesn't work. E.g. you can't know about held spinlocks in non-preemptible kernel.
And I'm not sure why need this. You know gfp flags here, so allocation in atomic context shouldn't be problem.

We don't have gfp flags for kfree.
I wonder how CONFIG_DEBUG_ATOMIC_SLEEP handles this. Maybe it has the answer. Alternatively, we can always assume that we are in atomic context in kfree.

Quoted 9 lines

lib/stackdepot.c line 236

Quoted 3 lines
lib/stackdepot.c new file +3 -0
+                     alloc_flags &= (__GFP_RECLAIM | __GFP_IO | __GFP_FS |
+                             __GFP_NOWARN | __GFP_NORETRY |
+                             __GFP_NOMEMALLOC | __GFP_DIRECT_RECLAIM);

I think blacklist approach would be better here.

Quoted 1 line

+ page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);

STACK_ALLOC_ORDER = 4 - that's a lot. Do you really need that much?

Part of the issue the atomic context above. When we can't allocate memory we still want to save the stack trace. When we have less than STACK_ALLOC_ORDER memory, we try to preallocate another STACK_ALLOC_ORDER in advance. So in the worst case, we have STACK_ALLOC_ORDER memory and that should be enough to handle all kmalloc/kfree in the atomic context. 1 page does not look enough. I think Alex did some measuring of the failure race (when we are out of memory and can't allocate more).

Quoted 29 lines

mm/kasan/Makefile CFLAGS_REMOVE_kasan.o = -pg · line 10

Quoted 9 lines
mm/kasan/Makefile +1 -0
index a61460d..32bd73a 100644
@@ -7,3 +7,4 @@ CFLAGS_REMOVE_kasan.o = -pg
 CFLAGS_kasan.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector)
 
 obj-y := kasan.o report.o kasan_init.o
+

Extra newline.

Quoted 16 lines
mm/kasan/kasan.h +1 -0
index 7b9e4ab9..b4e5942 100644
@@ -2,6 +2,7 @@
 #define __MM_KASAN_KASAN_H
 
 #include <linux/kasan.h>
+#include <linux/stackdepot.h>
 
 #define KASAN_SHADOW_SCALE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT)
 #define KASAN_SHADOW_MASK       (KASAN_SHADOW_SCALE_SIZE - 1)
@@ -64,10 +65,13 @@ enum kasan_state {
      KASAN_STATE_FREE
 };
 
Quoted 3 lines

mm/kasan/kasan.h enum kasan_state { · line 68

Quoted 1 line
mm/kasan/kasan.h +1 -0
+#define KASAN_STACK_DEPTH 64

I think, you can reduce this (32 perhaps?). Kernel stacks are not so deep usually.

We don't allocate whole KASAN_STACK_DEPTH for each stack, we allocate only required memory for a particular stack at hand. So large value here is not an issue. If most stacks are shallow, then we will allocate just that small amount of memory.

Quoted 9 lines

mm/kasan/kasan.h enum kasan_state { · line 76

Quoted 8 lines
mm/kasan/kasan.h +2 -0
+
 struct kasan_track {
      u64 cpu : 6;                    /* for NR_CPUS = 64 */
      u64 pid : 16;                   /* 65536 processes */
      u64 when : 42;                  /* ~140 years */
+     depot_stack_handle stack : sizeof(depot_stack_handle);
 };
 

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#14 lore raw

Re: [PATCH v4 5/7] mm, kasan: Stackdepot implementation. Enable stackdepot for SLAB

Quoted 13 lines · Dmitry Vyukov
Quoted 13 lines

lib/Makefile obj-$(CONFIG_SG_SPLIT) += sg_split.o · line 171

Quoted 10 lines
lib/Makefile +2 -0
index a7c26a4..10a4ae3 100644
@@ -167,6 +167,13 @@ obj-$(CONFIG_SG_SPLIT) += sg_split.o
 obj-$(CONFIG_STMP_DEVICE) += stmp_device.o
 obj-$(CONFIG_IRQ_POLL) += irq_poll.o
 
+ifeq ($(CONFIG_KASAN),y)
+ifeq ($(CONFIG_SLAB),y)

Just try to imagine that another subsystem wants to use stackdepot. How this gonna look like?

Quoted 5 lines
Quoted 5 lines

We have Kconfig to describe dependencies. So, this should be under CONFIG_STACKDEPOT. So any user of this feature can just do 'select STACKDEPOT' in Kconfig.

Quoted 2 lines
lib/Makefile +2 -0
+     obj-y   += stackdepot.o
+     KASAN_SANITIZE_slub.o := n

_stackdepot.o

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
lib/Makefile +6 -0
+
+     stack->hash = hash;
+     stack->size = size;
+     stack->handle.slabindex = depot_index;
+     stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
+     __memcpy(stack->entries, entries, size * sizeof(unsigned long));
Quoted 4 lines
Quoted 1 line

s/__memcpy/memcpy/

memcpy should be instrumented by asan/tsan, and we would like to avoid that instrumentation here.

KASAN_SANITIZE_* := n already takes care about this.
__memcpy() is a special thing solely for kasan internals and some assembly code. And it's not available generally.

Quoted 10 lines
Quoted 6 lines

lib/Makefile obj-$(CONFIG_SG_SPLIT) += sg_split.o

Quoted 2 lines
lib/Makefile +2 -0
+     if (unlikely(!smp_load_acquire(&next_slab_inited))) {
+             if (!preempt_count() && !in_irq()) {

If you trying to detect atomic context here, than this doesn't work. E.g. you can't know about held spinlocks in non-preemptible kernel.
And I'm not sure why need this. You know gfp flags here, so allocation in atomic context shouldn't be problem.

We don't have gfp flags for kfree.
I wonder how CONFIG_DEBUG_ATOMIC_SLEEP handles this. Maybe it has the answer.

It hasn't. It doesn't guarantee that atomic context always will be detected.

Quoted 2 lines

Alternatively, we can always assume that we are in atomic context in kfree.

Or do this allocation in separate context, put in work queue.

Quoted 12 lines
Quoted 11 lines

lib/Makefile

Quoted 5 lines
lib/Makefile +3 -0
+                     alloc_flags &= (__GFP_RECLAIM | __GFP_IO | __GFP_FS |
+                             __GFP_NOWARN | __GFP_NORETRY |
+                             __GFP_NOMEMALLOC | __GFP_DIRECT_RECLAIM);

I think blacklist approach would be better here.

Quoted 1 line

+ page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);

STACK_ALLOC_ORDER = 4 - that's a lot. Do you really need that much?

Quoted 9 lines

Part of the issue the atomic context above. When we can't allocate memory we still want to save the stack trace. When we have less than STACK_ALLOC_ORDER memory, we try to preallocate another STACK_ALLOC_ORDER in advance. So in the worst case, we have STACK_ALLOC_ORDER memory and that should be enough to handle all kmalloc/kfree in the atomic context. 1 page does not look enough. I think Alex did some measuring of the failure race (when we are out of memory and can't allocate more).

A lot of 4-order pages will lead to high fragmentation. You don't need physically contiguous memory here, so try to use vmalloc(). It is slower, but fragmentation won't be problem.

And one more thing. Take a look at mempool, because it's generally used to solve the problem you have here (guaranteed allocation in atomic context).

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author reviewer
#15 lore raw

Re: [PATCH v4 5/7] mm, kasan: Stackdepot implementation. Enable stackdepot for SLAB

Quoted 33 lines · Andrey Ryabinin
Quoted 13 lines · Dmitry Vyukov
Quoted 13 lines

lib/Makefile obj-$(CONFIG_SG_SPLIT) += sg_split.o · line 171

Quoted 10 lines
lib/Makefile +2 -0
index a7c26a4..10a4ae3 100644
@@ -167,6 +167,13 @@ obj-$(CONFIG_SG_SPLIT) += sg_split.o
 obj-$(CONFIG_STMP_DEVICE) += stmp_device.o
 obj-$(CONFIG_IRQ_POLL) += irq_poll.o
 
+ifeq ($(CONFIG_KASAN),y)
+ifeq ($(CONFIG_SLAB),y)

Just try to imagine that another subsystem wants to use stackdepot. How this gonna look like?

Quoted 5 lines
Quoted 5 lines

We have Kconfig to describe dependencies. So, this should be under CONFIG_STACKDEPOT. So any user of this feature can just do 'select STACKDEPOT' in Kconfig.

Quoted 2 lines
lib/Makefile +2 -0
+     obj-y   += stackdepot.o
+     KASAN_SANITIZE_slub.o := n

_stackdepot.o

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
lib/Makefile +6 -0
+
+     stack->hash = hash;
+     stack->size = size;
+     stack->handle.slabindex = depot_index;
+     stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
+     __memcpy(stack->entries, entries, size * sizeof(unsigned long));
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

s/__memcpy/memcpy/

memcpy should be instrumented by asan/tsan, and we would like to avoid that instrumentation here.

KASAN_SANITIZE_* := n already takes care about this.
__memcpy() is a special thing solely for kasan internals and some assembly code. And it's not available generally.

As far as I can see, KASAN_SANITIZE_*:=n does not guarantee it. It just removes KASAN flags from GCC command line, it does not necessarily replace memcpy() calls with some kind of a non-instrumented memcpy().

We see two possible ways to deal with this problem:
1. Define "memcpy" to "__memcpy" in lib/stackdepot.c under CONFIG_KASAN.
2. Create mm/kasan/kasan_stackdepot.c stub which will include lib/stackdepot.c, and define "memcpy" to "__memcpy" in that file. This way we'll be able to instrument the original stackdepot.c and won't miss reports from it if someone starts using it somewhere else.

Quoted 47 lines
Quoted 10 lines
Quoted 6 lines

lib/Makefile obj-$(CONFIG_SG_SPLIT) += sg_split.o

Quoted 2 lines
lib/Makefile +2 -0
+     if (unlikely(!smp_load_acquire(&next_slab_inited))) {
+             if (!preempt_count() && !in_irq()) {

If you trying to detect atomic context here, than this doesn't work. E.g. you can't know about held spinlocks in non-preemptible kernel.
And I'm not sure why need this. You know gfp flags here, so allocation in atomic context shouldn't be problem.

We don't have gfp flags for kfree.
I wonder how CONFIG_DEBUG_ATOMIC_SLEEP handles this. Maybe it has the answer.

It hasn't. It doesn't guarantee that atomic context always will be detected.

Quoted 2 lines

Alternatively, we can always assume that we are in atomic context in kfree.

Or do this allocation in separate context, put in work queue.

Quoted 12 lines
Quoted 11 lines

lib/Makefile

Quoted 5 lines
lib/Makefile +3 -0
+                     alloc_flags &= (__GFP_RECLAIM | __GFP_IO | __GFP_FS |
+                             __GFP_NOWARN | __GFP_NORETRY |
+                             __GFP_NOMEMALLOC | __GFP_DIRECT_RECLAIM);

I think blacklist approach would be better here.

Quoted 1 line

+ page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);

STACK_ALLOC_ORDER = 4 - that's a lot. Do you really need that much?

Quoted 9 lines

Part of the issue the atomic context above. When we can't allocate memory we still want to save the stack trace. When we have less than STACK_ALLOC_ORDER memory, we try to preallocate another STACK_ALLOC_ORDER in advance. So in the worst case, we have STACK_ALLOC_ORDER memory and that should be enough to handle all kmalloc/kfree in the atomic context. 1 page does not look enough. I think Alex did some measuring of the failure race (when we are out of memory and can't allocate more).

A lot of 4-order pages will lead to high fragmentation. You don't need physically contiguous memory here, so try to use vmalloc(). It is slower, but fragmentation won't be problem.

And one more thing. Take a look at mempool, because it's generally used to solve the problem you have here (guaranteed allocation in atomic context).

Signature · 16 lines
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#16 lore raw

Re: [PATCH v4 5/7] mm, kasan: Stackdepot implementation. Enable stackdepot for SLAB

2016-03-04 17:52 GMT+03:00 Alexander Potapenko <[email protected]>:

Quoted 39 lines · Andrey Ryabinin
Quoted 38 lines · Dmitry Vyukov
Quoted 14 lines
Quoted 14 lines

lib/Makefile obj-$(CONFIG_SG_SPLIT) += sg_split.o · line 171

Quoted 11 lines
lib/Makefile +2 -0
index a7c26a4..10a4ae3 100644
@@ -167,6 +167,13 @@ obj-$(CONFIG_SG_SPLIT) += sg_split.o
 obj-$(CONFIG_STMP_DEVICE) += stmp_device.o
 obj-$(CONFIG_IRQ_POLL) += irq_poll.o
 
+ifeq ($(CONFIG_KASAN),y)
+ifeq ($(CONFIG_SLAB),y)

Just try to imagine that another subsystem wants to use stackdepot. How this gonna look like?

Quoted 5 lines
Quoted 5 lines

We have Kconfig to describe dependencies. So, this should be under CONFIG_STACKDEPOT. So any user of this feature can just do 'select STACKDEPOT' in Kconfig.

Quoted 2 lines
lib/Makefile +2 -0
+     obj-y   += stackdepot.o
+     KASAN_SANITIZE_slub.o := n

_stackdepot.o

Quoted 12 lines
Quoted 9 lines

lib/Makefile obj-$(CONFIG_SG_SPLIT) += sg_split.o · line 179

Quoted 7 lines
lib/Makefile +6 -0
+
+     stack->hash = hash;
+     stack->size = size;
+     stack->handle.slabindex = depot_index;
+     stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
+     __memcpy(stack->entries, entries, size * sizeof(unsigned long));

s/__memcpy/memcpy/

memcpy should be instrumented by asan/tsan, and we would like to avoid that instrumentation here.

Quoted 8 lines
Quoted 3 lines

KASAN_SANITIZE_* := n already takes care about this.
__memcpy() is a special thing solely for kasan internals and some assembly code. And it's not available generally.

As far as I can see, KASAN_SANITIZE_*:=n does not guarantee it. It just removes KASAN flags from GCC command line, it does not necessarily replace memcpy() calls with some kind of a non-instrumented memcpy().

With removed kasan cflags '__SANITIZE_ADDRESS__' is not defined, hence enable the following defines from arch/x86/include/asm/string_64.h:

#if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__)

/*
* For files that not instrumented (e.g. mm/slub.c) we
* should use not instrumented version of mem* functions. */

#undef memcpy
#define memcpy(dst, src, len) __memcpy(dst, src, len) #define memmove(dst, src, len) __memmove(dst, src, len) #define memset(s, c, n) __memset(s, c, n) #endif

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author reviewer
#17 lore raw

Re: [PATCH v4 5/7] mm, kasan: Stackdepot implementation. Enable stackdepot for SLAB

Quoted 59 lines · Andrey Ryabinin

2016-03-04 17:52 GMT+03:00 Alexander Potapenko <[email protected]>:

Quoted 39 lines · Andrey Ryabinin
Quoted 38 lines · Dmitry Vyukov
Quoted 14 lines
Quoted 14 lines

lib/Makefile obj-$(CONFIG_SG_SPLIT) += sg_split.o · line 171

Quoted 11 lines
lib/Makefile +2 -0
index a7c26a4..10a4ae3 100644
@@ -167,6 +167,13 @@ obj-$(CONFIG_SG_SPLIT) += sg_split.o
 obj-$(CONFIG_STMP_DEVICE) += stmp_device.o
 obj-$(CONFIG_IRQ_POLL) += irq_poll.o
 
+ifeq ($(CONFIG_KASAN),y)
+ifeq ($(CONFIG_SLAB),y)

Just try to imagine that another subsystem wants to use stackdepot. How this gonna look like?

Quoted 5 lines
Quoted 5 lines

We have Kconfig to describe dependencies. So, this should be under CONFIG_STACKDEPOT. So any user of this feature can just do 'select STACKDEPOT' in Kconfig.

Quoted 2 lines
lib/Makefile +2 -0
+     obj-y   += stackdepot.o
+     KASAN_SANITIZE_slub.o := n

_stackdepot.o

Quoted 12 lines
Quoted 9 lines

lib/Makefile obj-$(CONFIG_SG_SPLIT) += sg_split.o · line 179

Quoted 7 lines
lib/Makefile +6 -0
+
+     stack->hash = hash;
+     stack->size = size;
+     stack->handle.slabindex = depot_index;
+     stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
+     __memcpy(stack->entries, entries, size * sizeof(unsigned long));

s/__memcpy/memcpy/

memcpy should be instrumented by asan/tsan, and we would like to avoid that instrumentation here.

Quoted 8 lines
Quoted 3 lines

KASAN_SANITIZE_* := n already takes care about this.
__memcpy() is a special thing solely for kasan internals and some assembly code. And it's not available generally.

As far as I can see, KASAN_SANITIZE_*:=n does not guarantee it. It just removes KASAN flags from GCC command line, it does not necessarily replace memcpy() calls with some kind of a non-instrumented memcpy().

With removed kasan cflags '__SANITIZE_ADDRESS__' is not defined, hence enable the following defines from arch/x86/include/asm/string_64.h:

#if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__)

/*
* For files that not instrumented (e.g. mm/slub.c) we
* should use not instrumented version of mem* functions. */

Quoted 5 lines

#undef memcpy
#define memcpy(dst, src, len) __memcpy(dst, src, len) #define memmove(dst, src, len) __memmove(dst, src, len) #define memset(s, c, n) __memset(s, c, n) #endif

Nice!
What do you think about providing stub .c files to decouple the shared code used by KASAN runtime from the rest of kernel?
(This is a completely different story though and can be done separately).

Signature · 16 lines
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#18 lore raw

Re: [PATCH v4 5/7] mm, kasan: Stackdepot implementation. Enable stackdepot for SLAB

2016-03-04 18:06 GMT+03:00 Alexander Potapenko <[email protected]>:

Quoted 34 lines · Andrey Ryabinin
Quoted 33 lines

2016-03-04 17:52 GMT+03:00 Alexander Potapenko <[email protected]>:

Quoted 13 lines · Andrey Ryabinin
Quoted 12 lines
Quoted 11 lines
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+
+     stack->hash = hash;
+     stack->size = size;
+     stack->handle.slabindex = depot_index;
+     stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
+     __memcpy(stack->entries, entries, size * sizeof(unsigned long));

s/__memcpy/memcpy/

memcpy should be instrumented by asan/tsan, and we would like to avoid that instrumentation here.

Quoted 8 lines
Quoted 3 lines

KASAN_SANITIZE_* := n already takes care about this.
__memcpy() is a special thing solely for kasan internals and some assembly code. And it's not available generally.

As far as I can see, KASAN_SANITIZE_*:=n does not guarantee it. It just removes KASAN flags from GCC command line, it does not necessarily replace memcpy() calls with some kind of a non-instrumented memcpy().

With removed kasan cflags '__SANITIZE_ADDRESS__' is not defined, hence enable the following defines from arch/x86/include/asm/string_64.h:

#if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__)

/*
* For files that not instrumented (e.g. mm/slub.c) we
* should use not instrumented version of mem* functions. */

Quoted 8 lines
Quoted 5 lines

#undef memcpy
#define memcpy(dst, src, len) __memcpy(dst, src, len) #define memmove(dst, src, len) __memmove(dst, src, len) #define memset(s, c, n) __memset(s, c, n) #endif

Nice!
What do you think about providing stub .c files to decouple the shared code used by KASAN runtime from the rest of kernel?

Actually, I'm not quite understand why you need that at all, but your idea will not link due to multiple definitions of the same functions. Link problem should be easy to workaround with 'objcopy --prefix-symbol=' though.

Quoted 7 lines

(This is a completely different story though and can be done separately).

--
Alexander Potapenko
Software Engineer

Quoted 7 lines

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author reviewer
#19 lore raw

Re: [PATCH v4 5/7] mm, kasan: Stackdepot implementation. Enable stackdepot for SLAB

In reply to Andrey Ryabinin
Quoted 83 lines · Andrey Ryabinin
Quoted 13 lines · Dmitry Vyukov
Quoted 13 lines

lib/Makefile obj-$(CONFIG_SG_SPLIT) += sg_split.o · line 171

Quoted 10 lines
lib/Makefile +2 -0
index a7c26a4..10a4ae3 100644
@@ -167,6 +167,13 @@ obj-$(CONFIG_SG_SPLIT) += sg_split.o
 obj-$(CONFIG_STMP_DEVICE) += stmp_device.o
 obj-$(CONFIG_IRQ_POLL) += irq_poll.o
 
+ifeq ($(CONFIG_KASAN),y)
+ifeq ($(CONFIG_SLAB),y)

Just try to imagine that another subsystem wants to use stackdepot. How this gonna look like?

Quoted 5 lines
Quoted 5 lines

We have Kconfig to describe dependencies. So, this should be under CONFIG_STACKDEPOT. So any user of this feature can just do 'select STACKDEPOT' in Kconfig.

Quoted 2 lines
lib/Makefile +2 -0
+     obj-y   += stackdepot.o
+     KASAN_SANITIZE_slub.o := n

_stackdepot.o

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
lib/Makefile +6 -0
+
+     stack->hash = hash;
+     stack->size = size;
+     stack->handle.slabindex = depot_index;
+     stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
+     __memcpy(stack->entries, entries, size * sizeof(unsigned long));
Quoted 4 lines
Quoted 1 line

s/__memcpy/memcpy/

memcpy should be instrumented by asan/tsan, and we would like to avoid that instrumentation here.

KASAN_SANITIZE_* := n already takes care about this.
__memcpy() is a special thing solely for kasan internals and some assembly code. And it's not available generally.

Quoted 10 lines
Quoted 6 lines

lib/Makefile obj-$(CONFIG_SG_SPLIT) += sg_split.o

Quoted 2 lines
lib/Makefile +2 -0
+     if (unlikely(!smp_load_acquire(&next_slab_inited))) {
+             if (!preempt_count() && !in_irq()) {

If you trying to detect atomic context here, than this doesn't work. E.g. you can't know about held spinlocks in non-preemptible kernel.
And I'm not sure why need this. You know gfp flags here, so allocation in atomic context shouldn't be problem.

We don't have gfp flags for kfree.
I wonder how CONFIG_DEBUG_ATOMIC_SLEEP handles this. Maybe it has the answer.

It hasn't. It doesn't guarantee that atomic context always will be detected.

Quoted 2 lines

Alternatively, we can always assume that we are in atomic context in kfree.

Or do this allocation in separate context, put in work queue.

Quoted 12 lines
Quoted 11 lines

lib/Makefile

Quoted 5 lines
lib/Makefile +3 -0
+                     alloc_flags &= (__GFP_RECLAIM | __GFP_IO | __GFP_FS |
+                             __GFP_NOWARN | __GFP_NORETRY |
+                             __GFP_NOMEMALLOC | __GFP_DIRECT_RECLAIM);

I think blacklist approach would be better here.

Quoted 1 line

+ page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);

STACK_ALLOC_ORDER = 4 - that's a lot. Do you really need that much?

Quoted 9 lines

Part of the issue the atomic context above. When we can't allocate memory we still want to save the stack trace. When we have less than STACK_ALLOC_ORDER memory, we try to preallocate another STACK_ALLOC_ORDER in advance. So in the worst case, we have STACK_ALLOC_ORDER memory and that should be enough to handle all kmalloc/kfree in the atomic context. 1 page does not look enough. I think Alex did some measuring of the failure race (when we are out of memory and can't allocate more).

Quoted 2 lines

A lot of 4-order pages will lead to high fragmentation. You don't need physically contiguous memory here, so try to use vmalloc(). It is slower, but fragmentation won't be problem.

I've tried using vmalloc(), but turned out it's calling KASAN hooks again. Dealing with reentrancy in this case sounds like an overkill. Given that we only require 9 Mb most of the time, is allocating physical pages still a problem?

Quoted 2 lines

And one more thing. Take a look at mempool, because it's generally used to solve the problem you have here (guaranteed allocation in atomic context).

As far as I understood the docs, mempools have a drawback of allocating too much memory which won't be available for any other use. O'Reily's "Linux Device Drivers" even suggests not using mempools in any case when it's easier to deal with allocation failures (that advice is for device drivers, not sure if that stands for other subsystems though).

Signature · 16 lines
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#20 lore raw

Re: [PATCH v4 5/7] mm, kasan: Stackdepot implementation. Enable stackdepot for SLAB

2016-03-08 14:42 GMT+03:00 Alexander Potapenko <[email protected]>:

Quoted 16 lines · Andrey Ryabinin
Quoted 15 lines
Quoted 14 lines
Quoted 4 lines
Quoted 2 lines

+ page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);

STACK_ALLOC_ORDER = 4 - that's a lot. Do you really need that much?

Part of the issue the atomic context above. When we can't allocate memory we still want to save the stack trace. When we have less than STACK_ALLOC_ORDER memory, we try to preallocate another STACK_ALLOC_ORDER in advance. So in the worst case, we have STACK_ALLOC_ORDER memory and that should be enough to handle all kmalloc/kfree in the atomic context. 1 page does not look enough. I think Alex did some measuring of the failure race (when we are out of memory and can't allocate more).

Quoted 4 lines
Quoted 2 lines

A lot of 4-order pages will lead to high fragmentation. You don't need physically contiguous memory here, so try to use vmalloc(). It is slower, but fragmentation won't be problem.

I've tried using vmalloc(), but turned out it's calling KASAN hooks again. Dealing with reentrancy in this case sounds like an overkill.

We'll have to deal with recursion eventually. Using stackdepot for page owner will cause recursion.

Quoted 3 lines

Given that we only require 9 Mb most of the time, is allocating physical pages still a problem?

This is not about size, this about fragmentation. vmalloc allows to utilize available low-order pages,
hence reduce the fragmentation.

Quoted 4 lines
Quoted 2 lines

And one more thing. Take a look at mempool, because it's generally used to solve the problem you have here (guaranteed allocation in atomic context).

As far as I understood the docs, mempools have a drawback of allocating too much memory which won't be available for any other use.

As far as I understood your code, it has a drawback of allocating too much memory which won't be available for any other use ;)

However, now I think that mempool doesn't fit here. We never free memory => never return it to pool.
And this will cause 5sec delays between allocation retries in mempool_alloc().

Quoted 10 lines

O'Reily's "Linux Device Drivers" even suggests not using mempools in any case when it's easier to deal with allocation failures (that advice is for device drivers, not sure if that stands for other subsystems though).

--
Alexander Potapenko
Software Engineer

Quoted 7 lines

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author reviewer
#21 lore raw

Re: [PATCH v4 5/7] mm, kasan: Stackdepot implementation. Enable stackdepot for SLAB

Quoted 25 lines · Andrey Ryabinin

2016-03-08 14:42 GMT+03:00 Alexander Potapenko <[email protected]>:

Quoted 16 lines · Andrey Ryabinin
Quoted 15 lines
Quoted 14 lines
Quoted 4 lines
Quoted 2 lines

+ page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);

STACK_ALLOC_ORDER = 4 - that's a lot. Do you really need that much?

Part of the issue the atomic context above. When we can't allocate memory we still want to save the stack trace. When we have less than STACK_ALLOC_ORDER memory, we try to preallocate another STACK_ALLOC_ORDER in advance. So in the worst case, we have STACK_ALLOC_ORDER memory and that should be enough to handle all kmalloc/kfree in the atomic context. 1 page does not look enough. I think Alex did some measuring of the failure race (when we are out of memory and can't allocate more).

Quoted 4 lines
Quoted 2 lines

A lot of 4-order pages will lead to high fragmentation. You don't need physically contiguous memory here, so try to use vmalloc(). It is slower, but fragmentation won't be problem.

I've tried using vmalloc(), but turned out it's calling KASAN hooks again. Dealing with reentrancy in this case sounds like an overkill.

We'll have to deal with recursion eventually. Using stackdepot for page owner will cause recursion.

Quoted 7 lines
Quoted 3 lines

Given that we only require 9 Mb most of the time, is allocating physical pages still a problem?

This is not about size, this about fragmentation. vmalloc allows to utilize available low-order pages,
hence reduce the fragmentation.

I've attempted to add __vmalloc(STACK_ALLOC_SIZE, alloc_flags, PAGE_KERNEL) (also tried vmalloc(STACK_ALLOC_SIZE)) instead of page_alloc() and am now getting a crash in kmem_cache_alloc_node_trace() in mm/slab.c, because it doesn't allow the kmem_cache pointer to be NULL (it's dereferenced when calling trace_kmalloc_node()).

Steven, do you know if this because of my code violating some contract (e.g. I'm calling vmalloc() too early, when kmalloc_caches[] haven't been initialized), or is this a bug in kmem_cache_alloc_node_trace() itself?

Quoted 30 lines
Quoted 4 lines
Quoted 2 lines

And one more thing. Take a look at mempool, because it's generally used to solve the problem you have here (guaranteed allocation in atomic context).

As far as I understood the docs, mempools have a drawback of allocating too much memory which won't be available for any other use.

As far as I understood your code, it has a drawback of allocating too much memory which won't be available for any other use ;)

However, now I think that mempool doesn't fit here. We never free memory => never return it to pool.
And this will cause 5sec delays between allocation retries in mempool_alloc().

Quoted 17 lines

O'Reily's "Linux Device Drivers" even suggests not using mempools in any case when it's easier to deal with allocation failures (that advice is for device drivers, not sure if that stands for other subsystems though).

--
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg

Signature · 16 lines
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#22 lore raw

Re: [PATCH v4 5/7] mm, kasan: Stackdepot implementation. Enable stackdepot for SLAB

Quoted 40 lines · Alexander Potapenko
Quoted 25 lines · Andrey Ryabinin

2016-03-08 14:42 GMT+03:00 Alexander Potapenko <[email protected]>:

Quoted 16 lines · Andrey Ryabinin
Quoted 15 lines
Quoted 14 lines
Quoted 4 lines
Quoted 2 lines

+ page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);

STACK_ALLOC_ORDER = 4 - that's a lot. Do you really need that much?

Part of the issue the atomic context above. When we can't allocate memory we still want to save the stack trace. When we have less than STACK_ALLOC_ORDER memory, we try to preallocate another STACK_ALLOC_ORDER in advance. So in the worst case, we have STACK_ALLOC_ORDER memory and that should be enough to handle all kmalloc/kfree in the atomic context. 1 page does not look enough. I think Alex did some measuring of the failure race (when we are out of memory and can't allocate more).

Quoted 4 lines
Quoted 2 lines

A lot of 4-order pages will lead to high fragmentation. You don't need physically contiguous memory here, so try to use vmalloc(). It is slower, but fragmentation won't be problem.

I've tried using vmalloc(), but turned out it's calling KASAN hooks again. Dealing with reentrancy in this case sounds like an overkill.

We'll have to deal with recursion eventually. Using stackdepot for page owner will cause recursion.

Quoted 7 lines
Quoted 3 lines

Given that we only require 9 Mb most of the time, is allocating physical pages still a problem?

This is not about size, this about fragmentation. vmalloc allows to utilize available low-order pages,
hence reduce the fragmentation.

I've attempted to add __vmalloc(STACK_ALLOC_SIZE, alloc_flags, PAGE_KERNEL) (also tried vmalloc(STACK_ALLOC_SIZE)) instead of page_alloc() and am now getting a crash in kmem_cache_alloc_node_trace() in mm/slab.c, because it doesn't allow the kmem_cache pointer to be NULL (it's dereferenced when calling trace_kmalloc_node()).

Quoted 3 lines

Steven, do you know if this because of my code violating some contract (e.g. I'm calling vmalloc() too early, when kmalloc_caches[] haven't been initialized),

Probably. kmem_cache_init() goes before vmalloc_init().

Quoted 3 lines

or is this a bug in kmem_cache_alloc_node_trace()
itself?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author reviewer
#23 lore raw

Re: [PATCH v4 5/7] mm, kasan: Stackdepot implementation. Enable stackdepot for SLAB

Quoted 43 lines · Andrey Ryabinin
Quoted 40 lines · Alexander Potapenko
Quoted 25 lines · Andrey Ryabinin

2016-03-08 14:42 GMT+03:00 Alexander Potapenko <[email protected]>:

Quoted 16 lines · Andrey Ryabinin
Quoted 15 lines
Quoted 14 lines
Quoted 4 lines
Quoted 2 lines

+ page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);

STACK_ALLOC_ORDER = 4 - that's a lot. Do you really need that much?

Part of the issue the atomic context above. When we can't allocate memory we still want to save the stack trace. When we have less than STACK_ALLOC_ORDER memory, we try to preallocate another STACK_ALLOC_ORDER in advance. So in the worst case, we have STACK_ALLOC_ORDER memory and that should be enough to handle all kmalloc/kfree in the atomic context. 1 page does not look enough. I think Alex did some measuring of the failure race (when we are out of memory and can't allocate more).

Quoted 4 lines
Quoted 2 lines

A lot of 4-order pages will lead to high fragmentation. You don't need physically contiguous memory here, so try to use vmalloc(). It is slower, but fragmentation won't be problem.

I've tried using vmalloc(), but turned out it's calling KASAN hooks again. Dealing with reentrancy in this case sounds like an overkill.

We'll have to deal with recursion eventually. Using stackdepot for page owner will cause recursion.

Quoted 7 lines
Quoted 3 lines

Given that we only require 9 Mb most of the time, is allocating physical pages still a problem?

This is not about size, this about fragmentation. vmalloc allows to utilize available low-order pages,
hence reduce the fragmentation.

I've attempted to add __vmalloc(STACK_ALLOC_SIZE, alloc_flags, PAGE_KERNEL) (also tried vmalloc(STACK_ALLOC_SIZE)) instead of page_alloc() and am now getting a crash in kmem_cache_alloc_node_trace() in mm/slab.c, because it doesn't allow the kmem_cache pointer to be NULL (it's dereferenced when calling trace_kmalloc_node()).

Quoted 5 lines
Quoted 3 lines

Steven, do you know if this because of my code violating some contract (e.g. I'm calling vmalloc() too early, when kmalloc_caches[] haven't been initialized),

Probably. kmem_cache_init() goes before vmalloc_init().

The solution I'm currently testing is to introduce a per-CPU recursion flag that depot_save_stack() checks and bails out if it's set. In addition I look at |kmalloc_caches[KMALLOC_SHIFT_HIGH]| and in_interrupt() to see if vmalloc() is available.
In the case it is not, I fall back to alloc_pages().

Right now (after 20 minutes of running Trinity) vmalloc() has been called 490 times, alloc_pages() - only 13 times.
I hope it's now much better from the fragmentation point of view.

Quoted 4 lines
Quoted 4 lines

or is this a bug in kmem_cache_alloc_node_trace()
itself?

Signature · 16 lines
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#24 lore raw

Re: [PATCH v4 5/7] mm, kasan: Stackdepot implementation. Enable stackdepot for SLAB

In reply to Andrey Ryabinin
Quoted 10 lines · On Fri, 11 Mar 2016 14:43:45 +0300 Andrey Ryabinin <[email protected]>…
Quoted 10 lines
Quoted 3 lines

This is not about size, this about fragmentation. vmalloc allows to utilize available low-order pages,
hence reduce the fragmentation.

I've attempted to add __vmalloc(STACK_ALLOC_SIZE, alloc_flags, PAGE_KERNEL) (also tried vmalloc(STACK_ALLOC_SIZE)) instead of page_alloc() and am now getting a crash in kmem_cache_alloc_node_trace() in mm/slab.c, because it doesn't allow the kmem_cache pointer to be NULL (it's dereferenced when calling trace_kmalloc_node()).

Quoted 5 lines
Quoted 3 lines

Steven, do you know if this because of my code violating some contract (e.g. I'm calling vmalloc() too early, when kmalloc_caches[] haven't been initialized),

Probably. kmem_cache_init() goes before vmalloc_init().

Agreed, that function can not be called with cachep NULL, nor can it be called before kmem_cache is set up to point to kmem_cache_boot.

-- Steve

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author reviewer
#25 lore raw

Re: [PATCH v4 5/7] mm, kasan: Stackdepot implementation. Enable stackdepot for SLAB

In reply to Andrey Ryabinin
Quoted 39 lines · Andrey Ryabinin
Quoted 36 lines · Alexander Potapenko

Stack depot will allow KASAN store allocation/deallocation stack traces for memory chunks. The stack traces are stored in a hash table and referenced by handles which reside in the kasan_alloc_meta and kasan_free_meta structures in the allocated memory chunks.

IRQ stack traces are cut below the IRQ entry point to avoid unnecessary duplication.

Right now stackdepot support is only enabled in SLAB allocator. Once KASAN features in SLAB are on par with those in SLUB we can switch SLUB to stackdepot as well, thus removing the dependency on SLUB stack bookkeeping, which wastes a lot of memory.

This patch is based on the "mm: kasan: stack depots" patch originally prepared by Dmitry Chernenkov.

---
v2: - per request from Joonsoo Kim, moved the stackdepot implementation to lib/, as there's a plan to use it for page owner
- added copyright comments
- added comments about smp_load_acquire()/smp_store_release()

v3: - minor description changes
---

lib/Makefile +0 -0
index a7c26a4..10a4ae3 100644
@@ -167,6 +167,13 @@ obj-$(CONFIG_SG_SPLIT) += sg_split.o
 obj-$(CONFIG_STMP_DEVICE) += stmp_device.o
 obj-$(CONFIG_IRQ_POLL) += irq_poll.o
 
Quoted 7 lines

lib/Makefile obj-$(CONFIG_SG_SPLIT) += sg_split.o · line 171

Quoted 2 lines
lib/Makefile +2 -0
+ifeq ($(CONFIG_KASAN),y)
+ifeq ($(CONFIG_SLAB),y)

Just try to imagine that another subsystem wants to use stackdepot. How this gonna look like?

We have Kconfig to describe dependencies. So, this should be under CONFIG_STACKDEPOT. So any user of this feature can just do 'select STACKDEPOT' in Kconfig.

Agreed. Will fix this in the updated patch.

Quoted 21 lines
Quoted 19 lines
lib/Makefile +5 -0
+     obj-y   += stackdepot.o
+     KASAN_SANITIZE_slub.o := n
+endif
+endif
+
 libfdt_files = fdt.o fdt_ro.o fdt_wip.o fdt_rw.o fdt_sw.o fdt_strerror.o \
             fdt_empty_tree.o
 $(foreach file, $(libfdt_files), \
lib/stackdepot.c new file +4 -0
new file mode 100644
index 0000000..f09b0da
 
 
+/* Allocation of a new stack in raw storage */
+static struct stack_record *depot_alloc_stack(unsigned long *entries, int size,
+             u32 hash, void **prealloc, gfp_t alloc_flags)
+{
Quoted 8 lines

lib/stackdepot.c line 135

Quoted 6 lines
lib/stackdepot.c new file +6 -0
+
+     stack->hash = hash;
+     stack->size = size;
+     stack->handle.slabindex = depot_index;
+     stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
+     __memcpy(stack->entries, entries, size * sizeof(unsigned long));

s/__memcpy/memcpy

Ack.

Quoted 49 lines
Quoted 49 lines
lib/stackdepot.c new file +47 -0
+     depot_offset += required_size;
+
+     return stack;
+}
+
 
 
+/*
+ * depot_save_stack - save stack in a stack depot.
+ * @trace - the stacktrace to save.
+ * @alloc_flags - flags for allocating additional memory if required.
+ *
+ * Returns the handle of the stack struct stored in depot.
+ */
+depot_stack_handle depot_save_stack(struct stack_trace *trace,
+                                 gfp_t alloc_flags)
+{
+     u32 hash;
+     depot_stack_handle retval = 0;
+     struct stack_record *found = NULL, **bucket;
+     unsigned long flags;
+     struct page *page = NULL;
+     void *prealloc = NULL;
+
+     if (unlikely(trace->nr_entries == 0))
+             goto exit;
+
+     hash = hash_stack(trace->entries, trace->nr_entries);
+     /* Bad luck, we won't store this stack. */
+     if (hash == 0)
+             goto exit;
+
+     bucket = &stack_table[hash & STACK_HASH_MASK];
+
+     /* Fast path: look the stack trace up without locking.
+      *
+      * The smp_load_acquire() here pairs with smp_store_release() to
+      * |bucket| below.
+      */
+     found = find_stack(smp_load_acquire(bucket), trace->entries,
+                        trace->nr_entries, hash);
+     if (found)
+             goto exit;
+
+     /* Check if the current or the next stack slab need to be initialized.
+      * If so, allocate the memory - we won't be able to do that under the
+      * lock.
+      *
+      * The smp_load_acquire() here pairs with smp_store_release() to
Quoted 8 lines

lib/stackdepot.c line 233

Quoted 4 lines
lib/stackdepot.c new file +4 -0
+      * |next_slab_inited| in depot_alloc_stack() and init_stack_slab().
+      */
+     if (unlikely(!smp_load_acquire(&next_slab_inited))) {
+             if (!preempt_count() && !in_irq()) {

If you trying to detect atomic context here, than this doesn't work. E.g. you can't know about held spinlocks in non-preemptible kernel.
And I'm not sure why need this. You know gfp flags here, so allocation in atomic context shouldn't be problem.

Yeah, we can just remove these checks. As discussed before, this will eliminate allocations from kfree(), but that's very unlikely to become a problem.

Quoted 6 lines

lib/stackdepot.c line 236

Quoted 4 lines
lib/stackdepot.c new file +3 -0
+                     alloc_flags &= (__GFP_RECLAIM | __GFP_IO | __GFP_FS |
+                             __GFP_NOWARN | __GFP_NORETRY |
+                             __GFP_NOMEMALLOC | __GFP_DIRECT_RECLAIM);

I think blacklist approach would be better here.

Perhaps we don't need to change the mask at all.

Quoted 3 lines
Quoted 1 line

+ page = alloc_pages(alloc_flags, STACK_ALLOC_ORDER);

STACK_ALLOC_ORDER = 4 - that's a lot. Do you really need that much?

Well, this is not "that" much, actually. The allocation happens only ~150 times within three hours under Trinity, which means only 9 megabytes.
At around 250 allocations the stack depot saturates and new stacks are very rare.
We can probably drop the order to 3 or 2, which will increase the number of allocations by just the factor of 2 to 4, but will be better from the point of page fragmentation.

Quoted 8 lines
Quoted 8 lines
mm/kasan/Makefile +0 -0
index a61460d..32bd73a 100644
@@ -7,3 +7,4 @@ CFLAGS_REMOVE_kasan.o = -pg
 CFLAGS_kasan.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector)
 
Quoted 4 lines

mm/kasan/Makefile CFLAGS_REMOVE_kasan.o = -pg · line 10

Quoted 2 lines
mm/kasan/Makefile +1 -0
 obj-y := kasan.o report.o kasan_init.o
+

Extra newline.

Ack.

Quoted 30 lines
Quoted 14 lines
mm/kasan/kasan.h +1 -0
index 7b9e4ab9..b4e5942 100644
@@ -2,6 +2,7 @@
 #define __MM_KASAN_KASAN_H
 
 #include <linux/kasan.h>
+#include <linux/stackdepot.h>
 
 #define KASAN_SHADOW_SCALE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT)
 #define KASAN_SHADOW_MASK       (KASAN_SHADOW_SCALE_SIZE - 1)

mm/kasan/kasan.h enum kasan_state { · line 68

Quoted 4 lines
mm/kasan/kasan.h +1 -0
@@ -64,10 +65,13 @@ enum kasan_state {
      KASAN_STATE_FREE
 };
 
+#define KASAN_STACK_DEPTH 64

I think, you can reduce this (32 perhaps?). Kernel stacks are not so deep usually.

Quoted 8 lines
mm/kasan/kasan.h +2 -0
+
 struct kasan_track {
      u64 cpu : 6;                    /* for NR_CPUS = 64 */
      u64 pid : 16;                   /* 65536 processes */
      u64 when : 42;                  /* ~140 years */
+     depot_stack_handle stack : sizeof(depot_stack_handle);
 };
 
Signature · 16 lines
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
author reviewer
#26 lore raw

PATCH v4 6/7 kasan: Test fix: Warn if the UAF could not be detected in kmalloc_uaf2

In reply to Alexander Potapenko
---
 lib/test_kasan.c | 2 ++
 1 file changed, 2 insertions(+)
lib/test_kasan.c +2 -0
index 90ad74f..82169fb 100644
@@ -294,6 +294,8 @@ static noinline void __init kmalloc_uaf2(void)
 	}
 
 	ptr1[40] = 'x';
+	if (ptr1 == ptr2)
+		pr_err("Could not detect use-after-free: ptr1 == ptr2\n");
 	kfree(ptr2);
 }
 
Signature · 7 lines
2.7.0.rc3.207.g0ac5344

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer Acked-by on v4 6/7
#27 lore raw

Re: [PATCH v4 6/7] kasan: Test fix: Warn if the UAF could not be detected in kmalloc_uaf2

Quoted 5 lines · Alexander Potapenko
---
 lib/test_kasan.c | 2 ++
 1 file changed, 2 insertions(+)
Quoted 6 lines
lib/test_kasan.c +0 -0
index 90ad74f..82169fb 100644

lib/test_kasan.c static noinline void __init kmalloc_uaf2(void) · line 301

Quoted 8 lines
lib/test_kasan.c +2 -0
@@ -294,6 +294,8 @@ static noinline void __init kmalloc_uaf2(void)
 	}
 
 	ptr1[40] = 'x';
+	if (ptr1 == ptr2)
+		pr_err("Could not detect use-after-free: ptr1 == ptr2\n");
 	kfree(ptr2);
 }
 

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author reviewer
#28 lore raw

PATCH v4 7/7 mm: kasan: Initial memory quarantine implementation

In reply to Alexander Potapenko

Quarantine isolates freed objects in a separate queue. The objects are returned to the allocator later, which helps to detect use-after-free errors.

Freed objects are first added to per-cpu quarantine queues. When a cache is destroyed or memory shrinking is requested, the objects are moved into the global quarantine queue. Whenever a kmalloc call allows memory reclaiming, the oldest objects are popped out of the global queue until the total size of objects in quarantine is less than 3/4 of the maximum quarantine size (which is a fraction of installed physical memory).

Right now quarantine support is only enabled in SLAB allocator. Unification of KASAN features in SLAB and SLUB will be done later.

This patch is based on the "mm: kasan: quarantine" patch originally prepared by Dmitry Chernenkov.

---
v2: - added copyright comments
- per request from Joonsoo Kim made __cache_free() more straightforward
- added comments for smp_load_acquire()/smp_store_release()

v3: - incorporate changes introduced by the "mm, kasan: SLAB support" patch

v4: - fix kbuild compile-time error (missing ___cache_free() declaration)
and a warning (wrong format specifier)

---
 include/linux/kasan.h |  30 +++--
 lib/test_kasan.c      |  29 +++++
 mm/kasan/Makefile     |   3 +
 mm/kasan/kasan.c      |  71 ++++++++++--
 mm/kasan/kasan.h      |  11 +-
 mm/kasan/quarantine.c | 306 ++++++++++++++++++++++++++++++++++++++++++++++++++
 mm/kasan/report.c     |   1 +
 mm/mempool.c          |   7 +-
 mm/page_alloc.c       |   2 +-
 mm/slab.c             |  14 ++-
 mm/slab.h             |   2 +
 mm/slab_common.c      |   2 +
 mm/slub.c             |   4 +-
 13 files changed, 454 insertions(+), 28 deletions(-)
include/linux/kasan.h +22 -8
index bf71ab0..355e722 100644
@@ -44,24 +44,29 @@ static inline void kasan_disable_current(void)
 void kasan_unpoison_shadow(const void *address, size_t size);
 
 void kasan_alloc_pages(struct page *page, unsigned int order);
-void kasan_free_pages(struct page *page, unsigned int order);
+void kasan_poison_free_pages(struct page *page, unsigned int order);
 
 void kasan_cache_create(struct kmem_cache *cache, size_t *size,
 			unsigned long *flags);
+void kasan_cache_shrink(struct kmem_cache *cache);
+void kasan_cache_destroy(struct kmem_cache *cache);
 
 void kasan_poison_slab(struct page *page);
 void kasan_unpoison_object_data(struct kmem_cache *cache, void *object);
 void kasan_poison_object_data(struct kmem_cache *cache, void *object);
 
 void kasan_kmalloc_large(const void *ptr, size_t size, gfp_t flags);
-void kasan_kfree_large(const void *ptr);
-void kasan_kfree(void *ptr);
+void kasan_poison_kfree_large(const void *ptr);
+void kasan_poison_kfree(void *ptr);
 void kasan_kmalloc(struct kmem_cache *s, const void *object, size_t size,
 		  gfp_t flags);
 void kasan_krealloc(const void *object, size_t new_size, gfp_t flags);
 
 void kasan_slab_alloc(struct kmem_cache *s, void *object, gfp_t flags);
-void kasan_slab_free(struct kmem_cache *s, void *object);
+/* kasan_slab_free() returns true if the object has been put into quarantine.
+ */
+bool kasan_slab_free(struct kmem_cache *s, void *object);
+void kasan_poison_slab_free(struct kmem_cache *s, void *object);
 
 struct kasan_cache {
 	int alloc_meta_offset;
@@ -79,11 +84,14 @@ static inline void kasan_enable_current(void) {}
 static inline void kasan_disable_current(void) {}
 
 static inline void kasan_alloc_pages(struct page *page, unsigned int order) {}
-static inline void kasan_free_pages(struct page *page, unsigned int order) {}
+static inline void kasan_poison_free_pages(struct page *page,
+						unsigned int order) {}
 
 static inline void kasan_cache_create(struct kmem_cache *cache,
 				      size_t *size,
 				      unsigned long *flags) {}
+static inline void kasan_cache_shrink(struct kmem_cache *cache) {}
+static inline void kasan_cache_destroy(struct kmem_cache *cache) {}
 
 static inline void kasan_poison_slab(struct page *page) {}
 static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
@@ -92,8 +100,8 @@ static inline void kasan_poison_object_data(struct kmem_cache *cache,
 					void *object) {}
 
 static inline void kasan_kmalloc_large(void *ptr, size_t size, gfp_t flags) {}
-static inline void kasan_kfree_large(const void *ptr) {}
-static inline void kasan_kfree(void *ptr) {}
+static inline void kasan_poison_kfree_large(const void *ptr) {}
+static inline void kasan_poison_kfree(void *ptr) {}
 static inline void kasan_kmalloc(struct kmem_cache *s, const void *object,
 				size_t size, gfp_t flags) {}
 static inline void kasan_krealloc(const void *object, size_t new_size,
@@ -101,7 +109,13 @@ static inline void kasan_krealloc(const void *object, size_t new_size,
 
 static inline void kasan_slab_alloc(struct kmem_cache *s, void *object,
 				   gfp_t flags) {}
-static inline void kasan_slab_free(struct kmem_cache *s, void *object) {}
+/* kasan_slab_free() returns true if the object has been put into quarantine.
+ */
+static inline bool kasan_slab_free(struct kmem_cache *s, void *object)
+{
+	return false;
+}
+static inline void kasan_poison_slab_free(struct kmem_cache *s, void *object) {}
 
 static inline int kasan_module_alloc(void *addr, size_t size) { return 0; }
 static inline void kasan_free_shadow(const struct vm_struct *vm) {}
lib/test_kasan.c +29 -0
index 82169fb..799c98e 100644
@@ -344,6 +344,32 @@ static noinline void __init kasan_stack_oob(void)
 	*(volatile char *)p;
 }
 
+#ifdef CONFIG_SLAB
+static noinline void __init kasan_quarantine_cache(void)
+{
+	struct kmem_cache *cache = kmem_cache_create(
+			"test", 137, 8, GFP_KERNEL, NULL);
+	int i;
+
+	for (i = 0; i <  100; i++) {
+		void *p = kmem_cache_alloc(cache, GFP_KERNEL);
+
+		kmem_cache_free(cache, p);
+		p = kmalloc(sizeof(u64), GFP_KERNEL);
+		kfree(p);
+	}
+	kmem_cache_shrink(cache);
+	for (i = 0; i <  100; i++) {
+		u64 *p = kmem_cache_alloc(cache, GFP_KERNEL);
+
+		kmem_cache_free(cache, p);
+		p = kmalloc(sizeof(u64), GFP_KERNEL);
+		kfree(p);
+	}
+	kmem_cache_destroy(cache);
+}
+#endif
+
 static int __init kmalloc_tests_init(void)
 {
 	kmalloc_oob_right();
@@ -367,6 +393,9 @@ static int __init kmalloc_tests_init(void)
 	kmem_cache_oob();
 	kasan_stack_oob();
 	kasan_global_oob();
+#ifdef CONFIG_SLAB
+	kasan_quarantine_cache();
+#endif
 	return -EAGAIN;
 }
 
mm/kasan/Makefile +3 -0
index 32bd73a..d1db41e 100644
@@ -8,3 +8,6 @@ CFLAGS_kasan.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector)
 
 obj-y := kasan.o report.o kasan_init.o
 
+ifdef CONFIG_SLAB
+	obj-y	+= quarantine.o
+endif
mm/kasan/kasan.c +60 -11

124 lines not on this page · raw message

mm/kasan/kasan.h +9 -2
index b4e5942..37e0b3a 100644
@@ -62,6 +62,7 @@ struct kasan_global {
 enum kasan_state {
 	KASAN_STATE_INIT,
 	KASAN_STATE_ALLOC,
+	KASAN_STATE_QUARANTINE,
 	KASAN_STATE_FREE
 };
 
@@ -81,8 +82,10 @@ struct kasan_alloc_meta {
 };
 
 struct kasan_free_meta {
-	/* Allocator freelist pointer, unused by KASAN. */
-	void **freelist;
+	/* This field is used while the object is in the quarantine.
+	 * Otherwise it might be used for the allocator freelist.
+	 */
+	void **quarantine_link;
 	struct kasan_track track;
 };
 
@@ -106,4 +109,8 @@ static inline bool kasan_report_enabled(void)
 void kasan_report(unsigned long addr, size_t size,
 		bool is_write, unsigned long ip);
 
+void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache);
+void quarantine_reduce(void);
+void quarantine_remove_cache(struct kmem_cache *cache);
+
 #endif
mm/kasan/quarantine.c new file +306 -0

306 lines not on this page · raw message

mm/kasan/report.c +1 -0
index 82d8858..958af33 100644
@@ -150,6 +150,7 @@ static void print_object(struct kmem_cache *cache, void *object)
 		print_track(&alloc_info->track);
 		break;
 	case KASAN_STATE_FREE:
+	case KASAN_STATE_QUARANTINE:
 		pr_err("Object freed, allocated with size %u bytes\n",
 		       alloc_info->alloc_size);
 		free_info = get_free_info(cache, object);
mm/mempool.c +4 -3
index b47c8a7..4beeeef 100644
@@ -105,11 +105,12 @@ static inline void poison_element(mempool_t *pool, void *element)
 static void kasan_poison_element(mempool_t *pool, void *element)
 {
 	if (pool->alloc == mempool_alloc_slab)
-		kasan_slab_free(pool->pool_data, element);
+		kasan_poison_slab_free(pool->pool_data, element);
 	if (pool->alloc == mempool_kmalloc)
-		kasan_kfree(element);
+		kasan_poison_kfree(element);
 	if (pool->alloc == mempool_alloc_pages)
-		kasan_free_pages(element, (unsigned long)pool->pool_data);
+		kasan_poison_free_pages(element,
+					(unsigned long)pool->pool_data);
 }
 
 static void kasan_unpoison_element(mempool_t *pool, void *element, gfp_t flags)
mm/page_alloc.c +1 -1
index 838ca8bb..a79ae81 100644
@@ -980,7 +980,7 @@ static bool free_pages_prepare(struct page *page, unsigned int order)
 
 	trace_mm_page_free(page, order);
 	kmemcheck_free_shadow(page, order);
-	kasan_free_pages(page, order);
+	kasan_poison_free_pages(page, order);
 
 	if (PageAnon(page))
 		page->mapping = NULL;
mm/slab.c +13 -1
index 52a7a8d..8884fdf 100644
@@ -3374,9 +3374,21 @@ free_done:
 static inline void __cache_free(struct kmem_cache *cachep, void *objp,
 				unsigned long caller)
 {
+#ifdef CONFIG_KASAN
+	if (kasan_slab_free(cachep, objp))
+		/* The object has been put into the quarantine, don't touch it
+		 * for now.
+		 */
+		return;
+#endif
+	___cache_free(cachep, objp, caller);
+}
+
+void ___cache_free(struct kmem_cache *cachep, void *objp,
+		unsigned long caller)
+{
 	struct array_cache *ac;
 
-	kasan_slab_free(cachep, objp);
 	ac = cpu_cache_get(cachep);
 
 	check_irq_off();
mm/slab.h +2 -0
index 2eedace..e5680ee 100644
@@ -373,4 +373,6 @@ void *slab_next(struct seq_file *m, void *p, loff_t *pos);
 void slab_stop(struct seq_file *m, void *p);
 int memcg_slab_show(struct seq_file *m, void *p);
 
+void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr);
+
 #endif /* MM_SLAB_H */
mm/slab_common.c +2 -0
index 538f616..7bcb5bc 100644
@@ -711,6 +711,7 @@ void kmem_cache_destroy(struct kmem_cache *s)
 	get_online_cpus();
 	get_online_mems();
 
+	kasan_cache_destroy(s);
 	mutex_lock(&slab_mutex);
 
 	s->refcount--;
@@ -749,6 +750,7 @@ int kmem_cache_shrink(struct kmem_cache *cachep)
 
 	get_online_cpus();
 	get_online_mems();
+	kasan_cache_shrink(cachep);
 	ret = __kmem_cache_shrink(cachep, false);
 	put_online_mems();
 	put_online_cpus();
mm/slub.c +2 -2
index 2978695..1819293 100644
@@ -1278,7 +1278,7 @@ static inline void kmalloc_large_node_hook(void *ptr, size_t size, gfp_t flags)
 static inline void kfree_hook(const void *x)
 {
 	kmemleak_free(x);
-	kasan_kfree_large(x);
+	kasan_poison_kfree_large(x);
 }
 
 static inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s,
@@ -1333,7 +1333,7 @@ static inline void slab_free_hook(struct kmem_cache *s, void *x)
 	if (!(s->flags & SLAB_DEBUG_OBJECTS))
 		debug_check_no_obj_freed(x, s->object_size);
 
-	kasan_slab_free(s, x);
+	kasan_poison_slab_free(s, x);
 }
 
 static inline void slab_free_freelist_hook(struct kmem_cache *s,
Signature · 1 line
2.7.0.rc3.207.g0ac5344
maintainer
#29 lore raw
In reply to Alexander Potapenko
Quoted 1 line · On Fri, 26 Feb 2016 17:48:40 +0100 Alexander Potapenko <[email protected]>…

This patch set implements SLAB support for KASAN

That's a lot of code and I'm not seeing much review activity from folks. There was one ack against an earlier version of [4/7] from Steven Rostedt but that ack wasn't maintained (bad!).

I scanned over these and my plan was to queue them for -next testing and to await more review/test before proceeding further. But alas, there are significant collisions with pending slab patches (all in linux-next) so could you please take a look at those?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>