From nobody Tue Aug 01 21:06:50 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 4RFnjT0Bl1z4prf6; Tue, 1 Aug 2023 21:06:52 +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 4RFnjQ2tpHz3LlC; Tue, 1 Aug 2023 21:06:50 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1690924010; 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=U5S/c9yBJ79ZmdPBsedr2YTR/ZFdhBibKSi1/QSD3qk=; b=pLzIQnuNzfSWUtBjqPd6aghRkIauncnyDQ4IqtP5k74TloWMDsfeMg9o8jWEU1exPfHYlL dYKFXma44RlSlSpGtzlMj5ylUh06SpiDONNxsQudBF0kD/IHlM9iEuRT1Yxs7ubHnsu2Iv NanorQVobaNCGx3IPgP6uFg82NG18dCzrEmq2uvZ/ktEYdi6ihtrBi1APGnOpKDHDLu6if ToZnXL9l/2zreA5JMDJc/KcEoL9iUn2XukDWWBcrVZA1I08R/X3iaprIFlLXJjOAcfluSW B1rtgdWet/rX52yauaebWBKo8x+jhWb224HzilGWqqT8Goc+1g7626gDK11HOQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1690924010; a=rsa-sha256; cv=none; b=WGr6Iep4JPjlTpP8PE6fo+dyXcF4BKcQkoQtVKK8e4+NJ/9GYNIbJh+FGioRo9JEavN68H yXBcGcbQWtM7eblC/e8jgj75XwytyzwvvQcahXgAVlqtheZMsJPL0kiggxc0f/HqxsJTRy I+/4nbA1Q8240ygvP6P2qRtGJKgyJhm2c7iN+gkysBTyD2W/B2E6KwXkz6KS2utSHsiBq7 +5pouUabGcg4AL7it3IuPf+doQj0c73nrmhG5JCY+RuSpPhtlOzRmu1iaLioFqemK4mAuA NhTw+hMO2iV3BrHID6pq8LbstYiYZPw+aHNQobfyXT8+tgWLHHztrQyy/zRDyw== 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=1690924010; 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=U5S/c9yBJ79ZmdPBsedr2YTR/ZFdhBibKSi1/QSD3qk=; b=FteO4Q7JhQwpEZXk+DszIgbkZmtfQEn//NZkdIjG/f5ebMmD71mI2l2EerEYDGWl9bny6C lt6FUy9Z4UG2lXk+trey3DXhTi9Rmn1/r99yNUM2DS5+L7H1svnIiwQvgP+wrT/yPj2N/e 2pZ/qehPRnpzpYoJdb+pBDjKusXzIm0GgI5EFlKzx2ha3R0kgc42Kuk2DeopUYGch9rsIt LHMKrKe6Qeo/gr1OmkreZqepsBS0tdGqFKPmK8ec2nJFUJQJXm4u3Fjq/rFVxKMauNRbF6 ZTxLCEOETJKkFDO6HQ0UuefOuw7lqyVYOh1k/kR6VF61u7hSH1NNDdEfY90eBA== 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 4RFnjQ1Lhwzsq5; Tue, 1 Aug 2023 21:06:50 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 371L6oFe013889; Tue, 1 Aug 2023 21:06:50 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 371L6o9B013888; Tue, 1 Aug 2023 21:06:50 GMT (envelope-from git) Date: Tue, 1 Aug 2023 21:06:50 GMT Message-Id: <202308012106.371L6o9B013888@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Jessica Clarke Subject: git: 051f41ddb517 - stable/13 - libpmc: Handle PMCALLOCATE log with PMC code on PMU event system 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: jrtc27 X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 051f41ddb517a9d3f6872678ccc3d0b6c0fffca1 Auto-Submitted: auto-generated The branch stable/13 has been updated by jrtc27: URL: https://cgit.FreeBSD.org/src/commit/?id=051f41ddb517a9d3f6872678ccc3d0b6c0fffca1 commit 051f41ddb517a9d3f6872678ccc3d0b6c0fffca1 Author: Jessica Clarke AuthorDate: 2023-06-07 14:21:18 +0000 Commit: Jessica Clarke CommitDate: 2023-08-01 20:42:53 +0000 libpmc: Handle PMCALLOCATE log with PMC code on PMU event system On an arm64 system that reports as a Cortex A72 r0p3, running pmcstat -P CPU_CYCLES command works, but pmcstat -P cpu-cycles command does not. This is because the former uses the PMU event from the JSON source, resulting in pl_event in the log event being a small index (here, 5) into the generated events table, whilst the latter does not match any of the JSON events and falls back on PMC's own tables, mapping it to the PMC event 0x14111, i.e. PMC_EV_ARMV8_EVENT_11H. Then, when libpmc gets the PMCALLOCATE event, it tries to use the event as an index into the JSON-derived table, but doing so only makes sense for the former, whilst for the latter it will go way out of bounds and either read junk (which may trigger the != NULL assertion) or segfault. As far as I can tell we don't have anything lying around to tell us which of the two cases we're in, but we can exploit the fact that the first 0x1000 PMC event codes are reserved, and that none of our PMU events tables reach that number of entries yet. PR: 268857 Reviewed by: mhorne MFC after: 1 month Differential Revision: https://reviews.freebsd.org/D39592 (cherry picked from commit 21f7397a61f7bff61a1221cc6340cd980a922540) --- lib/libpmc/libpmc.c | 9 ++++++++- lib/libpmc/pmclog.c | 27 +++++++++++++++++++++------ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/lib/libpmc/libpmc.c b/lib/libpmc/libpmc.c index dae3b4fb2d67..10a0aec03cc7 100644 --- a/lib/libpmc/libpmc.c +++ b/lib/libpmc/libpmc.c @@ -35,6 +35,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include #include #include @@ -1014,8 +1015,14 @@ pmc_allocate(const char *ctrspec, enum pmc_mode mode, r = spec_copy = strdup(ctrspec); ctrname = strsep(&r, ","); if (pmc_pmu_enabled()) { - if (pmc_pmu_pmcallocate(ctrname, &pmc_config) == 0) + if (pmc_pmu_pmcallocate(ctrname, &pmc_config) == 0) { + /* + * XXX: pmclog_get_event exploits this to disambiguate + * PMU from PMC event codes in PMCALLOCATE events. + */ + assert(pmc_config.pm_ev < PMC_EVENT_FIRST); goto found; + } /* Otherwise, reset any changes */ pmc_config.pm_ev = 0; diff --git a/lib/libpmc/pmclog.c b/lib/libpmc/pmclog.c index babcdc3c8d0d..b219ed53fe3f 100644 --- a/lib/libpmc/pmclog.c +++ b/lib/libpmc/pmclog.c @@ -356,12 +356,27 @@ pmclog_get_event(void *cookie, char **data, ssize_t *len, PMCLOG_READ32(le,ev->pl_u.pl_a.pl_flags); PMCLOG_READ32(le,noop); PMCLOG_READ64(le,ev->pl_u.pl_a.pl_rate); - ev->pl_u.pl_a.pl_evname = pmc_pmu_event_get_by_idx(ps->ps_cpuid, ev->pl_u.pl_a.pl_event); - if (ev->pl_u.pl_a.pl_evname != NULL) - break; - else if ((ev->pl_u.pl_a.pl_evname = - _pmc_name_of_event(ev->pl_u.pl_a.pl_event, ps->ps_arch)) - == NULL) { + + /* + * Could be either a PMC event code or a PMU event index; + * assume that their encodings don't overlap (i.e. no PMU event + * table is more than 0x1000 entries) to distinguish them here. + * Otherwise pmc_pmu_event_get_by_idx will go out of bounds if + * given a PMC event code when it knows about that CPU. + * + * XXX: Ideally we'd have user flags to give us that context. + */ + if (ev->pl_u.pl_a.pl_event < PMC_EVENT_FIRST) + ev->pl_u.pl_a.pl_evname = + pmc_pmu_event_get_by_idx(ps->ps_cpuid, + ev->pl_u.pl_a.pl_event); + else if (ev->pl_u.pl_a.pl_event <= PMC_EVENT_LAST) + ev->pl_u.pl_a.pl_evname = + _pmc_name_of_event(ev->pl_u.pl_a.pl_event, + ps->ps_arch); + else + ev->pl_u.pl_a.pl_evname = NULL; + if (ev->pl_u.pl_a.pl_evname == NULL) { printf("unknown event\n"); goto error; }