From nobody Wed Jun 14 16:46:42 2023 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 4QhBCQ5Ggkz4dHTQ; Wed, 14 Jun 2023 16:46:42 +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 4QhBCQ4stGz4PqK; Wed, 14 Jun 2023 16:46:42 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1686761202; 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=iJhM77plHcLCyzMbhkJ2VnI0fmKFS0xsHax+buv56uY=; b=EvCeZb8q9TyGBco1blf5+Zkb5wLgzfvNdTC5uVV4RO7cu1VrLMowhADsGVDdjxDirKt8to s0pVfUHb6CTP4+YB7blLeKb315DE7U0ALfCZJxVzKfcIpKeov/JDjRDV+KF0aKuAahnL09 3/bVLRf05HSHOCW0HDl9EiSwlaw6rBfncZYWnVYW1N0UHTqItgVoEZS+Q3M0L+NlPj6hQD WFVYHHxuZ9YOq6wu2TOamqT99TzrcPEsbjJBvQvgUrhl91xpzDK1okkc6J2tATqW4GCtdH TbBBasOwqNVNTtuNR1hqJMlUxAIGS4rcRO9dNbsqra6l1ES6ghafZc/0XEGjRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1686761202; 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=iJhM77plHcLCyzMbhkJ2VnI0fmKFS0xsHax+buv56uY=; b=jVa3mMn/0AM4ZZzgZ3pPgEl45mBC62RfEVelOIeC/saqdm7Eyug9OTerApd+XbEshqVTDP vI2C8hiK9/i2ugIfLwwM/oblPMnFqULfbA7MOkKBycNGKpuCD4L/quhw9HeWTcguSKF4js F5fEWeneECY87O33ATdNsqcYXwX5LTa8r09GKyXed9nzKJBZaH2Z2Fg/7jOoBHhN95zRH1 tSLKEWTN7krJojrJpIkl0FiiW08487C/q6TCapcZJjRDxEewOfrrB5CrsEY6rCPj0mcUmk RuvmVrEp+G7c5Wc2Te/Yzl7z2wNGvtXt1W2Zt8qAO8+rbbJUVhSm6gztvox03g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1686761202; a=rsa-sha256; cv=none; b=k0hSyas4CNdX2HjuKXalFeZdevYLQJSN5ObhUdpKDfMtzfwSgQdoAgeFWHf6GWkKQaq61M cz8bYaYGfJjxrZspu9S4xpyWOXvi0R0MFDlMgirOen9qsKsC53tBzn7sSQGQIehTXcOIf0 M7SqrYXwDXwL4Jke01Y21ghiGwukjv0zgkf8gsw1aZH4j69vc7Usa2qHnVMvOWXceYVlKp bB5cjCF/tTWC9QgZNYAzw5/OfTZwHhijbu44sRUzt2g4c+VLOPPztTnkzR1XQneEu/rUaG i5tb7oiEVVHgYNiMmSBrUTLy7JIo5qu4eiILDUY+y1RjokMNc6Xy0oQ7OnPp5w== 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 4QhBCQ3v37z1BDS; Wed, 14 Jun 2023 16:46:42 +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 35EGkgTX028427; Wed, 14 Jun 2023 16:46:42 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 35EGkgSw028426; Wed, 14 Jun 2023 16:46:42 GMT (envelope-from git) Date: Wed, 14 Jun 2023 16:46:42 GMT Message-Id: <202306141646.35EGkgSw028426@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: a133f64239cd - main - hwpmc: split out PMC_OP_PMCATTACH/PMC_OP_PMCDETACH 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: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@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: a133f64239cdbefa374cf6df134f3581f9b9f147 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by mhorne: URL: https://cgit.FreeBSD.org/src/commit/?id=a133f64239cdbefa374cf6df134f3581f9b9f147 commit a133f64239cdbefa374cf6df134f3581f9b9f147 Author: Mitchell Horne AuthorDate: 2023-06-14 16:32:11 +0000 Commit: Mitchell Horne CommitDate: 2023-06-14 16:34:21 +0000 hwpmc: split out PMC_OP_PMCATTACH/PMC_OP_PMCDETACH Begin splitting out the large chunks of functional logic from the syscall handler into separate helper functions. This keeps it separate from the syscall control-flow logic, resulting in better readability overall. It also wins back a level of indentation. For this and the similar changes to follow, try to keep copyin() and copyout() calls outside of the helper functions. The changes are intended to have no functional impact, but do address style issues. Reviewed by: jkoshy MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D40291 --- sys/dev/hwpmc/hwpmc_mod.c | 190 +++++++++++++++++++++++++--------------------- 1 file changed, 103 insertions(+), 87 deletions(-) diff --git a/sys/dev/hwpmc/hwpmc_mod.c b/sys/dev/hwpmc/hwpmc_mod.c index 987184b7fae8..24cd3ba0fc0f 100644 --- a/sys/dev/hwpmc/hwpmc_mod.c +++ b/sys/dev/hwpmc/hwpmc_mod.c @@ -3282,6 +3282,103 @@ static const char *pmc_op_to_name[] = { is_sx_downgraded = true; \ } while (0) +/* + * Main body of PMC_OP_PMCATTACH. + */ +static int +pmc_do_op_pmcattach(struct thread *td, struct pmc_op_pmcattach a) +{ + struct pmc *pm; + struct proc *p; + int error; + + sx_assert(&pmc_sx, SX_XLOCKED); + + if (a.pm_pid < 0) { + return (EINVAL); + } else if (a.pm_pid == 0) { + a.pm_pid = td->td_proc->p_pid; + } + + error = pmc_find_pmc(a.pm_pmc, &pm); + if (error != 0) + return (error); + + if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm))) + return (EINVAL); + + /* PMCs may be (re)attached only when allocated or stopped */ + if (pm->pm_state == PMC_STATE_RUNNING) { + return (EBUSY); + } else if (pm->pm_state != PMC_STATE_ALLOCATED && + pm->pm_state != PMC_STATE_STOPPED) { + return (EINVAL); + } + + /* lookup pid */ + if ((p = pfind(a.pm_pid)) == NULL) + return (ESRCH); + + /* + * Ignore processes that are working on exiting. + */ + if ((p->p_flag & P_WEXIT) != 0) { + PROC_UNLOCK(p); /* pfind() returns a locked process */ + return (ESRCH); + } + + /* + * We are allowed to attach a PMC to a process if we can debug it. + */ + error = p_candebug(curthread, p); + + PROC_UNLOCK(p); + + if (error == 0) + error = pmc_attach_process(p, pm); + + return (error); +} + +/* + * Main body of PMC_OP_PMCDETACH. + */ +static int +pmc_do_op_pmcdetach(struct thread *td, struct pmc_op_pmcattach a) +{ + struct pmc *pm; + struct proc *p; + int error; + + if (a.pm_pid < 0) { + return (EINVAL); + } else if (a.pm_pid == 0) + a.pm_pid = td->td_proc->p_pid; + + error = pmc_find_pmc(a.pm_pmc, &pm); + if (error != 0) + return (error); + + if ((p = pfind(a.pm_pid)) == NULL) + return (ESRCH); + + /* + * Treat processes that are in the process of exiting as if they were + * not present. + */ + if ((p->p_flag & P_WEXIT) != 0) { + PROC_UNLOCK(p); + return (ESRCH); + } + + PROC_UNLOCK(p); /* pfind() returns a locked process */ + + if (error == 0) + error = pmc_detach_process(p, pm); + + return (error); +} + static int pmc_syscall_handler(struct thread *td, void *syscall_args) { @@ -4004,114 +4101,33 @@ pmc_syscall_handler(struct thread *td, void *syscall_args) } break; - /* * Attach a PMC to a process. */ - case PMC_OP_PMCATTACH: { - struct pmc *pm; - struct proc *p; struct pmc_op_pmcattach a; - sx_assert(&pmc_sx, SX_XLOCKED); - - if ((error = copyin(arg, &a, sizeof(a))) != 0) - break; - - if (a.pm_pid < 0) { - error = EINVAL; - break; - } else if (a.pm_pid == 0) - a.pm_pid = td->td_proc->p_pid; - - if ((error = pmc_find_pmc(a.pm_pmc, &pm)) != 0) - break; - - if (PMC_IS_SYSTEM_MODE(PMC_TO_MODE(pm))) { - error = EINVAL; - break; - } - - /* PMCs may be (re)attached only when allocated or stopped */ - if (pm->pm_state == PMC_STATE_RUNNING) { - error = EBUSY; - break; - } else if (pm->pm_state != PMC_STATE_ALLOCATED && - pm->pm_state != PMC_STATE_STOPPED) { - error = EINVAL; - break; - } - - /* lookup pid */ - if ((p = pfind(a.pm_pid)) == NULL) { - error = ESRCH; - break; - } - - /* - * Ignore processes that are working on exiting. - */ - if (p->p_flag & P_WEXIT) { - error = ESRCH; - PROC_UNLOCK(p); /* pfind() returns a locked process */ + error = copyin(arg, &a, sizeof(a)); + if (error != 0) break; - } - /* - * we are allowed to attach a PMC to a process if - * we can debug it. - */ - error = p_candebug(curthread, p); - - PROC_UNLOCK(p); - - if (error == 0) - error = pmc_attach_process(p, pm); + error = pmc_do_op_pmcattach(td, a); } break; - /* * Detach an attached PMC from a process. */ - case PMC_OP_PMCDETACH: { - struct pmc *pm; - struct proc *p; struct pmc_op_pmcattach a; - if ((error = copyin(arg, &a, sizeof(a))) != 0) - break; - - if (a.pm_pid < 0) { - error = EINVAL; - break; - } else if (a.pm_pid == 0) - a.pm_pid = td->td_proc->p_pid; - - if ((error = pmc_find_pmc(a.pm_pmc, &pm)) != 0) - break; - - if ((p = pfind(a.pm_pid)) == NULL) { - error = ESRCH; + error = copyin(arg, &a, sizeof(a)); + if (error != 0) break; - } - /* - * Treat processes that are in the process of exiting - * as if they were not present. - */ - - if (p->p_flag & P_WEXIT) - error = ESRCH; - - PROC_UNLOCK(p); /* pfind() returns a locked process */ - - if (error == 0) - error = pmc_detach_process(p, pm); + error = pmc_do_op_pmcdetach(td, a); } break;