Hi Vlastimil,
On Mon, Aug 25, 2025 at 07:28:17PM +0200, Vlastimil Babka wrote:
On 8/25/25 03:34, 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
Good catch.
This can lead to undefined or incorrect ordering results. Fix it by
Wonder if it can really affect anything in practice other than swapping needlessly some records with an equal count?
It could result in some elements being incorrectly ordered, similar to what happened before in ACPI causing issues with s2idle [1][2]. But in this case, the worst impact is just the display order not matching the count, so it's not too critical.
[1]: https://lore.kernel.org/lkml/70674dc7-5586-4183-8953-8095567e73df@gmail.com/ [2]: https://lore.kernel.org/lkml/20240701205639.117194-1-visitorckw@gmail.com/
explicitly returning 0 when the counts are equal, ensuring that the comparison function follows the expected mathematical properties.
Agreed with the cmp_int() suggestion for a v2.
I'll make that change in v2.
Fixes: 553c0369b3e1 ("mm/slub: sort debugfs output by frequency of stack traces") Cc: stable@vger.kernel.org
I don't think it can cause any serious bugs so Cc: stable is unnecessary.
I'll drop it in v2.
Regards, Kuan-Wei
Signed-off-by: Kuan-Wei Chiu visitorckw@gmail.com
Thanks!
mm/slub.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/mm/slub.c b/mm/slub.c index 30003763d224..c91b3744adbc 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -7718,8 +7718,9 @@ static int cmp_loc_by_count(const void *a, const void *b, const void *data) if (loc1->count > loc2->count) return -1;
- else
- if (loc1->count < loc2->count) return 1;
- return 0;
} static void *slab_debugfs_start(struct seq_file *seq, loff_t *ppos)