diff mbox series

[RFC,07/10] kfence, kmemleak: make KFENCE compatible with KMEMLEAK

Message ID 20200907134055.2878499-8-elver@google.com
State New
Headers show
Series KFENCE: A low-overhead sampling-based memory safety error detector | expand

Commit Message

Marco Elver Sept. 7, 2020, 1:40 p.m. UTC
From: Alexander Potapenko <glider@google.com>

Add compatibility with KMEMLEAK, by making KMEMLEAK aware of the KFENCE
memory pool. This allows building debug kernels with both enabled, which
also helped in debugging KFENCE.

Co-developed-by: Marco Elver <elver@google.com>
Signed-off-by: Marco Elver <elver@google.com>
Signed-off-by: Alexander Potapenko <glider@google.com>
---
 mm/kmemleak.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

Comments

Catalin Marinas Sept. 8, 2020, 11:53 a.m. UTC | #1
On Mon, Sep 07, 2020 at 03:40:52PM +0200, Marco Elver wrote:
> From: Alexander Potapenko <glider@google.com>
> 
> Add compatibility with KMEMLEAK, by making KMEMLEAK aware of the KFENCE
> memory pool. This allows building debug kernels with both enabled, which
> also helped in debugging KFENCE.
> 
> Co-developed-by: Marco Elver <elver@google.com>
> Signed-off-by: Marco Elver <elver@google.com>
> Signed-off-by: Alexander Potapenko <glider@google.com>
> ---
>  mm/kmemleak.c | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/mm/kmemleak.c b/mm/kmemleak.c
> index 5e252d91eb14..2809c25c0a88 100644
> --- a/mm/kmemleak.c
> +++ b/mm/kmemleak.c
> @@ -97,6 +97,7 @@
>  #include <linux/atomic.h>
>  
>  #include <linux/kasan.h>
> +#include <linux/kfence.h>
>  #include <linux/kmemleak.h>
>  #include <linux/memory_hotplug.h>
>  
> @@ -1946,8 +1947,18 @@ void __init kmemleak_init(void)
>  	/* register the data/bss sections */
>  	create_object((unsigned long)_sdata, _edata - _sdata,
>  		      KMEMLEAK_GREY, GFP_ATOMIC);
> +#if defined(CONFIG_KFENCE) && defined(CONFIG_HAVE_ARCH_KFENCE_STATIC_POOL)
> +	/* KFENCE objects are located in .bss, which may confuse kmemleak. Skip them. */
> +	create_object((unsigned long)__bss_start, __kfence_pool - __bss_start,
> +		      KMEMLEAK_GREY, GFP_ATOMIC);
> +	create_object((unsigned long)__kfence_pool + KFENCE_POOL_SIZE,
> +		      __bss_stop - (__kfence_pool + KFENCE_POOL_SIZE),
> +		      KMEMLEAK_GREY, GFP_ATOMIC);
> +#else
>  	create_object((unsigned long)__bss_start, __bss_stop - __bss_start,
>  		      KMEMLEAK_GREY, GFP_ATOMIC);
> +#endif

Could you instead do:

#if defined(CONFIG_KFENCE) && defined(CONFIG_HAVE_ARCH_KFENCE_STATIC_POOL)
	delete_object_part((unsigned long)__kfence_pool, KFENCE_POOL_SIZE);
#endif
Alexander Potapenko Sept. 8, 2020, 12:29 p.m. UTC | #2
> Could you instead do:
>
> #if defined(CONFIG_KFENCE) && defined(CONFIG_HAVE_ARCH_KFENCE_STATIC_POOL)
>         delete_object_part((unsigned long)__kfence_pool, KFENCE_POOL_SIZE);
> #endif

Thanks, we'll apply this to v2!
diff mbox series

Patch

diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index 5e252d91eb14..2809c25c0a88 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -97,6 +97,7 @@ 
 #include <linux/atomic.h>
 
 #include <linux/kasan.h>
+#include <linux/kfence.h>
 #include <linux/kmemleak.h>
 #include <linux/memory_hotplug.h>
 
@@ -1946,8 +1947,18 @@  void __init kmemleak_init(void)
 	/* register the data/bss sections */
 	create_object((unsigned long)_sdata, _edata - _sdata,
 		      KMEMLEAK_GREY, GFP_ATOMIC);
+#if defined(CONFIG_KFENCE) && defined(CONFIG_HAVE_ARCH_KFENCE_STATIC_POOL)
+	/* KFENCE objects are located in .bss, which may confuse kmemleak. Skip them. */
+	create_object((unsigned long)__bss_start, __kfence_pool - __bss_start,
+		      KMEMLEAK_GREY, GFP_ATOMIC);
+	create_object((unsigned long)__kfence_pool + KFENCE_POOL_SIZE,
+		      __bss_stop - (__kfence_pool + KFENCE_POOL_SIZE),
+		      KMEMLEAK_GREY, GFP_ATOMIC);
+#else
 	create_object((unsigned long)__bss_start, __bss_stop - __bss_start,
 		      KMEMLEAK_GREY, GFP_ATOMIC);
+#endif
+
 	/* only register .data..ro_after_init if not within .data */
 	if (&__start_ro_after_init < &_sdata || &__end_ro_after_init > &_edata)
 		create_object((unsigned long)__start_ro_after_init,