perf fixes
Subsystems (2)
Linus,
Please pull the latest perf-urgent-for-linus git tree from:
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git perf-urgent-for-linus
# HEAD: 652521d460cbfa24ef27717b4b28acfac4281be6 perf/headers: Fix spelling s/EACCESS/EACCES/, s/privilidge/privilege/
Misc fixes: an ABI fix for a reserved field, AMD IBS fixes, an Intel uncore PMU driver fix and a header typo fix.
Thanks,
Ingo
------------------>
Alexander Shishkin (1):
perf/core: Start rejecting the syscall with attr.__reserved_2 set
Geert Uytterhoeven (1):
perf/headers: Fix spelling s/EACCESS/EACCES/, s/privilidge/privilege/
Kan Liang (1):
perf/x86/uncore: Fix event group support
Kim Phillips (2):
perf/x86/amd/ibs: Fix reading of the IBS OpData register and thus precise RIP validity perf/x86/amd/ibs: Handle erratum #420 only on the affected CPU family (10h)
arch/x86/events/amd/ibs.c | 8 +++++--- arch/x86/events/intel/uncore.c | 44 ++++++++++++++++++++++++++++++++++++------ arch/x86/events/intel/uncore.h | 12 ------------ include/linux/perf_event.h | 2 +- kernel/events/core.c | 2 +- 5 files changed, 45 insertions(+), 23 deletions(-)
arch/x86/events/amd/ibs.c +5 -3
@@ -377,7 +377,8 @@ static inline void perf_ibs_disable_event(struct perf_ibs *perf_ibs, struct hw_perf_event *hwc, u64 config) { config &= ~perf_ibs->cnt_mask; -wrmsrl (hwc->config_base, config); +if (boot_cpu_data.x86 ==0x10 ) +wrmsrl (hwc->config_base, config); config &= ~perf_ibs->enable_mask;wrmsrl (hwc->config_base, config); }
@@ -553,7 +554,8 @@ static struct perf_ibs perf_ibs_op = { }, .msr = MSR_AMD64_IBSOPCTL, .config_mask = IBS_OP_CONFIG_MASK, - .cnt_mask = IBS_OP_MAX_CNT, + .cnt_mask =IBS_OP_MAX_CNT |IBS_OP_CUR_CNT | + IBS_OP_CUR_CNT_RAND, .enable_mask = IBS_OP_ENABLE, .valid_mask = IBS_OP_VAL, .max_period = IBS_OP_MAX_CNT << 4,
@@ -614,7 +616,7 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)if (event->attr.sample_type &PERF_SAMPLE_RAW ) offset_max = perf_ibs->offset_max;else if (check_rip) - offset_max =2 ; + offset_max =3 ;else offset_max =1 ; do {
arch/x86/events/intel/uncore.c +38 -6
70 lines not on this page · raw message
arch/x86/events/intel/uncore.h +0 -12
@@ -441,18 +441,6 @@ static inline int uncore_freerunning_hw_config(struct intel_uncore_box *box,return -EINVAL ; } -static inline void uncore_disable_box (struct intel_uncore_box *box) -{ -if (box->pmu->type->ops->disable_box) - box->pmu->type->ops->disable_box (box); -} - -static inline void uncore_enable_box (struct intel_uncore_box *box) -{ -if (box->pmu->type->ops->enable_box) - box->pmu->type->ops->enable_box (box); -} -static inline void uncore_disable_event (struct intel_uncore_box *box,struct perf_event *event) {
include/linux/perf_event.h +1 -1
@@ -292,7 +292,7 @@ struct pmu {* -EBUSY -- @event is for this PMU but PMU temporarily unavailable * -EINVAL -- @event is for this PMU but @event is not valid * -EOPNOTSUPP -- @event is for this PMU, @event is valid, but not supported -* -EACCESS -- @event is for this PMU, @event is valid, but no privilidges +* -EACCES -- @event is for this PMU, @event is valid, but no privileges * * 0 -- @event is for this PMU and valid *
kernel/events/core.c +1 -1
@@ -10635,7 +10635,7 @@ static int perf_copy_attr(struct perf_event_attr __user *uattr, attr->size = size; -if (attr->__reserved_1) +if (attr->__reserved_1 || attr->__reserved_2)return -EINVAL ;if (attr->sample_type & ~(PERF_SAMPLE_MAX -1 ))
Quoted 3 lines · Ingo Molnar
Alexander Shishkin (1):
perf/core: Start rejecting the syscall with attr.__reserved_2 set
This seems to quite possibly break existing apps. Is there any reason to believe that existing users have actually cleared that field?
It's suspect for another reason too: the commit that added that field just added it to the end of the structure, with the argument that "aux_watermark will only matter for new AUX-aware code, so the old code should still be fine".
So by *definition* those old kinds of users would never have cleared that field, because that field didn't exist.
Honestly, this all shows a worrying complete disregard for backwards compatibility. Calling this a "fix" is questionable, when it is much more likely to break some old user.
I've pulled it, but I need people to be aware that this is utter garbage, and that if anybody ever reports it, this needs to be immediately reverted.
And the people involved should stop claiming this "fixes" anything, and should look hard at their random ABI expansions and "fixes".
The original code that said "old users would not be impacted" is correct. But this "fix" is very very very questionable indeed, and I get the feeling that somebody doesn't understand what ABI is, when they claim that this "fixes" anything.
Linus
The pull request you sent on Fri, 1 Nov 2019 18:48:40 +0100:
The pull request you sent on Fri, 1 Nov 2019 18:48:40 +0100:
Quoted 1 line
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.gitperf-urgent-for-linus
has been merged into torvalds/linux.git:
https://git.kernel.org/torvalds/c/355f83c1d098c27b3d912c7a397c13be17de6476
Thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.wiki.kernel.org/userdoc/prtracker
Quoted 7 lines · Linus Torvalds
Quoted 3 lines · Ingo Molnar
Alexander Shishkin (1):
perf/core: Start rejecting the syscall with attr.__reserved_2 setThis seems to quite possibly break existing apps. Is there any reason to believe that existing users have actually cleared that field?
Yes, there's good reason to believe that the field ends up being zero within the kernel even if old user-space uses the old, smaller structure:
Quoted 7 lines
It's suspect for another reason too: the commit that added that field just added it to the end of the structure, with the argument that "
aux_watermarkwill only matter for new AUX-aware code, so the old code should still be fine".So by *definition* those old kinds of users would never have cleared that field, because that field didn't exist.
Firstly, non-existent fields are initialized to zero by default in the perf ABI: *even if user-space was built well before that new field was introduced in the kernel*.
This is done in perf_copy_attr():
/* Zero the full structure, so that a short copy will be nice. */
memset(attr, 0, sizeof(*attr));The user-space structure that is passed in ('uattr' within that function) might indeed be short and not contain the new field - but we handle this via uattr->size, which is set by user-space - for example 'perf' sets it in event_attr_init() in tools/perf/util/util.c:
/* to capture ABI version */
attr->size = sizeof(*attr);Second, the kernel syscall then checks this size against the kernel's size of attr:
- if uattr->size < kattr_size: then old ABI user-space binary is running on new kernel, and we zero out residual fields.
- if uattr->size = kattr_size, this is the normal case of matching ABI (sizes)
- if uattr->size > kattr_size, then new ABI user-space binary is running on an old kernel - in this case the kernel copies in a shortened structure from the new tool and ignores the remaining fields. To not surprise new user-space the kernel copies back the recognized size to uattr->size and user-space can decide whether they want to run on this kernel or not:
put_user(sizeof(*attr), &uattr->size);
This way the perf ABI is *both* backwards and forward compatible, even across multiple ABI changes. We seemingly didn't "care" about the zeroing of the new field in old binaries in that changelog, because it's guaranteed by the syscall implementation.
[ Note that there *is* still potential for more subtle ABI breakage, see
further below. ]
This actually works fine in practice. I just tried out a brand new kernel with really old perf tooling in the v3.19 kernel, from 4.5 years ago:
$ git checkout v3.19 $ cd tools/perf/ $ make install WERROR=0
$ perf version perf version 3.19.gbfa76d49
$ perf top
This ancient version of 'perf top', 'perf record', 'perf report' works just fine on the new kernel, despite passing in a smaller 'attr':
[ 558.408907] perf kABI size: 112 [ 558.412855] perf uABI size: 104
when running new tooling on a new kernel the two sizes match:
[ 331.598089] perf kABI size: 112 [ 331.602050] perf uABI size: 112
and everything works as usual.
Quoted 7 lines
And the people involved should stop claiming this "fixes" anything, and should look hard at their random ABI expansions and "fixes".
The original code that said "old users would not be impacted" is correct. But this "fix" is very very very questionable indeed, and I get the feeling that somebody doesn't understand what ABI is, when they claim that this "fixes" anything.
So if your primary worry is about old user-space that didn't know about the new field passing in the new field with some random value, then if the perf ABI handling logic I outlined above is implemented correctly (and I believe it's correct), then this should not happen.
But arguably there's still a more subtle ABI breakage that this change might trigger: if due to some bug user-space *does* initialize this reserved field to non-zero, and the kernel ignored that non-zero field for 4 years and new kernels suddenly start rejecting these syscalls, that's an ABI bug and grounds for an immediate revert.
I don't expect this to happen though, due to the combination of how user-space and kernel-space handles the extension of the perf ABI - but if it happens then it's an insta-revert.
I'd also argue that because we didn't check this field for years, the next ABI extension should probably not use the __reserved_2 field - which is a good idea anyway as it has an unnatural __u16 size.
Quoted 7 lines
Honestly, this all shows a worrying complete disregard for backwards compatibility. Calling this a "fix" is questionable, when it is much more likely to break some old user.
I've pulled it, but I need people to be aware that this is utter garbage, and that if anybody ever reports it, this needs to be immediately reverted.
In light of the above logic I'd like to push back against this
characterization: this kind of ABI compatibility is actually one of the most important aspects of the perf ABI, and we very much take it seriously.
In fact we are proud of it: the perf ABI is one of the most iterated, yet also one of the most compatible and tooling-friendly ABIs in the kernel. :-)
The reason it wasn't even mentioned in the changelog is because we tried to design out this kind of mishap from the ABI, and all the main perf developers are aware of that design. Will try to include a more careful explanation next time.
Thanks,
Ingo
Quoted 18 lines · Ingo Molnar
This actually works fine in practice. I just tried out a brand new kernel with really old perf tooling in the v3.19 kernel, from 4.5 years ago:
$ git checkout v3.19 $ cd tools/perf/ $ make install WERROR=0$ perf version perf version 3.19.gbfa76d49$ perf topThis ancient version of 'perf top', 'perf record', 'perf report' works just fine on the new kernel, despite passing in a smaller 'attr':
[ 558.408907] perf kABI size: 112 [ 558.412855] perf uABI size: 104
Quoted 6 lines
when running new tooling on a new kernel the two sizes match:
[ 331.598089] perf kABI size: 112 [ 331.602050] perf uABI size: 112and everything works as usual.
Out of morbid curiosity I also tried out the v3.2 version of perf tooling, which is now more than 7.5 years old, and 'perf record' + 'perf report' is working fine on the latest kernel too:
$ perf version perf version 3.2.dirty
$ perf record ~/hackbench 20 Time: 0.082 [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.828 MB perf.data (~36166 samples) ]
$ perf report --stdio
...
# Overhead Command Shared Object Symbol
# ........ ......... ................. ..........................................
#
6.99% hackbench [kernel.kallsyms] [k] queued_spin_lock_slowpath
6.22% hackbench [kernel.kallsyms] [k] entry_SYSENTER_compat
4.06% hackbench [kernel.kallsyms] [k] sysret32_from_system_call
3.56% hackbench [kernel.kallsyms] [k] do_fast_syscall_32
3.40% hackbench [kernel.kallsyms] [k] unix_stream_read_generic
3.32% hackbench [kernel.kallsyms] [k] _raw_spin_lock
3.13% hackbench [kernel.kallsyms] [k] __slab_freeIt works despite 'perf record' using an ancient, 72 bytes long 'struct perf_attr' ABI:
[ 3013.531125] perf kABI size: 112 [ 3013.535097] perf uABI size: 72
If I'm reading the logs right then the perf ABI went through around ~15 iterations in the v3.2..v5.4 timeframe that extended struct perf_event_attr - but binary compatibility was maintained with ancient tooling.
This is also useful for tooling bisectability: tooling should work on new and old kernels as well, even if a bisection point moves across ABI extension boundaries.
Thanks,
Ingo
Quoted 15 lines · Ingo Molnar
Firstly, non-existent fields are initialized to zero by default in the perf ABI: *even if user-space was built well before that new field was introduced in the kernel*.
This is done in
perf_copy_attr():/* Zero the full structure, so that a short copy will be nice. */ memset(attr, 0, sizeof(*attr));The user-space structure that is passed in ('uattr' within that function) might indeed be short and not contain the new field - but we handle this via
uattr->size, which is set by user-space - for example 'perf' sets it inevent_attr_init()intools/perf/util/util.c:
Quoted 8 lines
/* to capture ABI version */ attr->size = sizeof(*attr);Second, the kernel syscall then checks this size against the kernel's size of attr:
- if
uattr->size<kattr_size: then old ABI user-space binary is running on new kernel, and we zero out residual fields.
Very good. These were the two pieces I was missing - just readfing the commit messages it wasn't clear that this was safe at all.
Thanks for following up on my worry,
Linus