Re: [PATCH v3 3/5] perf: Allow adding fixed random jitter to the alternate sampling period

From: Mark Barnett
Date: Mon Mar 10 2025 - 12:28:31 EST


On 3/10/25 12:47, Peter Zijlstra wrote:
On Fri, Mar 07, 2025 at 08:22:45PM +0000, mark.barnett@xxxxxxx wrote:
@@ -9922,7 +9923,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);

So, ... this here is NMI context, right? Have you looked at the guts of
get_random_u32_below() ?

I would strongly suggest you go do so.


Good catch. I think a pseudo-random generator would be fine here and it looks like the implementation of prandom is safe to use in an interrupt context. I can change to use that.

hwc->sample_period = sample_period;
hwc->using_alt_sample_period = !using_alt;