Skip to contentSkip to conversation

perf: Introduce function to report unsupported syscall attribute flags

PATCH In review Robert Richter 3 participants 5 messages Sep 12, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf: Introduce function to report unsupported syscall attribute flags

Often a syscall fails with

Error: sys_perf_event_open() syscall returned with 95 (Operation not supported). /bin/dmesg may provide additional information.

but no additional information.

This patch adds some macro magic and a helper function to check and report unsupported syscall attribute flags:

has_unsupported_flags(attr1, attr2); has_unsupported_flag(attr, flag); __has_unsupported_flags(const struct perf_event_attr *attr,

                         u64 notsup);

E.g. dmesg reports the following (checking for the exclude_guest flag):

perf: unsupported attribute flags: 0000000000100000

or (checking precise modifier :ppp):

perf: unsupported attribute flags: 0000000000018000

---
 arch/x86/kernel/cpu/perf_event.c         |   19 ++++++++++---------
 arch/x86/kernel/cpu/perf_event_amd_ibs.c |    3 ++-
 include/linux/perf_event.h               |   23 +++++++++++++++++++++++
 kernel/events/core.c                     |    2 +-
 4 files changed, 36 insertions(+), 11 deletions(-)
arch/x86/kernel/cpu/perf_event.c +10 -9
index 915b876..9ae8676 100644
@@ -336,7 +336,7 @@ int x86_setup_perfctr(struct perf_event *event)
 			return -EOPNOTSUPP;
 
 		/* BTS is currently only allowed for user-mode. */
-		if (!attr->exclude_kernel)
+		if (has_unsupported_flag(attr, exclude_kernel))
 			return -EOPNOTSUPP;
 	}
 
@@ -389,8 +389,11 @@ int x86_pmu_hw_config(struct perf_event *event)
 				precise++;
 		}
 
-		if (event->attr.precise_ip > precise)
+		if (event->attr.precise_ip > precise) {
+			has_unsupported_flag(&event->attr, precise_ip);
 			return -EOPNOTSUPP;
+		}
+
 		/*
 		 * check that PEBS LBR correction does not conflict with
 		 * whatever the user is asking with attr->branch_sample_type
@@ -398,13 +401,7 @@ int x86_pmu_hw_config(struct perf_event *event)
 		if (event->attr.precise_ip > 1) {
 			u64 *br_type = &event->attr.branch_sample_type;
 
-			if (has_branch_stack(event)) {
-				if (!precise_br_compat(event))
-					return -EOPNOTSUPP;
-
-				/* branch_sample_type is compatible */
-
-			} else {
+			if (!has_branch_stack(event)) {
 				/*
 				 * user did not specify  branch_sample_type
 				 *
@@ -419,7 +416,11 @@ int x86_pmu_hw_config(struct perf_event *event)
 
 				if (!event->attr.exclude_kernel)
 					*br_type |= PERF_SAMPLE_BRANCH_KERNEL;
+			} else if (!precise_br_compat(event)) {
+				has_unsupported_flag(&event->attr, precise_ip);
+				return -EOPNOTSUPP;
 			}
+			/* else: branch_sample_type is compatible */
 		}
 	}
 
arch/x86/kernel/cpu/perf_event_amd_ibs.c +2 -1
index 4af8275..fc145b0 100644
@@ -186,6 +186,7 @@ static int perf_ibs_precise_event(struct perf_event *event, u64 *config)
 	case 2:
 		break;
 	default:
+		has_unsupported_flag(&event->attr, precise_ip);
 		return -EOPNOTSUPP;
 	}
 
@@ -243,7 +244,7 @@ static int perf_ibs_init(struct perf_event *event)
 	if (event->pmu != &perf_ibs->pmu)
 		return -ENOENT;
 
-	if (perf_flags(&event->attr) & perf_flags(&ibs_notsupp))
+	if (has_unsupported_flags(&event->attr, &ibs_notsupp))
 		return -EINVAL;
 
 	if (config & ~perf_ibs->config_mask)
include/linux/perf_event.h +23 -0
index c36a04f..eb247a5 100644
@@ -305,6 +305,14 @@ struct perf_event_attr {
 };
 
 #define perf_flags(attr)	(*(&(attr)->read_format + 1))
