From nobody Thu Jul 11 11:07:56 2024 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 4WKX586rGMz5QPRq; Thu, 11 Jul 2024 11:07:56 +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 "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4WKX586Jnwz4YLL; Thu, 11 Jul 2024 11:07:56 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1720696076; 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=CZ/KRuR97uxWpBhkSpvPQNRdlb7wsKkUS9vux59gQuw=; b=NALTqJuu27Y/VcVg1Prp8nZp6JVoUqBw2wbBgbRwnlmIWkbTFecc3ZF7xfwsP9fI5nVrtQ VJxOv37Qk2p4McGwWma1QtooHMWzDwWMLHSNNzSWBKMKWOm0HmrBhOEBqebVx3mmq+wahZ FecolgGLo4ghU4zMR/EW3G6dlnpo0E8tvXkwm8AnzE2Fz2b3Ah8mZmWyP++R69dh1RBeni mWSQt96HdieOGHFn9UvPfPw0vmAqsbG9D3eweZ69aO/XsAQ/nOonMEPLVAbMR/vw/7SbvC PwXlMh0KFQZVIBjLpeLC8eeJYSR18cSrf1Wkhlx4Ya85uPv/bFJTDZRBz8dzXg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1720696076; a=rsa-sha256; cv=none; b=Y1GWylrkIhs34Iu2Xs3FNT0hMBfaBYqgsFQhri0mDj7cy0MdwZduQbDpYQAM6xeg5gaXAN AsGo9ICKn5frO3S1CPBQopkr+IyECetaEc/NhjtwmAtepPcadBdQfowceqbbVAx6Rjyact VzW3MXc1Sr67/yeOo8Kpthm31+ReGua9F0tZRHRNCCgQ9Gr23JZtWaBuqP7XDhDCTJDoF5 RhU23355V3z0MM7pfcUd9P2Dju54s+R1997093p+nEmkJ+o8vSn8+elYZ1YVbYqAvMhy+C n6o61gl+BcfXDJPhekJZ037aRa0/1BvgsTll4KpcvNaGHlWo0OYIcxHxOHOGKg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1720696076; 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=CZ/KRuR97uxWpBhkSpvPQNRdlb7wsKkUS9vux59gQuw=; b=ZjS44lU/bYVftVZS9ff/tPYGLsyvZUXQxZSxgUTdyTnINWFDj1XG0+AJTwo8EPLS7ajlJ9 dd4yg25RBXk6r72bJczU4mkUnkzXzoQoyIp+mLLHfqYsHZYoBrbQ0KUf7KnYfBpXNKlvJu bi7ygdQickgf7jwurCYplLwBluanqN/8C2zOAjdhu0igL2A7R85uzvpfmQSQhrXz8SyYZn hLcVBTZNis+zGs8Vrk2vC6DPg6JF1XVj7V4KCfvMxe4LljdHVTdZbm43e64l6sClmbCa1w oKphi2bYA7OHIOba/XoYOMIigCYE7aLZ1ymsg92itmgS5yMeTfhmWZkLOEvFgQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4WKX585vxmzxsg; Thu, 11 Jul 2024 11:07:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 46BB7uAf007080; Thu, 11 Jul 2024 11:07:56 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 46BB7uSb007077; Thu, 11 Jul 2024 11:07:56 GMT (envelope-from git) Date: Thu, 11 Jul 2024 11:07:56 GMT Message-Id: <202407111107.46BB7uSb007077@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mateusz Guzik Subject: git: 87ee63bac69d - main - locks: add a runtime check for missing turnstile 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 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mjg X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 87ee63bac69dc49291f55590b8baa57cad6c7d85 Auto-Submitted: auto-generated The branch main has been updated by mjg: URL: https://cgit.FreeBSD.org/src/commit/?id=87ee63bac69dc49291f55590b8baa57cad6c7d85 commit 87ee63bac69dc49291f55590b8baa57cad6c7d85 Author: Mateusz Guzik AuthorDate: 2024-07-11 00:17:27 +0000 Commit: Mateusz Guzik CommitDate: 2024-07-11 11:06:52 +0000 locks: add a runtime check for missing turnstile There are sometimes bugs which result in the unlock fast path failing, which in turns causes a not-helpful crash report when dereferencing a NULL turnstile. Help debugging such cases by pointing out what happened along with some debug. Sponsored by: Rubicon Communications, LLC ("Netgate") --- sys/kern/kern_mutex.c | 4 +++- sys/kern/kern_rwlock.c | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/sys/kern/kern_mutex.c b/sys/kern/kern_mutex.c index 90361b23c09a..0fa624cc4bb1 100644 --- a/sys/kern/kern_mutex.c +++ b/sys/kern/kern_mutex.c @@ -1053,7 +1053,9 @@ __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v) turnstile_chain_lock(&m->lock_object); _mtx_release_lock_quick(m); ts = turnstile_lookup(&m->lock_object); - MPASS(ts != NULL); + if (__predict_false(ts == NULL)) { + panic("got NULL turnstile on mutex %p v %zx", m, v); + } if (LOCK_LOG_TEST(&m->lock_object, opts)) CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m); turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE); diff --git a/sys/kern/kern_rwlock.c b/sys/kern/kern_rwlock.c index f53c69b5e6ec..31ff8a7213fd 100644 --- a/sys/kern/kern_rwlock.c +++ b/sys/kern/kern_rwlock.c @@ -770,11 +770,12 @@ __rw_runlock_hard(struct rwlock *rw, struct thread *td, uintptr_t v LOCK_FILE_LINE_ARG_DEF) { struct turnstile *ts; - uintptr_t setv, queue; + uintptr_t setv, passedv, queue; if (SCHEDULER_STOPPED()) return; + passedv = v; if (__rw_runlock_try(rw, td, &v)) goto out_lockstat; @@ -827,7 +828,10 @@ __rw_runlock_hard(struct rwlock *rw, struct thread *td, uintptr_t v * release the lock. */ ts = turnstile_lookup(&rw->lock_object); - MPASS(ts != NULL); + if (__predict_false(ts == NULL)) { + panic("got NULL turnstile on rwlock %p passedv %zx v %zx", + rw, passedv, v); + } turnstile_broadcast(ts, queue); turnstile_unpend(ts); td->td_rw_rlocks--; @@ -1206,7 +1210,7 @@ __rw_wunlock_hard(volatile uintptr_t *c, uintptr_t v LOCK_FILE_LINE_ARG_DEF) { struct rwlock *rw; struct turnstile *ts; - uintptr_t tid, setv; + uintptr_t tid, setv, passedv; int queue; tid = (uintptr_t)curthread; @@ -1254,6 +1258,7 @@ __rw_wunlock_hard(volatile uintptr_t *c, uintptr_t v LOCK_FILE_LINE_ARG_DEF) * of waiters or doing some complicated lock handoff gymnastics. */ setv = RW_UNLOCKED; + passedv = v; v = RW_READ_VALUE(rw); queue = TS_SHARED_QUEUE; if (v & RW_LOCK_WRITE_WAITERS) { @@ -1268,7 +1273,10 @@ __rw_wunlock_hard(volatile uintptr_t *c, uintptr_t v LOCK_FILE_LINE_ARG_DEF) queue == TS_SHARED_QUEUE ? "read" : "write"); ts = turnstile_lookup(&rw->lock_object); - MPASS(ts != NULL); + if (__predict_false(ts == NULL)) { + panic("got NULL turnstile on rwlock %p passedv %zx v %zx", rw, + passedv, v); + } turnstile_broadcast(ts, queue); turnstile_unpend(ts); turnstile_chain_unlock(&rw->lock_object);