From nobody Thu Nov 19 15:32:14 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 4jVjBk2Tnvz71FDB for ; Thu, 19 Nov 2026 15:32:14 +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) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jVjBk1VZNz4rcK for ; Thu, 19 Nov 2026 15:32:14 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1795102334; 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=5RjmjL0SOIf8dXggj5ofUsjFXUlMEy2caXtIS3v1w84=; b=LMBnplaf15q/FHr/7VRcIyd3sXnPZesdTpcuDoiQcTLicHuQo1V/rwjmHAJ7iZdTCbsUrg tx2UerZFY+guX4lW58DZ3Ef0bYOuLRAqWELvUbzrwEAjIiTzkOe5oQbaqkiYityQckUdQx Z8QdnUV4jXQt2LN6kWC5sefzOYtV3PQ95cWlNqsu/aiFlbqrzdm742ypOBiGqhvKVRJ3pu lMkMRK1N0aAfpxcHgPUW1yMpM3EYEzVIJfQ5gNir4cxHwP/Nh+F5cSUHVzPHemRk6Pgkh7 vFHAduuIpBHnPwbVkIxXQ4SguQT9Zg47IhveQUE2kCeTFbDyIaGuoIg0k6AYNg== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1795102334; b=iWpFDjXzuab3oXVaN1r6nmN8K2G/sgQWKI3ATBm25C2hDLUK0S32kAkxwv43EoVoAv3pR9 kre+JxAj51rx4Ck8sk9jbgBVeDEsutY69VE2ev9H41LJ+J+DatxPsGMrUpjT7+zvx7X8cH QKhz8/hKVaT/2oCXzULDm61P6DblQ8KN8P/BLOMkxoz+ZCZSKWe0yz4IcsBN9S2OaqPZur +Lf2Vlery+n0eFXJ3b5FXHikMqDbDYFyQnGA5aGMAtXtItz2p52pQ6nKU5e5s6IhthmyQ3 RUCS0Q0o+xq4C1lbq2iXY7nEx14SDDxxYZNQIsz8E/8yMIy8KpP+Ck6zuXFCOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1795102334; 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=5RjmjL0SOIf8dXggj5ofUsjFXUlMEy2caXtIS3v1w84=; b=FMTN2LeWB27bSK7aUwaF+M3J0M/ypVZhtM0WLsgDqBmJLeUtl81n4NsKVWEbkTTKgniKz9 L8Q64CATTxRftJ6I06Q988S3otR+PsZ2IDqgkP9+/5tbiIBeoYXSzgkJnyyXw6g5drZKva Wg3mlIOwhztEdPYmHM5saRZHXTDsu/xxgtfN+mpPq7I2fWyEJuSqlLp/AcXR56KltfgPrT 2wpEO8qC8j7CvX4FnpQ7lo8b4AkKbkL/EDnYkq9i1e34otIDtzVZv9jUNxl3fLY6n2x/uz qwQSXBQGA2lWtZVZ00Y5lssovKsJHc1TWQYvj0sQJR1snLb76nNy8DCr1y5DZQ== 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 4jVjBk0nWMz15Mf for ; Thu, 19 Nov 2026 15:32:14 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id d487 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Thu, 19 Nov 2026 15:32:14 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Olivier Certner Subject: git: 1744df209f7f - stable/15 - critical_exit_preempt(): Re-check for preemption locked List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org List-Id: Commit messages for all branches of the src repository 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: olce X-Git-Repository: src X-Git-Refname: refs/heads/stable/15 X-Git-Reftype: branch X-Git-Commit: 1744df209f7f6afa353574a2b91da74faefa9831 Auto-Submitted: auto-generated Date: Thu, 19 Nov 2026 15:32:14 +0000 Message-Id: <6aff167e.d487.5beebbfd@gitrepo.freebsd.org> The branch stable/15 has been updated by olce: URL: https://cgit.FreeBSD.org/src/commit/?id=1744df209f7f6afa353574a2b91da74faefa9831 commit 1744df209f7f6afa353574a2b91da74faefa9831 Author: Olivier Certner AuthorDate: 2026-10-13 13:20:43 +0000 Commit: Olivier Certner CommitDate: 2026-11-19 15:31:49 +0000 critical_exit_preempt(): Re-check for preemption locked Once we have the thread lock, we are in a spinlock section and thus will not be preempted, which ensures stability of 'td_owepreempt'. Check it again for the improbable case where we were already preempted before entering that section (after 'td_critnest' reached 0 in critical_exit() but before clearing 'td_owepreempt', because of an IPI_PREEMPT received in the meantime or any interrupt whose handler uses critical sections), in which case there is no need to switch to another thread. It is however still necessary to temporarily raise 'td_critnest' around the thread_lock() call, and that is not an optimization contrary to what the old comment stated. As explained in the new comment, thread_lock() can lead to entering and exiting a spinlock section multiple times, which would cause recursion with critical_exit_preempt() in some circumstances. Reviewed by: kib, markj MFC after: 3 days Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D60860 (cherry picked from commit 28c31242758323499eaf8af1f03fc4ae4a960205) --- sys/kern/kern_switch.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/sys/kern/kern_switch.c b/sys/kern/kern_switch.c index 42fee61a99a0..c6f749984217 100644 --- a/sys/kern/kern_switch.c +++ b/sys/kern/kern_switch.c @@ -215,12 +215,6 @@ critical_exit_preempt(void) struct thread *td; int flags; - /* - * If td_critnest is 0, it is possible that we are going to get - * preempted again before reaching the code below. This happens - * rarely and is harmless. However, this means td_owepreempt may - * now be unset. - */ td = curthread; if (td->td_critnest != 0) return; @@ -228,13 +222,29 @@ critical_exit_preempt(void) return; /* - * Microoptimization: we committed to switch, - * disable preemption in interrupt handlers - * while spinning for the thread lock. + * Temporarily disable preemption, as thread_lock() can enter/exit + * spinlock sections multiple times if the thread lock can't be acquired + * right away or changes. A call to spinlock_exit() there causes + * a fast-growing stack of recursive calls with critical_exit_preempt() + * as long as the condition persists (provided 'td_owepreempt' is still + * non-zero), easily leading to stack exhaustion. */ td->td_critnest = 1; thread_lock(td); td->td_critnest--; + /* + * We might have been preempted after the test on 'td_critnest' above + * and before returning from the thread_lock() call, on reception of an + * IPI_PREEMPT or some interrupt whose handler uses critical sections. + * If that is the case, 'td_owepreempt' was reset and there is no need + * to switch to another thread. We are in a spinlock section here so + * 'td_owepreempt' is stable and no incoming preemption will be lost. + */ + if (__predict_false(td->td_owepreempt == 0)) { + thread_unlock(td); + return; + } + flags = SW_INVOL | SW_PREEMPT; if (TD_IS_IDLETHREAD(td)) flags |= SWT_IDLE;