From nobody Wed Oct 07 17:06:35 2026 X-Original-To: dev-commits-src-all@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 4j0KKX3xpzz6wBbB for ; Wed, 07 Oct 2026 17:06:40 +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 4j0KKX3SFhz4bX8 for ; Wed, 07 Oct 2026 17:06:40 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1791392800; 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=xMVaIOxdIE7HDkDcCJmdSt1sFUldt/HGIfVRAfyx6FewybHcib4n4veS7XPgAFBhj6cq2/ Ge/5RzO3Xx0Dv8EPdbdH3yBTIL3r4PZMNeSyce6d/f3Sd7Fm+2keYMkjq37gzTAOjMgZDO GanqNAYxT4nA4aOGNqx2dP0nTjNcG3Frc9P0MYb+Ibj3NhEhpGTu3C0dVPGQo7hFq6RL6l 271IvmQ4bebkR1oDiHmQ6NFxFumzGhez9cbdoAPFX+8ZySiRLkuCjby8dufVNoZLR3qxww pLByFhxXkIznsCmaR45ZW5uwx4Apj0MTFlkJf4gFEZi56G8uW4KRnF2PhT1ohA== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1791392800; b=iGdZ8pANusWJ88PLZC4bPXUAOGukVKuPxrHXLmNyXutwqnRjLf6UucKcaunou2J+hvvOip Wq+yenrgI6rxriBmbbFvCwr/43vcBlKeOuU1YQr94+THAqIhQCpTpLLfaS3zezSLbnCWA5 Ife5/dXi3pQ4LYVga5vnu7/QWqssctoEQZPb5zZnSGvOeJZ8yVspr5g0m5LyUjpODFQtlv hjOc5BI/Pbt3O3kw3sz8dkSEgPnccB4+744y/5qBF5ahle2TP5rn0lDrE2a2QI3WAt6TIT bsEtFl9MojXBylwiJkDvzibk8x/tpFzn0aqMzu/gJ6ke1yw5zAvnwauKWzRa4A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1791392800; 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=FMXmE1C5w20JWE/GZ8Ocyv87Npn72S1Rct2ogGQB2S/yo+frwYNNWFU5PNT+oj+9K7DZ36 kmdtsFBY8Cx+CPk1uop1KVrlGsae/Ob033ExLOmqG4qQQJ+BN+0nFQSjNq5xoUoAqhxyIH q+cicL5fKX5dXwQGqiCdklu0PKSwabiMAhIyUzQUV/Wx32DO9N8QZBWmZvocFl5LxtznlJ M+VLBf4WUO9ZuaEUa/ihJLeDAnjMNhF7Oe4h3Bx4yo6Bq2JwLO/ZFRbGD3uaSYK013ImDh 2Pi5BOuWY8JTqSQExEYUZlJu7p4yL8DUhEHw+Zc1aLyFYxHSJow2ne1S0MVxGg== 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 4j0KKX2CzXz12gW for ; Wed, 07 Oct 2026 17:06:40 +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 all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@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 {