mm/slub: Fix debugfs stack trace sorting and simplify sort call
Subsystems (2)
v2 cover mm/slub: Fix debugfs stack trace sorting and simplify sort call
Fix the comparison function used for sorting stack trace locations in the slub debugfs interface. The original implementation violated the antisymmetry property required by sort(), which could lead to unreliable ordering of the output. The patches correct the comparison function to return 0 when counts are equal and replace the unnecessary use of sort_r() with the simpler sort().
---
Changes in v2:
* Use cmp_int().
* Drop Cc stable.
v1: https://lore.kernel.org/lkml/[email protected]/
Kuan-Wei Chiu (2):
mm/slub: Fix cmp_loc_by_count() to return 0 when counts are equal mm/slub: Replace sort_r() with sort() for debugfs stack trace sorting
mm/slub.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
Signature · 1 line
2.34.1
PATCH v2 1/2 mm/slub: Fix cmp_loc_by_count() to return 0 when counts are equal
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.
--- mm/slub.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
mm/slub.c +1 -4
@@ -7716,10 +7716,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)
Signature · 1 line
2.34.1
Re: [PATCH v2 1/2] mm/slub: Fix cmp_loc_by_count() to return 0 when counts are equal
Quoted 11 lines · Kuan-Wei Chiu
The comparison function
cmp_loc_by_count()used for sorting stack trace locations in debugfs currently returns -1 ifa->count>b->countand 1 otherwise. This breaks the antisymmetry property required bysort(), because when two counts are equal, bothcmp(a, b)andcmp(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.
Quoted 4 lines
Fixes 553c0369b3e1 ("mm/slub: sort debugfs output by frequency of stack traces")Reviewed-by Joshua Hahn <[email protected]>Signed-off-by Kuan-Wei Chiu <[email protected]>---
While the author withdrew the claim that it definitely leads to incorrect results, it remains true that the API requires both transitivity and antisymmetry for correctness, so:
Signature · 2 lines
Cheers, Harry / Hyeonggon
Quoted 21 lines
mm/slub.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)mm/slub.c +1
-4@@ -7716,10 +7716,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)Signature · 1 line
2.34.1
PATCH v2 2/2 mm/slub: Replace sort_r() with sort() for debugfs stack trace sorting
In reply to Kuan-Wei ChiuThe comparison function used to sort stack trace locations in debugfs never relied on the third argument. Therefore, sort_r() is unnecessary. Switch to sort() with a two-argument comparison function to keep the code simple and aligned with the intended usage.
--- mm/slub.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
mm/slub.c +3 -3
@@ -7711,7 +7711,7 @@ static void *slab_debugfs_next(struct seq_file *seq, void *v, loff_t *ppos)return NULL ; } -static int cmp_loc_by_count (const void *a,const void *b,const void *data) +static int cmp_loc_by_count (const void *a,const void *b) {struct location *loc1 = (struct location *)a;struct location *loc2 = (struct location *)b;
@@ -7778,8 +7778,8 @@ static int slab_debug_trace_open(struct inode *inode, struct file *filep) }/* Sort locations by count */ -sort_r (t->loc, t->count,sizeof (struct location ), - cmp_loc_by_count,NULL ,NULL ); +sort (t->loc, t->count,sizeof (struct location ), + cmp_loc_by_count,NULL );bitmap_free (obj_map);return 0 ;
Signature · 1 line
2.34.1
Re: [PATCH v2 2/2] mm/slub: Replace sort_r() with sort() for debugfs stack trace sorting
Quoted 7 lines · Kuan-Wei Chiu
The comparison function used to sort stack trace locations in debugfs never relied on the third argument. Therefore,
sort_r()is unnecessary. Switch tosort()with a two-argument comparison function to keep the code simple and aligned with the intended usage.Signed-off-by Kuan-Wei Chiu <[email protected]>---
Quoted 30 lines
mm/slub.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)mm/slub.c +3
-3@@ -7711,7 +7711,7 @@ static void *slab_debugfs_next(struct seq_file *seq, void *v, loff_t *ppos)return NULL ; } -static int cmp_loc_by_count (const void *a,const void *b,const void *data) +static int cmp_loc_by_count (const void *a,const void *b) {struct location *loc1 = (struct location *)a;struct location *loc2 = (struct location *)b;@@ -7778,8 +7778,8 @@ static int slab_debug_trace_open(struct inode *inode, struct file *filep) }/* Sort locations by count */ -sort_r (t->loc, t->count,sizeof (struct location ), - cmp_loc_by_count,NULL ,NULL ); +sort (t->loc, t->count,sizeof (struct location ), + cmp_loc_by_count,NULL );bitmap_free (obj_map);return 0 ;Signature · 1 line
2.34.1
Signature · 2 lines
Cheers, Harry / Hyeonggon
Vlastimil Babka
AppliedQuoted 11 lines · Kuan-Wei Chiu
Fix the comparison function used for sorting stack trace locations in the slub debugfs interface. The original implementation violated the antisymmetry property required by
sort(), which could lead to unreliable ordering of the output. The patches correct the comparison function to return 0 when counts are equal and replace the unnecessary use ofsort_r()with the simplersort().
---
Changes in v2:
* Usecmp_int().
* Drop Cc stable.
Quoted 5 lines
v1: https://lore.kernel.org/lkml/[email protected]/
Kuan-Wei Chiu (2):
mm/slub: Fixcmp_loc_by_count()to return 0 when counts are equal mm/slub: Replacesort_r()withsort()for debugfs stack trace sorting
Applied to slab/for-next, thanks!
Quoted 3 lines
mm/slub.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)