From nobody Mon Aug 17 12:10:17 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 4hNs9558lLz6pC7p for ; Mon, 17 Aug 2026 12:10:17 +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 4hNs953W9Sz3F7B for ; Mon, 17 Aug 2026 12:10:17 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1786968617; 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=jNT//wDLQ0C1kQlH0i8+bF35ZK3sI5bq4AdBNqPbd+s=; b=qKrKm2vtgEP29daZerYqevW2Nup8GwpTSpsSpGALm1HPO7iha09hGvJGB/bEdrroqrUJf3 jzYy7UH4NE+Y6drOhrvozBzUfl99f/uEbnfj5atqlIdTQLwJ2+u1x/P0u5AHG0zzRCeuyb w1eUKyTl2Mg5h20NigAtuSUku8N8avl1Wp3IdhtqWQvQmNH/hWneXbJOFLhjWFFgHhOKIc aLCiFnRRld/0rWUnsmbplxyLuEHXgoaPP/64Nzjl7gcSLOpgEGhvDkHcLMfTNmpr2uh2d3 Sn4xVLWDkqwtK4TIwMFnyseef1aRsmhwXeTE3H6+VZpQKQUBzrHQeqiIuvSTow== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1786968617; a=rsa-sha256; cv=none; b=gPMsBgVF5IKsV2C2eM0dvcg6jZQy5M7eMGa3UFRbTo1NQnrJvOZa2MnblKcdW5XhlWNciN lMS49+UGJQ+lbrs97pdYcr72OZBotufG5UnqWSbmO9+GkoEEJXLhJy/NtUpQdbYcxgXgy9 Cm9fj9HuelBIdFCXNpnYENFZHBTsgLFHxo1qkBEzQFrmr1/9i4bFxDUiex4LUdAYYdqrbN GM+T8Hmdq9OEp8zIoh8ecj47re8MYBWaPuE11nFUxDkvLP9llzvzyaQbfJJ1XKqWCyRtXm 5zcUfTHfarWpAmR018Fpp4TBdx4y3Hb8cjgI0O/8Jucny68g5ovLNDX0MgEkBw== 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=1786968617; 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=jNT//wDLQ0C1kQlH0i8+bF35ZK3sI5bq4AdBNqPbd+s=; b=kcAdqQYfLh4qnYpmEGI1H0467tac0x5zvISkMBaCiBG28WE4DYGVjvIEqZ55j7ja4fRuEp WgmBUQgRCZb18qQhVKHg/dDSudhtwOt8fnbxP3yLQPeo6JgcjQtgQZxUyCgPTM5yOEVJfa rPtQI7/QbAq7zwZQLJ7hEI6f/V+EbwnaKFnl1x41XMZQ1P+C9HoUotOtJFMuzpzEZh10JG ANADlzXADRipl9xG5KomPy38lvHpz4EPqSsVfhPd8K/SHzsj/4Ug2Wx2TUNaS2mX6r+8QU xje2JbD7ZOjknN/72jgfC2sTSBD9HiGNOtISqYyLN+OrtcavkZUFxVQ8EbxiSg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4hNs952PvPz1Cgd for ; Mon, 17 Aug 2026 12:10:17 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 3c59f by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Mon, 17 Aug 2026 12:10:17 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: 6e59c4eb574b - stable/15 - epoch: Fix epoch_drain_callbacks() 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: markj X-Git-Repository: src X-Git-Refname: refs/heads/stable/15 X-Git-Reftype: branch X-Git-Commit: 6e59c4eb574b8014f580c777876037dae4de7e22 Auto-Submitted: auto-generated Date: Mon, 17 Aug 2026 12:10:17 +0000 Message-Id: <6a82fa29.3c59f.5bc195cc@gitrepo.freebsd.org> The branch stable/15 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=6e59c4eb574b8014f580c777876037dae4de7e22 commit 6e59c4eb574b8014f580c777876037dae4de7e22 Author: Mark Johnston AuthorDate: 2026-07-08 17:13:01 +0000 Commit: Mark Johnston CommitDate: 2026-08-17 12:07:41 +0000 epoch: Fix epoch_drain_callbacks() This function is supposed to wait until all pending callbacks have been executed. This is useful in some contexts where we tear down some context (like a VNET jail and its associated UMA zones) synchronously, and we want to make sure that all pending asynchronous callbacks (which may free objects to said UMA zones) have run first. The implementation schedules a callback on each CPU and waits for them all to run. This assumes that, on a given CPU, callbacks are executed in the order that they are pushed. This assumption depends on the implementation of epoch_call_task() and ck_epoch_poll_deferred(), and it is not true in general. Callbacks are pushed onto a per-CPU stack in LIFO order. ck_epoch_poll_deferred() first pulls out the callbacks from epoch - 2, which are always safe to execute, and in so doing reorders them such that the oldest callback as at the top of the stack, so in this case, epoch_call_task() will execute them in order. However, ck_epoch_poll_deferred() may determine that it is safe to execute callbacks from epoch - 1 (or even from the current epoch if there are no active readers), and in this case it will push those callbacks onto the returned stack. This means that epoch_call_task() will invoke those newer destructors before the older ones, which means that epoch_drain_callbacks() may return early. Fix the correctness problem by simply doing all of this twice: once the first callback is invoked, we know that all of the callbacks that were pending at the time that epoch_drain_callbacks() was called are scheduled to be executed, so when the second callback is executed we know that they must be finished. This is slow, but it is already slow, and the slowness is less noticeable after commit dce56594991. I note that in an ideal world, this function would not exist, and all of the teardown would happen asynchronously, rather than the current mismash of synchronous and asynchronous cleanup. PR: 290201 Reviewed by: glebius MFC after: 1 month Differential Revision: https://reviews.freebsd.org/D58030 (cherry picked from commit 7bf11a2f0c9ad7af00996105fd34e61f1040402b) --- sys/kern/subr_epoch.c | 50 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/sys/kern/subr_epoch.c b/sys/kern/subr_epoch.c index 51d66d51db8d..266230b04b43 100644 --- a/sys/kern/subr_epoch.c +++ b/sys/kern/subr_epoch.c @@ -986,30 +986,40 @@ epoch_drain_callbacks(epoch_t epoch) sched_unbind(td); td->td_pinned = 0; - CPU_FOREACH(cpu) - epoch->e_drain_count++; - CPU_FOREACH(cpu) { - er = zpcpu_get_cpu(epoch->e_pcpu_record, cpu); - sched_bind(td, cpu); - epoch_call(epoch, &epoch_drain_cb, &er->er_drain_ctx); - } + /* + * Schedule a destructor on each CPU and block until all of them have + * run. Don't assume that destructors are invoked in order: once we're + * finished draining, do the same thing again to ensure that any + * destructors scheduled after the first pass have also run. + */ + for (int i = 0; i < 2; i++) { + CPU_FOREACH(cpu) + epoch->e_drain_count++; + CPU_FOREACH(cpu) { + er = zpcpu_get_cpu(epoch->e_pcpu_record, cpu); + sched_bind(td, cpu); + epoch_call(epoch, &epoch_drain_cb, &er->er_drain_ctx); + } - /* restore CPU binding, if any */ - if (was_bound != 0) { - sched_bind(td, old_cpu); - } else { - /* get thread back to initial CPU, if any */ - if (old_pinned != 0) + /* restore CPU binding, if any */ + if (was_bound != 0) { sched_bind(td, old_cpu); - sched_unbind(td); - } - /* restore pinned after bind */ - td->td_pinned = old_pinned; + } else { + /* get thread back to initial CPU, if any */ + if (old_pinned != 0) + sched_bind(td, old_cpu); + sched_unbind(td); + } + /* restore pinned after bind */ + td->td_pinned = old_pinned; + thread_unlock(td); - thread_unlock(td); + while (epoch->e_drain_count != 0) + msleep(epoch, &epoch->e_drain_mtx, PZERO, "EDRAIN", 0); - while (epoch->e_drain_count != 0) - msleep(epoch, &epoch->e_drain_mtx, PZERO, "EDRAIN", 0); + thread_lock(td); + } + thread_unlock(td); mtx_unlock(&epoch->e_drain_mtx); sx_xunlock(&epoch->e_drain_sx);