public inbox for cgroups@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] cgroup: ensure stable pid sorting in cmppid()
@ 2026-02-21  3:49 Kaushlendra Kumar
  2026-02-21  5:00 ` Tejun Heo
  0 siblings, 1 reply; 4+ messages in thread
From: Kaushlendra Kumar @ 2026-02-21  3:49 UTC (permalink / raw)
  To: tj, hannes, mkoutny; +Cc: cgroups, Kaushlendra Kumar

The subtraction-based comparator (a - b) in cmppid() can
overflow for large pid_t differences, producing incorrect
sign values. This breaks qsort() ordering guarantees and
may cause unstable or wrong sort results in pidlist output.

Replace with a three-way comparison idiom:
  (a > b) - (a < b)

This reliably returns -1, 0, or +1 without overflow,
ensuring correct and stable qsort() behavior for all
pid_t values.

Signed-off-by: Kaushlendra Kumar <kaushlendra.kumar@intel.com>
---
 kernel/cgroup/cgroup-v1.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/kernel/cgroup/cgroup-v1.c b/kernel/cgroup/cgroup-v1.c
index 724950c4b690..7fdfa37aaa5f 100644
--- a/kernel/cgroup/cgroup-v1.c
+++ b/kernel/cgroup/cgroup-v1.c
@@ -281,7 +281,10 @@ static int pidlist_uniq(pid_t *list, int length)
  */
 static int cmppid(const void *a, const void *b)
 {
-	return *(pid_t *)a - *(pid_t *)b;
+	pid_t pa = *(pid_t *)a;
+	pid_t pb = *(pid_t *)b;
+
+	return (pa > pb) - (pa < pb);
 }
 
 static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
-- 
2.34.1


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

end of thread, other threads:[~2026-02-21  6:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-21  3:49 [PATCH] cgroup: ensure stable pid sorting in cmppid() Kaushlendra Kumar
2026-02-21  5:00 ` Tejun Heo
2026-02-21  5:45   ` Kumar, Kaushlendra
2026-02-21  6:55     ` Tejun Heo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox