From nobody Wed Jun 7 14:26:24 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 4QbqQn269Pz4ZrjD; Wed, 7 Jun 2023 14:26:25 +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 4QbqQn0kNdz4MH3; Wed, 7 Jun 2023 14:26:25 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1686147985; 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=Gy2veYEH4xLJbd1IfttNpO1qjtO+kWlZHYJZPJTSyhY=; b=XGayyQLWEuP5tkF4yy/WuGOd8sjvsy/N94P+jGqZp81EnVFJHbi/xlq4Zpfr6R0OrLVj+b PFaaE3zkuuERdf9OBgYF0UahY55or3aCvqn/QCDob/uFD7DAccbWwgq0t72+ZHN8l+ceUl +Vmzo/JF8MIG9blwDU+6BqviE5wCi9pmMqRJne1oTNtyguqI8pb+rPIR0Q2G9B4Hlneiuq VV+ocLuDs/WcQTFYH+kuMBHKwlD7hdT4kO6qKOVcDkHfJJN0UHQEY8abIKBEzYKIIOCrIQ GRoCGjM8RzVBsdzvVpHGqU7XmLTBIQK178Ivl78RrEL5povjJtfxDLasx+1jTw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1686147985; 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=Gy2veYEH4xLJbd1IfttNpO1qjtO+kWlZHYJZPJTSyhY=; b=T4cRHDjjX9n3PGWbhJUw58IZRm9wqDrbPn3WfttoeG3u5QcFpI0s55bpB1lZY4j085Ye71 imA3XJeEpTGlu+iSiTPzF5ww0ETHmdn//6osxlPo5j+BfkOiX6RAAbgxGfxuJrIlu9MvGN DOPcXA1c+MTKPj41AfePh0C0IXVjJe1U4k0Lguwvh5cvwF+qUIcrupLjHin4WQvCi4vECG 3R8eRcmJ+HLFY0uOb+wL55cnhFAt4VfDMQpY9PJKtKsLq/XIt6HO5FoVYMpeUN9a+poqMc Hd2/6vnaIGk1BHQice8Ks5GY9rvRm9zd4qc8Njp7m1SsgBcjLC18QdVaO+cyAA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1686147985; a=rsa-sha256; cv=none; b=gaVwipVxesimLuK5gSfaEqEuM1TxryUD94acJvDkISY2Gd5SUe+cCxQXWygIoDLcSdIHSo InekuWuOfHG+qg+mj1Kr5yXWNWp1fXaiqL0ZCEU9XQjhVrRbckOr4oJ9Ta7UytADWsbZWB hx6GiDXqfNdHqp3auSDQ9FkMSOSUB4pbweGUDGizrssLZ7oQRmr86+uetbNfvJ8MluADfA 7zHCxGYC1RasF62zhcZ3tmVgigjYA3bhRrPR0yI5t547q5EAfgdhEDVQsR+8sbDsg24j5L 69XmoRQklGFXI8XPifPSa1vBNC7Tzdo23s44BqiB1Q+LR5jUL+NvKw31IRmVFA== 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 4QbqQm6r51zFW5; Wed, 7 Jun 2023 14:26:24 +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 357EQOh6076446; Wed, 7 Jun 2023 14:26:24 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 357EQOgl076445; Wed, 7 Jun 2023 14:26:24 GMT (envelope-from git) Date: Wed, 7 Jun 2023 14:26:24 GMT Message-Id: <202306071426.357EQOgl076445@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Jessica Clarke Subject: git: 21f7397a61f7 - main - libpmc: Handle PMCALLOCATE log with PMC code on PMU event system 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: jrtc27 X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 21f7397a61f7bff61a1221cc6340cd980a922540 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by jrtc27: URL: https://cgit.FreeBSD.org/src/commit/?id=21f7397a61f7bff61a1221cc6340cd980a922540 commit 21f7397a61f7bff61a1221cc6340cd980a922540 Author: Jessica Clarke AuthorDate: 2023-06-07 14:21:18 +0000 Commit: Jessica Clarke CommitDate: 2023-06-07 14:24:29 +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 --- 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 57b9c90de256..003d9615a44a 100644 --- a/lib/libpmc/libpmc.c +++ b/lib/libpmc/libpmc.c @@ -35,6 +35,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include #include #include @@ -1084,8 +1085,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 0db91cf51bc2..92bcb1c2b161 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; }