linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] mm/slub: Fix debugfs stack trace sorting and simplify sort call
@ 2025-08-26  6:23 Kuan-Wei Chiu
  2025-08-26  6:23 ` [PATCH v2 1/2] mm/slub: Fix cmp_loc_by_count() to return 0 when counts are equal Kuan-Wei Chiu
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Kuan-Wei Chiu @ 2025-08-26  6:23 UTC (permalink / raw)
  To: vbabka, akpm
  Cc: cl, rientjes, roman.gushchin, harry.yoo, glittao, jserv, chuang,
	cfmc.cs13, jhcheng.cs13, c.yuanhaur, linux-mm, linux-kernel,
	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 of sort_r() with the simpler sort().
---
Changes in v2:
* Use cmp_int().
* Drop Cc stable. 

v1: https://lore.kernel.org/lkml/20250825013419.240278-1-visitorckw@gmail.com/

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(-)

-- 
2.34.1



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 1/2] mm/slub: Fix cmp_loc_by_count() to return 0 when counts are equal
  2025-08-26  6:23 [PATCH v2 0/2] mm/slub: Fix debugfs stack trace sorting and simplify sort call Kuan-Wei Chiu
@ 2025-08-26  6:23 ` Kuan-Wei Chiu
  2025-08-29  2:13   ` Harry Yoo
  2025-08-26  6:23 ` [PATCH v2 2/2] mm/slub: Replace sort_r() with sort() for debugfs stack trace sorting Kuan-Wei Chiu
  2025-08-27  9:55 ` [PATCH v2 0/2] mm/slub: Fix debugfs stack trace sorting and simplify sort call Vlastimil Babka
  2 siblings, 1 reply; 6+ messages in thread
From: Kuan-Wei Chiu @ 2025-08-26  6:23 UTC (permalink / raw)
  To: vbabka, akpm
  Cc: cl, rientjes, roman.gushchin, harry.yoo, glittao, jserv, chuang,
	cfmc.cs13, jhcheng.cs13, c.yuanhaur, linux-mm, linux-kernel,
	Kuan-Wei Chiu, Joshua Hahn

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>
---
 mm/slub.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 30003763d224..081816ff89ab 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -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)
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 2/2] mm/slub: Replace sort_r() with sort() for debugfs stack trace sorting
  2025-08-26  6:23 [PATCH v2 0/2] mm/slub: Fix debugfs stack trace sorting and simplify sort call Kuan-Wei Chiu
  2025-08-26  6:23 ` [PATCH v2 1/2] mm/slub: Fix cmp_loc_by_count() to return 0 when counts are equal Kuan-Wei Chiu
@ 2025-08-26  6:23 ` Kuan-Wei Chiu
  2025-08-29  2:14   ` Harry Yoo
  2025-08-27  9:55 ` [PATCH v2 0/2] mm/slub: Fix debugfs stack trace sorting and simplify sort call Vlastimil Babka
  2 siblings, 1 reply; 6+ messages in thread
From: Kuan-Wei Chiu @ 2025-08-26  6:23 UTC (permalink / raw)
  To: vbabka, akpm
  Cc: cl, rientjes, roman.gushchin, harry.yoo, glittao, jserv, chuang,
	cfmc.cs13, jhcheng.cs13, c.yuanhaur, linux-mm, linux-kernel,
	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 to sort() with a two-argument comparison function to keep the
code simple and aligned with the intended usage.

Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
 mm/slub.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 081816ff89ab..39a238384892 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -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;
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 0/2] mm/slub: Fix debugfs stack trace sorting and simplify sort call
  2025-08-26  6:23 [PATCH v2 0/2] mm/slub: Fix debugfs stack trace sorting and simplify sort call Kuan-Wei Chiu
  2025-08-26  6:23 ` [PATCH v2 1/2] mm/slub: Fix cmp_loc_by_count() to return 0 when counts are equal Kuan-Wei Chiu
  2025-08-26  6:23 ` [PATCH v2 2/2] mm/slub: Replace sort_r() with sort() for debugfs stack trace sorting Kuan-Wei Chiu
@ 2025-08-27  9:55 ` Vlastimil Babka
  2 siblings, 0 replies; 6+ messages in thread
