linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Kuan-Wei Chiu <visitorckw@gmail.com>
To: Harry Yoo <harry.yoo@oracle.com>
Cc: Vlastimil Babka <vbabka@suse.cz>,
	akpm@linux-foundation.org, cl@gentwo.org, rientjes@google.com,
	roman.gushchin@linux.dev, glittao@gmail.com,
	jserv@ccns.ncku.edu.tw, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org,
	Joshua Hahn <joshua.hahnjy@gmail.com>,
	chuang@cs.nycu.edu.tw, cfmc.cs13@nycu.edu.tw,
	jhcheng.cs13@nycu.edu.tw, c.yuanhaur@wustl.edu
Subject: Re: [PATCH 1/2] mm/slub: Fix cmp_loc_by_count() to return 0 when counts are equal
Date: Fri, 29 Aug 2025 01:13:58 +0800	[thread overview]
Message-ID: <aLCOVoshch9phL5M@visitorckw-System-Product-Name> (raw)
In-Reply-To: <aK1n_t-V1AlN86JR@hyeyoo>

On Tue, Aug 26, 2025 at 04:53:34PM +0900, Harry Yoo wrote:
> On Tue, Aug 26, 2025 at 01:54:49AM +0800, Kuan-Wei Chiu wrote:
> > 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
> > > > 1.
> > > 
> > > 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.
> 
> Could you give an example where the previous cmp_loc_by_count() code
> produces an incorrectly sorted array?
> 
Sorry for the late reply.

I tried generating random arrays to find a concrete example where the
old cmp_loc_by_count() causes a wrong ordering, but I couldn't
reproduce one. So I would like to withdraw my earlier claim that it
definitely leads to incorrect results, since I cannot demonstrate a
failing case.

The complexity of the sort() implementation also makes it hard to
reason precisely whether such inputs exist.

That said, I still believe the patch should be merged, because sort()
only guarantees correct behavior if the comparison function satisfies
antisymmetry and transitivity. When those are violated, correctness
depends on implementation details, and future changes (e.g., switching
to a different sorting algorithm) could potentially break the ordering.

Regards,
Kuan-Wei

> > [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.
> 
> -- 
> Cheers,
> Harry / Hyeonggon


  reply	other threads:[~2025-08-28 17:15 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-25  1:34 [PATCH 0/2] mm/slub: Fix debugfs stack trace sorting and simplify sort call Kuan-Wei Chiu
2025-08-25  1:34 ` [PATCH 1/2] mm/slub: Fix cmp_loc_by_count() to return 0 when counts are equal Kuan-Wei Chiu
2025-08-25 14:48   ` Joshua Hahn
2025-08-25 16:18     ` Kuan-Wei Chiu
2025-08-25 17:12       ` Joshua Hahn
2025-08-25 17:28   ` Vlastimil Babka
2025-08-25 17:54     ` Kuan-Wei Chiu
2025-08-26  7:53       ` Harry Yoo
2025-08-28 17:13         ` Kuan-Wei Chiu [this message]
2025-08-29  2:06           ` Harry Yoo
2025-08-25  1:34 ` [PATCH 2/2] mm/slub: Replace sort_r() with sort() for debugfs stack trace sorting Kuan-Wei Chiu
2025-08-25 17:31   ` Vlastimil Babka

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aLCOVoshch9phL5M@visitorckw-System-Product-Name \
    --to=visitorckw@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=c.yuanhaur@wustl.edu \
    --cc=cfmc.cs13@nycu.edu.tw \
    --cc=chuang@cs.nycu.edu.tw \
    --cc=cl@gentwo.org \
    --cc=glittao@gmail.com \
    --cc=harry.yoo@oracle.com \
    --cc=jhcheng.cs13@nycu.edu.tw \
    --cc=joshua.hahnjy@gmail.com \
    --cc=jserv@ccns.ncku.edu.tw \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=stable@vger.kernel.org \
    --cc=vbabka@suse.cz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).