Skip to contentSkip to conversation

sched/mmcid: Use clamp() to simplify mm_cid_calc_pcpu_thrs()

PATCH RESEND NAK Thorsten Blum 2 participants 3 messages Jul 5 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 sched/mmcid: Use clamp() to simplify mm_cid_calc_pcpu_thrs()

Use clamp() to simplify the code and improve its readability.

---
 kernel/sched/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/core.c +1 -1
index 8b791e9e9f67..31739bd43176 100644
@@ -10785,7 +10785,7 @@ static inline unsigned int mm_cid_calc_pcpu_thrs(struct mm_mm_cid *mc)
 
 	opt_cids = min(mc->nr_cpus_allowed, mc->users);
 	/* Has to be at least 1 because 0 indicates PCPU mode off */
-	return max(min(opt_cids - opt_cids / 4, num_possible_cpus() / 2), 1);
+	return clamp(opt_cids - opt_cids / 4, 1, num_possible_cpus() / 2);
 }
 
 static bool mm_update_max_cids(struct mm_struct *mm)
#1 lore raw
Quoted 12 lines · On Sun, 5 Jul 2026 19:20:54 +0200 Thorsten Blum <[email protected]>…

Use clamp() to simplify the code and improve its readability.

---
 kernel/sched/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/core.c +0 -0
index 8b791e9e9f67..31739bd43176 100644

kernel/sched/core.c static inline unsigned int mm_cid_calc_pcpu_thrs(struct mm_mm_cid *mc) · line 10788

Quoted 5 lines
kernel/sched/core.c +1 -1
@@ -10785,7 +10785,7 @@ static inline unsigned int mm_cid_calc_pcpu_thrs(struct mm_mm_cid *mc)
 
 	opt_cids = min(mc->nr_cpus_allowed, mc->users);
 	/* Has to be at least 1 because 0 indicates PCPU mode off */
-	return max(min(opt_cids - opt_cids / 4, num_possible_cpus() / 2), 1);
+	return clamp(opt_cids - opt_cids / 4, 1, num_possible_cpus() / 2);

Nack, it isn't the same.
clamp() requires that lo <= hi but I suspect num_possible_cpus() can be zero.
IIRC clamp(val, lo, hi) is currently val > hi ? hi : val < lo ? lo : val so the above doesn't even generate the desired answer.
But relying on the is a bug anyway.
It would be nice to swap the order so that clamp(-5, 0, 5u) could be valid and have the expected value.

David

Quoted 4 lines
kernel/sched/core.c +0 -0
 }
 
 static bool mm_update_max_cids(struct mm_struct *mm)
#2 lore raw
Quoted 16 lines · David Laight
Quoted 13 lines · On Sun, 5 Jul 2026 19:20:54 +0200 Thorsten Blum <[email protected]>…

Use clamp() to simplify the code and improve its readability.

---
 kernel/sched/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/core.c +0 -0
index 8b791e9e9f67..31739bd43176 100644
@@ -10785,7 +10785,7 @@ static inline unsigned int mm_cid_calc_pcpu_thrs(struct mm_mm_cid *mc)
 
Quoted 8 lines

kernel/sched/core.c static inline unsigned int mm_cid_calc_pcpu_thrs(struct mm_mm_cid *mc) · line 10788

Quoted 4 lines
kernel/sched/core.c +1 -1
 	opt_cids = min(mc->nr_cpus_allowed, mc->users);
 	/* Has to be at least 1 because 0 indicates PCPU mode off */
-	return max(min(opt_cids - opt_cids / 4, num_possible_cpus() / 2), 1);
+	return clamp(opt_cids - opt_cids / 4, 1, num_possible_cpus() / 2);

Nack, it isn't the same.
clamp() requires that lo <= hi but I suspect num_possible_cpus() can be zero.

num_possible_cpus() probably shouldn't be 0, but you're right anyway, and that's the case I didn't consider: num_possible_cpus() / 2 == 0 when num_possible_cpus() is 1.

Let's drop this patch, and thanks for the feedback.