From patchwork Wed Sep 13 12:59:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tero Kristo X-Patchwork-Id: 139251 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp66682vqi; Wed, 13 Sep 2023 19:34:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH3LiNbQazVJxRcJJDwPkCMVjmx6vpB3hlFc6kOIHI6DH9m6iaIXVZ32xmc1RKAXo7KRQ4g X-Received: by 2002:a17:902:dac6:b0:1bf:7d3b:4404 with SMTP id q6-20020a170902dac600b001bf7d3b4404mr4714618plx.17.1694658875164; Wed, 13 Sep 2023 19:34:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694658875; cv=none; d=google.com; s=arc-20160816; b=KLshL7oPrJ2XUhFqAdq66LlJ9M8CUEq0eO6iC/bT/88+1I/0kSAZ8tHylGfhNVvvB9 vpTvL2jJCvugqWkxZNUq9w+PxFtSZbg0yfduPaw+LMSq0yqJkwkgjfIH21IemTIeHIFx wv120Q53pnXwAHVH61a9hMwVU7GvUL1Glzo1H3eE6jzYh5C1e7bb35e3xvUd+XgVBiKx TmfWeKTY4zBB+yv+xMJ7auRbpqIdYLAQHMl5CyhtBm3hXBNaw3efD20jI40aU6RRbBd6 +Q47f84z6rOgMxTMOu8mtWZEtrjYTin0q2OQwjIycEwQwCkQmKI/7GXx+njPUJ6ive8x Q+Cw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=lyy8SiFnGilHhcB1pP5lqOneUKeUntmXehiF6ckTwWQ=; fh=Ig6T0+oj6q6jaI/pQgTZfeOhWrDmP5zVdbfP15yfpJg=; b=c9segrJhJOwjkF6FTeaCGO3H0vRNnm5ZZ2gj9ii6PKGfTKB+pZler/r5Ges08ogYB+ hn0jZrnT/0MrIg6KPcI+WD8ywpi+g7vwOn9bMGYxPSJQEgRdGaZKh8TCcaLbHKdX/Jfi v9yal6wvAy7AU/YT1y4LZimFSoA9E7fs3taE7hLqdJJikFCwHmsLJY22bSHeQ7tyx919 Rpz67aa/6mR4FQatqmMgziohTxTnQhPUtz2ryE/fYzprTYRTI23c3NWpzQ67zUahcCld dHpmLOEMM+riP75Pe/Umgy6/U09boFwoYIfbPbA1lAO0ntW4b+FVL9dV/dYqkrxiQemM NTog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=abDz9kzU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id kk12-20020a170903070c00b001c3ae631b86si642133plb.112.2023.09.13.19.34.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 19:34:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=abDz9kzU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id C2AD68251380; Wed, 13 Sep 2023 06:00:22 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240710AbjIMNAK (ORCPT + 36 others); Wed, 13 Sep 2023 09:00:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240671AbjIMNAI (ORCPT ); Wed, 13 Sep 2023 09:00:08 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6882A1989; Wed, 13 Sep 2023 06:00:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1694610004; x=1726146004; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=SS5UdfEd5/aGjW95PuR1Hrc1ehfo5reasMr5Jb91wy0=; b=abDz9kzUWQ2kSs78Eu+X4vtTDYKdewXwQ4f83lRCbdq0qzG5xo74sORm JU//2HDLl7s1TqO7EhQyhKK8RrT3jIGNiijTSuaDL6NZVNf2M0EBsXVp0 bbt0+TkIvbFDfpHM5zQHaiIQK4lv7/jB1ooIwWiPTz8kp/ZvDcvYIFDwi u8/+rFdawT4wQH7oxpk9qQJjL3CdF64BivVoUFelVwhOSolMJEc4xUI1Z tqgWr9kZ/TIGWi+Zvhv+wc1WM0twa55iPY4tyOKeTXabswx6l2E3wryZS 0dL99OnYx9Ep7Q94vIYE6Akqrd6mFQcfdJjvy7UKe/tJ5Pzzvd+LNQPgQ g==; X-IronPort-AV: E=McAfee;i="6600,9927,10832"; a="375983355" X-IronPort-AV: E=Sophos;i="6.02,143,1688454000"; d="scan'208";a="375983355" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Sep 2023 06:00:03 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10832"; a="990908097" X-IronPort-AV: E=Sophos;i="6.02,143,1688454000"; d="scan'208";a="990908097" Received: from asjackso-mobl2.amr.corp.intel.com (HELO tkristo-desk.intel.com) ([10.249.45.219]) by fmsmga006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Sep 2023 05:59:59 -0700 From: Tero Kristo To: x86@kernel.org, bp@alien8.de, dave.hansen@linux.intel.com, tglx@linutronix.de Cc: hpa@zytor.com, irogers@google.com, jolsa@kernel.org, namhyung@kernel.org, adrian.hunter@intel.com, acme@kernel.org, mingo@redhat.com, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, alexander.shishkin@linux.intel.com, linux-perf-users@vger.kernel.org, peterz@infradead.org, mark.rutland@arm.com Subject: [PATCHv2 2/2] perf/core: Allow reading package events from perf_event_read_local Date: Wed, 13 Sep 2023 15:59:56 +0300 Message-Id: <20230913125956.3652667-1-tero.kristo@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230912124432.3616761-2-tero.kristo@linux.intel.com> References: <20230912124432.3616761-2-tero.kristo@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 13 Sep 2023 06:00:22 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776978624445349560 X-GMAIL-MSGID: 1776978624445349560 Per-package perf events are typically registered with a single CPU only, however they can be read across all the CPUs within the package. Currently perf_event_read maps the event CPU according to the topology information to avoid an unnecessary SMP call, however perf_event_read_local deals with hard values and rejects a read with a failure if the CPU is not the one exactly registered. Allow similar mapping within the perf_event_read_local if the perf event in question can support this. This allows users like BPF code to read the package perf events properly across different CPUs within a package. Signed-off-by: Tero Kristo --- v2: * prevent illegal array access in case event->oncpu == -1 * split the event->cpu / event->oncpu handling to their own variables kernel/events/core.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 4c72a41f11af..6b343bac0a71 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -4425,6 +4425,9 @@ static int __perf_event_read_cpu(struct perf_event *event, int event_cpu) { u16 local_pkg, event_pkg; + if (event_cpu < 0 || event_cpu >= nr_cpu_ids) + return event_cpu; + if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) { int local_cpu = smp_processor_id(); @@ -4528,6 +4531,8 @@ int perf_event_read_local(struct perf_event *event, u64 *value, { unsigned long flags; int ret = 0; + int event_cpu; + int event_oncpu; /* * Disabling interrupts avoids all counter scheduling (context @@ -4551,15 +4556,22 @@ int perf_event_read_local(struct perf_event *event, u64 *value, goto out; } + /* + * Get the event CPU numbers, and adjust them to local if the event is + * a per-package event that can be read locally + */ + event_oncpu = __perf_event_read_cpu(event, event->oncpu); + event_cpu = __perf_event_read_cpu(event, event->cpu); + /* If this is a per-CPU event, it must be for this CPU */ if (!(event->attach_state & PERF_ATTACH_TASK) && - event->cpu != smp_processor_id()) { + event_cpu != smp_processor_id()) { ret = -EINVAL; goto out; } /* If this is a pinned event it must be running on this CPU */ - if (event->attr.pinned && event->oncpu != smp_processor_id()) { + if (event->attr.pinned && event_oncpu != smp_processor_id()) { ret = -EBUSY; goto out; } @@ -4569,7 +4581,7 @@ int perf_event_read_local(struct perf_event *event, u64 *value, * or local to this CPU. Furthermore it means its ACTIVE (otherwise * oncpu == -1). */ - if (event->oncpu == smp_processor_id()) + if (event_oncpu == smp_processor_id()) event->pmu->read(event); *value = local64_read(&event->count);