Re: UBSAN: Undefined behaviour in arch/x86/events/intel/p6.c:116:29

From: Peter Zijlstra
Date: Wed Dec 04 2019 - 10:07:08 EST


On Wed, Dec 04, 2019 at 01:15:40PM +0100, Jiri Olsa wrote:
> On Tue, Dec 03, 2019 at 03:39:49PM +0200, Meelis Roos wrote:
> > > Does something like so fix it?
> >
> > Unfortunately not (tested on top of todays git):
>
> hi,
> which p6 model are you seeing this on?
> how do you trigger that?

Triggers on any p6 model. I hacked up perf and used "qemu-system-x86_64
-cpu pentium2".

The below seems to cure things.

---
diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
index 9a89d98c55bd..f17417644665 100644
--- a/arch/x86/events/core.c
+++ b/arch/x86/events/core.c
@@ -1642,9 +1643,12 @@ static struct attribute_group x86_pmu_format_group __ro_after_init = {

ssize_t events_sysfs_show(struct device *dev, struct device_attribute *attr, char *page)
{
- struct perf_pmu_events_attr *pmu_attr = \
+ struct perf_pmu_events_attr *pmu_attr =
container_of(attr, struct perf_pmu_events_attr, attr);
- u64 config = x86_pmu.event_map(pmu_attr->id);
+ u64 config = 0;
+
+ if (pmu_attr->id < x86_pmu.max_events)
+ x86_pmu.event_map(pmu_attr->id);

/* string trumps id */
if (pmu_attr->event_str)
@@ -1713,6 +1717,9 @@ is_visible(struct kobject *kobj, struct attribute *attr, int idx)
{
struct perf_pmu_events_attr *pmu_attr;

+ if (idx >= x86_pmu.max_events)
+ return 0;
+
pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr.attr);
/* str trumps id */
return pmu_attr->event_str || x86_pmu.event_map(idx) ? attr->mode : 0;