Skip to contentSkip to conversation

mm/slub: Improve data handling of krealloc() when orig_size is enabled

PATCH Superseded Feng Tang 6 participants 21 messages Sep 9, 2024 – Sep 10, 2024

Subsystems (6)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Feng Tang

author
#0 lore raw

cover mm/slub: Improve data handling of krealloc() when orig_size is enabled

Danilo Krummrich's patch [1] raised one problem about krealloc() that its caller doesn't know what's the actual request size, say the object is 64 bytes kmalloc one, but the original caller may only requested 48 bytes. And when krealloc() shrinks or grows in the same object, or allocate a new bigger object, it lacks this 'original size' information to do accurate data preserving or zeroing (when __GFP_ZERO is set).

And when some slub debug option is enabled, kmalloc caches do have this 'orig_size' feature. As suggested by Vlastimil, utilize it to do more accurate data handling, as well as enforce the kmalloc-redzone sanity check.

To make the 'orig_size' accurate, we adjust some kasan/slub meta data handling. Also add a slub kunit test case for krealloc().

This patchset has dependency over patches in both -mm tree and -slab trees, so it is written based on linux-next tree '20240905' version.

[1]. https://lore.kernel.org/lkml/[email protected]/

Thanks,
Feng

Feng Tang (5):
mm/kasan: Don't store metadata inside kmalloc object when
slub_debug_orig_size is on
mm/slub: Consider kfence case for get_orig_size()
mm/slub: Improve redzone check and zeroing for krealloc()
kunit: kfence: Make KFENCE_TEST_REQUIRES macro available for all kunit
case
mm/slub, kunit: Add testcase for krealloc redzone and zeroing

 include/kunit/test.h    |   6 ++
 lib/slub_kunit.c        |  46 +++++++++++++++
 mm/kasan/generic.c      |   5 +-
 mm/kfence/kfence_test.c |   9 +--
 mm/slab.h               |   6 ++
 mm/slab_common.c        |  84 ---------------------------
 mm/slub.c               | 125 ++++++++++++++++++++++++++++++++++------
 7 files changed, 171 insertions(+), 110 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/5 mm/kasan: Don't store metadata inside kmalloc object when slub_debug_orig_size is on

For a kmalloc object, when both kasan and slub redzone sanity check are enabled, they could both manipulate its data space like storing kasan free meta data and setting up kmalloc redzone, and may affect accuracy of that object's 'orig_size'.

As an accurate 'orig_size' will be needed by some function like krealloc() soon, save kasan's free meta data in slub's metadata area instead of inside object when 'orig_size' is enabled.

This will make it easier to maintain/understand the code. Size wise, when these two options are both enabled, the slub meta data space is already huge, and this just slightly increase the overall size.

---
 mm/kasan/generic.c |  5 ++++-
 mm/slab.h          |  6 ++++++
 mm/slub.c          | 17 -----------------
 3 files changed, 10 insertions(+), 18 deletions(-)
mm/kasan/generic.c +4 -1
index 6310a180278b..cad376199d47 100644
@@ -393,8 +393,11 @@ void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
 	 *    be touched after it was freed, or
 	 * 2. Object has a constructor, which means it's expected to
 	 *    retain its content until the next allocation.
+	 * 3. It is from a kmalloc cache which enables the debug option
+	 *    to store original size.
 	 */
-	if ((cache->flags & SLAB_TYPESAFE_BY_RCU) || cache->ctor) {
+	if ((cache->flags & SLAB_TYPESAFE_BY_RCU) || cache->ctor ||
+	     slub_debug_orig_size(cache)) {
 		cache->kasan_info.free_meta_offset = *size;
 		*size += sizeof(struct kasan_free_meta);
 		goto free_meta_added;
mm/slab.h +6 -0
index 90f95bda4571..7a0e9b34ba2a 100644
@@ -689,6 +689,12 @@ void __kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab)
 void __check_heap_object(const void *ptr, unsigned long n,
 			 const struct slab *slab, bool to_user);
 
+static inline bool slub_debug_orig_size(struct kmem_cache *s)
+{
+	return (kmem_cache_debug_flags(s, SLAB_STORE_USER) &&
+			(s->flags & SLAB_KMALLOC));
+}
+
 #ifdef CONFIG_SLUB_DEBUG
 void skip_orig_size_check(struct kmem_cache *s, const void *object);
 #endif
mm/slub.c +0 -17
index 23761533329d..996a72fa6f62 100644
@@ -230,12 +230,6 @@ static inline bool kmem_cache_debug(struct kmem_cache *s)
 	return kmem_cache_debug_flags(s, SLAB_DEBUG_FLAGS);
 }
 