+#define perf_flags_init(flag) ({					\
+	volatile __u64	flags = 0;					\
+	struct perf_event_attr *attr;					\
+	attr = (void *)((char *)&flags -				\
+			(char *)&perf_flags((struct perf_event_attr *)(0))); \
+	if (!WARN_ON(&perf_flags(attr) != &flags)) 			\
+		attr->flag = ~0;					\
+	flags;})
 
 /*
  * Ioctls that can be done on a perf event fd:
@@ -1208,6 +1216,21 @@ extern int perf_event_overflow(struct perf_event *event,
 				 struct perf_sample_data *data,
 				 struct pt_regs *regs);
 
+static inline bool __has_unsupported_flags(const struct perf_event_attr *attr,
+					   u64 notsup)
+{
+	notsup &= perf_flags(attr);
+	if (notsup)
+		pr_warn("perf: unsupported attribute flags: %016llx\n", notsup);
+	return notsup != 0;
+}
+
+#define has_unsupported_flags(attr1, attr2) \
+	__has_unsupported_flags((attr1), perf_flags(attr2))
+/* flag is the name of the member in struct perf_event_attr: */
+#define has_unsupported_flag(attr, flag) \
+	__has_unsupported_flags((attr), perf_flags_init(flag))
+
 static inline bool is_sampling_event(struct perf_event *event)
 {
 	return event->attr.sample_period != 0;
kernel/events/core.c +1 -1
index 2ba8904..2667ef6 100644
@@ -6291,7 +6291,7 @@ static int perf_copy_attr(struct perf_event_attr __user *uattr,
 	if (ret)
 		return -EFAULT;
 
-	if (attr->__reserved_1)
+	if (has_unsupported_flag(attr, __reserved_1))
 		return -EINVAL;
 
 	if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
Signature · 1 line
1.7.8.6
maintainer
#1 lore raw

include/linux/perf_event.h extern int perf_event_overflow(struct perf_event *event, · line 1224

Quoted 2 lines · Robert Richter
include/linux/perf_event.h +2 -0
+       if (notsup)
+               pr_warn("perf: unsupported attribute flags: %016llx\n", notsup); 

This is a dmesg DoS..

I'm also not sure dmesg is the right way.. could we not somehow change the attrs to provide better diagnostic?

#2 lore raw
Quoted 5 lines · Peter Zijlstra

include/linux/perf_event.h extern int perf_event_overflow(struct perf_event *event, · line 1224

Quoted 2 lines · Robert Richter
include/linux/perf_event.h +2 -0
+       if (notsup)
+               pr_warn("perf: unsupported attribute flags: %016llx\n", notsup); 

This is a dmesg DoS..

This could be avoided by introducing a cpuinfo like sysfs file for each pmu:

/sys/bus/event_source/devices/*/flags

Then, userspace should know how to correctly setup the syscall. All supported attributes would be known to perf and messages would thrown only if something goes unexpected wrong.

I suggested this already here:

https://lkml.org/lkml/2012/8/3/214
https://lkml.org/lkml/2012/9/6/472

Quoted 2 lines

I'm also not sure dmesg is the right way.. could we not somehow change the attrs to provide better diagnostic?

I found this discussion without a solution for the problem:

http://lwn.net/Articles/374794/

Other options could be pr_debug() or trace_printk()'s for debugging purposes. Or an error report in sysfs:

cat /sys/bus/event_source/devices/*/log

But the pmu type needs to be known for this.

-Robert

Signature · 2 lines
Advanced Micro Devices, Inc.
Operating System Research Center
#3 lore raw
In reply to Peter Zijlstra
Quoted 8 lines · Peter Zijlstra

include/linux/perf_event.h extern int perf_event_overflow(struct perf_event *event, · line 1224

Quoted 2 lines · Robert Richter
include/linux/perf_event.h +2 -0
+       if (notsup)
+               pr_warn("perf: unsupported attribute flags: %016llx\n", notsup); 

This is a dmesg DoS..

I'm also not sure dmesg is the right way.. could we not somehow change the attrs to provide better diagnostic?

Have a output argument for a string, that is then printed by the user tool?

I agree it's a problem today, i usually have to resort to ftrace to figure out what'swrong.

-Andi

Signature · 1 line
[email protected] -- Speaking for myself only
maintainer
#4 lore raw
Quoted 8 lines · Andi Kleen
Quoted 6 lines · Peter Zijlstra

include/linux/perf_event.h extern int perf_event_overflow(struct perf_event *event, · line 1224

Quoted 2 lines · Robert Richter
include/linux/perf_event.h +2 -0
+       if (notsup)
+               pr_warn("perf: unsupported attribute flags: %016llx\n", notsup); 

This is a dmesg DoS..

Quoted 7 lines
Quoted 2 lines

I'm also not sure dmesg is the right way.. could we not somehow change the attrs to provide better diagnostic?

Have a output argument for a string, that is then printed by the user tool?

I agree it's a problem today, i usually have to resort to ftrace to figure out what'swrong.

Yeah, its a pain.. something I was thinking of is zero-ing out the entire perf_event_attr and set the field that caused the fail to all 1s (for however many bits that field is wide).

It would destroy the user input, but it also would provide better feedback on what exact field you went astray.