Skip to contentSkip to conversation

cgroup: ensure stable pid sorting in cmppid()

PATCH In review Kaushlendra Kumar 2 participants 4 messages Feb 21 cgroup

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cgroup: ensure stable pid sorting in cmppid()

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.

---
 kernel/cgroup/cgroup-v1.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/cgroup/cgroup-v1.c +4 -1
index 724950c4b690..7fdfa37aaa5f 100644
@@ -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,
Signature · 1 line
2.34.1
maintainer
#1 lore raw

Hello,

Quoted 4 lines · 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.

Can you give examples of such an overflow? What values would cause that?

Thanks.

Signature · 1 line
tejun
#2 lore raw
Quoted 3 lines

Can you give examples of such an overflow? What values would cause that?

pid_t is a signed 32-bit integer. Consider:

a = 2147483647 (INT_MAX, 0x7FFFFFFF)
b = -1

a - b = 2147483647 - (-1) = 2147483648

This overflows signed int32, wrapping to a big negative value.

In practice, pid_t values in Linux are positive , so this overflow cannot happen with real PIDs today. However, the subtraction pattern is a known antipattern for comparison functions, and using the three-way idiom is the safer.(less, greater and equal)

If you prefer, I can adjust the commit message to note that this is a correctness hardening rather than a fix for a currently triggerable bug.

BR,
Kaushlendra

maintainer
#3 lore raw
Quoted 5 lines · Kaushlendra
Quoted 2 lines

Can you give examples of such an overflow? What values would cause that?

pid_t is a signed 32-bit integer. Consider:

Quoted 8 lines

a = 2147483647 (INT_MAX, 0x7FFFFFFF)
b = -1

a - b = 2147483647 - (-1) = 2147483648

This overflows signed int32, wrapping to a big negative value.

In practice, pid_t values in Linux are positive

and limited to PID_MAX_LIMIT (4mil).

Quoted 4 lines

, so this overflow cannot happen with real PIDs today. However, the subtraction pattern is a known antipattern for comparison functions, and using the three-way idiom is the safer.(less, greater and equal)

It's a bigger anti pattern to complicate code for non-existent problems.

Thanks.

Signature · 1 line
tejun