git: 706303829968 - stable/13 - Ensure 'struct thread' is aligned to a cache line

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

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

commit 7063038299682f421a3b56ade0dbd40c2ce69292
Author:     Olivier Certner <olce.freebsd@certner.fr>
AuthorDate: 2023-10-13 08:52:31 +0000
Commit:     Olivier Certner <olce@FreeBSD.org>
CommitDate: 2023-12-21 13:40:10 +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
    Sponsored by:           The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D42266
    
    (cherry picked from commit 7d1469e555bdce32b3dfc898478ae5564d5072b1)
    
    Approved by:    markj (mentor)
---
 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 cab0d7446d2a..14015ef5fdc2 100644
--- a/sys/kern/kern_thread.c
+++ b/sys/kern/kern_thread.c
@@ -525,9 +525,15 @@ threadinit(void)
 	 */
 	flags |= UMA_ZONE_CONTIG;
 #endif
+	/*
+	 * 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, flags);
+	    UMA_ALIGN_CACHE_AND_MASK(32 - 1), flags);
 	tidhashtbl = hashinit(maxproc / 2, M_TIDHASH, &tidhash);
 	tidhashlock = (tidhash + 1) / 64;
 	if (tidhashlock > 0)