From nobody Tue Jul 30 14:39:33 2024 X-Original-To: dev-commits-src-main@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 4WYHtY6vKNz5S51G; Tue, 30 Jul 2024 14:39:33 +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 "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4WYHtY4nvVz5303; Tue, 30 Jul 2024 14:39:33 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1722350373; 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=2lXj28LtMNECdvdjmor8BlVn6Jx24VQgFkysJsyStQU=; b=qCwCBX2eeZRUexz3MnQ77aSEvMoxtSZs8tIuCnNICJdd/PWizW90wQjVjblbpN7dRdRaLP didCJAzUEEc8DOuy2e5DC67l6S11khfdhB5OfwYAcNnkzPWVYCmqIUB6dJ7pFobp4SJiL1 pEO6TWeFmfqR8TlYcT4c9FRd2t7peKb+QoSRFUWB+lH+nXypvANNiGtBQQuws4DiDGes2Y 2vGs4k6FrbJ77XgxPzzB9h3RI4bf6mPWoFA70w0RWTlwelhVHEQWBql8NvOLItwioS0bZt VesYOTzkGSP17zufj89OXdVcmCwRyTCo59nUc0rfWxP741gUJMU+ymi9RTp6Dg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1722350373; a=rsa-sha256; cv=none; b=PF/th4bcPLGjorZEKkOFBz0edFhlrAmI2w+5EdzNyrYkr9pCUtMUq8+fW1V0xCsTtnjka0 94hOBKQNjdEHMRCfCwUrmVRrqp5fyulgRJyyMNTETHKH0T7POaFXA1Tq1/RkKJYuQGwjh0 ZmwXFvniGkuWtfz506iUiyV0vqbtO13N5YG4hCG+R1qNifbnCXLOjR5McgbLiIRRBCX1Mx mSfFnjuM82I3XwnPq8YfXR7pptM2a9dz1JOl3CYLFFyZDM/GeOBSzPi36aatVgGXPVx3nd tWA6vacZVLn9giGBB+DdyVS5ETlrHcN5IssFDdIqNbZZLG1v+8yYhrnnqJWdMA== 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=1722350373; 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=2lXj28LtMNECdvdjmor8BlVn6Jx24VQgFkysJsyStQU=; b=QQDg1Dfx2YhYtIx/1fOJIPSY6SBXhZm+wKlsZZUen1TiguvCrbzZZki0oBknPjEnLoaMOx xBU1lUf1bHAg+S21vtXsGkGAUR7dBZlDUZgrHBs2YMCTv8Rjdl1Mc53UMZxcSSyqUdmC21 8EepwNGmmCxs/PDqm4Pq4uo3MdOKOt6fb234X64whtuu13n4OFLJAAZHC33KMMuXMFtnA0 Jp0DXxFVHZlr/lWW/yOhwU6C4j0L8OgHA00G3UUYoa9WHwWefDC8iRwlOAirjtJDRkQmFZ TpSpjU9UAnNAGKRzuGuGtHsPLlQQ94r4L5azS4cd6nPGe2rqQSNojEpzu4aewA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4WYHtY4QGyzM85; Tue, 30 Jul 2024 14:39:33 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 46UEdXZO046307; Tue, 30 Jul 2024 14:39:33 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 46UEdXOh046304; Tue, 30 Jul 2024 14:39:33 GMT (envelope-from git) Date: Tue, 30 Jul 2024 14:39:33 GMT Message-Id: <202407301439.46UEdXOh046304@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 8381e9f49ec7 - main - ithread: Improve synchronization in ithread_destroy() List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org 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: 8381e9f49ec733437754a822ef2e8344115289ac Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=8381e9f49ec733437754a822ef2e8344115289ac commit 8381e9f49ec733437754a822ef2e8344115289ac Author: Mark Johnston AuthorDate: 2024-07-30 14:36:54 +0000 Commit: Mark Johnston CommitDate: 2024-07-30 14:37:32 +0000 ithread: Improve synchronization in ithread_destroy() Previously, to destroy an ithread we would set IT_DEAD in its flags, and then wake it up if it wasn't already running. After doing this, intr_event_destroy() would free the intr_event structure. However, it did not wait for the ithread to exit, so it was possible for the ithread to access the intr_event after it was freed. This use-after-free happens readily when running the pf tests in parallel, since they frequently create and destroy VNET jails, and pf registers several VNET-local swi handlers. Fix the race by modifying ithread_destroy() to wait until the ithread has signaled that it is about to exit by setting ie->ie_thread = NULL. Existing callers of intr_event_destroy() are allowed to sleep. Reported by: KASAN Reviewed by: kib, jhb MFC after: 3 weeks Differential Revision: https://reviews.freebsd.org/D45492 --- sys/kern/kern_intr.c | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/sys/kern/kern_intr.c b/sys/kern/kern_intr.c index a4ec45d0c4f8..ad0cc135167e 100644 --- a/sys/kern/kern_intr.c +++ b/sys/kern/kern_intr.c @@ -541,14 +541,10 @@ intr_event_destroy(struct intr_event *ie) return (EBUSY); } TAILQ_REMOVE(&event_list, ie, ie_list); -#ifndef notyet - if (ie->ie_thread != NULL) { + mtx_unlock(&event_lock); + if (ie->ie_thread != NULL) ithread_destroy(ie->ie_thread); - ie->ie_thread = NULL; - } -#endif mtx_unlock(&ie->ie_lock); - mtx_unlock(&event_lock); mtx_destroy(&ie->ie_lock); free(ie, M_ITHREAD); return (0); @@ -581,10 +577,16 @@ ithread_create(const char *name) static void ithread_destroy(struct intr_thread *ithread) { + struct intr_event *ie; struct thread *td; - CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name); td = ithread->it_thread; + ie = ithread->it_event; + + mtx_assert(&ie->ie_lock, MA_OWNED); + + CTR2(KTR_INTR, "%s: killing %s", __func__, ie->ie_name); + thread_lock(td); ithread->it_flags |= IT_DEAD; if (TD_AWAITING_INTR(td)) { @@ -592,6 +594,8 @@ ithread_destroy(struct intr_thread *ithread) sched_wakeup(td, SRQ_INTR); } else thread_unlock(td); + while (ie->ie_thread != NULL) + msleep(ithread, &ie->ie_lock, 0, "ithd_dth", 0); } int @@ -1235,7 +1239,7 @@ ithread_loop(void *arg) struct intr_event *ie; struct thread *td; struct proc *p; - int wake, epoch_count; + int epoch_count; bool needs_epoch; td = curthread; @@ -1245,7 +1249,6 @@ ithread_loop(void *arg) ("%s: ithread and proc linkage out of sync", __func__)); ie = ithd->it_event; ie->ie_count = 0; - wake = 0; /* * As long as we have interrupts outstanding, go through the @@ -1255,9 +1258,14 @@ ithread_loop(void *arg) /* * If we are an orphaned thread, then just die. */ - if (ithd->it_flags & IT_DEAD) { + if (__predict_false((ithd->it_flags & IT_DEAD) != 0)) { CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__, p->p_pid, td->td_name); + mtx_lock(&ie->ie_lock); + ie->ie_thread = NULL; + wakeup(ithd); + mtx_unlock(&ie->ie_lock); + free(ithd, M_ITHREAD); kthread_exit(); } @@ -1302,17 +1310,12 @@ ithread_loop(void *arg) TD_SET_IWAIT(td); ie->ie_count = 0; mi_switch(SW_VOL | SWT_IWAIT); - } else { - if (ithd->it_flags & IT_WAIT) { - wake = 1; - ithd->it_flags &= ~IT_WAIT; - } + } else if ((ithd->it_flags & IT_WAIT) != 0) { + ithd->it_flags &= ~IT_WAIT; thread_unlock(td); - } - if (wake) { wakeup(ithd); - wake = 0; - } + } else + thread_unlock(td); } }