From nobody Wed Oct 16 17:51:09 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 4XTJRf1hRtz5YnZM; Wed, 16 Oct 2024 17:51:10 +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 4XTJRf14G7z4pYJ; Wed, 16 Oct 2024 17:51:10 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729101070; 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=Kgb9JRLbZbiTJuLP2DF0B0DPFmK120+qp9kAGDWw00k=; b=v3+BbQVupT6JeHxC2MxGxEZ6FAJkI0kRwOQ+Zy9MO3etkV2HuGi6OIwmTOZ/JKlwf3wuTb AQ2zuYK+EMI3VM70jxea75tbxlmFqgBORCqai4Is+XxrRVtQHKqxgkx7QUhcU9r2sKS+sX ZEFBeLSpZqoYfhOjm/Qd6uhs3MErD4JX22fDR36g/bw2xUv7bxHfT394zDdUz55rq8MJQx V80xodtc0f562gV13CvPiBOOjwHVExXsbFUKszQbx7Xxr0Jtkk8/cJEXFnJjo5RI7D2POl SE6qkIiX5p73/LigAQm4OsM/nt7RGw5cVwAuP7c4+ysl4akoeyu1w4YOeIg/mA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729101070; 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=Kgb9JRLbZbiTJuLP2DF0B0DPFmK120+qp9kAGDWw00k=; b=kCmFv59Bk2RQIFxddVkimVf+EDpE2l0P4eKKG6wJsE7xPqYEtmc0szPSil8DB2RcJ3tfeu 2sMXgOeSWlXS3TirWX0RH4qvxxifd7zSuckBQ/pjsAUiW46szKbpo/oDYyhbtRcmqzvI+c LTILbyXf/4CgdDUgFqZOV0JzatuDskJ1JmNDF5WWcfEww3jvLOsBmtndNIsju80RtwQ2hY Lj59Ah+Hmpimk3L21w/ZaGOBcHRlyG8QrqBJTz4y3MKV/B1AE1UnexkLpdYjUvN9uFlln4 v+OeKx98A/CNTaX1bvIeK+K24Hi5MLDys1KFwy0I7MoXupCwa3fLroR3DmDNHQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1729101070; a=rsa-sha256; cv=none; b=VSvm2v/YdP0RkNFYmuArR9idyOZnqOVd6KlcmfjgYJRewpW3TQhtD9V85Go+2RzbcFFN+B vftz/GztYBYSckeyPNiBYetlmd6hatOhmM3UoU00rt6JqAx2PWrYcenbRK/QRvet6z9jGJ Ejtlst6b2vpeNvwDeYOoE94SIFQLxnfOXiiAi6b/QzuXO7B117ZehBF84YEfQZDQwmz3uj 7fEHY/nC0M9GPVRG/OtaDM25lGJWO3OEYvSoAU9xGCC5xPn/yS5JwWmX1A1IBSTAKbz5Z2 wLfyzOYy2czk7o0iiDpeW70rTErnT9Yt2LnQFXm4DQTeWe4fCzWkLAk4f7eN4Q== 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 4XTJRf0hSbzFYV; Wed, 16 Oct 2024 17:51:10 +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 49GHp9Ca024528; Wed, 16 Oct 2024 17:51:09 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 49GHp9AP024525; Wed, 16 Oct 2024 17:51:09 GMT (envelope-from git) Date: Wed, 16 Oct 2024 17:51:09 GMT Message-Id: <202410161751.49GHp9AP024525@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: John Baldwin Subject: git: 47f49dd4bbb4 - main - sdt: Tear down probes in kernel modules during kldunload 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: jhb X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 47f49dd4bbb4a72e53d31046964ce3c111ee0d12 Auto-Submitted: auto-generated The branch main has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=47f49dd4bbb4a72e53d31046964ce3c111ee0d12 commit 47f49dd4bbb4a72e53d31046964ce3c111ee0d12 Author: John Baldwin AuthorDate: 2024-10-16 17:50:37 +0000 Commit: John Baldwin CommitDate: 2024-10-16 17:50:37 +0000 sdt: Tear down probes in kernel modules during kldunload Previously only providers in kernel modules were removed leaving dangling pointers to tracepoints, etc. in unloaded kernel modules. PR: 281825 Reported by: Sony Arpita Das Reviewed by: markj Fixes: ddf0ed09bd8f sdt: Implement SDT probes using hot-patching Sponsored by: Chelsio Communications Differential Revision: https://reviews.freebsd.org/D46890 --- sys/cddl/dev/sdt/sdt.c | 111 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 102 insertions(+), 9 deletions(-) diff --git a/sys/cddl/dev/sdt/sdt.c b/sys/cddl/dev/sdt/sdt.c index 88ceb390876b..a8da618204af 100644 --- a/sys/cddl/dev/sdt/sdt.c +++ b/sys/cddl/dev/sdt/sdt.c @@ -429,18 +429,15 @@ sdt_kld_load(void *arg __unused, struct linker_file *lf) sdt_kld_load_probes(lf); } -static void -sdt_kld_unload_try(void *arg __unused, struct linker_file *lf, int *error) +static bool +sdt_kld_unload_providers(struct linker_file *lf) { struct sdt_provider *prov, **curr, **begin, **end, *tmp; - if (*error != 0) - /* We already have an error, so don't do anything. */ - return; - else if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end, + if (linker_file_lookup_set(lf, "sdt_providers_set", &begin, &end, NULL)) /* No DTrace providers are declared in this file. */ - return; + return (true); /* * Go through all the providers declared in this linker file and @@ -453,8 +450,7 @@ sdt_kld_unload_try(void *arg __unused, struct linker_file *lf, int *error) if (prov->sdt_refs == 1) { if (dtrace_unregister(prov->id) != 0) { - *error = 1; - return; + return (false); } TAILQ_REMOVE(&sdt_prov_list, prov, prov_entry); free(prov->name, M_SDT); @@ -464,6 +460,103 @@ sdt_kld_unload_try(void *arg __unused, struct linker_file *lf, int *error) break; } } + + return (true); +} + +static bool +sdt_kld_unload_probes(struct linker_file *lf) +{ + struct sdt_probe **p_begin, **p_end; + struct sdt_argtype **a_begin, **a_end; + struct sdt_tracepoint *tp_begin, *tp_end; + + if (linker_file_lookup_set(lf, __XSTRING(_SDT_TRACEPOINT_SET), + &tp_begin, &tp_end, NULL) == 0) { + for (struct sdt_tracepoint *tp = tp_begin; tp < tp_end; tp++) { + struct sdt_tracepoint *tp2; + + if (!sdt_tracepoint_valid(tp->patchpoint, tp->target)) + continue; + + /* Only remove the entry if it is in the list. */ + tp2 = STAILQ_FIRST(&tp->probe->tracepoint_list); + if (tp2 == tp) { + STAILQ_REMOVE_HEAD(&tp->probe->tracepoint_list, + tracepoint_entry); + } else if (tp2 != NULL) { + struct sdt_tracepoint *tp3; + + for (;;) { + tp3 = STAILQ_NEXT(tp2, + tracepoint_entry); + if (tp3 == NULL) + break; + if (tp3 == tp) { + STAILQ_REMOVE_AFTER( + &tp->probe->tracepoint_list, + tp2, tracepoint_entry); + break; + } + tp2 = tp3; + } + } + } + } + + if (linker_file_lookup_set(lf, "sdt_argtypes_set", &a_begin, &a_end, + NULL) == 0) { + for (struct sdt_argtype **argtype = a_begin; argtype < a_end; + argtype++) { + struct sdt_argtype *argtype2; + + /* Only remove the entry if it is in the list. */ + TAILQ_FOREACH(argtype2, + &(*argtype)->probe->argtype_list, argtype_entry) { + if (argtype2 == *argtype) { + (*argtype)->probe->n_args--; + TAILQ_REMOVE( + &(*argtype)->probe->argtype_list, + *argtype, argtype_entry); + break; + } + } + } + } + + if (linker_file_lookup_set(lf, "sdt_probes_set", &p_begin, &p_end, + NULL) == 0) { + for (struct sdt_probe **probe = p_begin; probe < p_end; + probe++) { + if ((*probe)->sdtp_lf == lf) { + if (!TAILQ_EMPTY(&(*probe)->argtype_list)) + return (false); + if (!STAILQ_EMPTY(&(*probe)->tracepoint_list)) + return (false); + + /* + * Don't destroy the probe as there + * might be multiple instances of the + * same probe in different modules. + */ + } + } + } + + return (true); +} + +static void +sdt_kld_unload_try(void *arg __unused, struct linker_file *lf, int *error) +{ + if (*error != 0) + /* We already have an error, so don't do anything. */ + return; + + if (!sdt_kld_unload_probes(lf)) + *error = 1; + else if (!sdt_kld_unload_providers(lf)) + *error = 1; } static int