public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] s390/mm: Simplify gap clamping in mmap_base() using clamp()
@ 2025-02-04 16:25 Qasim Ijaz
  2025-02-14 10:58 ` Alexander Gordeev
  2025-02-21 23:55 ` Vasily Gorbik
  0 siblings, 2 replies; 4+ messages in thread
From: Qasim Ijaz @ 2025-02-04 16:25 UTC (permalink / raw)
  To: agordeev, gerald.schaefer, hca, gor, borntraeger, svens
  Cc: linux-s390, linux-kernel

mmap_base() has logic to ensure that the variable "gap" stays within the 
range defined by "gap_min" and "gap_max". Replace this with the clamp() 
macro to shorten and simplify code.

Signed-off-by: Qasim Ijaz <qasdev00@gmail.com>
---
 arch/s390/mm/mmap.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/arch/s390/mm/mmap.c b/arch/s390/mm/mmap.c
index 76f376876e0d..a3d3e09a2828 100644
--- a/arch/s390/mm/mmap.c
+++ b/arch/s390/mm/mmap.c
@@ -63,11 +63,7 @@ static inline unsigned long mmap_base(unsigned long rnd,
 	 */
 	gap_min = SZ_128M;
 	gap_max = (STACK_TOP / 6) * 5;
-
-	if (gap < gap_min)
-		gap = gap_min;
-	else if (gap > gap_max)
-		gap = gap_max;
+	gap = clamp(gap, gap_min, gap_max);
 
 	return PAGE_ALIGN(STACK_TOP - gap - rnd);
 }
-- 
2.39.5


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

* Re: [PATCH] s390/mm: Simplify gap clamping in mmap_base() using clamp()
  2025-02-04 16:25 Qasim Ijaz
@ 2025-02-14 10:58 ` Alexander Gordeev
  2025-02-21 23:55 ` Vasily Gorbik
  1 sibling, 0 replies; 4+ messages in thread
From: Alexander Gordeev @ 2025-02-14 10:58 UTC (permalink / raw)
  To: Qasim Ijaz
  Cc: gerald.schaefer, hca, gor, borntraeger, svens, linux-s390,
	linux-kernel

On Tue, Feb 04, 2025 at 04:25:08PM +0000, Qasim Ijaz wrote:
> mmap_base() has logic to ensure that the variable "gap" stays within the 
> range defined by "gap_min" and "gap_max". Replace this with the clamp() 
> macro to shorten and simplify code.
> 
> Signed-off-by: Qasim Ijaz <qasdev00@gmail.com>
> ---
>  arch/s390/mm/mmap.c | 6 +-----
>  1 file changed, 1 insertion(+), 5 deletions(-)
> 
> diff --git a/arch/s390/mm/mmap.c b/arch/s390/mm/mmap.c
> index 76f376876e0d..a3d3e09a2828 100644
> --- a/arch/s390/mm/mmap.c
> +++ b/arch/s390/mm/mmap.c
> @@ -63,11 +63,7 @@ static inline unsigned long mmap_base(unsigned long rnd,
>  	 */
>  	gap_min = SZ_128M;
>  	gap_max = (STACK_TOP / 6) * 5;
> -
> -	if (gap < gap_min)
> -		gap = gap_min;
> -	else if (gap > gap_max)
> -		gap = gap_max;
> +	gap = clamp(gap, gap_min, gap_max);
>  
>  	return PAGE_ALIGN(STACK_TOP - gap - rnd);
>  }

Whenever possible I personally prefer branches over ternary operators.
But if one wants to clump it in one line, then gap_min and gap_max
are also redundant.

Thanks!

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

* Re: [PATCH] s390/mm: Simplify gap clamping in mmap_base() using clamp()
@ 2025-02-15 12:31 Qasim Ijaz
  0 siblings, 0 replies; 4+ messages in thread
From: Qasim Ijaz @ 2025-02-15 12:31 UTC (permalink / raw)
  To: Alexander Gordeev
  Cc: gerald.schaefer, hca, gor, borntraeger, svens, linux-s390,
	linux-kernel

