From patchwork Mon Jan 6 12:01:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: mark.barnett@arm.com X-Patchwork-Id: 13927334 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0421FE77188 for ; Mon, 6 Jan 2025 12:13:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=sJ/tx5WuTMFe8amMR9CRdVXM56dnWt/AucZ5g3/CtaU=; b=E+ZAMLdtVSr5CGzVW0yVfwFYZZ 9F4EVNRxomv3ON8PHP9lXOUCJ4crGUNSx3a1suhNhV7hpNDW8WwsyM7zRWnMBhJmCut89cPSVChrw Eca978v6x8+6j1f2CVplFt9bmUQQKAC2kyYz16g6I1uzoHY8+tpnw0XkuagzPC3g/QrOkXbAYtIwb MsjTwU7XXo1jTTqGoInnFCqZSeDXsbJoUkyiDFoCsB0mNvqpbt/66y8ajackdlEexp5YsIF3i3b+z g+EKqKv6bXxgKJTWnDcho2ZpfXLNYwX+3I9Kcall5I6TB9kMYq10sQ1G8tpieB/B9pvUFV0brmSov UMCz8Q4A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tUlzL-00000001AN7-0X6f; Mon, 06 Jan 2025 12:13:43 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tUloe-000000017mQ-3pgy for linux-arm-kernel@lists.infradead.org; Mon, 06 Jan 2025 12:02:42 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id AE99122BE; Mon, 6 Jan 2025 04:03:08 -0800 (PST) Received: from e128066.arm.com (unknown [10.57.93.133]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 3B4C23F673; Mon, 6 Jan 2025 04:02:36 -0800 (PST) From: mark.barnett@arm.com To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, irogers@google.com Cc: ben.gainey@arm.com, deepak.surti@arm.com, ak@linux.intel.com, will@kernel.org, james.clark@arm.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, adrian.hunter@intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Mark Barnett Subject: [PATCH v2 2/5] perf: Allow adding fixed random jitter to the alternate sampling period Date: Mon, 6 Jan 2025 12:01:53 +0000 Message-Id: <20250106120156.227273-3-mark.barnett@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20250106120156.227273-1-mark.barnett@arm.com> References: <20250106120156.227273-1-mark.barnett@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250106_040241_012486_6255773B X-CRM114-Status: GOOD ( 15.88 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Ben Gainey This change modifies the core perf overflow handler, adding some small random jitter to each sample period whenever an event switches between the two alternate sample periods. A new flag is added to perf_event_attr to opt into this behaviour. This change follows the discussion in [1], where it is recognized that it may be possible for certain patterns of execution to end up with biased results. [1] https://lore.kernel.org/linux-perf-users/Zc24eLqZycmIg3d2@tassilo/ Signed-off-by: Ben Gainey Signed-off-by: Mark Barnett --- include/uapi/linux/perf_event.h | 7 ++++++- kernel/events/core.c | 9 ++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h index 499a8673df8e..c0076ce8f80a 100644 --- a/include/uapi/linux/perf_event.h +++ b/include/uapi/linux/perf_event.h @@ -461,7 +461,12 @@ struct perf_event_attr { inherit_thread : 1, /* children only inherit if cloned with CLONE_THREAD */ remove_on_exec : 1, /* event is removed from task on exec */ sigtrap : 1, /* send synchronous SIGTRAP on event */ - __reserved_1 : 26; + /* + * Add a limited amount of jitter on each alternate period, where + * the jitter is between [0, (2< #include #include +#include #include #include #include @@ -9878,7 +9879,10 @@ static int __perf_event_overflow(struct perf_event *event, if (event->attr.alt_sample_period) { bool using_alt = hwc->using_alt_sample_period; u64 sample_period = (using_alt ? event->attr.sample_period - : event->attr.alt_sample_period); + : event->attr.alt_sample_period) + + (event->attr.jitter_alt_period + ? get_random_u32_below(2 << event->attr.jitter_alt_period) + : 0); hwc->sample_period = sample_period; hwc->using_alt_sample_period = !using_alt; @@ -12803,6 +12807,9 @@ SYSCALL_DEFINE5(perf_event_open, } } + if (attr.jitter_alt_period && !attr.alt_sample_period) + return -EINVAL; + /* Only privileged users can get physical addresses */ if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR)) { err = perf_allow_kernel(&attr);