From nobody Wed Jun 14 16:46:41 2023 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 4QhBCP5TW2z4dHx9; Wed, 14 Jun 2023 16:46:41 +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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4QhBCP448Vz4PkW; Wed, 14 Jun 2023 16:46:41 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1686761201; 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=+VPHNm7ng0q1WSCK597DKT4TNzWkYOYA/r2BgqN0Kd0=; b=Jdu+JvAQwIUy9vium78LAmvfcbvbUsK1teitZPyVqkXd80QPuhjTJ0TtUstDvkKgGt9X2M 6ZdN2NvPYQrBk9FGVp3kgO5seorPu2B8SSkLs4Pp1dWWs8RBE2MYXpVSiqXSe+HXQdbK8G t4UkqK0nfehJr8X8tqPnOTJzse4jF3EvNRtU0fX7IiRZfv9xw7lUtXQPE3NpM3phAFZWXU /YCIYHRHhGXoZLyeLmHr0kv4v4rxLLB2zyOBWzi5IKjg4Exc6Sipsv4WaE5eos9Xj4iPud 6O2GjVDL3Y2scJcHJNllAQFvKYl4XFiVMjz71tlohz4gTrqVCEmvKCD/+t/afA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1686761201; 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=+VPHNm7ng0q1WSCK597DKT4TNzWkYOYA/r2BgqN0Kd0=; b=G8DcyGKVmPS0pq/Kgk4ZdG40Da3xlDhRvPWrFG7Tyl0gMi8eZAoLEhQww1IfvmfjMRFfxw 68zw/BFdbMLkTeRRbIGTZNuDL1f1l4Zxe8MvEFgjvTWgaQYi7kMDXFBVDWDCXIMhktQLYd Qgj+FpUtvkTaS4fAVOlfCNXfPbz9TK3NesdYza3TqlH0okKbsHVF/NbCZptK3iFnXhOmcX TwbvylnbzgtGYD7j11BtySGHWSFBltRB9P67TXw6791X48sQVyu0TqTj+fQ7uf8Qwfa0oI vsGGAhvJ6jYlZqywWjgFaWeGDs6TB3AxeD3hpYAek3vV7m/Mz6KxS7mBm4Uy6A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1686761201; a=rsa-sha256; cv=none; b=kPZTusFtl6crW7coE6XpYb7yOeDoxX/Z/EG45Pu+BN5zI59GRAB8riC5tnFJqF8LLzBFMh ksUCSR2k+mfoV7KFg5ZI8MW0Nyq61B8rjlZsCdnnUxxx7CeqbJalzoTmrt8F68tCAbEQSC s3yr38DyA5/7OCFO/f2aKhV1fF8mdpYn82aV6FEvyc5qc1G7JUsOJ/G+nFrFcjG9FJwOZp Oh6qdhtAxv0V+T7dJLnV7UIZH6tMyJnd5E5bW6qK4ebuqYTOYxQbGXPjAyfaaEYg4eb9kR TgBnByju7yZwlJzXwyw9XuU7JDHKthf2RPL6z5mG8ycigm3p8ufuN89lWB8q6w== 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 4QhBCP34rRz1B08; Wed, 14 Jun 2023 16:46:41 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 35EGkfC0028408; Wed, 14 Jun 2023 16:46:41 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 35EGkfMS028407; Wed, 14 Jun 2023 16:46:41 GMT (envelope-from git) Date: Wed, 14 Jun 2023 16:46:41 GMT Message-Id: <202306141646.35EGkfMS028407@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mitchell Horne Subject: git: c498169442f3 - main - hwpmc: split out PMC_FN_PROCESS_EXEC 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: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mhorne X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: c498169442f3ebf769eb0157260c592729e48dc1 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by mhorne: URL: https://cgit.FreeBSD.org/src/commit/?id=c498169442f3ebf769eb0157260c592729e48dc1 commit c498169442f3ebf769eb0157260c592729e48dc1 Author: Mitchell Horne AuthorDate: 2023-06-14 16:32:01 +0000 Commit: Mitchell Horne CommitDate: 2023-06-14 16:34:21 +0000 hwpmc: split out PMC_FN_PROCESS_EXEC Move the functionality into a separate helper function. All other actions in pmc_hook_handler() already have this. While here make some small style improvements. Restructure one for loop. Reviewed by: jkoshy MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D40333 --- sys/dev/hwpmc/hwpmc_mod.c | 232 ++++++++++++++++++++++++---------------------- 1 file changed, 120 insertions(+), 112 deletions(-) diff --git a/sys/dev/hwpmc/hwpmc_mod.c b/sys/dev/hwpmc/hwpmc_mod.c index e5d166866125..987184b7fae8 100644 --- a/sys/dev/hwpmc/hwpmc_mod.c +++ b/sys/dev/hwpmc/hwpmc_mod.c @@ -241,6 +241,8 @@ static void pmc_post_callchain_callback(void); static void pmc_process_allproc(struct pmc *pm); static void pmc_process_csw_in(struct thread *td); static void pmc_process_csw_out(struct thread *td); +static void pmc_process_exec(struct thread *td, + struct pmckern_procexec *pk); static void pmc_process_exit(void *arg, struct proc *p); static void pmc_process_fork(void *arg, struct proc *p1, struct proc *p2, int n); @@ -1318,6 +1320,122 @@ done: return (0); } +/* + * Handle events after an exec() for a process: + * - Inform log owners of the new exec() event + * - Release any PMCs owned by the process before the exec() + * - Detach PMCs from the target if required + */ +static void +pmc_process_exec(struct thread *td, struct pmckern_procexec *pk) +{ + struct pmc *pm; + struct pmc_owner *po; + struct pmc_process *pp; + struct proc *p; + char *fullpath, *freepath; + u_int ri; + bool is_using_hwpmcs; + + sx_assert(&pmc_sx, SX_XLOCKED); + + p = td->td_proc; + pmc_getfilename(p->p_textvp, &fullpath, &freepath); + + PMC_EPOCH_ENTER(); + /* Inform owners of SS mode PMCs of the exec event. */ + CK_LIST_FOREACH(po, &pmc_ss_owners, po_ssnext) { + if ((po->po_flags & PMC_PO_OWNS_LOGFILE) != 0) { + pmclog_process_procexec(po, PMC_ID_INVALID, p->p_pid, + pk->pm_baseaddr, pk->pm_dynaddr, fullpath); + } + } + PMC_EPOCH_EXIT(); + + PROC_LOCK(p); + is_using_hwpmcs = (p->p_flag & P_HWPMC) != 0; + PROC_UNLOCK(p); + + if (!is_using_hwpmcs) { + if (freepath != NULL) + free(freepath, M_TEMP); + return; + } + + /* + * PMCs are not inherited across an exec(): remove any PMCs that this + * process is the owner of. + */ + if ((po = pmc_find_owner_descriptor(p)) != NULL) { + pmc_remove_owner(po); + pmc_destroy_owner_descriptor(po); + } + + /* + * If the process being exec'ed is not the target of any PMC, we are + * done. + */ + if ((pp = pmc_find_process_descriptor(p, 0)) == NULL) { + if (freepath != NULL) + free(freepath, M_TEMP); + return; + } + + /* + * Log the exec event to all monitoring owners. Skip owners who have + * already received the event because they had system sampling PMCs + * active. + */ + for (ri = 0; ri < md->pmd_npmc; ri++) { + if ((pm = pp->pp_pmcs[ri].pp_pmc) == NULL) + continue; + + po = pm->pm_owner; + if (po->po_sscount == 0 && + (po->po_flags & PMC_PO_OWNS_LOGFILE) != 0) { + pmclog_process_procexec(po, pm->pm_id, p->p_pid, + pk->pm_baseaddr, pk->pm_dynaddr, fullpath); + } + } + + if (freepath != NULL) + free(freepath, M_TEMP); + + PMCDBG4(PRC,EXC,1, "exec proc=%p (%d, %s) cred-changed=%d", + p, p->p_pid, p->p_comm, pk->pm_credentialschanged); + + if (pk->pm_credentialschanged == 0) /* no change */ + return; + + /* + * If the newly exec()'ed process has a different credential + * than before, allow it to be the target of a PMC only if + * the PMC's owner has sufficient privilege. + */ + for (ri = 0; ri < md->pmd_npmc; ri++) { + if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) { + if (pmc_can_attach(pm, td->td_proc) != 0) { + pmc_detach_one_process(td->td_proc, pm, + PMC_FLAG_NONE); + } + } + } + + KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt <= md->pmd_npmc, + ("[pmc,%d] Illegal ref count %u on pp %p", __LINE__, + pp->pp_refcnt, pp)); + + /* + * If this process is no longer the target of any + * PMCs, we can remove the process entry and free + * up space. + */ + if (pp->pp_refcnt == 0) { + pmc_remove_process_descriptor(pp); + pmc_destroy_process_descriptor(pp); + } +} + /* * Thread context switch IN. */ @@ -2045,119 +2163,9 @@ pmc_hook_handler(struct thread *td, int function, void *arg) pmc_hooknames[function], arg); switch (function) { - - /* - * Process exec() - */ case PMC_FN_PROCESS_EXEC: - { - char *fullpath, *freepath; - unsigned int ri; - int is_using_hwpmcs; - struct pmc *pm; - struct proc *p; - struct pmc_owner *po; - struct pmc_process *pp; - struct pmckern_procexec *pk; - - sx_assert(&pmc_sx, SX_XLOCKED); - - p = td->td_proc; - pmc_getfilename(p->p_textvp, &fullpath, &freepath); - - pk = (struct pmckern_procexec *) arg; - - PMC_EPOCH_ENTER(); - /* Inform owners of SS mode PMCs of the exec event. */ - CK_LIST_FOREACH(po, &pmc_ss_owners, po_ssnext) - if (po->po_flags & PMC_PO_OWNS_LOGFILE) - pmclog_process_procexec(po, PMC_ID_INVALID, - p->p_pid, pk->pm_baseaddr, pk->pm_dynaddr, - fullpath); - PMC_EPOCH_EXIT(); - - PROC_LOCK(p); - is_using_hwpmcs = p->p_flag & P_HWPMC; - PROC_UNLOCK(p); - - if (!is_using_hwpmcs) { - if (freepath) - free(freepath, M_TEMP); - break; - } - - /* - * PMCs are not inherited across an exec(): remove any - * PMCs that this process is the owner of. - */ - - if ((po = pmc_find_owner_descriptor(p)) != NULL) { - pmc_remove_owner(po); - pmc_destroy_owner_descriptor(po); - } - - /* - * If the process being exec'ed is not the target of any - * PMC, we are done. - */ - if ((pp = pmc_find_process_descriptor(p, 0)) == NULL) { - if (freepath) - free(freepath, M_TEMP); - break; - } - - /* - * Log the exec event to all monitoring owners. Skip - * owners who have already received the event because - * they had system sampling PMCs active. - */ - for (ri = 0; ri < md->pmd_npmc; ri++) - if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) { - po = pm->pm_owner; - if (po->po_sscount == 0 && - po->po_flags & PMC_PO_OWNS_LOGFILE) - pmclog_process_procexec(po, pm->pm_id, - p->p_pid, pk->pm_baseaddr, - pk->pm_dynaddr, fullpath); - } - - if (freepath) - free(freepath, M_TEMP); - - - PMCDBG4(PRC,EXC,1, "exec proc=%p (%d, %s) cred-changed=%d", - p, p->p_pid, p->p_comm, pk->pm_credentialschanged); - - if (pk->pm_credentialschanged == 0) /* no change */ - break; - - /* - * If the newly exec()'ed process has a different credential - * than before, allow it to be the target of a PMC only if - * the PMC's owner has sufficient privilege. - */ - for (ri = 0; ri < md->pmd_npmc; ri++) - if ((pm = pp->pp_pmcs[ri].pp_pmc) != NULL) - if (pmc_can_attach(pm, td->td_proc) != 0) - pmc_detach_one_process(td->td_proc, - pm, PMC_FLAG_NONE); - - KASSERT(pp->pp_refcnt >= 0 && pp->pp_refcnt <= md->pmd_npmc, - ("[pmc,%d] Illegal ref count %u on pp %p", __LINE__, - pp->pp_refcnt, pp)); - - /* - * If this process is no longer the target of any - * PMCs, we can remove the process entry and free - * up space. - */ - if (pp->pp_refcnt == 0) { - pmc_remove_process_descriptor(pp); - pmc_destroy_process_descriptor(pp); - break; - } - } - break; + pmc_process_exec(td, (struct pmckern_procexec *)arg); + break; case PMC_FN_CSW_IN: pmc_process_csw_in(td);