Skip to contentSkip to conversation

slab: fix and cleanup of slub_debug

PATCH Superseded Chengming Zhou 4 participants 12 messages May 28, 2024 – Jun 3, 2024

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover slab: fix and cleanup of slub_debug

Hello,

This series includes minor fix and cleanup of slub_debug, please see the commits for details.

---
Chengming Zhou (3):
slab: check the return value of check_bytes_and_report()
slab: don't put freepointer outside of object if only orig_size
slab: delete useless RED_INACTIVE and RED_ACTIVE

 include/linux/poison.h       |  7 ++-----
 mm/slub.c                    | 11 ++++++-----
 tools/include/linux/poison.h |  7 ++-----
 3 files changed, 10 insertions(+), 15 deletions(-)

---
base-commit: 1613e604df0cd359cf2a7fbd9be7a0bcfacfabd0
change-id: 20240528-b4-slab-debug-1d8179fc996a

Best regards,

Signature · 1 line
Chengming Zhou <[email protected]>
#1 lore raw

PATCH 1/3 slab: check the return value of check_bytes_and_report()

All check_bytes_and_report() callers have checked its return value except for this one. We should also check its return value here obviously, so fix it.

---
 mm/slub.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
mm/slub.c +3 -2
index 0809760cf789..de57512734ac 100644
@@ -1324,9 +1324,10 @@ static int check_object(struct kmem_cache *s, struct slab *slab,
 		}
 	} else {
 		if ((s->flags & SLAB_POISON) && s->object_size < s->inuse) {
-			check_bytes_and_report(s, slab, p, "Alignment padding",
+			if (!check_bytes_and_report(s, slab, p, "Alignment padding",
 				endobject, POISON_INUSE,
-				s->inuse - s->object_size);
+				s->inuse - s->object_size))
+				return 0;
 		}
 	}
 
Signature · 1 line
2.45.1
#2 lore raw

Re: [PATCH 1/3] slab: check the return value of check_bytes_and_report()

Quoted 6 lines · Chengming Zhou
mm/slub.c +0 -0
index 0809760cf789..de57512734ac 100644
@@ -1324,9 +1324,10 @@ static int check_object(struct kmem_cache *s, struct slab *slab,
 		}

mm/slub.c static int check_object(struct kmem_cache *s, struct slab *slab, · line 1332

Quoted 10 lines
mm/slub.c +3 -2
 	} else {
 		if ((s->flags & SLAB_POISON) && s->object_size < s->inuse) {
-			check_bytes_and_report(s, slab, p, "Alignment padding",
+			if (!check_bytes_and_report(s, slab, p, "Alignment padding",
 				endobject, POISON_INUSE,
-				s->inuse - s->object_size);
+				s->inuse - s->object_size))
+				return 0;
 		}
 	}

This change means we will then skip the rest of the checks in check_object() such as the poison check.

#3 lore raw

Re: [PATCH 1/3] slab: check the return value of check_bytes_and_report()

