sched/mmcid: Use clamp() to simplify mm_cid_calc_pcpu_thrs()
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
@@ -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)
David Laight
NAKQuoted 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.Signed-off-by Thorsten Blum <[email protected]>--- kernel/sched/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/sched/core.c +0
-0
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)
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.Signed-off-by Thorsten Blum <[email protected]>--- kernel/sched/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/sched/core.c +0
-0@@ -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
-1opt_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 suspectnum_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.