From nobody Wed Aug 19 21:03:58 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 4hQJvy5JC6z6pfQ8 for ; Wed, 19 Aug 2026 21:03:58 +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 4hQJvy3n3hz3lWx for ; Wed, 19 Aug 2026 21:03:58 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1787173438; 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=yNojCnN5oh3NBEkuA7KmcDerptQDV5tqBL3YJrU0aY0=; b=XJ0kQziSxYEBJpsCDBV+SGzI6hdpqMivDP8JTmzsQ3dyOtST6c5UHp3oVybWGIQljgbsz0 dEnApapoBqmLYdveMXmbtrofshDAQnf3MOZe9dh53ANH//sZ5nQyE3PD3i8XONhqmFnLa9 /w1oCVpTXbHbwbDaDNPpAVJtkGm+MbdIRI07MbNGO1ZXlTf6vz3pL4xgPIdMcXdOvay5bn 9buma93cpwXSR4X79mnOK7bXrex/Cq2FxFvDwrhbki1TLCLKhLDUdFSiXHuThOh/uCqAW/ N+4Sg9mkqexakSEnSoDyMyTFlJjPAZKqzvMBdFpkKa7Ge0e7hahaoyr1C56j9g== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1787173438; a=rsa-sha256; cv=none; b=CWnBuNF2k/se/Qgwag1obVkPwvrUkb8thdjVkWLTbK34Efwl1TuWuaSglgAkap/np47A1Y xbW+ZRhdC2dBsYNNYD/6FKlX6pYUbP05cUk5WqP6EG+5eRdUxD5KXFTDJH5De1nPAWD8NV 4KU5WQyYrQe+z8uPZ8lAwDfn6cJqQzzJx06yTNIa2RWIvKJLJYkEam//kUr5YWBp1J/4eR r9j9pJKm6AxHfN1dQGV4NcFpAWwTK5faG1B0EG2GnpQbPPyhbKSCKMgJfncvghQrigoaxn JYptDFEQ0BviiF3z2C8dOa+/1f8AjIEJlWq5rmiveglu4f7vmwG4cM193Uku/g== 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=1787173438; 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=yNojCnN5oh3NBEkuA7KmcDerptQDV5tqBL3YJrU0aY0=; b=OceD0AyAdOaAmYDgYbiXmJpSIYlAFdDt5QauWBEuLQKruslAoQXe7to6GPPHUqaymJ1Nyc LpSNJdlFHsqKYFm7rHkjCNtdNBxPfITuVcow3Ampllfj2hdtu961dMnA9M8yKeFYY4cx8b fgiZzcET+XaS0R8wdIiftlzA42xFTavBBFE/KeSsgBT+GKEh9k1ENgHWOP1sNF8gVttLyD yEskk/Xy4POXxRviwor2xg2OdyuRfDEKup7frf9KC9TDTpNiXs/zn3Z7GtRmvSgw2EytsP VAW5RUNakWHNqe2G169z0zuycc5iV/KTbzTjXxtWVHT0e79D6NcfjfQm0MnkVg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4hQJvy2f2lznjt for ; Wed, 19 Aug 2026 21:03:58 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 3229d by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Wed, 19 Aug 2026 21:03:58 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: 8eef4664327f - stable/14 - 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/14 X-Git-Reftype: branch X-Git-Commit: 8eef4664327fcc2fe04eb74e03e2e793df90d5af Auto-Submitted: auto-generated Date: Wed, 19 Aug 2026 21:03:58 +0000 Message-Id: <6a861a3e.3229d.7b94385@gitrepo.freebsd.org> The branch stable/14 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=8eef4664327fcc2fe04eb74e03e2e793df90d5af commit 8eef4664327fcc2fe04eb74e03e2e793df90d5af Author: Mark Johnston AuthorDate: 2026-07-08 17:13:01 +0000 Commit: Mark Johnston CommitDate: 2026-08-19 18:08:59 +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 fb7953c54a63..84ccce9f77b2 100644 --- a/sys/kern/subr_epoch.c +++ b/sys/kern/subr_epoch.c @@ -987,30 +987,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);