From nobody Wed Jun 14 16:46:45 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 4QhBCV31Zdz4dHrv; Wed, 14 Jun 2023 16:46:46 +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 4QhBCV0LB2z4Q00; Wed, 14 Jun 2023 16:46:46 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1686761206; 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=9qmMPoyF9+k8xeO8H2g32ebFyW2pW09bxf1CLakLZGU=; b=t6Hz3GtAbF+Zr65IGhA1y9Mlyg7QUcuRaMBc3EYItis+Wcz/hfuNq1IbGylMbBSUG/gBzF tHxQs8XZpy+RklXLjw1S9SMthHS+Zq1QdYFKuBu5U9+8Dpq3wiCHVUjNCOM2JUJwpHykoO PcIRl73W7bb1BaDGWSfYtycoQBFgy+SC5hkiTBtrD92HzAY7Rr37ai9vAIZ55bmFBF2NOU YyxYhP4I7DajbHfLy/VQ6+SJG1RwW3Jt50jcuu5uuTiRTjnocgf/q3ZtL85m06PdcqeJFJ V+ecBVHnvVyag6i/lGWtHIoIpbR9jjPaVTGhw0l457CKYr3jkqG/onAM6INn1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1686761206; 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=9qmMPoyF9+k8xeO8H2g32ebFyW2pW09bxf1CLakLZGU=; b=Fy1P3SysQA0q+EoQQcop1XBZmktWR2LdGNdxqtFa6pMFziUgFeiXloCPq3TPbJp7c3Mjcq Wlmku6ZDFF80LC1azq3gE/HJntc6ev+Je3qP5Hi/hm2giw0sZ3qFLoqnWDu2RJgYxPrrcu qW+kQRFjqD4GXUXCBDsVRwzPcqG0fyG8p0EDxsj82qQeyfL+aayx171K8gvJmkl8RSx1Bu E+po2/W1XjjFECbKu3jzyMQyX/XcKjQ/qKhKC4PCBeGGLX5JdugsC8dDtbhia/xErghYGX /7WDFAykgPq4rTstMnMiy9k/9fwepBr4GHgOkPZmGGY6YOyjUPX5tSXTCgQvVQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1686761206; a=rsa-sha256; cv=none; b=hVCABvJPnAzNJCR+CLM/85vNK29gI6rtWeZda0yXxtTHoK934Ak1yND98s0tbCnUzOmQQR LiiFI6Pmtc7kRGN2wMoausTI/xt3TROezi0EV9BRmhsfSTiaBzo2Lrn+siPPtfAbbRa8Ie RxgwP/BNMwtQ2qUPdGJl0w9KHQTFbnOk8RV4+S26w3k6efDoMZTKbWdcptr+vm7llT+jkd Dtnv47WEH6m50aW7x+2S8revoEdRvRUAvdsMSE9Nco3T0VD0IUfQu8wQR68RqYmNekZjDd Z+WnxAdMnTfGSxXxyKRXoGwHe1TtITXg35e3jJ6nskrcUDHUkBv4+7YjF4NUMw== 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 4QhBCT6VKGz1BDT; Wed, 14 Jun 2023 16:46:45 +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 35EGkjdF028499; Wed, 14 Jun 2023 16:46:45 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 35EGkjrh028498; Wed, 14 Jun 2023 16:46:45 GMT (envelope-from git) Date: Wed, 14 Jun 2023 16:46:45 GMT Message-Id: <202306141646.35EGkjrh028498@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: 93fe5909babb - main - hwpmc: split out PMC_OP_PMCRW 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: 93fe5909babb0ef7dbe8f5e3e93d429500503704 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by mhorne: URL: https://cgit.FreeBSD.org/src/commit/?id=93fe5909babb0ef7dbe8f5e3e93d429500503704 commit 93fe5909babb0ef7dbe8f5e3e93d429500503704 Author: Mitchell Horne AuthorDate: 2023-06-14 16:33:41 +0000 Commit: Mitchell Horne CommitDate: 2023-06-14 16:34:21 +0000 hwpmc: split out PMC_OP_PMCRW Split out the functional logic from the syscall handler into a helper function. This keeps it separate from the syscall control-flow logic, resulting in better readability overall. It also wins back a level of indentation. Reviewed by: jkoshy MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D40294 --- sys/dev/hwpmc/hwpmc_mod.c | 229 +++++++++++++++++++++++----------------------- 1 file changed, 114 insertions(+), 115 deletions(-) diff --git a/sys/dev/hwpmc/hwpmc_mod.c b/sys/dev/hwpmc/hwpmc_mod.c index f9ea87769cae..b6e7baf1e7cd 100644 --- a/sys/dev/hwpmc/hwpmc_mod.c +++ b/sys/dev/hwpmc/hwpmc_mod.c @@ -3656,6 +3656,110 @@ pmc_do_op_pmcrelease(pmc_id_t pmcid) return (error); } +/* + * Main body of PMC_OP_PMCRW. + */ +static int +pmc_do_op_pmcrw(const struct pmc_op_pmcrw *prw, pmc_value_t *valp) +{ + struct pmc_binding pb; + struct pmc_classdep *pcd; + struct pmc *pm; + u_int cpu, ri, adjri; + int error; + + PMCDBG2(PMC,OPS,1, "rw id=%d flags=0x%x", prw->pm_pmcid, prw->pm_flags); + + /* Must have at least one flag set. */ + if ((prw->pm_flags & (PMC_F_OLDVALUE | PMC_F_NEWVALUE)) == 0) + return (EINVAL); + + /* Locate PMC descriptor. */ + error = pmc_find_pmc(prw->pm_pmcid, &pm); + if (error != 0) + return (error); + + /* Can't read a PMC that hasn't been started. */ + if (pm->pm_state != PMC_STATE_ALLOCATED && + pm->pm_state != PMC_STATE_STOPPED && + pm->pm_state != PMC_STATE_RUNNING) + return (EINVAL); + + /* Writing a new value is allowed only for 'STOPPED' PMCs. */ + if (pm->pm_state == PMC_STATE_RUNNING && + (prw->pm_flags & PMC_F_NEWVALUE) != 0) + return (EBUSY); + + if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm))) { + /* + * If this PMC is attached to its owner (i.e., the process + * requesting this operation) and is running, then attempt to + * get an upto-date reading from hardware for a READ. Writes + * are only allowed when the PMC is stopped, so only update the + * saved value field. + * + * If the PMC is not running, or is not attached to its owner, + * read/write to the savedvalue field. + */ + + ri = PMC_TO_ROWINDEX(pm); + pcd = pmc_ri_to_classdep(md, ri, &adjri); + + mtx_pool_lock_spin(pmc_mtxpool, pm); + cpu = curthread->td_oncpu; + + if ((prw->pm_flags & PMC_F_OLDVALUE) != 0) { + if ((pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) && + (pm->pm_state == PMC_STATE_RUNNING)) { + error = (*pcd->pcd_read_pmc)(cpu, adjri, pm, + valp); + } else { + *valp = pm->pm_gv.pm_savedvalue; + } + } + + if ((prw->pm_flags & PMC_F_NEWVALUE) != 0) + pm->pm_gv.pm_savedvalue = prw->pm_value; + + mtx_pool_unlock_spin(pmc_mtxpool, pm); + } else { /* System mode PMCs */ + cpu = PMC_TO_CPU(pm); + ri = PMC_TO_ROWINDEX(pm); + pcd = pmc_ri_to_classdep(md, ri, &adjri); + + if (!pmc_cpu_is_active(cpu)) + return (ENXIO); + + /* Move this thread to CPU 'cpu'. */ + pmc_save_cpu_binding(&pb); + pmc_select_cpu(cpu); + critical_enter(); + + /* Save old value. */ + if ((prw->pm_flags & PMC_F_OLDVALUE) != 0) + error = (*pcd->pcd_read_pmc)(cpu, adjri, pm, valp); + + /* Write out new value. */ + if (error == 0 && (prw->pm_flags & PMC_F_NEWVALUE) != 0) + error = (*pcd->pcd_write_pmc)(cpu, adjri, pm, + prw->pm_value); + + critical_exit(); + pmc_restore_cpu_binding(&pb); + if (error != 0) + return (error); + } + +#ifdef HWPMC_DEBUG + if ((prw->pm_flags & PMC_F_NEWVALUE) != 0) + PMCDBG3(PMC,OPS,2, "rw id=%d new %jx -> old %jx", + ri, prw->pm_value, *valp); + else + PMCDBG2(PMC,OPS,2, "rw id=%d -> old %jx", ri, *valp); +#endif + return (error); +} + static int pmc_syscall_handler(struct thread *td, void *syscall_args) { @@ -4253,136 +4357,31 @@ pmc_syscall_handler(struct thread *td, void *syscall_args) } break; - /* * Read and/or write a PMC. */ - case PMC_OP_PMCRW: { - int adjri; - struct pmc *pm; - uint32_t cpu, ri; - pmc_value_t oldvalue; - struct pmc_binding pb; struct pmc_op_pmcrw prw; - struct pmc_classdep *pcd; struct pmc_op_pmcrw *pprw; + pmc_value_t oldvalue; PMC_DOWNGRADE_SX(); - if ((error = copyin(arg, &prw, sizeof(prw))) != 0) - break; - - PMCDBG2(PMC,OPS,1, "rw id=%d flags=0x%x", prw.pm_pmcid, - prw.pm_flags); - - /* must have at least one flag set */ - if ((prw.pm_flags & (PMC_F_OLDVALUE|PMC_F_NEWVALUE)) == 0) { - error = EINVAL; - break; - } - - /* locate pmc descriptor */ - if ((error = pmc_find_pmc(prw.pm_pmcid, &pm)) != 0) + error = copyin(arg, &prw, sizeof(prw)); + if (error != 0) break; - /* Can't read a PMC that hasn't been started. */ - if (pm->pm_state != PMC_STATE_ALLOCATED && - pm->pm_state != PMC_STATE_STOPPED && - pm->pm_state != PMC_STATE_RUNNING) { - error = EINVAL; + error = pmc_do_op_pmcrw(&prw, &oldvalue); + if (error != 0) break; - } - /* writing a new value is allowed only for 'STOPPED' pmcs */ - if (pm->pm_state == PMC_STATE_RUNNING && - (prw.pm_flags & PMC_F_NEWVALUE)) { - error = EBUSY; - break; + /* Return old value if requested. */ + if ((prw.pm_flags & PMC_F_OLDVALUE) != 0) { + pprw = arg; + error = copyout(&oldvalue, &pprw->pm_value, + sizeof(prw.pm_value)); } - - if (PMC_IS_VIRTUAL_MODE(PMC_TO_MODE(pm))) { - - /* - * If this PMC is attached to its owner (i.e., - * the process requesting this operation) and - * is running, then attempt to get an - * upto-date reading from hardware for a READ. - * Writes are only allowed when the PMC is - * stopped, so only update the saved value - * field. - * - * If the PMC is not running, or is not - * attached to its owner, read/write to the - * savedvalue field. - */ - - ri = PMC_TO_ROWINDEX(pm); - pcd = pmc_ri_to_classdep(md, ri, &adjri); - - mtx_pool_lock_spin(pmc_mtxpool, pm); - cpu = curthread->td_oncpu; - - if (prw.pm_flags & PMC_F_OLDVALUE) { - if ((pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) && - (pm->pm_state == PMC_STATE_RUNNING)) - error = (*pcd->pcd_read_pmc)(cpu, adjri, - pm, &oldvalue); - else - oldvalue = pm->pm_gv.pm_savedvalue; - } - if (prw.pm_flags & PMC_F_NEWVALUE) - pm->pm_gv.pm_savedvalue = prw.pm_value; - - mtx_pool_unlock_spin(pmc_mtxpool, pm); - - } else { /* System mode PMCs */ - cpu = PMC_TO_CPU(pm); - ri = PMC_TO_ROWINDEX(pm); - pcd = pmc_ri_to_classdep(md, ri, &adjri); - - if (!pmc_cpu_is_active(cpu)) { - error = ENXIO; - break; - } - - /* move this thread to CPU 'cpu' */ - pmc_save_cpu_binding(&pb); - pmc_select_cpu(cpu); - - critical_enter(); - /* save old value */ - if (prw.pm_flags & PMC_F_OLDVALUE) { - if ((error = (*pcd->pcd_read_pmc)(cpu, adjri, - pm, &oldvalue))) - goto error; - } - /* write out new value */ - if (prw.pm_flags & PMC_F_NEWVALUE) - error = (*pcd->pcd_write_pmc)(cpu, adjri, pm, - prw.pm_value); - error: - critical_exit(); - pmc_restore_cpu_binding(&pb); - if (error) - break; - } - - pprw = (struct pmc_op_pmcrw *) arg; - -#ifdef HWPMC_DEBUG - if (prw.pm_flags & PMC_F_NEWVALUE) - PMCDBG3(PMC,OPS,2, "rw id=%d new %jx -> old %jx", - ri, prw.pm_value, oldvalue); - else if (prw.pm_flags & PMC_F_OLDVALUE) - PMCDBG2(PMC,OPS,2, "rw id=%d -> old %jx", ri, oldvalue); -#endif - /* return old value if requested */ - if (prw.pm_flags & PMC_F_OLDVALUE) - if ((error = copyout(&oldvalue, &pprw->pm_value, - sizeof(prw.pm_value)))) - break; } break;