From: Sandipan Das <sandipan.das@amd.com>
To: Peter Zijlstra <peterz@infradead.org>
Cc: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
mingo@redhat.com, acme@kernel.org, namhyung@kernel.org,
mark.rutland@arm.com, alexander.shishkin@linux.intel.com,
jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com,
kan.liang@linux.intel.com, tglx@linutronix.de, bp@alien8.de,
dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com,
eranian@google.com, songliubraving@meta.com,
ravi.bangoria@amd.com, ananth.narayan@amd.com
Subject: Re: [PATCH 2/4] perf/x86/amd/uncore: Use hrtimer for handling overflows
Date: Thu, 10 Apr 2025 14:10:11 +0530 [thread overview]
Message-ID: <083586a9-8717-4cc5-aab1-a7f40e6e8621@amd.com> (raw)
In-Reply-To: <20250410082041.GY9833@noisy.programming.kicks-ass.net>
On 4/10/2025 1:50 PM, Peter Zijlstra wrote:
> On Wed, Apr 09, 2025 at 01:27:07PM +0530, Sandipan Das wrote:
>> Uncore counters do not provide mechanisms like interrupts to report
>> overflows and the accumulated user-visible count is incorrect if there
>> is more than one overflow between two successive read requests for the
>> same event because the value of prev_count goes out-of-date for
>> calculating the correct delta.
>>
>> To avoid this, start a hrtimer to periodically initiate a pmu->read() of
>> the active counters for keeping prev_count up-to-date. It should be
>> noted that the hrtimer duration should be lesser than the shortest time
>> it takes for a counter to overflow for this approach to be effective.
>>
>> Signed-off-by: Sandipan Das <sandipan.das@amd.com>
>> ---
>> arch/x86/events/amd/uncore.c | 72 ++++++++++++++++++++++++++++++++++++
>> 1 file changed, 72 insertions(+)
>>
>> diff --git a/arch/x86/events/amd/uncore.c b/arch/x86/events/amd/uncore.c
>> index 010024f09f2c..ee6528f2189f 100644
>> --- a/arch/x86/events/amd/uncore.c
>> +++ b/arch/x86/events/amd/uncore.c
>> @@ -21,6 +21,7 @@
>> #define NUM_COUNTERS_NB 4
>> #define NUM_COUNTERS_L2 4
>> #define NUM_COUNTERS_L3 6
>> +#define NUM_COUNTERS_MAX 64
>>
>> #define RDPMC_BASE_NB 6
>> #define RDPMC_BASE_LLC 10
>> @@ -38,6 +39,10 @@ struct amd_uncore_ctx {
>> int refcnt;
>> int cpu;
>> struct perf_event **events;
>> + unsigned long active_mask[BITS_TO_LONGS(NUM_COUNTERS_MAX)];
>> + int nr_active;
>> + struct hrtimer hrtimer;
>> + u64 hrtimer_duration;
>> };
>>
>> struct amd_uncore_pmu {
>> @@ -87,6 +92,51 @@ static struct amd_uncore_pmu *event_to_amd_uncore_pmu(struct perf_event *event)
>> return container_of(event->pmu, struct amd_uncore_pmu, pmu);
>> }
>>
>> +static enum hrtimer_restart amd_uncore_hrtimer(struct hrtimer *hrtimer)
>> +{
>> + struct amd_uncore_ctx *ctx;
>> + struct perf_event *event;
>> + unsigned long flags;
>> + int bit;
>> +
>> + ctx = container_of(hrtimer, struct amd_uncore_ctx, hrtimer);
>> +
>> + if (!ctx->nr_active || ctx->cpu != smp_processor_id())
>> + return HRTIMER_NORESTART;
>> +
>> + /*
>> + * Disable local interrupts to prevent pmu->start() or pmu->stop()
>> + * from interrupting the update process
>> + */
>> + local_irq_save(flags);
>> +
>> + for_each_set_bit(bit, ctx->active_mask, NUM_COUNTERS_MAX) {
>> + event = ctx->events[bit];
>> + event->pmu->read(event);
>> + }
>> +
>> + local_irq_restore(flags);
>> +
>> + hrtimer_forward_now(hrtimer, ns_to_ktime(ctx->hrtimer_duration));
>> + return HRTIMER_RESTART;
>> +}
>> +
>> +static void amd_uncore_start_hrtimer(struct amd_uncore_ctx *ctx)
>> +{
>> + hrtimer_start(&ctx->hrtimer, ns_to_ktime(ctx->hrtimer_duration),
>> + HRTIMER_MODE_REL_PINNED);
>> +}
>
> So I know you copied this from the Intel uncore driver; but would not
> both be improved by using HRTIMER_MODE_HARD?
>
> It makes no sense to me to bounce the thing to SoftIRQ only to then
> disable IRQs in the handler again. Not to mention that the whole SoftIRQ
> things delays things further, giving more room/time to reach overflow
> wrap.
Agreed. Will address this in v2 for both the drivers.
next prev parent reply other threads:[~2025-04-10 8:40 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-09 7:57 [PATCH 0/4] perf/x86/amd/uncore: Overflow handling enhancements Sandipan Das
2025-04-09 7:57 ` [PATCH 1/4] perf/x86/amd/uncore: Remove unused member from amd_uncore_ctx Sandipan Das
2025-04-09 7:57 ` [PATCH 2/4] perf/x86/amd/uncore: Use hrtimer for handling overflows Sandipan Das
2025-04-10 8:20 ` Peter Zijlstra
2025-04-10 8:40 ` Sandipan Das [this message]
2025-04-09 7:57 ` [PATCH 3/4] perf/x86/amd/uncore: Add parameter to configure hrtimer Sandipan Das
2025-04-09 7:57 ` [PATCH 4/4] perf/x86/amd/uncore: Prevent UMC counters from saturating Sandipan Das
2025-04-16 19:15 ` Song Liu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=083586a9-8717-4cc5-aab1-a7f40e6e8621@amd.com \
--to=sandipan.das@amd.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=ananth.narayan@amd.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=eranian@google.com \
--cc=hpa@zytor.com \
--cc=irogers@google.com \
--cc=jolsa@kernel.org \
--cc=kan.liang@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=ravi.bangoria@amd.com \
--cc=songliubraving@meta.com \
--cc=tglx@linutronix.de \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®