From: Vlastimil Babka @ 2025-08-27  9:55 UTC (permalink / raw)
  To: Kuan-Wei Chiu, akpm
  Cc: cl, rientjes, roman.gushchin, harry.yoo, glittao, jserv, chuang,
	cfmc.cs13, jhcheng.cs13, c.yuanhaur, linux-mm, linux-kernel

On 8/26/25 08:23, Kuan-Wei Chiu wrote:
> 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/20250825013419.240278-1-visitorckw@gmail.com/
> 
> 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

Applied to slab/for-next, thanks!

>  mm/slub.c | 11 ++++-------
>  1 file changed, 4 insertions(+), 7 deletions(-)
> 



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 1/2] mm/slub: Fix cmp_loc_by_count() to return 0 when counts are equal
  2025-08-26  6:23 ` [PATCH v2 1/2] mm/slub: Fix cmp_loc_by_count() to return 0 when counts are equal Kuan-Wei Chiu
@ 2025-08-29  2:13   ` Harry Yoo
  0 siblings, 0 replies; 6+ messages in thread
From: Harry Yoo @ 2025-08-29  2:13 UTC (permalink / raw)
  To: Kuan-Wei Chiu
  Cc: vbabka, akpm, cl, rientjes, roman.gushchin, glittao, jserv,
	chuang, cfmc.cs13, jhcheng.cs13, c.yuanhaur, linux-mm,
	linux-kernel, Joshua Hahn

On Tue, Aug 26, 2025 at 02:23:14PM +0800, Kuan-Wei Chiu wrote:
> 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>
> ---

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:

Reviewed-by: Harry Yoo <harry.yoo@oracle.com>

-- 
Cheers,
Harry / Hyeonggon

>  mm/slub.c | 5 +----
>  1 file changed, 1 insertion(+), 4 deletions(-)
> 
> diff --git a/mm/slub.c b/mm/slub.c
> index 30003763d224..081816ff89ab 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -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)
> -- 
> 2.34.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 2/2] mm/slub: Replace sort_r() with sort() for debugfs stack trace sorting
  2025-08-26  6:23 ` [PATCH v2 2/2] mm/slub: Replace sort_r() with sort() for debugfs stack trace sorting Kuan-Wei Chiu
@ 2025-08-29  2:14   ` Harry Yoo
  0 siblings, 0 replies; 6+ messages in thread
From: Harry Yoo @ 2025-08-29  2:14 UTC (permalink / raw)
  To: Kuan-Wei Chiu
  Cc: vbabka, akpm, cl, rientjes, roman.gushchin, glittao, jserv,
	chuang, cfmc.cs13, jhcheng.cs13, c.yuanhaur, linux-mm,
	linux-kernel

On Tue, Aug 26, 2025 at 02:23:15PM +0800, Kuan-Wei Chiu wrote:
> The 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.
> 
> Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
> ---

Reviewed-by: Harry Yoo <harry.yoo@oracle.com>

>  mm/slub.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/mm/slub.c b/mm/slub.c
> index 081816ff89ab..39a238384892 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -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;
> -- 
> 2.34.1
> 

-- 
Cheers,
Harry / Hyeonggon


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-08-29  2:15 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-26  6:23 [PATCH v2 0/2] mm/slub: Fix debugfs stack trace sorting and simplify sort call Kuan-Wei Chiu
2025-08-26  6:23 ` [PATCH v2 1/2] mm/slub: Fix cmp_loc_by_count() to return 0 when counts are equal Kuan-Wei Chiu
2025-08-29  2:13   ` Harry Yoo
2025-08-26  6:23 ` [PATCH v2 2/2] mm/slub: Replace sort_r() with sort() for debugfs stack trace sorting Kuan-Wei Chiu
2025-08-29  2:14   ` Harry Yoo
2025-08-27  9:55 ` [PATCH v2 0/2] mm/slub: Fix debugfs stack trace sorting and simplify sort call Vlastimil Babka

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).