-static inline bool slub_debug_orig_size(struct kmem_cache *s)
-{
-	return (kmem_cache_debug_flags(s, SLAB_STORE_USER) &&
-			(s->flags & SLAB_KMALLOC));
-}
-
 void *fixup_red_left(struct kmem_cache *s, void *p)
 {
 	if (kmem_cache_debug_flags(s, SLAB_RED_ZONE))
@@ -760,21 +754,10 @@ static inline void set_orig_size(struct kmem_cache *s,
 				void *object, unsigned int orig_size)
 {
 	void *p = kasan_reset_tag(object);
-	unsigned int kasan_meta_size;
 
 	if (!slub_debug_orig_size(s))
 		return;
 
-	/*
-	 * KASAN can save its free meta data inside of the object at offset 0.
-	 * If this meta data size is larger than 'orig_size', it will overlap
-	 * the data redzone in [orig_size+1, object_size]. Thus, we adjust
-	 * 'orig_size' to be as at least as big as KASAN's meta data.
-	 */
-	kasan_meta_size = kasan_metadata_size(s, true);
-	if (kasan_meta_size > orig_size)
-		orig_size = kasan_meta_size;
-
 	p += get_info_end(s);
 	p += sizeof(struct track) * 2;
 
Signature · 1 line
2.34.1
reviewer Acked-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] mm/kasan: Don't store metadata inside kmalloc object when slub_debug_orig_size is on

Quoted 26 lines · Feng Tang

For a kmalloc object, when both kasan and slub redzone sanity check are enabled, they could both manipulate its data space like storing kasan free meta data and setting up kmalloc redzone, and may affect accuracy of that object's 'orig_size'.

As an accurate 'orig_size' will be needed by some function like krealloc() soon, save kasan's free meta data in slub's metadata area instead of inside object when 'orig_size' is enabled.

This will make it easier to maintain/understand the code. Size wise, when these two options are both enabled, the slub meta data space is already huge, and this just slightly increase the overall size.

---
 mm/kasan/generic.c |  5 ++++-
 mm/slab.h          |  6 ++++++
 mm/slub.c          | 17 -----------------
 3 files changed, 10 insertions(+), 18 deletions(-)
mm/kasan/generic.c +0 -0
index 6310a180278b..cad376199d47 100644

mm/kasan/generic.c void kasan_cache_create(struct kmem_cache *cache, unsigned int *size, · line 395

Quoted 3 lines
mm/kasan/generic.c +0 -0
@@ -393,8 +393,11 @@ void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
         *    be touched after it was freed, or
         * 2. Object has a constructor, which means it's expected to
         *    retain its content until the next allocation.

Nit: ", or" above.

Quoted 66 lines
mm/kasan/generic.c +4 -1
+        * 3. It is from a kmalloc cache which enables the debug option
+        *    to store original size.
         */
-       if ((cache->flags & SLAB_TYPESAFE_BY_RCU) || cache->ctor) {
+       if ((cache->flags & SLAB_TYPESAFE_BY_RCU) || cache->ctor ||
+            slub_debug_orig_size(cache)) {
                cache->kasan_info.free_meta_offset = *size;
                *size += sizeof(struct kasan_free_meta);
                goto free_meta_added;
mm/slab.h +6 -0
index 90f95bda4571..7a0e9b34ba2a 100644
@@ -689,6 +689,12 @@ void __kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab)
 void __check_heap_object(const void *ptr, unsigned long n,
                         const struct slab *slab, bool to_user);
 
+static inline bool slub_debug_orig_size(struct kmem_cache *s)
+{
+       return (kmem_cache_debug_flags(s, SLAB_STORE_USER) &&
+                       (s->flags & SLAB_KMALLOC));
+}
+
 #ifdef CONFIG_SLUB_DEBUG
 void skip_orig_size_check(struct kmem_cache *s, const void *object);
 #endif
mm/slub.c +0 -18
index 23761533329d..996a72fa6f62 100644
@@ -230,12 +230,6 @@ static inline bool kmem_cache_debug(struct kmem_cache *s)
        return kmem_cache_debug_flags(s, SLAB_DEBUG_FLAGS);
 }
 
-static inline bool slub_debug_orig_size(struct kmem_cache *s)
-{
-       return (kmem_cache_debug_flags(s, SLAB_STORE_USER) &&
-                       (s->flags & SLAB_KMALLOC));
-}
-
 void *fixup_red_left(struct kmem_cache *s, void *p)
 {
        if (kmem_cache_debug_flags(s, SLAB_RED_ZONE))
@@ -760,21 +754,10 @@ static inline void set_orig_size(struct kmem_cache *s,
                                void *object, unsigned int orig_size)
 {
        void *p = kasan_reset_tag(object);
-       unsigned int kasan_meta_size;
 
        if (!slub_debug_orig_size(s))
                return;
 
-       /*
-        * KASAN can save its free meta data inside of the object at offset 0.
-        * If this meta data size is larger than 'orig_size', it will overlap
-        * the data redzone in [orig_size+1, object_size]. Thus, we adjust
-        * 'orig_size' to be as at least as big as KASAN's meta data.
-        */
-       kasan_meta_size = kasan_metadata_size(s, true);
-       if (kasan_meta_size > orig_size)
-               orig_size = kasan_meta_size;
-
        p += get_info_end(s);
        p += sizeof(struct track) * 2;
 
--
Quoted 2 lines

2.34.1

author
#3 lore raw

Re: [PATCH 1/5] mm/kasan: Don't store metadata inside kmalloc object when slub_debug_orig_size is on

Quoted 22 lines · Andrey Konovalov
Quoted 21 lines · Feng Tang

For a kmalloc object, when both kasan and slub redzone sanity check are enabled, they could both manipulate its data space like storing kasan free meta data and setting up kmalloc redzone, and may affect accuracy of that object's 'orig_size'.

As an accurate 'orig_size' will be needed by some function like krealloc() soon, save kasan's free meta data in slub's metadata area instead of inside object when 'orig_size' is enabled.

This will make it easier to maintain/understand the code. Size wise, when these two options are both enabled, the slub meta data space is already huge, and this just slightly increase the overall size.

---
 mm/kasan/generic.c |  5 ++++-
 mm/slab.h          |  6 ++++++
 mm/slub.c          | 17 -----------------
 3 files changed, 10 insertions(+), 18 deletions(-)
Quoted 10 lines

mm/kasan/generic.c void kasan_cache_create(struct kmem_cache *cache, unsigned int *size, · line 395

Quoted 8 lines
mm/kasan/generic.c +0 -0
index 6310a180278b..cad376199d47 100644
@@ -393,8 +393,11 @@ void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
         *    be touched after it was freed, or
         * 2. Object has a constructor, which means it's expected to
         *    retain its content until the next allocation.

Nit: ", or" above.

Aha, yes, I missed that.

Hi Vlastimil,

Could you help to change it when taking the patches, or you prefer me to send a new version? thanks!

Quoted 66 lines
Quoted 66 lines
mm/kasan/generic.c +4 -1
+        * 3. It is from a kmalloc cache which enables the debug option
+        *    to store original size.
         */
-       if ((cache->flags & SLAB_TYPESAFE_BY_RCU) || cache->ctor) {
+       if ((cache->flags & SLAB_TYPESAFE_BY_RCU) || cache->ctor ||
+            slub_debug_orig_size(cache)) {
                cache->kasan_info.free_meta_offset = *size;
                *size += sizeof(struct kasan_free_meta);
                goto free_meta_added;
mm/slab.h +6 -0
index 90f95bda4571..7a0e9b34ba2a 100644
@@ -689,6 +689,12 @@ void __kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab)
 void __check_heap_object(const void *ptr, unsigned long n,
                         const struct slab *slab, bool to_user);
 
+static inline bool slub_debug_orig_size(struct kmem_cache *s)
+{
+       return (kmem_cache_debug_flags(s, SLAB_STORE_USER) &&
+                       (s->flags & SLAB_KMALLOC));
+}
+
 #ifdef CONFIG_SLUB_DEBUG
 void skip_orig_size_check(struct kmem_cache *s, const void *object);
 #endif
mm/slub.c +0 -17
index 23761533329d..996a72fa6f62 100644
@@ -230,12 +230,6 @@ static inline bool kmem_cache_debug(struct kmem_cache *s)
        return kmem_cache_debug_flags(s, SLAB_DEBUG_FLAGS);
 }
 
-static inline bool slub_debug_orig_size(struct kmem_cache *s)
-{
-       return (kmem_cache_debug_flags(s, SLAB_STORE_USER) &&
-                       (s->flags & SLAB_KMALLOC));
-}
-
 void *fixup_red_left(struct kmem_cache *s, void *p)
 {
        if (kmem_cache_debug_flags(s, SLAB_RED_ZONE))
@@ -760,21 +754,10 @@ static inline void set_orig_size(struct kmem_cache *s,
                                void *object, unsigned int orig_size)
 {
        void *p = kasan_reset_tag(object);
-       unsigned int kasan_meta_size;
 
        if (!slub_debug_orig_size(s))
                return;
 
-       /*
-        * KASAN can save its free meta data inside of the object at offset 0.
-        * If this meta data size is larger than 'orig_size', it will overlap
-        * the data redzone in [orig_size+1, object_size]. Thus, we adjust
-        * 'orig_size' to be as at least as big as KASAN's meta data.
-        */
-       kasan_meta_size = kasan_metadata_size(s, true);
-       if (kasan_meta_size > orig_size)
-               orig_size = kasan_meta_size;
-
        p += get_info_end(s);
        p += sizeof(struct track) * 2;
 
Quoted 5 lines
Quoted 3 lines

--
2.34.1

Thank you!

- Feng

author
#4 lore raw

PATCH 2/5 mm/slub: Consider kfence case for get_orig_size()

In reply to Feng Tang

When 'orig_size' of kmalloc object is enabled by debug option, it should either contains the actual requested size or the cache's 'object_size'.

But it's not true if that object is a kfence-allocated one, and its 'orig_size' in metadata could be zero or other values. This is not a big issue for current 'orig_size' usage, as init_object() and check_object() during alloc/free process will be skipped for kfence addresses.

As 'orig_size' will be used by some function block like krealloc(), handle it by returning the 'object_size' in get_orig_size() for kfence addresses.

---
 mm/slub.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/slub.c +1 -1
index 996a72fa6f62..4cb3822dba08 100644
@@ -768,7 +768,7 @@ static inline unsigned int get_orig_size(struct kmem_cache *s, void *object)
 {
 	void *p = kasan_reset_tag(object);
 
-	if (!slub_debug_orig_size(s))
+	if (!slub_debug_orig_size(s) || is_kfence_address(object))
 		return s->object_size;
 
 	p += get_info_end(s);
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH 3/5 mm/slub: Improve redzone check and zeroing for krealloc()

In reply to Feng Tang

For current krealloc(), one problem is its caller doesn't know what's the actual request size, say the object is 64 bytes kmalloc one, but the original caller may only requested 48 bytes. And when krealloc() shrinks or grows in the same object, or allocate a new bigger object, it lacks this 'original size' information to do accurate data preserving or zeroing (when __GFP_ZERO is set).

And when some slub debug option is enabled, kmalloc caches do have this 'orig_size' feature. So utilize it to do more accurate data handling, as well as enforce the kmalloc-redzone sanity check.

The krealloc() related code is moved from slab_common.c to slub.c for more efficient function calling.

---
 mm/slab_common.c |  84 -------------------------------------
 mm/slub.c        | 106 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 106 insertions(+), 84 deletions(-)
mm/slab_common.c +0 -84
index ad438ba62485..e59942fb7970 100644
@@ -1297,90 +1297,6 @@ module_init(slab_proc_init);
 
 #endif /* CONFIG_SLUB_DEBUG */
 
-static __always_inline __realloc_size(2) void *
-__do_krealloc(const void *p, size_t new_size, gfp_t flags)
-{
-	void *ret;
-	size_t ks;
-
-	/* Check for double-free before calling ksize. */
-	if (likely(!ZERO_OR_NULL_PTR(p))) {
-		if (!kasan_check_byte(p))
-			return NULL;
-		ks = ksize(p);
-	} else
-		ks = 0;
-
-	/* If the object still fits, repoison it precisely. */
-	if (ks >= new_size) {
-		/* Zero out spare memory. */
-		if (want_init_on_alloc(flags)) {
-			kasan_disable_current();
-			memset((void *)p + new_size, 0, ks - new_size);
-			kasan_enable_current();
-		}
-
-		p = kasan_krealloc((void *)p, new_size, flags);
-		return (void *)p;
-	}
-
-	ret = kmalloc_node_track_caller_noprof(new_size, flags, NUMA_NO_NODE, _RET_IP_);
-	if (ret && p) {
-		/* Disable KASAN checks as the object's redzone is accessed. */
-		kasan_disable_current();
-		memcpy(ret, kasan_reset_tag(p), ks);
-		kasan_enable_current();
-	}
-
-	return ret;
-}
-
-/**
- * krealloc - reallocate memory. The contents will remain unchanged.
- * @p: object to reallocate memory for.
- * @new_size: how many bytes of memory are required.
- * @flags: the type of memory to allocate.
- *
- * If @p is %NULL, krealloc() behaves exactly like kmalloc().  If @new_size
- * is 0 and @p is not a %NULL pointer, the object pointed to is freed.
- *
- * If __GFP_ZERO logic is requested, callers must ensure that, starting with the
- * initial memory allocation, every subsequent call to this API for the same
- * memory allocation is flagged with __GFP_ZERO. Otherwise, it is possible that
- * __GFP_ZERO is not fully honored by this API.
- *
- * This is the case, since krealloc() only knows about the bucket size of an
- * allocation (but not the exact size it was allocated with) and hence
- * implements the following semantics for shrinking and growing buffers with
- * __GFP_ZERO.
- *
- *         new             bucket
- * 0       size             size
- * |--------|----------------|
- * |  keep  |      zero      |
- *
- * In any case, the contents of the object pointed to are preserved up to the
- * lesser of the new and old sizes.
- *
- * Return: pointer to the allocated memory or %NULL in case of error
- */
-void *krealloc_noprof(const void *p, size_t new_size, gfp_t flags)
-{
-	void *ret;
-
-	if (unlikely(!new_size)) {
-		kfree(p);
-		return ZERO_SIZE_PTR;
-	}
-
-	ret = __do_krealloc(p, new_size, flags);
-	if (ret && kasan_reset_tag(p) != kasan_reset_tag(ret))
-		kfree(p);
-
-	return ret;
-}
-EXPORT_SYMBOL(krealloc_noprof);
-
 /**
  * kfree_sensitive - Clear sensitive information in memory before freeing
  * @p: object to free memory of
mm/slub.c +106 -0

112 lines not on this page · raw message

Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH 3/5] mm/slub: Improve redzone check and zeroing for krealloc()

Quoted 7 lines · Feng Tang

For current krealloc(), one problem is its caller doesn't know what's the actual request size, say the object is 64 bytes kmalloc one, but the original caller may only requested 48 bytes. And when krealloc() shrinks or grows in the same object, or allocate a new bigger object, it lacks this 'original size' information to do accurate data preserving or zeroing (when __GFP_ZERO is set).

Quoted 6 lines

And when some slub debug option is enabled, kmalloc caches do have this 'orig_size' feature. So utilize it to do more accurate data handling, as well as enforce the kmalloc-redzone sanity check.

The krealloc() related code is moved from slab_common.c to slub.c for more efficient function calling.

I think it would be good to do this in a separate commit, for a better diff and history.

Quoted 172 lines
---
 mm/slab_common.c |  84 -------------------------------------
 mm/slub.c        | 106 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 106 insertions(+), 84 deletions(-)
mm/slab_common.c +0 -84
index ad438ba62485..e59942fb7970 100644
@@ -1297,90 +1297,6 @@ module_init(slab_proc_init);
 
 #endif /* CONFIG_SLUB_DEBUG */
 
-static __always_inline __realloc_size(2) void *
-__do_krealloc(const void *p, size_t new_size, gfp_t flags)
-{
-	void *ret;
-	size_t ks;
-
-	/* Check for double-free before calling ksize. */
-	if (likely(!ZERO_OR_NULL_PTR(p))) {
-		if (!kasan_check_byte(p))
-			return NULL;
-		ks = ksize(p);
-	} else
-		ks = 0;
-
-	/* If the object still fits, repoison it precisely. */
-	if (ks >= new_size) {
-		/* Zero out spare memory. */
-		if (want_init_on_alloc(flags)) {
-			kasan_disable_current();
-			memset((void *)p + new_size, 0, ks - new_size);
-			kasan_enable_current();
-		}
-
-		p = kasan_krealloc((void *)p, new_size, flags);
-		return (void *)p;
-	}
-
-	ret = kmalloc_node_track_caller_noprof(new_size, flags, NUMA_NO_NODE, _RET_IP_);
-	if (ret && p) {
-		/* Disable KASAN checks as the object's redzone is accessed. */
-		kasan_disable_current();
-		memcpy(ret, kasan_reset_tag(p), ks);
-		kasan_enable_current();
-	}
-
-	return ret;
-}
-
-/**
- * krealloc - reallocate memory. The contents will remain unchanged.
- * @p: object to reallocate memory for.
- * @new_size: how many bytes of memory are required.
- * @flags: the type of memory to allocate.
- *
- * If @p is %NULL, krealloc() behaves exactly like kmalloc().  If @new_size
- * is 0 and @p is not a %NULL pointer, the object pointed to is freed.
- *
- * If __GFP_ZERO logic is requested, callers must ensure that, starting with the
- * initial memory allocation, every subsequent call to this API for the same
- * memory allocation is flagged with __GFP_ZERO. Otherwise, it is possible that
- * __GFP_ZERO is not fully honored by this API.
- *
- * This is the case, since krealloc() only knows about the bucket size of an
- * allocation (but not the exact size it was allocated with) and hence
- * implements the following semantics for shrinking and growing buffers with
- * __GFP_ZERO.
- *
- *         new             bucket
- * 0       size             size
- * |--------|----------------|
- * |  keep  |      zero      |
- *
- * In any case, the contents of the object pointed to are preserved up to the
- * lesser of the new and old sizes.
- *
- * Return: pointer to the allocated memory or %NULL in case of error
- */
-void *krealloc_noprof(const void *p, size_t new_size, gfp_t flags)
-{
-	void *ret;
-
-	if (unlikely(!new_size)) {
-		kfree(p);
-		return ZERO_SIZE_PTR;
-	}
-
-	ret = __do_krealloc(p, new_size, flags);
-	if (ret && kasan_reset_tag(p) != kasan_reset_tag(ret))
-		kfree(p);
-
-	return ret;
-}
-EXPORT_SYMBOL(krealloc_noprof);
-
 /**
  * kfree_sensitive - Clear sensitive information in memory before freeing
  * @p: object to free memory of
mm/slub.c +61 -0
index 4cb3822dba08..d4c938dfb89e 100644
@@ -4709,6 +4709,112 @@ void kfree(const void *object)
 }
 EXPORT_SYMBOL(kfree);
 
+static __always_inline __realloc_size(2) void *
+__do_krealloc(const void *p, size_t new_size, gfp_t flags)
+{
+	void *ret;
+	size_t ks;
+	int orig_size = 0;
+	struct kmem_cache *s;
+
+	/* Check for double-free before calling ksize. */
+	if (likely(!ZERO_OR_NULL_PTR(p))) {
+		if (!kasan_check_byte(p))
+			return NULL;
+
+		s = virt_to_cache(p);
+		orig_size = get_orig_size(s, (void *)p);
+		ks = s->object_size;
+	} else
+		ks = 0;
+
+	/* If the object doesn't fit, allocate a bigger one */
+	if (new_size > ks)
+		goto alloc_new;
+
+	/* Zero out spare memory. */
+	if (want_init_on_alloc(flags)) {
+		kasan_disable_current();
+		if (orig_size < new_size)
+			memset((void *)p + orig_size, 0, new_size - orig_size);
+		else
+			memset((void *)p + new_size, 0, ks - new_size);
+		kasan_enable_current();
+	}
+
+	if (slub_debug_orig_size(s) && !is_kfence_address(p)) {
+		set_orig_size(s, (void *)p, new_size);
+		if (s->flags & SLAB_RED_ZONE && new_size < ks)
+			memset_no_sanitize_memory((void *)p + new_size,
+						SLUB_RED_ACTIVE, ks - new_size);
+	}
+
+	p = kasan_krealloc((void *)p, new_size, flags);
+	return (void *)p;
+
+alloc_new:
+	ret = kmalloc_node_track_caller_noprof(new_size, flags, NUMA_NO_NODE, _RET_IP_);
+	if (ret && p) {
+		/* Disable KASAN checks as the object's redzone is accessed. */
+		kasan_disable_current();
+		if (orig_size)
+			memcpy(ret, kasan_reset_tag(p), orig_size);
+		kasan_enable_current();
+	}
+
+	return ret;
+}
+
+/**
+ * krealloc - reallocate memory. The contents will remain unchanged.
+ * @p: object to reallocate memory for.
+ * @new_size: how many bytes of memory are required.
+ * @flags: the type of memory to allocate.

mm/slub.c void kfree(const void *object) · line 4782

Quoted 10 lines
mm/slub.c +10 -0
+ *
+ * If @p is %NULL, krealloc() behaves exactly like kmalloc().  If @new_size
+ * is 0 and @p is not a %NULL pointer, the object pointed to is freed.
+ *
+ * If __GFP_ZERO logic is requested, callers must ensure that, starting with the
+ * initial memory allocation, every subsequent call to this API for the same
+ * memory allocation is flagged with __GFP_ZERO. Otherwise, it is possible that
+ * __GFP_ZERO is not fully honored by this API.
+ *
+ * When slub_debug_orig_size() is off,  since krealloc() only knows about the

I think you want to remove ' since ' here.

mm/slub.c void kfree(const void *object) · line 4792

Quoted 10 lines
mm/slub.c +10 -0
+ * bucket size of an allocation (but not the exact size it was allocated with)
+ * and hence implements the following semantics for shrinking and growing
+ * buffers with __GFP_ZERO.
+ *
+ *         new             bucket
+ * 0       size             size
+ * |--------|----------------|
+ * |  keep  |      zero      |
+ *
+ * Otherwize, the original allocation size 'orig_size' could be used to

Typo in 'otherwise'.

Quoted 31 lines
mm/slub.c +25 -0
+ * precisely clear the requested size, and the new size will also be stored as
+ * the new 'orig_size'.
+ *
+ * In any case, the contents of the object pointed to are preserved up to the
+ * lesser of the new and old sizes.
+ *
+ * Return: pointer to the allocated memory or %NULL in case of error
+ */
+void *krealloc_noprof(const void *p, size_t new_size, gfp_t flags)
+{
+	void *ret;
+
+	if (unlikely(!new_size)) {
+		kfree(p);
+		return ZERO_SIZE_PTR;
+	}
+
+	ret = __do_krealloc(p, new_size, flags);
+	if (ret && kasan_reset_tag(p) != kasan_reset_tag(ret))
+		kfree(p);
+
+	return ret;
+}
+EXPORT_SYMBOL(krealloc_noprof);
+
 struct detached_freelist {
 	struct slab *slab;
 	void *tail;
Signature · 1 line
2.34.1
author
#7 lore raw

Re: [PATCH 3/5] mm/slub: Improve redzone check and zeroing for krealloc()

Hi Danilo,

Thanks for the review!

Quoted 12 lines · Danilo Krummrich
Quoted 11 lines · Feng Tang

For current krealloc(), one problem is its caller doesn't know what's the actual request size, say the object is 64 bytes kmalloc one, but the original caller may only requested 48 bytes. And when krealloc() shrinks or grows in the same object, or allocate a new bigger object, it lacks this 'original size' information to do accurate data preserving or zeroing (when __GFP_ZERO is set).

And when some slub debug option is enabled, kmalloc caches do have this 'orig_size' feature. So utilize it to do more accurate data handling, as well as enforce the kmalloc-redzone sanity check.

Quoted 5 lines
Quoted 2 lines

The krealloc() related code is moved from slab_common.c to slub.c for more efficient function calling.

I think it would be good to do this in a separate commit, for a better diff and history.

Agreed. will do.

Quoted 7 lines
Quoted 7 lines
---
 mm/slab_common.c |  84 -------------------------------------
 mm/slub.c        | 106 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 106 insertions(+), 84 deletions(-)

[...]

Quoted 10 lines
Quoted 10 lines
mm/slub.c +10 -0
+
+/**
+ * krealloc - reallocate memory. The contents will remain unchanged.
+ * @p: object to reallocate memory for.
+ * @new_size: how many bytes of memory are required.
+ * @flags: the type of memory to allocate.
+ *
+ * If @p is %NULL, krealloc() behaves exactly like kmalloc().  If @new_size
+ * is 0 and @p is not a %NULL pointer, the object pointed to is freed.
+ *
Quoted 8 lines
Quoted 6 lines

+ * If __GFP_ZERO logic is requested, callers must ensure that, starting with the
+ * initial memory allocation, every subsequent call to this API for the same
+ * memory allocation is flagged with __GFP_ZERO. Otherwise, it is possible that
+ * __GFP_ZERO is not fully honored by this API.
+ *
+ * When slub_debug_orig_size() is off, since krealloc() only knows about the

I think you want to remove ' since ' here.

Yes! :)

Quoted 11 lines
Quoted 10 lines

+ * bucket size of an allocation (but not the exact size it was allocated with)
+ * and hence implements the following semantics for shrinking and growing
+ * buffers with __GFP_ZERO.
+ *
+ * new bucket
+ * 0 size size

+ * |--------|----------------|
+ * |  keep  |      zero      |

+ *
+ * Otherwize, the original allocation size 'orig_size' could be used to

Quoted 1 line

Typo in 'otherwise'.

Will fix.

Thanks,
Feng

#8 lore raw

Re: [PATCH 3/5] mm/slub: Improve redzone check and zeroing for krealloc()

In reply to Feng Tang
Quoted 2 lines · Feng Tang

For current krealloc(), one problem is its caller doesn't know what's the actual request size, say the object is 64 bytes kmalloc one, but

It's more accurate to say the caller doesn't pass the old size (it might actually know it).

Quoted 4 lines

the original caller may only requested 48 bytes. And when krealloc() shrinks or grows in the same object, or allocate a new bigger object, it lacks this 'original size' information to do accurate data preserving or zeroing (when __GFP_ZERO is set).

Let's describe the problem specifically by adding:

Thus with slub debug redzone and object tracking enabled, parts of the object after krealloc() might contain redzone data instead of zeroes, which is violating the __GFP_ZERO guarantees.

Quoted 6 lines

And when some slub debug option is enabled, kmalloc caches do have this 'orig_size' feature. So utilize it to do more accurate data handling, as well as enforce the kmalloc-redzone sanity check.

The krealloc() related code is moved from slab_common.c to slub.c for more efficient function calling.

Agreed with Danilo that having the move as separate preparatory patch would make review easier.

Thanks.

author
#9 lore raw

Re: [PATCH 3/5] mm/slub: Improve redzone check and zeroing for krealloc()

Quoted 6 lines · Vlastimil Babka
Quoted 2 lines · Feng Tang

For current krealloc(), one problem is its caller doesn't know what's the actual request size, say the object is 64 bytes kmalloc one, but

It's more accurate to say the caller doesn't pass the old size (it might actually know it).

Right!

Quoted 11 lines
Quoted 5 lines

the original caller may only requested 48 bytes. And when krealloc() shrinks or grows in the same object, or allocate a new bigger object, it lacks this 'original size' information to do accurate data preserving or zeroing (when __GFP_ZERO is set).

Let's describe the problem specifically by adding:

Thus with slub debug redzone and object tracking enabled, parts of the object after krealloc() might contain redzone data instead of zeroes, which is violating the __GFP_ZERO guarantees.

Will add it. Thanks for the enhancement!

Quoted 9 lines
Quoted 6 lines

And when some slub debug option is enabled, kmalloc caches do have this 'orig_size' feature. So utilize it to do more accurate data handling, as well as enforce the kmalloc-redzone sanity check.

The krealloc() related code is moved from slab_common.c to slub.c for more efficient function calling.

Agreed with Danilo that having the move as separate preparatory patch would make review easier.

Yes and will do.

Thanks,
Feng

Quoted 2 lines

Thanks.

author
#10 lore raw

PATCH 4/5 kunit: kfence: Make KFENCE_TEST_REQUIRES macro available for all kunit case

In reply to Feng Tang

KFENCE_TEST_REQUIRES macro is convenient for judging if a prerequisite of a test case exists. Lift it into kunit/test.h so that all kunit test cases can benefit from it.

---
 include/kunit/test.h    | 6 ++++++
 mm/kfence/kfence_test.c | 9 ++-------
 2 files changed, 8 insertions(+), 7 deletions(-)
include/kunit/test.h +6 -0
index 5ac237c949a0..8a8027e10b89 100644
@@ -643,6 +643,12 @@ void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt,
 	WRITE_ONCE(test->last_seen.line, __LINE__);			       \
 } while (0)
 
+#define KUNIT_TEST_REQUIRES(test, cond) do {			\
+	if (!(cond))						\
+		kunit_skip((test), "Test requires: " #cond);	\
+} while (0)
+
+
 /**
  * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
  * @test: The test context object.
mm/kfence/kfence_test.c +2 -7
index 00fd17285285..5dbb22c8c44f 100644
@@ -32,11 +32,6 @@
 #define arch_kfence_test_address(addr) (addr)
 #endif
 
-#define KFENCE_TEST_REQUIRES(test, cond) do {			\
-	if (!(cond))						\
-		kunit_skip((test), "Test requires: " #cond);	\
-} while (0)
-
 /* Report as observed from console. */
 static struct {
 	spinlock_t lock;
@@ -561,7 +556,7 @@ static void test_init_on_free(struct kunit *test)
 	};
 	int i;
 
-	KFENCE_TEST_REQUIRES(test, IS_ENABLED(CONFIG_INIT_ON_FREE_DEFAULT_ON));
+	KUNIT_TEST_REQUIRES(test, IS_ENABLED(CONFIG_INIT_ON_FREE_DEFAULT_ON));
 	/* Assume it hasn't been disabled on command line. */
 
 	setup_test_cache(test, size, 0, NULL);
@@ -609,7 +604,7 @@ static void test_gfpzero(struct kunit *test)
 	int i;
 
 	/* Skip if we think it'd take too long. */
-	KFENCE_TEST_REQUIRES(test, kfence_sample_interval <= 100);
+	KUNIT_TEST_REQUIRES(test, kfence_sample_interval <= 100);
 
 	setup_test_cache(test, size, 0, NULL);
 	buf1 = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY);
Signature · 1 line
2.34.1
#11 lore raw

Re: [PATCH 4/5] kunit: kfence: Make KFENCE_TEST_REQUIRES macro available for all kunit case

Quoted 5 lines · Feng Tang

KFENCE_TEST_REQUIRES macro is convenient for judging if a prerequisite of a test case exists. Lift it into kunit/test.h so that all kunit test cases can benefit from it.

I think you should have Cc'd kunit and kfence maintainers on this one. But if that's necessary depends on the review for patch 5...

Quoted 56 lines
---
 include/kunit/test.h    | 6 ++++++
 mm/kfence/kfence_test.c | 9 ++-------
 2 files changed, 8 insertions(+), 7 deletions(-)
include/kunit/test.h +6 -0
index 5ac237c949a0..8a8027e10b89 100644
@@ -643,6 +643,12 @@ void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt,
 	WRITE_ONCE(test->last_seen.line, __LINE__);			       \
 } while (0)
 
+#define KUNIT_TEST_REQUIRES(test, cond) do {			\
+	if (!(cond))						\
+		kunit_skip((test), "Test requires: " #cond);	\
+} while (0)
+
+
 /**
  * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
  * @test: The test context object.
mm/kfence/kfence_test.c +2 -7
index 00fd17285285..5dbb22c8c44f 100644
@@ -32,11 +32,6 @@
 #define arch_kfence_test_address(addr) (addr)
 #endif
 
-#define KFENCE_TEST_REQUIRES(test, cond) do {			\
-	if (!(cond))						\
-		kunit_skip((test), "Test requires: " #cond);	\
-} while (0)
-
 /* Report as observed from console. */
 static struct {
 	spinlock_t lock;
@@ -561,7 +556,7 @@ static void test_init_on_free(struct kunit *test)
 	};
 	int i;
 
-	KFENCE_TEST_REQUIRES(test, IS_ENABLED(CONFIG_INIT_ON_FREE_DEFAULT_ON));
+	KUNIT_TEST_REQUIRES(test, IS_ENABLED(CONFIG_INIT_ON_FREE_DEFAULT_ON));
 	/* Assume it hasn't been disabled on command line. */
 
 	setup_test_cache(test, size, 0, NULL);
@@ -609,7 +604,7 @@ static void test_gfpzero(struct kunit *test)
 	int i;
 
 	/* Skip if we think it'd take too long. */
-	KFENCE_TEST_REQUIRES(test, kfence_sample_interval <= 100);
+	KUNIT_TEST_REQUIRES(test, kfence_sample_interval <= 100);
 
 	setup_test_cache(test, size, 0, NULL);
 	buf1 = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY);
author
#12 lore raw

Re: [PATCH 4/5] kunit: kfence: Make KFENCE_TEST_REQUIRES macro available for all kunit case

Quoted 9 lines · Vlastimil Babka
Quoted 5 lines · Feng Tang

KFENCE_TEST_REQUIRES macro is convenient for judging if a prerequisite of a test case exists. Lift it into kunit/test.h so that all kunit test cases can benefit from it.

I think you should have Cc'd kunit and kfence maintainers on this one. But if that's necessary depends on the review for patch 5...

I added Marco Elver, Shuah Khan, David Gow and [email protected] for kence and kunit review. That should be incomplete, will add more in next verion. Thanks for the reminder!

- Feng

Quoted 58 lines
Quoted 57 lines
---
 include/kunit/test.h    | 6 ++++++
 mm/kfence/kfence_test.c | 9 ++-------
 2 files changed, 8 insertions(+), 7 deletions(-)
include/kunit/test.h +6 -0
index 5ac237c949a0..8a8027e10b89 100644
@@ -643,6 +643,12 @@ void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt,
 	WRITE_ONCE(test->last_seen.line, __LINE__);			       \
 } while (0)
 
+#define KUNIT_TEST_REQUIRES(test, cond) do {			\
+	if (!(cond))						\
+		kunit_skip((test), "Test requires: " #cond);	\
+} while (0)
+
+
 /**
  * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
  * @test: The test context object.
mm/kfence/kfence_test.c +2 -7
index 00fd17285285..5dbb22c8c44f 100644
@@ -32,11 +32,6 @@
 #define arch_kfence_test_address(addr) (addr)
 #endif
 
-#define KFENCE_TEST_REQUIRES(test, cond) do {			\
-	if (!(cond))						\
-		kunit_skip((test), "Test requires: " #cond);	\
-} while (0)
-
 /* Report as observed from console. */
 static struct {
 	spinlock_t lock;
@@ -561,7 +556,7 @@ static void test_init_on_free(struct kunit *test)
 	};
 	int i;
 
-	KFENCE_TEST_REQUIRES(test, IS_ENABLED(CONFIG_INIT_ON_FREE_DEFAULT_ON));
+	KUNIT_TEST_REQUIRES(test, IS_ENABLED(CONFIG_INIT_ON_FREE_DEFAULT_ON));
 	/* Assume it hasn't been disabled on command line. */
 
 	setup_test_cache(test, size, 0, NULL);
@@ -609,7 +604,7 @@ static void test_gfpzero(struct kunit *test)
 	int i;
 
 	/* Skip if we think it'd take too long. */
-	KFENCE_TEST_REQUIRES(test, kfence_sample_interval <= 100);
+	KUNIT_TEST_REQUIRES(test, kfence_sample_interval <= 100);
 
 	setup_test_cache(test, size, 0, NULL);
 	buf1 = test_alloc(test, size, GFP_KERNEL, ALLOCATE_ANY);
maintainer Reviewed-by
#13 lore raw

Re: [PATCH 4/5] kunit: kfence: Make KFENCE_TEST_REQUIRES macro available for all kunit case

Quoted 8 lines · Feng Tang
Quoted 6 lines · Vlastimil Babka
Quoted 5 lines · Feng Tang

KFENCE_TEST_REQUIRES macro is convenient for judging if a prerequisite of a test case exists. Lift it into kunit/test.h so that all kunit test cases can benefit from it.

(assuming KUNIT maintainers are fine with the change)

maintainer Reviewed-by
#14 lore raw

Re: [PATCH 4/5] kunit: kfence: Make KFENCE_TEST_REQUIRES macro available for all kunit case

In reply to Feng Tang
Quoted 7 lines · Feng Tang
Quoted 5 lines · Vlastimil Babka
Quoted 4 lines · Feng Tang

KFENCE_TEST_REQUIRES macro is convenient for judging if a prerequisite of a test case exists. Lift it into kunit/test.h so that all kunit test cases can benefit from it.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

I think you should have Cc'd kunit and kfence maintainers on this one. But if that's necessary depends on the review for patch 5...

I added Marco Elver, Shuah Khan, David Gow and [email protected] for kence and kunit review. That should be incomplete, will add more in next verion. Thanks for the reminder!

Glad to see you found this macro generally useful. But do await KUnit maintainer Ack as well.

Thanks,
-- Marco

author
#15 lore raw

PATCH 5/5 mm/slub, kunit: Add testcase for krealloc redzone and zeroing

In reply to Feng Tang

Danilo Krummrich raised issue about krealloc+GFP_ZERO [1], and Vlastimil suggested to add some test case which can sanity test the kmalloc-redzone and zeroing by utilizing the kmalloc's 'orig_size' debug feature.

It covers the grow and shrink case of krealloc() re-using current kmalloc object, and the case of re-allocating a new bigger object.

User can add "slub_debug" kernel cmdline parameter to test it.

[1]. https://lore.kernel.org/lkml/[email protected]/

---
 lib/slub_kunit.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
lib/slub_kunit.c +46 -0
index 6e3a1e5a7142..03e0089149ad 100644
@@ -186,6 +186,51 @@ static void test_leak_destroy(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, 1, slab_errors);
 }
 
+static void test_krealloc_redzone_zeroing(struct kunit *test)
+{
+	char *p;
+	int i;
+
+	KUNIT_TEST_REQUIRES(test, __slub_debug_enabled());
+
+	/* Allocate a 64B kmalloc object */
+	p = kzalloc(48, GFP_KERNEL);
+	if (unlikely(is_kfence_address(p))) {
+		kfree(p);
+		return;
+	}
+	memset(p, 0xff, 48);
+
+	kasan_disable_current();
+	OPTIMIZER_HIDE_VAR(p);
+
+	/* Test shrink */
+	p = krealloc(p, 40, GFP_KERNEL | __GFP_ZERO);
+	for (i = 40; i < 64; i++)
+		KUNIT_EXPECT_EQ(test, p[i], SLUB_RED_ACTIVE);
+
+	/* Test grow within the same 64B kmalloc object */
+	p = krealloc(p, 56, GFP_KERNEL | __GFP_ZERO);
+	for (i = 40; i < 56; i++)
+		KUNIT_EXPECT_EQ(test, p[i], 0);
+	for (i = 56; i < 64; i++)
+		KUNIT_EXPECT_EQ(test, p[i], SLUB_RED_ACTIVE);
+
+	/* Test grow with allocating a bigger 128B object */
+	p = krealloc(p, 112, GFP_KERNEL | __GFP_ZERO);
+	if (unlikely(is_kfence_address(p)))
+		goto exit;
+
+	for (i = 56; i < 112; i++)
+		KUNIT_EXPECT_EQ(test, p[i], 0);
+	for (i = 112; i < 128; i++)
+		KUNIT_EXPECT_EQ(test, p[i], SLUB_RED_ACTIVE);
+
+exit:
+	kfree(p);
+	kasan_enable_current();
+}
+
 static int test_init(struct kunit *test)
 {
 	slab_errors = 0;
@@ -196,6 +241,7 @@ static int test_init(struct kunit *test)
 }
 
 static struct kunit_case test_cases[] = {
+	KUNIT_CASE(test_krealloc_redzone_zeroing),
 	KUNIT_CASE(test_clobber_zone),
 
 #ifndef CONFIG_KASAN
Signature · 1 line
2.34.1
Reviewed-by on v1 5/5
#16 lore raw

Re: [PATCH 5/5] mm/slub, kunit: Add testcase for krealloc redzone and zeroing

Quoted 7 lines · Feng Tang

Danilo Krummrich raised issue about krealloc+GFP_ZERO [1], and Vlastimil suggested to add some test case which can sanity test the kmalloc-redzone and zeroing by utilizing the kmalloc's 'orig_size' debug feature.

It covers the grow and shrink case of krealloc() re-using current kmalloc object, and the case of re-allocating a new bigger object.

Quoted 6 lines

User can add "slub_debug" kernel cmdline parameter to test it.

[1]. https://lore.kernel.org/lkml/[email protected]/

Quoted 71 lines
---
 lib/slub_kunit.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
lib/slub_kunit.c +46 -0
index 6e3a1e5a7142..03e0089149ad 100644
@@ -186,6 +186,51 @@ static void test_leak_destroy(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, 1, slab_errors);
 }
 
+static void test_krealloc_redzone_zeroing(struct kunit *test)
+{
+	char *p;
+	int i;
+
+	KUNIT_TEST_REQUIRES(test, __slub_debug_enabled());
+
+	/* Allocate a 64B kmalloc object */
+	p = kzalloc(48, GFP_KERNEL);
+	if (unlikely(is_kfence_address(p))) {
+		kfree(p);
+		return;
+	}
+	memset(p, 0xff, 48);
+
+	kasan_disable_current();
+	OPTIMIZER_HIDE_VAR(p);
+
+	/* Test shrink */
+	p = krealloc(p, 40, GFP_KERNEL | __GFP_ZERO);
+	for (i = 40; i < 64; i++)
+		KUNIT_EXPECT_EQ(test, p[i], SLUB_RED_ACTIVE);
+
+	/* Test grow within the same 64B kmalloc object */
+	p = krealloc(p, 56, GFP_KERNEL | __GFP_ZERO);
+	for (i = 40; i < 56; i++)
+		KUNIT_EXPECT_EQ(test, p[i], 0);
+	for (i = 56; i < 64; i++)
+		KUNIT_EXPECT_EQ(test, p[i], SLUB_RED_ACTIVE);
+
+	/* Test grow with allocating a bigger 128B object */
+	p = krealloc(p, 112, GFP_KERNEL | __GFP_ZERO);
+	if (unlikely(is_kfence_address(p)))
+		goto exit;
+
+	for (i = 56; i < 112; i++)
+		KUNIT_EXPECT_EQ(test, p[i], 0);
+	for (i = 112; i < 128; i++)
+		KUNIT_EXPECT_EQ(test, p[i], SLUB_RED_ACTIVE);
+
+exit:
+	kfree(p);
+	kasan_enable_current();
+}
+
 static int test_init(struct kunit *test)
 {
 	slab_errors = 0;
@@ -196,6 +241,7 @@ static int test_init(struct kunit *test)
 }
 
 static struct kunit_case test_cases[] = {
+	KUNIT_CASE(test_krealloc_redzone_zeroing),
 	KUNIT_CASE(test_clobber_zone),
 
 #ifndef CONFIG_KASAN
Signature · 1 line
2.34.1
#17 lore raw

Re: [PATCH 5/5] mm/slub, kunit: Add testcase for krealloc redzone and zeroing

In reply to Feng Tang
Quoted 22 lines · Feng Tang

Danilo Krummrich raised issue about krealloc+GFP_ZERO [1], and Vlastimil suggested to add some test case which can sanity test the kmalloc-redzone and zeroing by utilizing the kmalloc's 'orig_size' debug feature.

It covers the grow and shrink case of krealloc() re-using current kmalloc object, and the case of re-allocating a new bigger object.

User can add "slub_debug" kernel cmdline parameter to test it.

[1]. https://lore.kernel.org/lkml/[email protected]/

---
 lib/slub_kunit.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
lib/slub_kunit.c +0 -0
index 6e3a1e5a7142..03e0089149ad 100644

lib/slub_kunit.c static void test_leak_destroy(struct kunit *test) · line 194

Quoted 9 lines
lib/slub_kunit.c +6 -0
@@ -186,6 +186,51 @@ static void test_leak_destroy(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, 1, slab_errors);
 }
 
+static void test_krealloc_redzone_zeroing(struct kunit *test)
+{
+	char *p;
+	int i;
+
+	KUNIT_TEST_REQUIRES(test, __slub_debug_enabled());

AFAICS this is insufficient, because the static key may be enabled due to debugging enabled for different caches than kmalloc, or it might not include both red zone and object tracking.

But it should be possible to instead create a fake kmalloc cache of size 64 and use __kmalloc_cache_noprof() like test_kmalloc_redzone_access()?

Quoted 16 lines
lib/slub_kunit.c +16 -0
+
+	/* Allocate a 64B kmalloc object */
+	p = kzalloc(48, GFP_KERNEL);
+	if (unlikely(is_kfence_address(p))) {
+		kfree(p);
+		return;
+	}
+	memset(p, 0xff, 48);
+
+	kasan_disable_current();
+	OPTIMIZER_HIDE_VAR(p);
+
+	/* Test shrink */
+	p = krealloc(p, 40, GFP_KERNEL | __GFP_ZERO);
+	for (i = 40; i < 64; i++)
+		KUNIT_EXPECT_EQ(test, p[i], SLUB_RED_ACTIVE);

lib/slub_kunit.c static void test_leak_destroy(struct kunit *test) · line 220

Quoted 10 lines
lib/slub_kunit.c +10 -0
+
+	/* Test grow within the same 64B kmalloc object */
+	p = krealloc(p, 56, GFP_KERNEL | __GFP_ZERO);
+	for (i = 40; i < 56; i++)
+		KUNIT_EXPECT_EQ(test, p[i], 0);
+	for (i = 56; i < 64; i++)
+		KUNIT_EXPECT_EQ(test, p[i], SLUB_RED_ACTIVE);
+
+	/* Test grow with allocating a bigger 128B object */
+	p = krealloc(p, 112, GFP_KERNEL | __GFP_ZERO);

The only downside is that krealloc() here might use kmalloc-128 cache that's not doing red zoning and object tracking....

lib/slub_kunit.c static void test_leak_destroy(struct kunit *test) · line 225

Quoted 5 lines
lib/slub_kunit.c +5 -0
+	if (unlikely(is_kfence_address(p)))
+		goto exit;
+
+	for (i = 56; i < 112; i++)
+		KUNIT_EXPECT_EQ(test, p[i], 0);

... but this test is still valid and necessary

lib/slub_kunit.c static void test_leak_destroy(struct kunit *test) · line 227

Quoted 2 lines
lib/slub_kunit.c +2 -0
+	for (i = 112; i < 128; i++)
+		KUNIT_EXPECT_EQ(test, p[i], SLUB_RED_ACTIVE);

... we might skip this test as the red zoning is not done by __do_krealloc() anyway in the alloc_new case.

lib/slub_kunit.c static void test_leak_destroy(struct kunit *test) · line 230

Quoted 3 lines
lib/slub_kunit.c +3 -0
+
+exit:
+	kfree(p);

Ideally we'd also validate the fake kmalloc cache we created and expect zero slab_errors.

Hopefully this approach works and I'm not missing something...

Quoted 14 lines
lib/slub_kunit.c +4 -0
+	kasan_enable_current();
+}
+
 static int test_init(struct kunit *test)
 {
 	slab_errors = 0;
@@ -196,6 +241,7 @@ static int test_init(struct kunit *test)
 }
 
 static struct kunit_case test_cases[] = {
+	KUNIT_CASE(test_krealloc_redzone_zeroing),
 	KUNIT_CASE(test_clobber_zone),
 
 #ifndef CONFIG_KASAN
author
#18 lore raw

Re: [PATCH 5/5] mm/slub, kunit: Add testcase for krealloc redzone and zeroing

Quoted 26 lines · Vlastimil Babka
Quoted 25 lines · Feng Tang

Danilo Krummrich raised issue about krealloc+GFP_ZERO [1], and Vlastimil suggested to add some test case which can sanity test the kmalloc-redzone and zeroing by utilizing the kmalloc's 'orig_size' debug feature.

It covers the grow and shrink case of krealloc() re-using current kmalloc object, and the case of re-allocating a new bigger object.

User can add "slub_debug" kernel cmdline parameter to test it.

[1]. https://lore.kernel.org/lkml/[email protected]/

---
 lib/slub_kunit.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
lib/slub_kunit.c +0 -0
index 6e3a1e5a7142..03e0089149ad 100644
@@ -186,6 +186,51 @@ static void test_leak_destroy(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, 1, slab_errors);
 }
 
Quoted 10 lines

lib/slub_kunit.c static void test_leak_destroy(struct kunit *test) · line 194

Quoted 6 lines
lib/slub_kunit.c +6 -0
+static void test_krealloc_redzone_zeroing(struct kunit *test)
+{
+	char *p;
+	int i;
+
+	KUNIT_TEST_REQUIRES(test, __slub_debug_enabled());

AFAICS this is insufficient, because the static key may be enabled due to debugging enabled for different caches than kmalloc, or it might not include both red zone and object tracking.

You are right, that concerned me too. In first version, I make it depend on CONFIG_SLUB_DEBUG_ON==y, but most user' and distribution's kernel won't enable it, and user have to rebuild kernel to test. So I changed to this check finally.

If there is a way to judge whether 'slub_debug' is enabled, that would solve this issue.

Quoted 3 lines

But it should be possible to instead create a fake kmalloc cache of size 64 and use __kmalloc_cache_noprof() like test_kmalloc_redzone_access()?

Yep, I thought about that, and the problem was the krealloc a new 128B object.

Quoted 21 lines
Quoted 21 lines
lib/slub_kunit.c +21 -0
+
+	/* Allocate a 64B kmalloc object */
+	p = kzalloc(48, GFP_KERNEL);
+	if (unlikely(is_kfence_address(p))) {
+		kfree(p);
+		return;
+	}
+	memset(p, 0xff, 48);
+
+	kasan_disable_current();
+	OPTIMIZER_HIDE_VAR(p);
+
+	/* Test shrink */
+	p = krealloc(p, 40, GFP_KERNEL | __GFP_ZERO);
+	for (i = 40; i < 64; i++)
+		KUNIT_EXPECT_EQ(test, p[i], SLUB_RED_ACTIVE);
+
+	/* Test grow within the same 64B kmalloc object */
+	p = krealloc(p, 56, GFP_KERNEL | __GFP_ZERO);
+	for (i = 40; i < 56; i++)
+		KUNIT_EXPECT_EQ(test, p[i], 0);
Quoted 8 lines

lib/slub_kunit.c static void test_leak_destroy(struct kunit *test) · line 220

Quoted 5 lines
lib/slub_kunit.c +5 -0
+	for (i = 56; i < 64; i++)
+		KUNIT_EXPECT_EQ(test, p[i], SLUB_RED_ACTIVE);
+
+	/* Test grow with allocating a bigger 128B object */
+	p = krealloc(p, 112, GFP_KERNEL | __GFP_ZERO);

The only downside is that krealloc() here might use kmalloc-128 cache that's not doing red zoning and object tracking....

Yes.

Quoted 14 lines

lib/slub_kunit.c static void test_leak_destroy(struct kunit *test) · line 225

Quoted 5 lines
lib/slub_kunit.c +5 -0
+	if (unlikely(is_kfence_address(p)))
+		goto exit;
+
+	for (i = 56; i < 112; i++)
+		KUNIT_EXPECT_EQ(test, p[i], 0);

... but this test is still valid and necessary

lib/slub_kunit.c static void test_leak_destroy(struct kunit *test) · line 227

Quoted 2 lines
lib/slub_kunit.c +2 -0
+	for (i = 112; i < 128; i++)
+		KUNIT_EXPECT_EQ(test, p[i], SLUB_RED_ACTIVE);

... we might skip this test as the red zoning is not done by __do_krealloc() anyway in the alloc_new case.

Quoted 8 lines

lib/slub_kunit.c static void test_leak_destroy(struct kunit *test) · line 230

Quoted 3 lines
lib/slub_kunit.c +3 -0
+
+exit:
+	kfree(p);

Ideally we'd also validate the fake kmalloc cache we created and expect zero slab_errors.

Hopefully this approach works and I'm not missing something...

Yep, this should work. As redzone was tested in earlier check, and not necessary to be checked again here. Will do some test on this.

Thanks,
Feng

#19 lore raw
In reply to Feng Tang
Quoted 11 lines · Feng Tang

Danilo Krummrich's patch [1] raised one problem about krealloc() that its caller doesn't know what's the actual request size, say the object is 64 bytes kmalloc one, but the original caller may only requested 48 bytes. And when krealloc() shrinks or grows in the same object, or allocate a new bigger object, it lacks this 'original size' information to do accurate data preserving or zeroing (when __GFP_ZERO is set).

And when some slub debug option is enabled, kmalloc caches do have this 'orig_size' feature. As suggested by Vlastimil, utilize it to do more accurate data handling, as well as enforce the kmalloc-redzone sanity check.

Quoted 5 lines

To make the 'orig_size' accurate, we adjust some kasan/slub meta data handling. Also add a slub kunit test case for krealloc().

This patchset has dependency over patches in both -mm tree and -slab trees, so it is written based on linux-next tree '20240905' version.

Thanks, given the timing with merge window opening soon, I would take this into the slab tree after the merge window, when the current -next becomes 6.12-rc1.

Quoted 24 lines

[1]. https://lore.kernel.org/lkml/[email protected]/

Thanks,
Feng

Feng Tang (5):
mm/kasan: Don't store metadata inside kmalloc object when
slub_debug_orig_size is on
mm/slub: Consider kfence case for get_orig_size()
mm/slub: Improve redzone check and zeroing for krealloc()
kunit: kfence: Make KFENCE_TEST_REQUIRES macro available for all kunit
case
mm/slub, kunit: Add testcase for krealloc redzone and zeroing

 include/kunit/test.h    |   6 ++
 lib/slub_kunit.c        |  46 +++++++++++++++
 mm/kasan/generic.c      |   5 +-
 mm/kfence/kfence_test.c |   9 +--
 mm/slab.h               |   6 ++
 mm/slab_common.c        |  84 ---------------------------
 mm/slub.c               | 125 ++++++++++++++++++++++++++++++++++------
 7 files changed, 171 insertions(+), 110 deletions(-)
author
#20 lore raw
Quoted 15 lines · Vlastimil Babka
Quoted 14 lines · Feng Tang

Danilo Krummrich's patch [1] raised one problem about krealloc() that its caller doesn't know what's the actual request size, say the object is 64 bytes kmalloc one, but the original caller may only requested 48 bytes. And when krealloc() shrinks or grows in the same object, or allocate a new bigger object, it lacks this 'original size' information to do accurate data preserving or zeroing (when __GFP_ZERO is set).

And when some slub debug option is enabled, kmalloc caches do have this 'orig_size' feature. As suggested by Vlastimil, utilize it to do more accurate data handling, as well as enforce the kmalloc-redzone sanity check.

To make the 'orig_size' accurate, we adjust some kasan/slub meta data handling. Also add a slub kunit test case for krealloc().

Quoted 6 lines
Quoted 2 lines

This patchset has dependency over patches in both -mm tree and -slab trees, so it is written based on linux-next tree '20240905' version.

Thanks, given the timing with merge window opening soon, I would take this into the slab tree after the merge window, when the current -next becomes 6.12-rc1.

Sounds good to me. Thanks for the review!

- Feng

Quoted 25 lines
Quoted 24 lines

[1]. https://lore.kernel.org/lkml/[email protected]/

Thanks,
Feng

Feng Tang (5):
mm/kasan: Don't store metadata inside kmalloc object when
slub_debug_orig_size is on
mm/slub: Consider kfence case for get_orig_size()
mm/slub: Improve redzone check and zeroing for krealloc()
kunit: kfence: Make KFENCE_TEST_REQUIRES macro available for all kunit
case
mm/slub, kunit: Add testcase for krealloc redzone and zeroing

 include/kunit/test.h    |   6 ++
 lib/slub_kunit.c        |  46 +++++++++++++++
 mm/kasan/generic.c      |   5 +-
 mm/kfence/kfence_test.c |   9 +--
 mm/slab.h               |   6 ++
 mm/slab_common.c        |  84 ---------------------------
 mm/slub.c               | 125 ++++++++++++++++++++++++++++++++++------
 7 files changed, 171 insertions(+), 110 deletions(-)