From nobody Tue Nov 03 17:42:53 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 4jJwrt0Y9cz6yfQh for ; Tue, 03 Nov 2026 17:42:54 +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 4jJwrs6NK7z4cch for ; Tue, 03 Nov 2026 17:42:53 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1793727773; 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=ZyxJYfwMMyfTtvlJNmUNld+wRlVMzxQhibKz4EyS+GU=; b=GP/noIsK9pderk1orqh7AMdSVz3nMndKOpdiwgIDx+NgGol4TN/CFK+91cbHF6Y1g+MJnw oUeddjX9Ge3dPPmCPCWfrcYaoqUYcsc83ylipUXGyOQC55fDrYsQT+fha85jIzXoKSMTo+ /3sHTaiMQ49Zs6qhNm5CH9yrRARIMB69PNqRHuvacEnJlTaUjYHKQ93GWNZkMdsBoObgIQ XZYbsXvtL1Pgx2wjrc1PHgjYzxvUTJMKkLcL6SRVYgI1+7VCSAsYb5p+gUjIlUszpNQmIa XTubi4M7n0DRQlwB1hwIm9HfzkXp3Gq782WC4PF2y19sHO96Mt7tH6AdD6kSvw== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1793727773; b=D4m1JFup0kFlRfILnOR4uO+PP4wG3sc6rSQ+NJmgmZqL2ttbwWS0zoioHgrX42laj9iLPs T6f/WVnAbjA6I0FCz+OMgKMmaSJ1VqvqO1zz57KQewbyMoFkT8MqlebfjRDjsXsM02hqGz 1sernNEJiv4/gMirq0hvpwAAf/kGyLXs7bIUpYEqiPxJ+G1s39ZdemVvmAl7cTGL7ys+vz UxKsO41WaMIGA881JLtez0Wl6FgNuBShaQnbJ16PwqaswShhREibNtOmyuZsXd9/IuhVZH f9ALiI5Y9hJE7Dsz0V7yisFaRMYVW2MJzPHqwFd4GBKMFLkgHZhkVWzEXUwDUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1793727773; 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=ZyxJYfwMMyfTtvlJNmUNld+wRlVMzxQhibKz4EyS+GU=; b=CifBBZEjpi5sY5vG9QDE1wW+2/AIuk4W6rNhFczeTXrQAa/AsMdLudMY6MwMF6B1/UfN85 6Mp0GDYt8rSWofhL5d8Pkduo1Gm3DuKdyVcGm3NSQ9irC2xBGf7Z/fihp4cjsE0D2MGPRP 2JmaLKirP9OTeZWfaE07Uiv0MYJxXJHNYpXINv8VDozSqLauDdPhbsjMkNrCYfg+ip5Mjr SSYM2cITamAX8FqAAamDEo6yrFNXz2rT3E3qAN5fPTxsolxmzOgk/y1ak4pk3BJv9TrGxW /y219DkA/2EsqagSPRuyQwqaw7BhbFjR4I/HGPox1vY7EzZkNx1nOqmVD5124g== 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 4jJwrs62dnzQZx for ; Tue, 03 Nov 2026 17:42:53 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 259a9 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Tue, 03 Nov 2026 17:42:53 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Olivier Certner Subject: git: 28c312427583 - main - 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 Sender: owner-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/main X-Git-Reftype: branch X-Git-Commit: 28c31242758323499eaf8af1f03fc4ae4a960205 Auto-Submitted: auto-generated Date: Tue, 03 Nov 2026 17:42:53 +0000 Message-Id: <6aea1d1d.259a9.4e84c6eb@gitrepo.freebsd.org> The branch main has been updated by olce: URL: https://cgit.FreeBSD.org/src/commit/?id=28c31242758323499eaf8af1f03fc4ae4a960205 commit 28c31242758323499eaf8af1f03fc4ae4a960205 Author: Olivier Certner AuthorDate: 2026-10-13 13:20:43 +0000 Commit: Olivier Certner CommitDate: 2026-11-03 17:42:17 +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 --- 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;