Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp2173476lqt; Mon, 22 Apr 2024 03:50:14 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX6WLt8B0YE0URnmVF+r2IXuQBP8wiZx3NZFR/GVhCnciUQoGAYwBPLgggkhFXLJjHxemlGc/0YN2O4Zy0XWfu4aAnvLW5DIjcQlPKIhA== X-Google-Smtp-Source: AGHT+IGcsVuabDVCxMwqNl8PsS2I2FbJgu8YJT+sjm/VFFLt0V3ohRPvFH20I9V5nvFQoIMRfhoM X-Received: by 2002:a17:90b:234f:b0:2ae:63e9:113f with SMTP id ms15-20020a17090b234f00b002ae63e9113fmr214135pjb.44.1713783014059; Mon, 22 Apr 2024 03:50:14 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713783014; cv=pass; d=google.com; s=arc-20160816; b=xsbssRuMu60UPxTVopfMeD8aIxWNykRbqS37gqU7TpXF1Z3uklU3lTiXh7OKJaG/B4 y4eTPp6n04cdEKFKZdd5ZpurvagPTsGlq/az0BTNWH6aZm1oyIOpuxhTgJT1qtZh+xHo dhPK9FCPs+n+L1rigAzIyXhEESnC/co64jgCDq+5U+xJlTcQZ5Bgk0Pr9mQcGHjO8/1M QkEaT82/8GT8YMNMHLlxRfzixtdVO9ET6nppkFmFJZHXMbpBN1YTEqOAfcyVN/Dk9x3S s8NcZ0HWnZ0I4ux0NPHw3FCY20fo0x6wvEOq7op6IUuwC8QGeV8FJM5Z0o8p3Koa9Vu6 T2PQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=9seoxncvKMYDpGWjbRX/Gda8EtdDd3cL6LtgUhPlw5s=; fh=/ZrbN2cjVR3iZGMDnE+GGecwHsuzyoxn0TelbLYD4bs=; b=OF/SScz/fyQDMlTfMrEM/Jo9OAoXx+3VY9NR/zlkUWOz+V1cQKONOpyXnxALbyneBg oGJ9UgOMezaxYITijB6bjjCU5NL68P/79rKt/b8yq5zfgJhJRRvpEoUKeyCiuBN5ihnN LTvDjGd6Ee9GfXX9uNK978lQOxF2JiKvV3C2Fwn0LzKYUezc9lsLiNnTvMA73o4gnrow tMMsElZ3jZT7X2X1oUQFCHvm0IQmjzGlkgjOImO8055qDhzJ5BSDQr6O21yXmNUjyL6x nCrsXxZWZMggkgGCkNcpdvyOTRIGIzJFZXAaeRIJt1J7NHTlL0omb5qpHQt4A6+CBat3 n7Cw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-153233-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-153233-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id gb5-20020a17090b060500b002a6b7cc391dsi9431135pjb.165.2024.04.22.03.50.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Apr 2024 03:50:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-153233-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-153233-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-153233-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id B3E8B281B00 for ; Mon, 22 Apr 2024 10:50:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4C1231465A7; Mon, 22 Apr 2024 10:49:44 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 59DE0145B1D; Mon, 22 Apr 2024 10:49:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713782983; cv=none; b=KYjZ7uqrqN21weldBTpksc1tauckMahQT8lTyfPFUqpTQxz/mJo3PXUoM7CVp6nGHwQqPTHYGUSXI2zJovkn1rscnM+GsOR45a8DxD10ryPrs33+R49kvEEDS26ZhEW+agd1ECmLhLOBNZWmwaM/LW+59VKEMP/daduEVOOuGZE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713782983; c=relaxed/simple; bh=JlMo9/yymdi+OtMCepRzEEO4IMsqxHF7a01c0tOVimw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rVFgFEhCLG55VndRo6AEu9DI16EstZ/XVMSr2oUx4ubk+PGY7wKEm6qgkjBPO9RW/SPIRaEN4LCWkzfozlCxqJ8siV+XQyKnh5VImJ5o/OydxydVaiYx7l6FChyieUvDz2Sb5yrgr5uuwZ/r71iMeKhFIQttWuhUmRv9YTSE2dY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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