Re: [PATCH v2 1/5] perf: Allow periodic events to alternate between two sample periods

From: Mark Barnett
Date: Fri Mar 07 2025 - 15:28:47 EST


On 1/21/25 13:01, Leo Yan wrote:
local64_set(&hwc->period_left, hwc->sample_period);
+ if (attr->alt_sample_period) {
+ hwc->sample_period = attr->alt_sample_period;
+ hwc->using_alt_sample_period = true;
+ }

My understanding it sets a short sample window for the first period.
Would it initialize the `hwc->period_left` with the updated sample
period?


It sets the long period first: hwc->period_left is used to program the PMU when setting up the event, and hwc->sample_period is queued up as the next period to switch to.

+
+ /*
+ * alt_sample_period cannot be used with freq
+ */
+ if (attr->freq && attr->alt_sample_period)
+ goto err_ns;
+

It is good to validate parameters first. So move the checking before
the adjustment for the alt sample period.


Ack. Done.

/*
* We do not support PERF_SAMPLE_READ on inherited events unless
* PERF_SAMPLE_TID is also selected, which allows inherited events to
@@ -12763,9 +12788,19 @@ SYSCALL_DEFINE5(perf_event_open,
if (attr.freq) {
if (attr.sample_freq > sysctl_perf_event_sample_rate)
return -EINVAL;
+ if (attr.alt_sample_period)
+ return -EINVAL;
} else {
if (attr.sample_period & (1ULL << 63))
return -EINVAL;
+ if (attr.alt_sample_period) {
+ if (!attr.sample_period)
+ return -EINVAL;
+ if (attr.alt_sample_period & (1ULL << 63))
+ return -EINVAL;
+ if (attr.alt_sample_period == attr.sample_period)
+ attr.alt_sample_period = 0;

In theory, the attr.alt_sample_period should be less than
attr.sample_period, right?


Added some validation for this.