From nobody Wed Oct 07 17:06:35 2026 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4j0KKR543vz6wBLH for ; Wed, 07 Oct 2026 17:06:35 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "YR2" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4j0KKR4PQhz4bfS for ; Wed, 07 Oct 2026 17:06:35 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1791392795; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=nPM+G3MPLPrgJIi+oUK1cYhuDYjOjQ4QP8/2SjsfFU4=; b=fSQyZWrQfQmJNcyxCOs8GlU/uYU8d2E+tnjLZs32w5wXHvi4USsggqIdLDODKrmzaKYcQL g8zBzjINzdila/by8Otv+UqUyMdbaK7Hnz8hzgXK388KdVLw0ix+MTtGNY1mRaUPWbCPne K8NTLUR28FPZN27e78/FBeJfSkuAjcxOzG0mGXXOYT5FuZLm3YKQCVee2BA1gJRezcYqOF Tsm5Y30XgiFPqXVqzpVh/ZvTJGCNY5I9YR3KNQ3NV4NB12KY1vfYOo31doiLhXIp7sOWoc tY06SDhlYqcpdI4fm4GTuFVMK80bwtDaag3f1JlsC1P4YctzWYHZHPBW3uhxew== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1791392795; b=af+B7rtGb5ivSHd7ZjMFHc6iGTEl6VQF8mHyHvKXtFpX7aTH12EAxpRAgualX0tNiIFcUe 6OfEY/sa7XXiozOhgyWriKf3zTdyVegFAlr0l027WTdAaTydDE/RgFMAyKMYaVoowiANxL 3oEb2o7go4p3Oa+xqaLPqF8XWQS5jUHMNdkhgg2V/3uq992NF/w4N/iMUNOhzvsBmnZPKY vjVGkKMQkWqOqDKRNJ/jbDU7oWilkjsEdmtGEOJ5eFyHBa/n11mMgt0mUdQu0BsSpB/FSB +TqU1hSD4grI1hKDwpPJp3KTU0Fb+s70pI8c7JNA5G/EvCYoq46ful7U6kB7Wg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1791392795; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=nPM+G3MPLPrgJIi+oUK1cYhuDYjOjQ4QP8/2SjsfFU4=; b=uYWTWSGrlcBrn8U+me4CJIg6mjMAJqqoA79avtngSCoqn8+P7Q1XhN3BOBbVCSLHLK9Ddg QW8eyL+wp6+0Dx3/vFqptLrmsgMnvhXgh21nGzGYeP4AY4D/Kc5NhILIf/ASx2DQElds9v B/NYjvma4B9BV1iXuDddEcAjOENckC3B4HsfnaMvXAEmceW9nQ5BpzKrOsFlmcgvMOVyI5 C6sgK/d/3S44Yix5t6+4X1M4NsU1+CP+h6LzdGssrRKU6WSCe4PYpMBxmOBxUUrcb/zPAJ tG40JkPWSjc9f65/6YNEPOtzlnCsOTa6HNHhrgkiJLaTQVOiit46gJtyP+04aQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4j0KKR2pVQz12Tt for ; Wed, 07 Oct 2026 17:06:35 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 36b64 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Wed, 07 Oct 2026 17:06:35 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: ShengYi Hung Subject: git: 99edcc3bd526 - main - libthr: Reorder pthread mutex to prevent false sharing List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: aokblast X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 99edcc3bd52614c6cf972ea8424a7807ed3fe1e3 Auto-Submitted: auto-generated Date: Wed, 07 Oct 2026 17:06:35 +0000 Message-Id: <6ac67c1b.36b64.46e2d4d6@gitrepo.freebsd.org> The branch main has been updated by aokblast: URL: https://cgit.FreeBSD.org/src/commit/?id=99edcc3bd52614c6cf972ea8424a7807ed3fe1e3 commit 99edcc3bd52614c6cf972ea8424a7807ed3fe1e3 Author: ShengYi Hung AuthorDate: 2026-10-07 07:22:13 +0000 Commit: ShengYi Hung CommitDate: 2026-10-07 17:05:58 +0000 libthr: Reorder pthread mutex to prevent false sharing On a 64 byte cache line CPU (amd64), m_owner is contended by all of the shared processes. However, after finding one winner, mtx is enqueued, which trigger a cache refresh from other processors. Reorder mtx so that m_lock and m_qu will not be in the same cache line. Notice that this doesn't affet arch that expose 128 bytes cache line line aarch64 or powerpc. Reviewed by: kib, markj MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D60434 --- lib/libthr/thread/thr_mutex.c | 9 +++++++++ lib/libthr/thread/thr_private.h | 6 +++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/libthr/thread/thr_mutex.c b/lib/libthr/thread/thr_mutex.c index ccb3094db16d..139dc768b7ba 100644 --- a/lib/libthr/thread/thr_mutex.c +++ b/lib/libthr/thread/thr_mutex.c @@ -53,6 +53,15 @@ _Static_assert(sizeof(struct pthread_mutex) <= THR_PAGE_SIZE_MIN, "pthread_mutex is too large for off-page"); +/* + * Keep the thread-private linkage off the cache line holding the lock word; + * Only meaningful where cache line is 64 bytes. + */ +#if defined(__LP64__) && CACHE_LINE_SIZE == 64 +_Static_assert(offsetof(struct pthread_mutex, m_qe) >= CACHE_LINE_SIZE, + "m_qe must not share a cache line with m_lock.m_owner"); +#endif + /* * For adaptive mutexes, how many times to spin doing trylock2 * before entering the kernel to block diff --git a/lib/libthr/thread/thr_private.h b/lib/libthr/thread/thr_private.h index 2edf09d761ae..cf90ac5bd4ef 100644 --- a/lib/libthr/thread/thr_private.h +++ b/lib/libthr/thread/thr_private.h @@ -171,6 +171,11 @@ struct pthread_mutex { int m_spinloops; int m_yieldloops; int m_ps; /* pshared init stage */ + /* + * Deliberately put m_qe and m_lock into different cache line to prevent + * false sharing. + */ + struct pthread_mutex *m_rb_prev; /* * Link for all mutexes a thread currently owns, of the same * prio type. @@ -178,7 +183,6 @@ struct pthread_mutex { TAILQ_ENTRY(pthread_mutex) m_qe; /* Link for all private mutexes a thread currently owns. */ TAILQ_ENTRY(pthread_mutex) m_pqe; - struct pthread_mutex *m_rb_prev; }; struct pthread_mutex_attr {