mm: slub: Print the broken data before restoring them
authorHyesoo Yu <hyesoo.yu@samsung.com>
Wed, 26 Feb 2025 08:12:00 +0000 (17:12 +0900)
committerVlastimil Babka <vbabka@suse.cz>
Tue, 4 Mar 2025 07:53:50 +0000 (08:53 +0100)
Previously, the restore occurred after printing the object in slub.
After commit 47d911b02cbe ("slab: make check_object() more consistent"),
the bytes are printed after the restore. This information about the bytes
before the restore is highly valuable for debugging purpose.
For instance, in a event of cache issue, it displays byte patterns
by breaking them down into 64-bytes units. Without this information,
we can only speculate on how it was broken. Hence the corrupted regions
should be printed prior to the restoration process. However if an object
breaks in multiple places, the same log may be output multiple times.
Therefore the slub log is reported only once to prevent redundant printing,
by sending a parameter indicating whether an error has occurred previously.

Signed-off-by: Hyesoo Yu <hyesoo.yu@samsung.com>
Reviewed-by: Harry Yoo <harry.yoo@oracle.com>
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
mm/slub.c

index 1f7d1d260eebd8646fc4a379de1f0b5a786a33d1..faea4a1dbcca90d60b7d5dc4ea30d834b077bcfb 100644 (file)
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -1181,8 +1181,8 @@ static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
 
 static pad_check_attributes int
 check_bytes_and_report(struct kmem_cache *s, struct slab *slab,
-                      u8 *object, char *what,
-                      u8 *start, unsigned int value, unsigned int bytes)
+                      u8 *object, char *what, u8 *start, unsigned int value,
+                      unsigned int bytes, bool slab_obj_print)
 {
        u8 *fault;
        u8 *end;
@@ -1201,10 +1201,11 @@ check_bytes_and_report(struct kmem_cache *s, struct slab *slab,
        if (slab_add_kunit_errors())
                goto skip_bug_print;
 
-       slab_bug(s, "%s overwritten", what);
-       pr_err("0x%p-0x%p @offset=%tu. First byte 0x%x instead of 0x%x\n",
-                                       fault, end - 1, fault - addr,
-                                       fault[0], value);
+       pr_err("[%s overwritten] 0x%p-0x%p @offset=%tu. First byte 0x%x instead of 0x%x\n",
+              what, fault, end - 1, fault - addr, fault[0], value);
+
+       if (slab_obj_print)
+               object_err(s, slab, object, "Object corrupt");
 
 skip_bug_print:
        restore_bytes(s, what, value, fault, end);
@@ -1268,7 +1269,7 @@ static int check_pad_bytes(struct kmem_cache *s, struct slab *slab, u8 *p)
                return 1;
 
        return check_bytes_and_report(s, slab, p, "Object padding",
-                       p + off, POISON_INUSE, size_from_object(s) - off);
+                       p + off, POISON_INUSE, size_from_object(s) - off, true);
 }
 
 /* Check the pad bytes at the end of a slab page */
@@ -1318,11 +1319,11 @@ static int check_object(struct kmem_cache *s, struct slab *slab,
 
        if (s->flags & SLAB_RED_ZONE) {
                if (!check_bytes_and_report(s, slab, object, "Left Redzone",
-                       object - s->red_left_pad, val, s->red_left_pad))
+                       object - s->red_left_pad, val, s->red_left_pad, ret))
                        ret = 0;
 
                if (!check_bytes_and_report(s, slab, object, "Right Redzone",
-                       endobject, val, s->inuse - s->object_size))
+                       endobject, val, s->inuse - s->object_size, ret))
                        ret = 0;
 
                if (slub_debug_orig_size(s) && val == SLUB_RED_ACTIVE) {
@@ -1331,7 +1332,7 @@ static int check_object(struct kmem_cache *s, struct slab *slab,
                        if (s->object_size > orig_size  &&
                                !check_bytes_and_report(s, slab, object,
                                        "kmalloc Redzone", p + orig_size,
-                                       val, s->object_size - orig_size)) {
+                                       val, s->object_size - orig_size, ret)) {
                                ret = 0;
                        }
                }
@@ -1339,7 +1340,7 @@ static int check_object(struct kmem_cache *s, struct slab *slab,
                if ((s->flags & SLAB_POISON) && s->object_size < s->inuse) {
                        if (!check_bytes_and_report(s, slab, p, "Alignment padding",
                                endobject, POISON_INUSE,
-                               s->inuse - s->object_size))
+                               s->inuse - s->object_size, ret))
                                ret = 0;
                }
        }
@@ -1355,11 +1356,11 @@ static int check_object(struct kmem_cache *s, struct slab *slab,
                        if (kasan_meta_size < s->object_size - 1 &&
                            !check_bytes_and_report(s, slab, p, "Poison",
                                        p + kasan_meta_size, POISON_FREE,
-                                       s->object_size - kasan_meta_size - 1))
+                                       s->object_size - kasan_meta_size - 1, ret))
                                ret = 0;
                        if (kasan_meta_size < s->object_size &&
                            !check_bytes_and_report(s, slab, p, "End Poison",
-                                       p + s->object_size - 1, POISON_END, 1))
+                                       p + s->object_size - 1, POISON_END, 1, ret))
                                ret = 0;
                }
                /*
@@ -1385,11 +1386,6 @@ static int check_object(struct kmem_cache *s, struct slab *slab,
                ret = 0;
        }
 
-       if (!ret && !slab_in_kunit_test()) {
-               print_trailer(s, slab, object);
-               add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
-       }
-
        return ret;
 }