From nobody Thu Jul 9 13:41:46 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 4gwx2g1B6Jz6lDZP for ; Thu, 09 Jul 2026 13:41:47 +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 "YR1" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4gwx2f4Rv9z46qp for ; Thu, 09 Jul 2026 13:41:46 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1783604506; 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=2mVXL4/XT4GxsKpENtJ7BDIpBWGlmNjhXk3jXUm6gjA=; b=s+E6AkoPagoDqTKeb5eTzM0KUfyS/Iw3UV4gu6Cs+wkDMQ1Vpy0uG7P8tKaHFOp/LDemSg dF9jJclWVquVWlbbOFRkJoz0zffkqJ+t9edGGxpRgq4vu6IKqq3yA4/aX6FUfPTHEBBdz3 vGbcpqFvVBtBgZWpO3ZpVQfXREVT0iMmjKwlaPGOkS2XlPeeBtFSOPaDrErASVOlRZY62a xCkOO+Cfff0nBcT49Ww9PFDJZOjnbOdxzLofDjyNeZxfEdiENsuspaQQtzVP+6anqvUMka BqwquQhStrinlawyZ/tXI33KwOHgcetmGmPU25FyFeQ2KUxubuaBnIhQntdqQw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1783604506; a=rsa-sha256; cv=none; b=WW59Z2vO0ij/cmjL5vk1+ZnnU7HbIR4tH9QdIFTrbGOzEoFLqDZJOxxzMI79hSpJJvDJ9Y QgdaKlkGPTWDxJzWZ4Ue6O/BwifTPZQwLkx+nDMcbzAdX9se/nmTdglJF3E2G37lXLygYR Q9wxGfb6elhSJ8wYBE5HgsNcNmEgza2rp/3rQPFOWQfJuhfbXnBzLjnrMToMClhP84Fdjd 0a8vijl1g27mOrzNZEhwdgJYu87PNXOSgEJl75oaSle+kwuRWvgwSRuP/lpUrckBa1JMkt GcXGB5Dhc6PyTbpQKppOTMPmKLDyOO58axJoT7oYN0wNYCtjTDoTkR+20zcsSw== 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=1783604506; 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=2mVXL4/XT4GxsKpENtJ7BDIpBWGlmNjhXk3jXUm6gjA=; b=MaswEi+NSx4tIL8E6WWmzUibyKJKD8Hvhs0Cf8DqHwKEjxTt61reMr71dbIDAtO/8w8XfG N9GMrvfo+QjwnHtKcmGKy6BK4kWNE4TTmTnR1X0M3742x7I8u8dsdGXtab+pyaX5uaCd7C kOP3iKLErh5yivf9806ppte89B56DOvmgHCo2hgXsLRLagGIjVDL2/cMaqDVIimnDwty0Q OzvTH8k8l8lXX2ua1mUwQUNHo4oKGrZ4oWRznbo3Y7A4QpsCm6/1RH5als7EF2H7NeCUX1 cb+uz1IzLQ34yhDS05YeZ3iMPjSTIBGdT/Wzz/ZFkHv0YWJPkTe4wiFzKjeW4A== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4gwx2f31Wnz19S5 for ; Thu, 09 Jul 2026 13:41:46 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 1cae7 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Thu, 09 Jul 2026 13:41:46 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 7bf11a2f0c9a - main - 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/main X-Git-Reftype: branch X-Git-Commit: 7bf11a2f0c9ad7af00996105fd34e61f1040402b Auto-Submitted: auto-generated Date: Thu, 09 Jul 2026 13:41:46 +0000 Message-Id: <6a4fa51a.1cae7.38844096@gitrepo.freebsd.org> The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=7bf11a2f0c9ad7af00996105fd34e61f1040402b commit 7bf11a2f0c9ad7af00996105fd34e61f1040402b Author: Mark Johnston AuthorDate: 2026-07-08 17:13:01 +0000 Commit: Mark Johnston CommitDate: 2026-07-09 13:41:27 +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 --- 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);