From nobody Tue Aug 20 14:05:34 2024 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 4WpB7f4FRRz5TBxX; Tue, 20 Aug 2024 14:05:34 +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 4WpB7f2Jsyz4gVY; Tue, 20 Aug 2024 14:05:34 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1724162734; 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=V9G5g8+HH7U+4AroY6b9LJ7v0jPqom5Cbe37JJIEFE8=; b=c8DWGTZze00XCb8I+VQvGMV21WG3bfE0cnPrKY2Vue9q3STPeBv8WkIxsiHAzIp/rqRnbE qf3OMIVn6sO2nKSsJGlTMMffxPQIQQAeplzzvzbxTePnYaHa7hYM4S8H8xTKDYCPqL28yr TJqlJld7WJD43l057wVpX10pNnsJZnEE+qLgw8N5L5ZDlO5/G3Vd127jWIc6kgXK8pBzGa i/Edmr4cGPTjIZVCE0gCqcNUQEYj3Ahk6Y+eY47NWSz92xfyGo00DbO4c/l66F0dtG88k/ 3g/QX1eUzX8f9tcoDx0Su/okC1Q+5PCehwNz5iq7/vHKj0Iu9hFY8ZyS4rgrRg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1724162734; a=rsa-sha256; cv=none; b=r/005KHJqUAD+HIirVy7RsnXcMlvC+sc5WWtBiXL4BCNRZlY19VwuAIgP3YqlMnJae5pzK WNUYCkBSDZGG1c1dqq+pD3JmHjUkJ/gDvnjbzUyudAGxmo8+eModB3OiFv8FWjXYH/rlFR uTWiVK6hHZbt/zXKzabjsMnBlonUQ1FxkoRrRPokYe76COIO8707Fjzwbhu90EHfQzVy3j BQuYrMRB/34c3FUMO3blnnnk1iscotJdZ7OkWyXG7MtjevN/GnapFWQ7fgnJzjSmbL4Aqm EviRMsazMaiQqzJzbCxW9xlZJK3zklX6YyvYRVL0cIkKINdY1wOWsy7LDGzaWg== 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=1724162734; 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=V9G5g8+HH7U+4AroY6b9LJ7v0jPqom5Cbe37JJIEFE8=; b=ff1TJ7TsoKFrN4fgJzDSB3AXTGjfPtoNbrlMbC+4rOmgeuZi/UvppwozO+UZoPqiCs7x+o e0CnFm7Cd0/tJ9kXW00rIDxM1APXBbFaV2w112gO7kahSdW5esXDO2fhvwbK45jdqGiHc+ e8sswBj87TDaabaLVyIPCU4Jtqc+t/ABSuJKEiQ4FYbCCBTvrTF+ZUpiFzsBo5oxKrMiVH +dj40V9BZhsvLlFz5J2b/vo+8fV5x52eBQaHDWaS2Q2NpIN6cWGJPTaTKsbu/t7UMhIqR9 4THLHPEn9gE1r9dIQItwkO7r6QXxXVYACZvGe0gRZDQyZwOC1fS3eZkUCNJXsQ== 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 4WpB7f1wxhz13Mv; Tue, 20 Aug 2024 14:05:34 +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 47KE5Y0o091243; Tue, 20 Aug 2024 14:05:34 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 47KE5Y3F091240; Tue, 20 Aug 2024 14:05:34 GMT (envelope-from git) Date: Tue, 20 Aug 2024 14:05:34 GMT Message-Id: <202408201405.47KE5Y3F091240@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: 04716d51ba5b - stable/14 - ithread: Improve synchronization in ithread_destroy() 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 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: 04716d51ba5bb3b78f244d4acc6b67d933d2a0f1 Auto-Submitted: auto-generated The branch stable/14 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=04716d51ba5bb3b78f244d4acc6b67d933d2a0f1 commit 04716d51ba5bb3b78f244d4acc6b67d933d2a0f1 Author: Mark Johnston AuthorDate: 2024-07-30 14:36:54 +0000 Commit: Mark Johnston CommitDate: 2024-08-20 13:27:05 +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 (cherry picked from commit 8381e9f49ec733437754a822ef2e8344115289ac) --- 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 2e8ed6c5ece9..739b4ea4b2b5 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); } }