Received: by 2002:a05:7412:798b:b0:fc:a2b0:25d7 with SMTP id fb11csp472864rdb; Thu, 22 Feb 2024 09:12:06 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVS7/WPAfy251QDMMHDAya96y0tyy8pb6GDPHmPIv1GmClaXJUDcqYYvIX3FLIgby0p+gZCAeTwK5MgNR8rre1J6ST0SBTO1qSdRVFNmA== X-Google-Smtp-Source: AGHT+IE8rIBo+9pIesnX2zDK0dXKi6dQoBJ5L6UBy2Jkc7Klr/bsZWokwOYibztV96GWf5nyNCnN X-Received: by 2002:a17:906:6889:b0:a3f:9cef:53dd with SMTP id n9-20020a170906688900b00a3f9cef53ddmr1009355ejr.73.1708621926672; Thu, 22 Feb 2024 09:12:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708621926; cv=pass; d=google.com; s=arc-20160816; b=AR7/hff7iXeQZxf/q0E7bqfoFTAJV8QmbKrLIBHu02el37xdVgYVfBDdIQJkfzXVfW 5yaOtdBbEMEVGJxavU6b2ynTDSuKvig0Wnsg8qEnGDcErvyAUqCsmGehMLPEYc74T396 K2lfvgzj+iB+XQc1pWcrF52ySM7paMAnDDxLPgC6Rj+6Qe5+V0uIaiJ4bSUoO12WlLPF r3g+YiTqiV1OjArwlxHv7AgGKxV0rrCOAp9mpR395VhaNKCHi3m0nEcpVyaBayRilwd2 y2q8qlAah7ZTmjrUjUqnH4R/iKwdjk+ZZeJpRV9Z23Kc61abx37cyE9FGVLWpkDZO5dR bvEg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence; bh=Fbb24HI7mwnQHPbFmcorW8M6W1udgr86FpNgr1pAX8g=; fh=FD4AZyif1RVq8u6CcvN6S6M50LAdDsfss16CL204eQ0=; b=ju7gWyYHSUqb3S3LgtBhiXA2b5IOM0nyH3hGlfZfY6Ym3jprDCnYnnCW95Xsj44H+9 9NVL/2lJr6mAYpNM4px1lcsZtyRoG1CGPNU3xOri+ti5QqUhAmUp/1/1oMWcLjFc1aWh ovliPAYumnm1u2FM3NZzTrs/tzZgw8Ao4stXRhHz4oe2JHJVLrpFwE3tuxv2uC5GVoYq CrtKZkQihDArhcCtalkI4VE4Q+ze0M6riGRYNtLKt+TjDCdNeRw6hKjGS1zQ+XHT+/oi r72aOlT6cc21nELruW+0ZlNjsCQUBd/ez/zAlUoG2kWQHr3FEcFIvvYmxi4Si2+cozpV fPHw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-76918-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-76918-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id mf18-20020a170906cb9200b00a3eaadd1893si3639571ejb.836.2024.02.22.09.12.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 09:12:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-76918-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-76918-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-76918-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 1D1181F27184 for ; Thu, 22 Feb 2024 17:02:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6CAB1154420; Thu, 22 Feb 2024 17:02:30 +0000 (UTC) Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 45688155308; Thu, 22 Feb 2024 17:02:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708621348; cv=none; b=DdpBZ/6B7DbP00rQ8YtP4AsXVn3vC4q9qU7y1V9H64XaUApKTfJOe9RPZEb3RqqzMj8yD4ZSX6AGbY+mYnI0rAvGjwbYEsqNYor3RGlM+cp8diR3UbEFH9dZhr5nPDuF5I8bgq0OVUAHYcfzVgO64/fVajaYSQySbnfbJRoz+u8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708621348; c=relaxed/simple; bh=o2z62yIkjV+//Puh8dbCXY7hYl26G7rkj5+/iE/jb38=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=CZFFPSJikqm8HvM0JgMgSvPKHZtnTjxumggVcIRtD6wfqQbROWaHKm2MXIK0kLxXB6d3MawRGwjiEQ0w0udUDWbYf0vrqoIf1UmfFcHLMrtZELd6F6Fkug+wR0j1DSgf8M5l+kHKFJ+qzCC2JB5xnwcNUIy5Op1h+Ztb9dnNOtA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.214.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-1d934c8f8f7so75771255ad.2; Thu, 22 Feb 2024 09:02:17 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708621336; x=1709226136; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Fbb24HI7mwnQHPbFmcorW8M6W1udgr86FpNgr1pAX8g=; b=a08VLnpcRAtYrzxdfTxVBHyGJs12bqsc5GigEI4/3Kgsc0JCp0UYZ+9ATRq1tjSDK2 Oc5nK1/4ipxni8BveRhKjD0+w3c6bmpO3liqxk7oGRN14oPvNsZMuV4FP/NKtTgK9Jvk 5A7JdoUsCEsXbVUZvpG91M2wRtKbd8jRyTph0JptxuyQRMCF6nRt9Ygb6SJrApdjXmzz 6YeJjISoR6INsIkMHRhMUxTGu2W/PXGNoYki/p9cFc4g/AeTqIJ+AdOhUxMYRtzNSBNQ k7h6zWjqTbKDZArRbUbMGcednB0f72ZWf9tBoHaG5zEbRs49ofzQhYHtEPGc2SxZ0jPj zA3g== X-Forwarded-Encrypted: i=1; AJvYcCXc0PZlR7ztUUAEnhT/F5AX4/C7KQQPb7nraYuM6wqZ62Yay9Tgw9xUl5nvp2kZFHUOGuxgnOHMgJdj+b6ThXUcehz7cXwpRqFKOHYTTzvpFKf+0vJx/qQXJ/JUOGbmV7f2ZT3C0bj2pRCFeIxB0A== X-Gm-Message-State: AOJu0YwkUG7vey/RaphInatUGcVZja/KFYQaEMU9gskpck0PhekHf+7I 61TAts49pGe9S4nmwNYh+e3PY4KDZul4GLys/LuT1VZUPp09KhpsfAZvdrziyHNq2yC2N9fU5MC L3lQiqmHFCn9YfaEPxjo4xpOrS2E= X-Received: by 2002:a17:90a:3486:b0:299:398e:5cee with SMTP id p6-20020a17090a348600b00299398e5ceemr14241027pjb.13.1708621336292; Thu, 22 Feb 2024 09:02:16 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240126145605.1005472-1-mark.rutland@arm.com> In-Reply-To: From: Namhyung Kim Date: Thu, 22 Feb 2024 09:02:04 -0800 Message-ID: Subject: Re: [PATCH v2] perf print-events: make is_event_supported() more robust To: marcan@marcan.st, maz@kernel.org Cc: Ian Rogers , Mark Rutland , linux-kernel@vger.kernel.org, acme@redhat.com, james.clark@arm.com, john.g.garry@oracle.com, leo.yan@linaro.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, mike.leach@linaro.org, suzuki.poulose@arm.com, tmricht@linux.ibm.com, will@kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hector and Marc, can you please take a look? Thanks, Namhyung On Wed, Feb 21, 2024 at 12:19=E2=80=AFPM Ian Rogers wr= ote: > > On Fri, Jan 26, 2024 at 6:56=E2=80=AFAM Mark Rutland wrote: > > > > Currently the perf tool doesn't detect support for extended event types > > on Apple M1/M2 systems, and will not auto-expand plain PERF_EVENT_TYPE > > hardware events into per-PMU events. This is due to the detection of > > extended event types not handling mandatory filters required by the > > M1/M2 PMU driver. > > > > PMU drivers and the core perf_events code can require that > > perf_event_attr::exclude_* filters are configured in a specific way and > > may reject certain configurations of filters, for example: > > > > (a) Many PMUs lack support for any event filtering, and require all > > perf_event_attr::exclude_* bits to be clear. This includes Alpha's > > CPU PMU, and ARM CPU PMUs prior to the introduction of PMUv2 in > > ARMv7, > > > > (b) When /proc/sys/kernel/perf_event_paranoid >=3D 2, the perf core > > requires that perf_event_attr::exclude_kernel is set. > > > > (c) The Apple M1/M2 PMU requires that perf_event_attr::exclude_guest is > > set as the hardware PMU does not count while a guest is running (bu= t > > might be extended in future to do so). > > > > In is_event_supported(), we try to account for cases (a) and (b), first > > attempting to open an event without any filters, and if this fails, > > retrying with perf_event_attr::exclude_kernel set. We do not account fo= r > > case (c), or any other filters that drivers could theoretically require > > to be set. > > > > Thus is_event_supported() will fail to detect support for any events > > targeting an Apple M1/M2 PMU, even where events would be supported with > > perf_event_attr:::exclude_guest set. > > > > Since commit: > > > > 82fe2e45cdb00de4 ("perf pmus: Check if we can encode the PMU number i= n perf_event_attr.type") > > > > ... we use is_event_supported() to detect support for extended types, > > with the PMU ID encoded into the perf_event_attr::type. As above, on an > > Apple M1/M2 system this will always fail to detect that the event is > > supported, and consequently we fail to detect support for extended type= s > > even when these are supported, as they have been since commit: > > > > 5c816728651ae425 ("arm_pmu: Add PERF_PMU_CAP_EXTENDED_HW_TYPE capabil= ity") > > > > Due to this, the perf tool will not automatically expand plain > > PERF_TYPE_HARDWARE events into per-PMU events, even when all the > > necessary kernel support is present. > > > > This patch updates is_event_supported() to additionally try opening > > events with perf_event_attr::exclude_guest set, allowing support for > > events to be detected on Apple M1/M2 systems. I believe that this is > > sufficient for all contemporary CPU PMU drivers, though in future it ma= y > > be necessary to check for other combinations of filter bits. > > > > I've deliberately changed the check to not expect a specific error code > > for missing filters, as today ;the kernel may return a number of > > different error codes for missing filters (e.g. -EACCESS, -EINVAL, or > > -EOPNOTSUPP) depending on why and where the filter configuration is > > rejected, and retrying for any error is more robust. > > > > Note that this does not remove the need for commit: > > > > a24d9d9dc096fc0d ("perf parse-events: Make legacy events lower priori= ty than sysfs/JSON") > > > > ... which is still necessary so that named-pmu/event/ events work on > > kernels without extended type support, even if the event name happens t= o > > be the same as a PERF_EVENT_TYPE_HARDWARE event (e.g. as is the case fo= r > > the M1/M2 PMU's 'cycles' and 'instructions' events). > > > > Fixes: 82fe2e45cdb00de4 ("perf pmus: Check if we can encode the PMU num= ber in perf_event_attr.type") > > Signed-off-by: Mark Rutland > > Tested-by: Ian Rogers > > Tested-by: James Clark > > Cc: Arnaldo Carvalho de Melo > > Cc: Hector Martin > > Cc: Ian Rogers > > Cc: James Clark > > Cc: John Garry > > Cc: Leo Yan > > Cc: Marc Zyngier > > Cc: Mike Leach > > Cc: Namhyung Kim > > Cc: Suzuki K Poulose > > Cc: Thomas Richter > > Cc: Will Deacon > > Ping. Could we land this one? > > Thanks, > Ian > > > --- > > tools/perf/util/print-events.c | 27 +++++++++++++++++++-------- > > 1 file changed, 19 insertions(+), 8 deletions(-) > > > > Since v1 [1]: > > * Fix typos in commit message > > * Accumulate tags > > > > [1] https://lore.kernel.org/lkml/20240116170348.463479-1-mark.rutland@a= rm.com/ > > > > Mark. > > > > diff --git a/tools/perf/util/print-events.c b/tools/perf/util/print-eve= nts.c > > index b0fc48be623f3..4f67e8f00a4d6 100644 > > --- a/tools/perf/util/print-events.c > > +++ b/tools/perf/util/print-events.c > > @@ -232,7 +232,6 @@ void print_sdt_events(const struct print_callbacks = *print_cb, void *print_state) > > bool is_event_supported(u8 type, u64 config) > > { > > bool ret =3D true; > > - int open_return; > > struct evsel *evsel; > > struct perf_event_attr attr =3D { > > .type =3D type, > > @@ -246,20 +245,32 @@ bool is_event_supported(u8 type, u64 config) > > > > evsel =3D evsel__new(&attr); > > if (evsel) { > > - open_return =3D evsel__open(evsel, NULL, tmap); > > - ret =3D open_return >=3D 0; > > + ret =3D evsel__open(evsel, NULL, tmap) >=3D 0; > > > > - if (open_return =3D=3D -EACCES) { > > + if (!ret) { > > /* > > - * This happens if the paranoid value > > + * The event may fail to open if the paranoid v= alue > > * /proc/sys/kernel/perf_event_paranoid is set = to 2 > > - * Re-run with exclude_kernel set; we don't do = that > > - * by default as some ARM machines do not suppo= rt it. > > - * > > + * Re-run with exclude_kernel set; we don't do = that by > > + * default as some ARM machines do not support = it. > > */ > > evsel->core.attr.exclude_kernel =3D 1; > > ret =3D evsel__open(evsel, NULL, tmap) >=3D 0; > > } > > + > > + if (!ret) { > > + /* > > + * The event may fail to open if the PMU requir= es > > + * exclude_guest to be set (e.g. as the Apple M= 1 PMU > > + * requires). > > + * Re-run with exclude_guest set; we don't do t= hat by > > + * default as it's equally legitimate for anoth= er PMU > > + * driver to require that exclude_guest is clea= r. > > + */ > > + evsel->core.attr.exclude_guest =3D 1; > > + ret =3D evsel__open(evsel, NULL, tmap) >=3D 0; > > + } > > + > > evsel__delete(evsel); > > } > > > > -- > > 2.30.2 > >