Skip to contentSkip to conversation

s390/mm: Simplify gap clamping in mmap_base() using clamp()

PATCH Applied Qasim Ijaz 3 participants 3 messages Feb 4, 2025 – Feb 21, 2025

Subsystems (2)
lore
Share & tools
lore

Qasim Ijaz

author
#0 lore raw

PATCH 1/1 s390/mm: Simplify gap clamping in mmap_base() using clamp()

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.

---
 arch/s390/mm/mmap.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
arch/s390/mm/mmap.c +1 -5
index 76f376876e0d..a3d3e09a2828 100644
@@ -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);
 }
Signature · 1 line
2.39.5
#1 lore raw
Quoted 16 lines · Qasim Ijaz

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.

---
 arch/s390/mm/mmap.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
arch/s390/mm/mmap.c +0 -0
index 76f376876e0d..a3d3e09a2828 100644
@@ -63,11 +63,7 @@ static inline unsigned long mmap_base(unsigned long rnd,
 	 */
 	gap_min = SZ_128M;

arch/s390/mm/mmap.c static inline unsigned long mmap_base(unsigned long rnd, · line 69

Quoted 10 lines
arch/s390/mm/mmap.c +1 -5
 	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!

maintainer Reviewed-by on v1 1/1 Applied
#2 lore raw
In reply to Qasim Ijaz
Quoted 17 lines · Qasim Ijaz

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.

---
 arch/s390/mm/mmap.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
arch/s390/mm/mmap.c +0 -0
index 76f376876e0d..a3d3e09a2828 100644
@@ -63,11 +63,7 @@ static inline unsigned long mmap_base(unsigned long rnd,
 	 */
 	gap_min = SZ_128M;

arch/s390/mm/mmap.c static inline unsigned long mmap_base(unsigned long rnd, · line 69

Quoted 10 lines
arch/s390/mm/mmap.c +1 -5
 	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);
 }

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/