]> www.infradead.org Git - users/hch/misc.git/commitdiff
mm/slub: Fix cmp_loc_by_count() to return 0 when counts are equal
authorKuan-Wei Chiu <visitorckw@gmail.com>
Tue, 26 Aug 2025 06:23:14 +0000 (14:23 +0800)
committerVlastimil Babka <vbabka@suse.cz>
Wed, 10 Sep 2025 20:52:33 +0000 (22:52 +0200)
The comparison function cmp_loc_by_count() used for sorting stack trace
locations in debugfs currently returns -1 if a->count > b->count and 1
otherwise. This breaks the antisymmetry property required by sort(),
because when two counts are equal, both cmp(a, b) and cmp(b, a) return
1.

This can lead to undefined or incorrect ordering results. Fix it by
updating the comparison logic to explicitly handle the case when counts
are equal, and use cmp_int() to ensure the comparison function adheres
to the required mathematical properties of antisymmetry.

Fixes: 553c0369b3e1 ("mm/slub: sort debugfs output by frequency of stack traces")
Reviewed-by: Joshua Hahn <joshua.hahnjy@gmail.com>
Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
Reviewed-by: Harry Yoo <harry.yoo@oracle.com>
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
mm/slub.c

index d257141896c9531cd654b468cbd802135eceeefe..264fc76455d7395c2e469c72c269a1d0ec222982 100644 (file)
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -7731,10 +7731,7 @@ static int cmp_loc_by_count(const void *a, const void *b, const void *data)
        struct location *loc1 = (struct location *)a;
        struct location *loc2 = (struct location *)b;
 
-       if (loc1->count > loc2->count)
-               return -1;
-       else
-               return 1;
+       return cmp_int(loc2->count, loc1->count);
 }
 
 static void *slab_debugfs_start(struct seq_file *seq, loff_t *ppos)