The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH RESEND] sched/mmcid: Use clamp() to simplify mm_cid_calc_pcpu_thrs()
@ 2026-07-05 17:20 Thorsten Blum
  2026-07-05 19:07 ` David Laight
  0 siblings, 1 reply; 3+ messages in thread
From: Thorsten Blum @ 2026-07-05 17:20 UTC (permalink / raw)
  To: Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot,
	Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
	Valentin Schneider, K Prateek Nayak
  Cc: Thorsten Blum, linux-kernel

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

Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
 kernel/sched/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 8b791e9e9f67..31739bd43176 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -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)

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

* Re: [PATCH RESEND] sched/mmcid: Use clamp() to simplify mm_cid_calc_pcpu_thrs()
  2026-07-05 17:20 [PATCH RESEND] sched/mmcid: Use clamp() to simplify mm_cid_calc_pcpu_thrs() Thorsten Blum
@ 2026-07-05 19:07 ` David Laight
  2026-07-05 21:07   ` Thorsten Blum
  0 siblings, 1 reply; 3+ messages in thread
From: David Laight @ 2026-07-05 19:07 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot,
	Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
	Valentin Schneider, K Prateek Nayak, linux-kernel

On Sun,  5 Jul 2026 19:20:54 +0200
Thorsten Blum <thorsten.blum@linux.dev> wrote:

> Use clamp() to simplify the code and improve its readability.
> 
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---
>  kernel/sched/core.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index 8b791e9e9f67..31739bd43176 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -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

>  }
>  
>  static bool mm_update_max_cids(struct mm_struct *mm)
> 


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

* Re: [PATCH RESEND] sched/mmcid: Use clamp() to simplify mm_cid_calc_pcpu_thrs()
  2026-07-05 19:07 ` David Laight
@ 2026-07-05 21:07   ` Thorsten Blum
  0 siblings, 0 replies; 3+ messages in thread
From: Thorsten Blum @ 2026-07-05 21:07 UTC (permalink / raw)
  To: David Laight
  Cc: Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot,
	Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
	Valentin Schneider, K Prateek Nayak, linux-kernel

On Sun, Jul 05, 2026 at 08:07:23PM +0100, David Laight wrote:
> On Sun,  5 Jul 2026 19:20:54 +0200
> Thorsten Blum <thorsten.blum@linux.dev> wrote:
> 
> > Use clamp() to simplify the code and improve its readability.
> > 
> > Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> > ---
> >  kernel/sched/core.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> > index 8b791e9e9f67..31739bd43176 100644
> > --- a/kernel/sched/core.c
> > +++ b/kernel/sched/core.c
> > @@ -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.

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.

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

end of thread, other threads:[~2026-07-05 21:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-05 17:20 [PATCH RESEND] sched/mmcid: Use clamp() to simplify mm_cid_calc_pcpu_thrs() Thorsten Blum
2026-07-05 19:07 ` David Laight
2026-07-05 21:07   ` Thorsten Blum

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