Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 16 Nov 2023 20:54:48 GMT
From:      Mark Johnston <markj@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: e191c0283e86 - stable/14 - Ensure 'struct thread' is aligned to a cache line
Message-ID:  <202311162054.3AGKsm9v054054@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/14 has been updated by markj:

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

commit e191c0283e8640d46ed6b725e3311d0f37f3620c
Author:     Olivier Certner <olce.freebsd@certner.fr>
AuthorDate: 2023-10-13 08:52:31 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2023-11-16 15:07:30 +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
    
    (cherry picked from commit 7d1469e555bdce32b3dfc898478ae5564d5072b1)
---
 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 4175d1594af4..d8ba94925fec 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)



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202311162054.3AGKsm9v054054>