Many hardware pmus have constraints about sample period. For ex, minimum
supported sample period for IBS Op pmu is 0x90, the sample period must
be multiple of 0x10 for IBS Fetch and IBS Op.
Add an optional callback adjust_period() to struct pmu to allow pmu
specific drivers to adjust sample period calculated by generic code.
This will ensure the sample_period value will always be valid and no
additional code is required in PMU specific drivers to re-adjust the
period.
Signed-off-by: Ravi Bangoria <ravi.bangoria@amd.com>
---
arch/x86/events/amd/ibs.c | 11 +++++++++++
include/linux/perf_event.h | 5 +++++
kernel/events/core.c | 12 ++++++++++--
3 files changed, 26 insertions(+), 2 deletions(-)
diff --git a/arch/x86/events/amd/ibs.c b/arch/x86/events/amd/ibs.c
index 33728ed6d7a6..0d1db2fffc5b 100644
--- a/arch/x86/events/amd/ibs.c
+++ b/arch/x86/events/amd/ibs.c
@@ -573,6 +573,15 @@ static int perf_ibs_check_period(struct perf_event *event, u64 value)
return 0;
}
+static u64 perf_ibs_adjust_period(struct perf_event *event, u64 period)
+{
+ struct perf_ibs *perf_ibs = container_of(event->pmu, struct perf_ibs, pmu);
+
+ period &= ~0xFULL;
+
+ return period < perf_ibs->min_period ? perf_ibs->min_period : period;
+}
+
/*
* We need to initialize with empty group if all attributes in the
* group are dynamic.
@@ -699,6 +708,7 @@ static struct perf_ibs perf_ibs_fetch = {
.stop = perf_ibs_stop,
.read = perf_ibs_read,
.check_period = perf_ibs_check_period,
+ .adjust_period = perf_ibs_adjust_period,
.capabilities = PERF_PMU_CAP_NO_EXCLUDE,
},
.msr = MSR_AMD64_IBSFETCHCTL,
@@ -725,6 +735,7 @@ static struct perf_ibs perf_ibs_op = {
.stop = perf_ibs_stop,
.read = perf_ibs_read,
.check_period = perf_ibs_check_period,
+ .adjust_period = perf_ibs_adjust_period,
.capabilities = PERF_PMU_CAP_NO_EXCLUDE,
},
.msr = MSR_AMD64_IBSOPCTL,
diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index 1a8942277dda..eca8581d8e5d 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -540,6 +540,11 @@ struct pmu {
* Check period value for PERF_EVENT_IOC_PERIOD ioctl.
*/
int (*check_period) (struct perf_event *event, u64 value); /* optional */
+
+ /*
+ * Adjust period value according to pmu constraints.
+ */
+ u64 (*adjust_period) (struct perf_event *event, u64 period); /* optional */
};
enum perf_addr_filter_action_t {
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 8a6c6bbcd658..f3de683ec716 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -4100,9 +4100,9 @@ static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bo
if (!sample_period)
sample_period = 1;
- hwc->sample_period = sample_period;
+ hwc->sample_period = event->pmu->adjust_period(event, sample_period);
- if (local64_read(&hwc->period_left) > 8*sample_period) {
+ if (local64_read(&hwc->period_left) > 8*hwc->sample_period) {
if (disable)
event->pmu->stop(event, PERF_EF_UPDATE);
@@ -11363,6 +11363,11 @@ static int perf_event_nop_int(struct perf_event *event, u64 value)
return 0;
}
+static u64 perf_pmu_nop_adjust_period(struct perf_event *event, u64 period)
+{
+ return period;
+}
+
static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
@@ -11641,6 +11646,9 @@ int perf_pmu_register(struct pmu *pmu, const char *name, int type)
if (!pmu->check_period)
pmu->check_period = perf_event_nop_int;
+ if (!pmu->adjust_period)
+ pmu->adjust_period = perf_pmu_nop_adjust_period;
+
if (!pmu->event_idx)
pmu->event_idx = perf_event_idx_default;
--
2.46.2