git: 7d1469e555bd - main - Ensure 'struct thread' is aligned to a cache line

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Thu, 02 Nov 2023 13:40:12 UTC
The branch main has been updated by markj:

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

commit 7d1469e555bdce32b3dfc898478ae5564d5072b1
Author:     Olivier Certner <olce.freebsd@certner.fr>
AuthorDate: 2023-10-13 08:52:31 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2023-11-02 13:30:03 +0000

    Ensure 'struct thread' is aligned to a cache line
    
    Using the new UMA_ALIGN_CACHE_AND_MASK() facility, which allows to
    simultaneously guarantee a minimum of 32 bytes of alignment (the 5 lower
    bits are always 0).
    
    For the record, to this day, here's a (possibly non-exhaustive) list of
    synchronization primitives using lower bits to store flags in pointers
    to thread structures:
    - lockmgr, rwlock and sx all use the 5 bits directly.
    - rmlock indirectly relies on sx, so can use the 5 bits.
    - mtx (non-spin) relies on the 3 lower bits.
    
    Reviewed by:            markj, kib
    MFC after:              2 week
    Sponsored by:           The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D42266
---
 sys/kern/kern_thread.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/sys/kern/kern_thread.c b/sys/kern/kern_thread.c
index 3bc8546db594..d948665392d4 100644
--- a/sys/kern/kern_thread.c
+++ b/sys/kern/kern_thread.c
@@ -568,9 +568,15 @@ threadinit(void)
 	if (tid0 != THREAD0_TID)
 		panic("tid0 %d != %d\n", tid0, THREAD0_TID);
 
+	/*
+	 * Thread structures are specially aligned so that (at least) the
+	 * 5 lower bits of a pointer to 'struct thead' must be 0.  These bits
+	 * are used by synchronization primitives to store flags in pointers to
+	 * such structures.
+	 */
 	thread_zone = uma_zcreate("THREAD", sched_sizeof_thread(),
 	    thread_ctor, thread_dtor, thread_init, thread_fini,
-	    32 - 1, UMA_ZONE_NOFREE);
+	    UMA_ALIGN_CACHE_AND_MASK(32 - 1), UMA_ZONE_NOFREE);
 	tidhashtbl = hashinit(maxproc / 2, M_TIDHASH, &tidhash);
 	tidhashlock = (tidhash + 1) / 64;
 	if (tidhashlock > 0)