On Fri, Feb 14, 2025 at 11:58:24AM +0100, Alexander Gordeev wrote:
> On Tue, Feb 04, 2025 at 04:25:08PM +0000, Qasim Ijaz wrote:
> > mmap_base() has logic to ensure that the variable "gap" stays within the 
> > range defined by "gap_min" and "gap_max". Replace this with the clamp() 
> > macro to shorten and simplify code.
> > 
> > Signed-off-by: Qasim Ijaz <qasdev00@gmail.com>
> > ---
> >  arch/s390/mm/mmap.c | 6 +-----
> >  1 file changed, 1 insertion(+), 5 deletions(-)
> > 
> > diff --git a/arch/s390/mm/mmap.c b/arch/s390/mm/mmap.c
> > index 76f376876e0d..a3d3e09a2828 100644
> > --- a/arch/s390/mm/mmap.c
> > +++ b/arch/s390/mm/mmap.c
> > @@ -63,11 +63,7 @@ static inline unsigned long mmap_base(unsigned long rnd,
> >  	 */
> >  	gap_min = SZ_128M;
> >  	gap_max = (STACK_TOP / 6) * 5;
> > -
> > -	if (gap < gap_min)
> > -		gap = gap_min;
> > -	else if (gap > gap_max)
> > -		gap = gap_max;
> > +	gap = clamp(gap, gap_min, gap_max);
> >  
> >  	return PAGE_ALIGN(STACK_TOP - gap - rnd);
> >  }
> 
> Whenever possible I personally prefer branches over ternary operators.
> But if one wants to clump it in one line, then gap_min and gap_max
> are also redundant.

Thank you for the feedback Alexander, would you like me to send a v2
patch without the gap_min and gap_max variables as you suggested?

> 
> Thanks!

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

* Re: [PATCH] s390/mm: Simplify gap clamping in mmap_base() using clamp()
  2025-02-04 16:25 Qasim Ijaz
  2025-02-14 10:58 ` Alexander Gordeev
@ 2025-02-21 23:55 ` Vasily Gorbik
  1 sibling, 0 replies; 4+ messages in thread
From: Vasily Gorbik @ 2025-02-21 23:55 UTC (permalink / raw)
  To: Qasim Ijaz
  Cc: agordeev, gerald.schaefer, hca, borntraeger, svens, linux-s390,
	linux-kernel

On Tue, Feb 04, 2025 at 04:25:08PM +0000, Qasim Ijaz wrote:
> mmap_base() has logic to ensure that the variable "gap" stays within the 
> range defined by "gap_min" and "gap_max". Replace this with the clamp() 
> macro to shorten and simplify code.
> 
> Signed-off-by: Qasim Ijaz <qasdev00@gmail.com>
> ---
>  arch/s390/mm/mmap.c | 6 +-----
>  1 file changed, 1 insertion(+), 5 deletions(-)
> 
> diff --git a/arch/s390/mm/mmap.c b/arch/s390/mm/mmap.c
> index 76f376876e0d..a3d3e09a2828 100644
> --- a/arch/s390/mm/mmap.c
> +++ b/arch/s390/mm/mmap.c
> @@ -63,11 +63,7 @@ static inline unsigned long mmap_base(unsigned long rnd,
>  	 */
>  	gap_min = SZ_128M;
>  	gap_max = (STACK_TOP / 6) * 5;
> -
> -	if (gap < gap_min)
> -		gap = gap_min;
> -	else if (gap > gap_max)
> -		gap = gap_max;
> +	gap = clamp(gap, gap_min, gap_max);
>  
>  	return PAGE_ALIGN(STACK_TOP - gap - rnd);
>  }
> -- 

Reviewed-by: Vasily Gorbik <gor@linux.ibm.com>

Applied, with a fixup removing the gap_min and gap_max variables as
Alexander proposed and aligning with your patch for x86. Thank you!

https://lore.kernel.org/all/174014934373.10177.13398467994659612713.tip-bot2@tip-bot2/

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

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

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-15 12:31 [PATCH] s390/mm: Simplify gap clamping in mmap_base() using clamp() Qasim Ijaz
  -- strict thread matches above, loose matches on Subject: below --
2025-02-04 16:25 Qasim Ijaz
2025-02-14 10:58 ` Alexander Gordeev
2025-02-21 23:55 ` Vasily Gorbik

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