From mboxrd@z Thu Jan 1 00:00:00 1970 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 19765C10F15 for ; Mon, 22 Apr 2024 10:50:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=d+XPCBNop1Eszs8U39/LdW9/rXHBdzPf8zIN34rLCeo=; b=b6tPNpDwI1rv3Q TnNxEcf8PVAqANarpgyijR4lJkQNoX3sjkDL3dI2l93E1bC6D3w3B6xstpNVlgDljhlJzZrOFxN6B zfN9a1bUj8iFU0x68KM+FYRRu427R4kTGwLk3HK5k4leRTGsYuo5J4BvYHs4mA2hiPQXvQh/Et10/ hItiN+/p1algFjUtd7VVPxYa/FJjetCHFkKtDgoP6pyH51E2jCVoKuYhX+zdjAL/w4zREii3vl+wE NUKw264HSJ01TetRBL8qTd6Aa5wWTyg7avs+K/jat8pTr0PeCDBx1Z66bmjPQmZLyWR7jd7fq+BbB kjRv/vxE0fyRw6Y7yOOQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1ryrFC-0000000DEWD-1dak; Mon, 22 Apr 2024 10:49:54 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1ryrF0-0000000DES9-1TZ0 for linux-arm-kernel@lists.infradead.org; Mon, 22 Apr 2024 10:49:44 +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 21F4913D5; Mon, 22 Apr 2024 03:50:10 -0700 (PDT) Received: from e126817.cambridge.arm.com (e126817.cambridge.arm.com [10.2.3.5]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id C605B3F7BD; Mon, 22 Apr 2024 03:49:39 -0700 (PDT) From: Ben Gainey To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org Cc: ak@linux.intel.com, will@kernel.org, james.clark@arm.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Ben Gainey Subject: [RFC PATCH v2 2/4] perf: Allow adding fixed random jitter to the alternate sampling period Date: Mon, 22 Apr 2024 11:49:27 +0100 Message-ID: <20240422104929.264241-3-ben.gainey@arm.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240422104929.264241-1-ben.gainey@arm.com> References: <20240422104929.264241-1-ben.gainey@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240422_034942_531883_2996674F X-CRM114-Status: GOOD ( 14.18 ) 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: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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 --- include/uapi/linux/perf_event.h | 3 ++- kernel/events/core.c | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h index 5c1701d091cf..dd3697a4b300 100644 --- a/include/uapi/linux/perf_event.h +++ b/include/uapi/linux/perf_event.h @@ -461,7 +461,8 @@ 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; + jitter_alternate_period : 1, /* add a limited amount of jitter on each alternate period */ + __reserved_1 : 25; union { __u32 wakeup_events; /* wakeup every n events */ diff --git a/kernel/events/core.c b/kernel/events/core.c index 07f1f931e18e..079ae520e836 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -9546,6 +9547,8 @@ static inline bool sample_is_allowed(struct perf_event *event, struct pt_regs *r return true; } +# define MAX_ALT_SAMPLE_PERIOD_JITTER 16 + /* * Generic event overflow handling, sampling. */ @@ -9573,7 +9576,10 @@ static int __perf_event_overflow(struct perf_event *event, if (event->attr.alternative_sample_period) { bool using_alt = hwc->using_alternative_sample_period; u64 sample_period = (using_alt ? event->attr.sample_period - : event->attr.alternative_sample_period); + : event->attr.alternative_sample_period) + + (event->attr.jitter_alternate_period + ? get_random_u32_below(MAX_ALT_SAMPLE_PERIOD_JITTER) + : 0); hwc->sample_period = sample_period; hwc->using_alternative_sample_period = !using_alt; @@ -12503,6 +12509,9 @@ SYSCALL_DEFINE5(perf_event_open, } } + if (attr.jitter_alternate_period && !attr.alternative_sample_period) + return -EINVAL; + /* Only privileged users can get physical addresses */ if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR)) { err = perf_allow_kernel(&attr); -- 2.44.0 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel