git: c7904405a8d4 - main - Remove PAGE_SIZE from libthr

From: Andrew Turner <andrew_at_FreeBSD.org>
Date: Tue, 03 May 2022 10:05:53 UTC
The branch main has been updated by andrew:

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

commit c7904405a8d47f64c3b0e73158572e2dc8ef0217
Author:     Andrew Turner <andrew@FreeBSD.org>
AuthorDate: 2022-04-07 16:24:46 +0000
Commit:     Andrew Turner <andrew@FreeBSD.org>
CommitDate: 2022-05-03 10:04:41 +0000

    Remove PAGE_SIZE from libthr
    
    In libthr we use PAGE_SIZE when allocating memory with mmap and to check
    various structs will fit into a single page so we can use this allocator
    for them.
    
    Ask the kernel for the page size on init for use by the page allcator
    and add a new machine dependent macro to hold the smallest page size
    the architecture supports to check the structure is small enough.
    
    This allows us to use the same libthr on arm64 with either 4k or 16k
    pages.
    
    Reviewed by:    kib, markj, imp
    Sponsored by:   The FreeBSD Foundation
    Differential Revision: https://reviews.freebsd.org/D34984
---
 lib/libthr/arch/aarch64/include/pthread_md.h |  3 +++
 lib/libthr/arch/amd64/include/pthread_md.h   |  3 +++
 lib/libthr/arch/arm/include/pthread_md.h     |  3 +++
 lib/libthr/arch/i386/include/pthread_md.h    |  3 +++
 lib/libthr/arch/powerpc/include/pthread_md.h |  3 +++
 lib/libthr/arch/riscv/include/pthread_md.h   |  3 +++
 lib/libthr/thread/thr_barrier.c              |  2 +-
 lib/libthr/thread/thr_cond.c                 |  2 +-
 lib/libthr/thread/thr_malloc.c               |  3 +--
 lib/libthr/thread/thr_mutex.c                |  2 +-
 lib/libthr/thread/thr_pshared.c              | 13 +++++++++----
 lib/libthr/thread/thr_pspinlock.c            |  2 +-
 lib/libthr/thread/thr_rwlock.c               |  2 +-
 13 files changed, 33 insertions(+), 11 deletions(-)

diff --git a/lib/libthr/arch/aarch64/include/pthread_md.h b/lib/libthr/arch/aarch64/include/pthread_md.h
index f3ce2d0bdbce..2ae5a0d2e808 100644
--- a/lib/libthr/arch/aarch64/include/pthread_md.h
+++ b/lib/libthr/arch/aarch64/include/pthread_md.h
@@ -41,6 +41,9 @@
 
 #define	CPU_SPINWAIT
 
