From nobody Fri Sep 18 13:36:20 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 4hmYYk31d6z6sSn3 for ; Fri, 18 Sep 2026 13:36:26 +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 4hmYYk1Htkz4bPD for ; Fri, 18 Sep 2026 13:36:26 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1789738586; 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=T3T0Bsnu1saNc1u4BfE1thRtXkwFMpcd00Rx4jNSJQQ=; b=R4TlVGor6YAOBd2g07F3QuszgmS6Sd91zoY3vVupL5XJ29JAzFwwHElwDmLBrpUPXwkiXG W7sF2yF/gFtDF8hCj2XDBUiXzV2E+PEfjQBae2NBn0vmq5q23kKQNpR1PrhjaZ4yU1Tl82 0sDhPU6LDHPYHuKImm5siWcHQ3Zf5M/oLWCdXBeMRDIPT0Z5jdJToivC6K/3q8jnBXGgAl IWy1VzZgelkumZvfJNWR7B+QsARYPTBvboVy6bKdIUXhf1A8tXbuWi3O3UtIYJdBDOhCyC oxbr8yBLZxfHyaj3a1Yfnwo5yjV7B0kDJhcQhwsqqI18cTVn3CtN6NWTljqQeg== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1789738586; b=U2oJk4cMxoOcbK8EiblPx4KKGeb18SkE9uyGuRr77tB/BBa/lFfo2zORdEm84GoH2Dpt9p KdwdUyGNgJqzCC/pus27nHMu30HucADnX4bdXQDyCOtRw21tmLkdWXaxQKE3nwfzsZmC3H uRoa+9d5O6k6zQhW0nOWKaWqJz5DMszCimFouRnzXHjhQ+4p6EERO2yKROZ8RHbwNAYQTl x6euMTctW5YGCENrRVv1XWB7Nm9gqmJs6nhQKdzcAoxFf7jKGzY9d5UYeyjvrnHN8HfU3D iopwyHZHzvj6JpeQ1B8Abboo/PyB9VpolGRKr5JYdeh+JFpKUo4ttUaUloSZBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1789738586; 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=T3T0Bsnu1saNc1u4BfE1thRtXkwFMpcd00Rx4jNSJQQ=; b=u7OifTlxlpVtVHt//GyW0JhVBYpZCpxhGAV1I6/z7IiYhWz/xE48KDmVY08PvgHG/+om1C G+9ZhTPicmNYuwTV2Uiz/Ocgl0T66+AGEuXbRJLlikqO43xqi0EAEB1Pkl8GvVJWHbuJN6 UO37FLRDZXuwAhe01o9lEqfpNEAcO/AiNheUObjn0hk6/7/fbV5zTNABync0B6ta9rYMZX Hzz7TeDyVSBJTMHvVBhp7jjFYh4Wz27kipQfCLAKvOXvlZelAJLrA9wMQCx/OWG5jcqnj9 vdQL+rGqxaxsypKAfZqqhzSVOj40Um3vdzf4YomsH1pg1KjTgP2cdFtVcFllog== 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 4hmYYj6qcKzLJR for ; Fri, 18 Sep 2026 13:36:25 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 402ec by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Fri, 18 Sep 2026 13:36:20 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Piotr Kubaj Subject: git: d7cf7209efab - stable/14 - umtx: do not sleep on an unowned mutex after a spurious CAS failure 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: pkubaj X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: d7cf7209efab6d3a88620a035c7d74d60a17e114 Auto-Submitted: auto-generated Date: Fri, 18 Sep 2026 13:36:20 +0000 Message-Id: <6aad3e54.402ec.6a23c6c2@gitrepo.freebsd.org> The branch stable/14 has been updated by pkubaj: URL: https://cgit.FreeBSD.org/src/commit/?id=d7cf7209efab6d3a88620a035c7d74d60a17e114 commit d7cf7209efab6d3a88620a035c7d74d60a17e114 Author: Piotr Kubaj AuthorDate: 2026-09-03 14:08:45 +0000 Commit: Piotr Kubaj CommitDate: 2026-09-18 13:36:09 +0000 umtx: do not sleep on an unowned mutex after a spurious CAS failure On ll/sc architectures casueword32() may report a spurious store-conditional failure (reservation lost to an interrupt, preemption, or another CPU touching the same reservation granule), and this is indistinguishable from a genuine comparison mismatch: both return 1. That is intentional since D20772 and documented in casueword(9) ("The store can fail on load-linked/store-conditional architectures."), so callers must cope. do_lock_normal() does not fully cope. When the initial UMUTEX_UNOWNED -> id acquire CAS fails spuriously, the observed owner is still UMUTEX_UNOWNED, so neither the UMUTEX_CONTESTED branch nor the real-owner case applies, and execution falls through past the "rv == 1 but not contested, likely store failure" comment into the sleep path. There, the contested-bit CAS (expecting the observed owner, i.e. UMUTEX_UNOWNED) succeeds because the mutex really is unowned, stamping m_owner = UMUTEX_CONTESTED with no owner tid, and the thread sleeps on "umtxn" forever: nobody owns the mutex, so no unlock and no wakeup ever arrive. In _UMUTEX_TRY mode the same situation returns a false EBUSY for a free mutex. Treat an observed owner of UMUTEX_UNOWNED like UMUTEX_CONTESTED: try to acquire the mutex, setting the contested bit, instead of falling through to the sleep path. rv == 1 with the observed value equal to the expected value can only mean a spurious store failure, so the mutex is free. If the acquire CAS fails again, the outer loop restarts and re-evaluates ownership. The contested bit set with no waiters present only costs the matching unlock one trip through the kernel. This was hit in practice on powerpc64le (POWER9): the Swift runtime's Synchronization.Mutex issues _umtx_op(UMTX_OP_MUTEX_LOCK) directly with no userspace fast path, so an uncontended lock of an unowned mutex runs the kernel CAS exactly where a spurious failure deadlocks (single-threaded process parked on "umtxn" with m_owner == 0x80000000, observed as Foundation.Process.run() hanging). libthr mostly masks the bug because pthread_mutex_lock() enters the kernel only when there is a real owner that will eventually issue a wakeup. The mechanism was confirmed with an experimental powerpc kernel that instead retried the ll/sc sequence inside casueword32()/casueword(); that also eliminated the hang, but is not proposed here since the single-attempt semantics of casueword(9) are intentional. Reviewed by: kib MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D59338 (cherry picked from commit c5d9485e7d7faeba1019301ed83a781dac2d0cb0) --- sys/kern/kern_umtx.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/sys/kern/kern_umtx.c b/sys/kern/kern_umtx.c index 7c01586b4dd7..37c187a2ea7f 100644 --- a/sys/kern/kern_umtx.c +++ b/sys/kern/kern_umtx.c @@ -1420,19 +1420,23 @@ do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags, } /* - * If no one owns it but it is contested try - * to acquire it. + * If no one owns it, but it is contested or + * the CAS above failed spuriously (possible + * on ll/sc architectures), try to acquire it. + * Sleeping would be forever in the spurious + * case: no owner exists to wake us. */ MPASS(rv == 1); - if (owner == UMUTEX_CONTESTED) { - rv = casueword32(&m->m_owner, - UMUTEX_CONTESTED, &owner, - id | UMUTEX_CONTESTED); + if (owner == UMUTEX_CONTESTED || + owner == UMUTEX_UNOWNED) { + rv = casueword32(&m->m_owner, owner, + &owner, id | UMUTEX_CONTESTED); /* The address was invalid. */ if (rv == -1) return (EFAULT); if (rv == 0) { - MPASS(owner == UMUTEX_CONTESTED); + MPASS(owner == UMUTEX_CONTESTED || + owner == UMUTEX_UNOWNED); return (0); } if (rv == 1) { @@ -1448,7 +1452,7 @@ do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags, continue; } - /* rv == 1 but not contested, likely store failure */ + /* rv == 1 with a real owner, fall through to sleep. */ rv = thread_check_susp(td, false); if (rv != 0) return (rv);