git: 9121202fcb27 - stable/13 - uma: Make the cache alignment mask unsigned

From: Olivier Certner <olce_at_FreeBSD.org>
Date: Thu, 21 Dec 2023 13:43:56 UTC
The branch stable/13 has been updated by olce:

URL: https://cgit.FreeBSD.org/src/commit/?id=9121202fcb27e981bc4d2a7695fbba463361df93

commit 9121202fcb27e981bc4d2a7695fbba463361df93
Author:     Olivier Certner <olce.freebsd@certner.fr>
AuthorDate: 2023-10-13 12:49:11 +0000
Commit:     Olivier Certner <olce@FreeBSD.org>
CommitDate: 2023-12-21 13:39:53 +0000

    uma: Make the cache alignment mask unsigned
    
    In uma_set_align_mask(), ensure that the passed value doesn't have its
    highest bit set, which would lead to problems since keg/zone alignment
    is internally stored as signed integers.  Such big values do not make
    sense anyway and indicate some programming error.  A future commit will
    introduce checks for this case and other ones.
    
    Reviewed by:            kib, markj
    Sponsored by:           The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D42262
    
    (cherry picked from commit 3d8f548b9e5772ff6890bdc01f7ba7b76203857d)
    
    Approved by:    markj (mentor)
---
 sys/arm/arm/cpufunc.c   |  6 +++---
 sys/vm/uma.h            |  2 +-
 sys/vm/uma_align_mask.h |  2 +-
 sys/vm/uma_core.c       | 15 ++++++++++-----
 4 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/sys/arm/arm/cpufunc.c b/sys/arm/arm/cpufunc.c
index 42f718b9773e..e94565cdc61f 100644
--- a/sys/arm/arm/cpufunc.c
+++ b/sys/arm/arm/cpufunc.c
@@ -61,8 +61,8 @@
 
 /* PRIMARY CACHE VARIABLES */
 
-int	arm_dcache_align;
-int	arm_dcache_align_mask;
+unsigned int	arm_dcache_align;
+unsigned int	arm_dcache_align_mask;
 
 #ifdef CPU_MV_PJ4B
 static void pj4bv7_setup(void);
@@ -170,7 +170,7 @@ get_cachetype_cp15(void)
 				    : : "r" (sel));
 				__asm __volatile("mrc p15, 1, %0, c0, c0, 0"
 				    : "=r" (csize));
-				arm_dcache_align = 1 <<
+				arm_dcache_align = 1U <<
 				    (CPUV7_CT_xSIZE_LEN(csize) + 4);
 			}
 			if (type == CACHE_ICACHE || type == CACHE_SEP_CACHE) {
diff --git a/sys/vm/uma.h b/sys/vm/uma.h
index c748a7843374..852473d69c27 100644
--- a/sys/vm/uma.h
+++ b/sys/vm/uma.h
@@ -475,7 +475,7 @@ void uma_zone_reclaim_domain(uma_zone_t, int req, int domain);
  * Returns:
  *	Nothing
  */
-void uma_set_cache_align_mask(int mask);
+void uma_set_cache_align_mask(unsigned int mask);
 
 #include <vm/uma_align_mask.h>
 
diff --git a/sys/vm/uma_align_mask.h b/sys/vm/uma_align_mask.h
index 666633350b9d..b4e9ac835d93 100644
--- a/sys/vm/uma_align_mask.h
+++ b/sys/vm/uma_align_mask.h
@@ -31,6 +31,6 @@
 #ifndef _VM_UMA_ALIGN_MASK_H_
 #define _VM_UMA_ALIGN_MASK_H_
 
-int uma_get_cache_align_mask(void) __pure;
+unsigned int uma_get_cache_align_mask(void) __pure;
 
 #endif /* !_VM_UMA_ALIGN_MASK_H_ */
diff --git a/sys/vm/uma_core.c b/sys/vm/uma_core.c
index 19ea5433559d..0f3d2591c893 100644
--- a/sys/vm/uma_core.c
+++ b/sys/vm/uma_core.c
@@ -149,7 +149,7 @@ static uma_zone_t slabzones[2];
 static uma_zone_t hashzone;
 
 /* The boot-time adjusted value for cache line alignment. */
-static int uma_cache_align_mask = 64 - 1;
+static unsigned int uma_cache_align_mask = 64 - 1;
 
 static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets");
 static MALLOC_DEFINE(M_UMA, "UMA", "UMA Misc");
@@ -3191,15 +3191,20 @@ uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
 /* Public functions */
 /* See uma.h */
 void
-uma_set_cache_align_mask(int mask)
+uma_set_cache_align_mask(unsigned int mask)
 {
 
-	if (mask >= 0)
-		uma_cache_align_mask = mask;
+	/*
+	 * Make sure the stored align mask doesn't have its highest bit set,
+	 * which would cause implementation-defined behavior when passing it as
+	 * the 'align' argument of uma_zcreate().  Such very large alignments do
+	 * not make sense anyway.
+	 */
+	uma_cache_align_mask = mask & ~(1U << 31);
 }
 
 /* Returns the alignment mask to use to request cache alignment. */
-int
+unsigned int
 uma_get_cache_align_mask(void)
 {
 	return (uma_cache_align_mask);