Quoted 12 lines · Christoph Lameter (Ampere)
Quoted 10 lines · Chengming Zhou
mm/slub.c +1 -1
index 0809760cf789..de57512734ac 100644
@@ -1324,9 +1324,10 @@ static int check_object(struct kmem_cache *s, struct slab *slab,
         }
     } else {
         if ((s->flags & SLAB_POISON) && s->object_size < s->inuse) {
-            check_bytes_and_report(s, slab, p, "Alignment padding",
+            if (!check_bytes_and_report(s, slab, p, "Alignment padding",
Quoted 8 lines

mm/slub.c static int check_object(struct kmem_cache *s, struct slab *slab, · line 1332

Quoted 6 lines
mm/slub.c +2 -1
                 endobject, POISON_INUSE,
-                s->inuse - s->object_size);
+                s->inuse - s->object_size))
+                return 0;
         }
     }

This change means we will then skip the rest of the checks in check_object() such as the poison check.

Yeah, only when this padding checking failed.

Now, we always abort checking and return 0 when the first checking error happens, such as redzones checking above.

Thanks.

#4 lore raw

Re: [PATCH 1/3] slab: check the return value of check_bytes_and_report()

Quoted 22 lines · Chengming Zhou
Quoted 12 lines · Christoph Lameter (Ampere)
Quoted 10 lines · Chengming Zhou
mm/slub.c +1 -1
index 0809760cf789..de57512734ac 100644
@@ -1324,9 +1324,10 @@ static int check_object(struct kmem_cache *s, struct slab *slab,
         }
     } else {
         if ((s->flags & SLAB_POISON) && s->object_size < s->inuse) {
-            check_bytes_and_report(s, slab, p, "Alignment padding",
+            if (!check_bytes_and_report(s, slab, p, "Alignment padding",
Quoted 8 lines

mm/slub.c static int check_object(struct kmem_cache *s, struct slab *slab, · line 1332

Quoted 6 lines
mm/slub.c +2 -1
                 endobject, POISON_INUSE,
-                s->inuse - s->object_size);
+                s->inuse - s->object_size))
+                return 0;
         }
     }

This change means we will then skip the rest of the checks in check_object() such as the poison check.

Quoted 4 lines

Yeah, only when this padding checking failed.

Now, we always abort checking and return 0 when the first checking error happens, such as redzones checking above.

Yes your patch will make it consistent. But IMHO it would be better to do all the checks without skipping, report their specific error findings in check_bytes_and_report() but not print_trailer(). Once all checks were done, if any found an error, print the trailer once from check_object(). Thoughts?

Quoted 1 line

Thanks.

#5 lore raw

Re: [PATCH 1/3] slab: check the return value of check_bytes_and_report()

Quoted 25 lines · Vlastimil Babka
Quoted 24 lines · Chengming Zhou
Quoted 12 lines · Christoph Lameter (Ampere)
Quoted 10 lines · Chengming Zhou
mm/slub.c +1 -1
index 0809760cf789..de57512734ac 100644
@@ -1324,9 +1324,10 @@ static int check_object(struct kmem_cache *s, struct slab *slab,
         }
     } else {
         if ((s->flags & SLAB_POISON) && s->object_size < s->inuse) {
-            check_bytes_and_report(s, slab, p, "Alignment padding",
+            if (!check_bytes_and_report(s, slab, p, "Alignment padding",
Quoted 8 lines

mm/slub.c static int check_object(struct kmem_cache *s, struct slab *slab, · line 1332

Quoted 6 lines
mm/slub.c +2 -1
                 endobject, POISON_INUSE,
-                s->inuse - s->object_size);
+                s->inuse - s->object_size))
+                return 0;
         }
     }

This change means we will then skip the rest of the checks in check_object() such as the poison check.

Yeah, only when this padding checking failed.

Quoted 7 lines
Quoted 2 lines

Now, we always abort checking and return 0 when the first checking error happens, such as redzones checking above.

Yes your patch will make it consistent. But IMHO it would be better to do all the checks without skipping, report their specific error findings in check_bytes_and_report() but not print_trailer(). Once all checks were done, if any found an error, print the trailer once from check_object(). Thoughts?

Ok, it's feasible, will change to this.

Quoted 3 lines
Quoted 2 lines

Thanks.

#6 lore raw

PATCH 2/3 slab: don't put freepointer outside of object if only orig_size

In reply to Chengming Zhou

The commit 946fa0dbf2d8 ("mm/slub: extend redzone check to extra allocated kmalloc space than requested") will extend right redzone when allocating for orig_size < object_size. So we can't overlay the freepointer in the object space in this case.

But the code looks like it forgot to check SLAB_RED_ZONE, since there won't be extended right redzone if only orig_size enabled.

---
 mm/slub.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/slub.c +1 -1
index de57512734ac..b92d9a557852 100644
@@ -5150,7 +5150,7 @@ static int calculate_sizes(struct kmem_cache *s)
 	 */
 	s->inuse = size;
 
-	if (slub_debug_orig_size(s) ||
+	if (((flags & SLAB_RED_ZONE) && slub_debug_orig_size(s)) ||
 	    (flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)) ||
 	    ((flags & SLAB_RED_ZONE) && s->object_size < sizeof(void *)) ||
 	    s->ctor) {
Signature · 1 line
2.45.1
Reviewed-by on v1 2/3
#7 lore raw

Re: [PATCH 2/3] slab: don't put freepointer outside of object if only orig_size

Quoted 7 lines · Chengming Zhou

The commit 946fa0dbf2d8 ("mm/slub: extend redzone check to extra allocated kmalloc space than requested") will extend right redzone when allocating for orig_size < object_size. So we can't overlay the freepointer in the object space in this case.

But the code looks like it forgot to check SLAB_RED_ZONE, since there won't be extended right redzone if only orig_size enabled.

Look good to me. Thanks!

Quoted 23 lines
---
 mm/slub.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/slub.c +1 -1
index de57512734ac..b92d9a557852 100644
@@ -5150,7 +5150,7 @@ static int calculate_sizes(struct kmem_cache *s)
 	 */
 	s->inuse = size;
 
-	if (slub_debug_orig_size(s) ||
+	if (((flags & SLAB_RED_ZONE) && slub_debug_orig_size(s)) ||
 	    (flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)) ||
 	    ((flags & SLAB_RED_ZONE) && s->object_size < sizeof(void *)) ||
 	    s->ctor) {
Signature · 2 lines
-- 
2.45.1
#8 lore raw

Re: [PATCH 2/3] slab: don't put freepointer outside of object if only orig_size

In reply to Chengming Zhou
Quoted 9 lines · Chengming Zhou

The commit 946fa0dbf2d8 ("mm/slub: extend redzone check to extra allocated kmalloc space than requested") will extend right redzone when allocating for orig_size < object_size. So we can't overlay the freepointer in the object space in this case.

But the code looks like it forgot to check SLAB_RED_ZONE, since there won't be extended right redzone if only orig_size enabled.

Seems OK.

Quoted 9 lines
---
 mm/slub.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/slub.c +0 -0
index de57512734ac..b92d9a557852 100644

mm/slub.c static int calculate_sizes(struct kmem_cache *s) · line 5155

Quoted 7 lines
mm/slub.c +1 -1
@@ -5150,7 +5150,7 @@ static int calculate_sizes(struct kmem_cache *s)
 	 */
 	s->inuse = size;
 
-	if (slub_debug_orig_size(s) ||
+	if (((flags & SLAB_RED_ZONE) && slub_debug_orig_size(s)) ||
 	    (flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)) ||
 	    ((flags & SLAB_RED_ZONE) && s->object_size < sizeof(void *)) ||

Should we consolidate the two cases with flags & SLAB_RED_ZONE?

Also below this is a comment that could also mention the slub_debug_orig_size().

Quoted 2 lines
mm/slub.c +0 -0
 	    s->ctor) {
#9 lore raw

Re: [PATCH 2/3] slab: don't put freepointer outside of object if only orig_size

Quoted 25 lines · Vlastimil Babka
Quoted 9 lines · Chengming Zhou

The commit 946fa0dbf2d8 ("mm/slub: extend redzone check to extra allocated kmalloc space than requested") will extend right redzone when allocating for orig_size < object_size. So we can't overlay the freepointer in the object space in this case.

But the code looks like it forgot to check SLAB_RED_ZONE, since there won't be extended right redzone if only orig_size enabled.

Seems OK.

Quoted 12 lines
---
 mm/slub.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/slub.c +0 -0
index de57512734ac..b92d9a557852 100644
@@ -5150,7 +5150,7 @@ static int calculate_sizes(struct kmem_cache *s)
 	 */
 	s->inuse = size;
 
Quoted 6 lines

mm/slub.c static int calculate_sizes(struct kmem_cache *s) · line 5155

Quoted 4 lines
mm/slub.c +1 -1
-	if (slub_debug_orig_size(s) ||
+	if (((flags & SLAB_RED_ZONE) && slub_debug_orig_size(s)) ||
 	    (flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)) ||
 	    ((flags & SLAB_RED_ZONE) && s->object_size < sizeof(void *)) ||

Should we consolidate the two cases with flags & SLAB_RED_ZONE?

Yes, we can.

Quoted 2 lines

Also below this is a comment that could also mention the slub_debug_orig_size().

Ok, will add.

Thanks.

Quoted 4 lines
Quoted 3 lines
mm/slub.c +0 -0
 	    s->ctor) {
#10 lore raw

PATCH 3/3 slab: delete useless RED_INACTIVE and RED_ACTIVE

In reply to Chengming Zhou

These seem useless since we use the SLUB_RED_INACTIVE and SLUB_RED_ACTIVE, so just delete them, no functional change.

---
 include/linux/poison.h       | 7 ++-----
 mm/slub.c                    | 4 ++--
 tools/include/linux/poison.h | 7 ++-----
 3 files changed, 6 insertions(+), 12 deletions(-)
include/linux/poison.h +2 -5
index 1f0ee2459f2a..9c1a035af97c 100644
@@ -38,11 +38,8 @@
  * Magic nums for obj red zoning.
  * Placed in the first word before and the first word after an obj.
  */
-#define	RED_INACTIVE	0x09F911029D74E35BULL	/* when obj is inactive */
-#define	RED_ACTIVE	0xD84156C5635688C0ULL	/* when obj is active */
-
-#define SLUB_RED_INACTIVE	0xbb
-#define SLUB_RED_ACTIVE		0xcc
+#define SLUB_RED_INACTIVE	0xbb	/* when obj is inactive */
+#define SLUB_RED_ACTIVE		0xcc	/* when obj is active */
 
 /* ...and for poisoning */
 #define	POISON_INUSE	0x5a	/* for use-uninitialised poisoning */
mm/slub.c +2 -2
index b92d9a557852..9af868fa68a4 100644
@@ -1216,8 +1216,8 @@ static int check_bytes_and_report(struct kmem_cache *s, struct slab *slab,
  * 	Padding is extended by another word if Redzoning is enabled and
  * 	object_size == inuse.
  *
- * 	We fill with 0xbb (RED_INACTIVE) for inactive objects and with
- * 	0xcc (RED_ACTIVE) for objects in use.
+ * 	We fill with 0xbb (SLUB_RED_INACTIVE) for inactive objects and with
+ * 	0xcc (SLUB_RED_ACTIVE) for objects in use.
  *
  * object + s->inuse
  * 	Meta data starts here.
tools/include/linux/poison.h +2 -5
index 2e6338ac5eed..e530e54046c9 100644
@@ -47,11 +47,8 @@
  * Magic nums for obj red zoning.
  * Placed in the first word before and the first word after an obj.
  */
-#define	RED_INACTIVE	0x09F911029D74E35BULL	/* when obj is inactive */
-#define	RED_ACTIVE	0xD84156C5635688C0ULL	/* when obj is active */
-
-#define SLUB_RED_INACTIVE	0xbb
-#define SLUB_RED_ACTIVE		0xcc
+#define SLUB_RED_INACTIVE	0xbb	/* when obj is inactive */
+#define SLUB_RED_ACTIVE		0xcc	/* when obj is active */
 
 /* ...and for poisoning */
 #define	POISON_INUSE	0x5a	/* for use-uninitialised poisoning */
Signature · 1 line
2.45.1
#11 lore raw

Re: [PATCH 3/3] slab: delete useless RED_INACTIVE and RED_ACTIVE

Quoted 4 lines · Chengming Zhou

These seem useless since we use the SLUB_RED_INACTIVE and SLUB_RED_ACTIVE, so just delete them, no functional change.

Hm right, only SLAB was using those. Thanks.