+/* For use in _Static_assert to check structs will fit in a page */
+#define	THR_PAGE_SIZE_MIN	PAGE_SIZE_4K
+
 static __inline struct pthread *
 _get_curthread(void)
 {
diff --git a/lib/libthr/arch/amd64/include/pthread_md.h b/lib/libthr/arch/amd64/include/pthread_md.h
index d7bf5d5e5753..acfbd551b3de 100644
--- a/lib/libthr/arch/amd64/include/pthread_md.h
+++ b/lib/libthr/arch/amd64/include/pthread_md.h
@@ -41,6 +41,9 @@
 
 #define	CPU_SPINWAIT		__asm __volatile("pause")
 
+/* For use in _Static_assert to check structs will fit in a page */
+#define	THR_PAGE_SIZE_MIN	PAGE_SIZE
+
 static __inline struct pthread *
 _get_curthread(void)
 {
diff --git a/lib/libthr/arch/arm/include/pthread_md.h b/lib/libthr/arch/arm/include/pthread_md.h
index 75570337c627..dfd7e7d951bf 100644
--- a/lib/libthr/arch/arm/include/pthread_md.h
+++ b/lib/libthr/arch/arm/include/pthread_md.h
@@ -39,6 +39,9 @@
 
 #define	CPU_SPINWAIT
 
+/* For use in _Static_assert to check structs will fit in a page */
+#define	THR_PAGE_SIZE_MIN	PAGE_SIZE
+
 static __inline struct pthread *
 _get_curthread(void)
 {
diff --git a/lib/libthr/arch/i386/include/pthread_md.h b/lib/libthr/arch/i386/include/pthread_md.h
index 525a9c4368a3..8940acee6fee 100644
--- a/lib/libthr/arch/i386/include/pthread_md.h
+++ b/lib/libthr/arch/i386/include/pthread_md.h
@@ -41,6 +41,9 @@
 
 #define	CPU_SPINWAIT		__asm __volatile("pause")
 
+/* For use in _Static_assert to check structs will fit in a page */
+#define	THR_PAGE_SIZE_MIN	PAGE_SIZE
+
 static __inline struct pthread *
 _get_curthread(void)
 {
diff --git a/lib/libthr/arch/powerpc/include/pthread_md.h b/lib/libthr/arch/powerpc/include/pthread_md.h
index 10c787ae189a..076c43e1ef43 100644
--- a/lib/libthr/arch/powerpc/include/pthread_md.h
+++ b/lib/libthr/arch/powerpc/include/pthread_md.h
@@ -40,6 +40,9 @@
 
 #define	CPU_SPINWAIT
 
+/* For use in _Static_assert to check structs will fit in a page */
+#define	THR_PAGE_SIZE_MIN	PAGE_SIZE
+
 static __inline struct pthread *
 _get_curthread(void)
 {
diff --git a/lib/libthr/arch/riscv/include/pthread_md.h b/lib/libthr/arch/riscv/include/pthread_md.h
index 092d40266e1c..7c7b9a787c24 100644
--- a/lib/libthr/arch/riscv/include/pthread_md.h
+++ b/lib/libthr/arch/riscv/include/pthread_md.h
@@ -46,6 +46,9 @@
 
 #define	CPU_SPINWAIT
 
+/* For use in _Static_assert to check structs will fit in a page */
+#define	THR_PAGE_SIZE_MIN	PAGE_SIZE
+
 static __inline struct pthread *
 _get_curthread(void)
 {
diff --git a/lib/libthr/thread/thr_barrier.c b/lib/libthr/thread/thr_barrier.c
index fa205ad509c9..fc42588cb412 100644
--- a/lib/libthr/thread/thr_barrier.c
+++ b/lib/libthr/thread/thr_barrier.c
@@ -37,7 +37,7 @@ __FBSDID("$FreeBSD$");
 
 #include "thr_private.h"
 
-_Static_assert(sizeof(struct pthread_barrier) <= PAGE_SIZE,
+_Static_assert(sizeof(struct pthread_barrier) <= THR_PAGE_SIZE_MIN,
     "pthread_barrier is too large for off-page");
 
 __weak_reference(_pthread_barrier_init,		pthread_barrier_init);
diff --git a/lib/libthr/thread/thr_cond.c b/lib/libthr/thread/thr_cond.c
index 9eba3dd437f0..925792a92662 100644
--- a/lib/libthr/thread/thr_cond.c
+++ b/lib/libthr/thread/thr_cond.c
@@ -43,7 +43,7 @@ __FBSDID("$FreeBSD$");
 
 #include "thr_private.h"
 
-_Static_assert(sizeof(struct pthread_cond) <= PAGE_SIZE,
+_Static_assert(sizeof(struct pthread_cond) <= THR_PAGE_SIZE_MIN,
     "pthread_cond too large");
 
 /*
diff --git a/lib/libthr/thread/thr_malloc.c b/lib/libthr/thread/thr_malloc.c
index 80a81f9c6c27..b4a627cc55eb 100644
--- a/lib/libthr/thread/thr_malloc.c
+++ b/lib/libthr/thread/thr_malloc.c
@@ -51,8 +51,7 @@ __thr_malloc_init(void)
 		return;
 	npagesizes = getpagesizes(pagesizes_d, nitems(pagesizes_d));
 	if (npagesizes == -1) {
-		npagesizes = 1;
-		pagesizes_d[0] = PAGE_SIZE;
+		PANIC("Unable to read page sizes");
 	}
 	pagesizes = pagesizes_d;
 	_thr_umutex_init(&thr_malloc_umtx);
diff --git a/lib/libthr/thread/thr_mutex.c b/lib/libthr/thread/thr_mutex.c
index 303386db7fe7..8dccdf4dfa8c 100644
--- a/lib/libthr/thread/thr_mutex.c
+++ b/lib/libthr/thread/thr_mutex.c
@@ -53,7 +53,7 @@ __FBSDID("$FreeBSD$");
 
 #include "thr_private.h"
 
-_Static_assert(sizeof(struct pthread_mutex) <= PAGE_SIZE,
+_Static_assert(sizeof(struct pthread_mutex) <= THR_PAGE_SIZE_MIN,
     "pthread_mutex is too large for off-page");
 
 /*
diff --git a/lib/libthr/thread/thr_pshared.c b/lib/libthr/thread/thr_pshared.c
index 83714785f9b1..d0219d0488f4 100644
--- a/lib/libthr/thread/thr_pshared.c
+++ b/lib/libthr/thread/thr_pshared.c
@@ -50,12 +50,17 @@ static struct pshared_hash_head pshared_hash[HASH_SIZE];
 #define	PSHARED_KEY_HASH(key)	(((unsigned long)(key) >> 8) % HASH_SIZE)
 /* XXXKIB: lock could be split to per-hash chain, if appears contested */
 static struct urwlock pshared_lock = DEFAULT_URWLOCK;
+static int page_size;
 
 void
 __thr_pshared_init(void)
 {
 	int i;
 
+	page_size = getpagesize();
+	THR_ASSERT(page_size >= THR_PAGE_SIZE_MIN,
+	    "THR_PAGE_SIZE_MIN is too large");
+
 	_thr_urwlock_init(&pshared_lock);
 	for (i = 0; i < HASH_SIZE; i++)
 		LIST_INIT(&pshared_hash[i]);
@@ -112,7 +117,7 @@ pshared_gc(struct pthread *curthread)
 			if (error == 0)
 				continue;
 			LIST_REMOVE(h, link);
-			munmap(h->val, PAGE_SIZE);
+			munmap(h->val, page_size);
 			free(h);
 		}
 	}
@@ -164,7 +169,7 @@ pshared_insert(void *key, void **val)
 		 */
 		if (h->key == key) {
 			if (h->val != *val) {
-				munmap(*val, PAGE_SIZE);
+				munmap(*val, page_size);
 				*val = h->val;
 			}
 			return (1);
@@ -204,7 +209,7 @@ pshared_clean(void *key, void *val)
 {
 
 	if (val != NULL)
-		munmap(val, PAGE_SIZE);
+		munmap(val, page_size);
 	_umtx_op(NULL, UMTX_OP_SHM, UMTX_SHM_DESTROY, key, NULL);
 }
 
@@ -225,7 +230,7 @@ __thr_pshared_offpage(void *key, int doalloc)
 	    UMTX_SHM_LOOKUP, key, NULL);
 	if (fd == -1)
 		return (NULL);
-	res = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+	res = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
 	close(fd);
 	if (res == MAP_FAILED)
 		return (NULL);
diff --git a/lib/libthr/thread/thr_pspinlock.c b/lib/libthr/thread/thr_pspinlock.c
index c71bdbb3f196..d1a53446f654 100644
--- a/lib/libthr/thread/thr_pspinlock.c
+++ b/lib/libthr/thread/thr_pspinlock.c
@@ -41,7 +41,7 @@ __FBSDID("$FreeBSD$");
 
 #include "thr_private.h"
 
-_Static_assert(sizeof(struct pthread_spinlock) <= PAGE_SIZE,
+_Static_assert(sizeof(struct pthread_spinlock) <= THR_PAGE_SIZE_MIN,
     "pthread_spinlock is too large for off-page");
 
 #define SPIN_COUNT 100000
diff --git a/lib/libthr/thread/thr_rwlock.c b/lib/libthr/thread/thr_rwlock.c
index 22c6c878b2a5..1d110a7bf285 100644
--- a/lib/libthr/thread/thr_rwlock.c
+++ b/lib/libthr/thread/thr_rwlock.c
@@ -38,7 +38,7 @@ __FBSDID("$FreeBSD$");
 #include "un-namespace.h"
 #include "thr_private.h"
 
-_Static_assert(sizeof(struct pthread_rwlock) <= PAGE_SIZE,
+_Static_assert(sizeof(struct pthread_rwlock) <= THR_PAGE_SIZE_MIN,
     "pthread_rwlock is too large for off-page");
 
 __weak_reference(_thr_rwlock_destroy, pthread_rwlock_destroy);