Skip to contentSkip to conversation

Add support for synchronous signals on perf events

PATCH Superseded Marco Elver 5 participants 39 messages Mar 24, 2021 – Mar 31, 2021

Subsystems (9)
x86 VFS Kselftest perf events +5 more subsystems

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Add support for synchronous signals on perf events

The perf subsystem today unifies various tracing and monitoring features, from both software and hardware. One benefit of the perf subsystem is automatically inheriting events to child tasks, which enables process-wide events monitoring with low overheads. By default perf events are non-intrusive, not affecting behaviour of the tasks being monitored.

For certain use-cases, however, it makes sense to leverage the generality of the perf events subsystem and optionally allow the tasks being monitored to receive signals on events they are interested in. This patch series adds the option to synchronously signal user space on events.

To better support process-wide synchronous self-monitoring, without events propagating to children that do not share the current process's shared environment, two pre-requisite patches are added to optionally restrict inheritance to CLONE_THREAD, and remove events on exec (without affecting the parent).

Examples how to use these features can be found in the tests added at the end of the series. In addition to the tests added, the series has also been subjected to syzkaller fuzzing (focus on 'kernel/events/' coverage).

Motivation and Example Uses
---------------------------

1. Our immediate motivation is low-overhead sampling-based race
detection for user space [1]. By using perf_event_open() at process initialization, we can create hardware breakpoint/watchpoint events that are propagated automatically to all threads in a process. As far as we are aware, today no existing kernel facility (such as ptrace) allows us to set up process-wide watchpoints with minimal overheads (that are comparable to mprotect() of whole pages).

2. Other low-overhead error detectors that rely on detecting
accesses to certain memory locations or code, process-wide and also only in a specific set of subtasks or threads.

[1] https://llvm.org/devmtg/2020-09/slides/Morehouse-GWP-Tsan.pdf

Other ideas for use-cases we found interesting, but should only illustrate the range of potential to further motivate the utility (we're sure there are more):

3. Code hot patching without full stop-the-world. Specifically, by
setting a code breakpoint to entry to the patched routine, then send signals to threads and check that they are not in the routine, but without stopping them further. If any of the threads will enter the routine, it will receive SIGTRAP and pause.

4. Safepoints without mprotect(). Some Java implementations use
"load from a known memory location" as a safepoint. When threads need to be stopped, the page containing the location is mprotect()ed and threads get a signal. This could be replaced with a watchpoint, which does not require a whole page nor DTLB shootdowns.

5. Threads receiving signals on performance events to
throttle/unthrottle themselves.

6. Tracking data flow globally.

Changelog
---------

v3:
* Add patch "perf: Rework perf_event_exit_event()" to beginning of series, courtesy of Peter Zijlstra.
* Rework "perf: Add support for event removal on exec" based on the added "perf: Rework perf_event_exit_event()".
* Fix kselftests to work with more recent libc, due to the way it forces using the kernel's own siginfo_t.
* Add basic perf-tool built-in test.

v2/RFC: https://lkml.kernel.org/r/[email protected]
* Patch "Support only inheriting events if cloned with CLONE_THREAD" added to series.
* Patch "Add support for event removal on exec" added to series.
* Patch "Add kselftest for process-wide sigtrap handling" added to series.
* Patch "Add kselftest for remove_on_exec" added to series.
* Implicitly restrict inheriting events if sigtrap, but the child was cloned with CLONE_CLEAR_SIGHAND, because it is not generally safe if the child cleared all signal handlers to continue sending SIGTRAP.
* Various minor fixes (see details in patches).

v1/RFC: https://lkml.kernel.org/r/[email protected]

Pre-series: The discussion at [2] led to the changes in this series. The approach taken in "Add support for SIGTRAP on perf events" to trigger the signal was suggested by Peter Zijlstra in [3].

[2] https://lore.kernel.org/lkml/CACT4Y+YPrXGw+AtESxAgPyZ84TYkNZdP0xpocX2jwVAbZD=-XQ@mail.gmail.com/

[3] https://lore.kernel.org/lkml/[email protected]/

Marco Elver (10):
perf: Apply PERF_EVENT_IOC_MODIFY_ATTRIBUTES to children
perf: Support only inheriting events if cloned with CLONE_THREAD
perf: Add support for event removal on exec
signal: Introduce TRAP_PERF si_code and si_perf to siginfo
perf: Add support for SIGTRAP on perf events
perf: Add breakpoint information to siginfo on SIGTRAP selftests/perf_events: Add kselftest for process-wide sigtrap handling selftests/perf_events: Add kselftest for remove_on_exec tools headers uapi: Sync tools/include/uapi/linux/perf_event.h perf test: Add basic stress test for sigtrap handling

Peter Zijlstra (1):
perf: Rework perf_event_exit_event()

 arch/m68k/kernel/signal.c                     |   3 +
 arch/x86/kernel/signal_compat.c               |   5 +-
 fs/signalfd.c                                 |   4 +
 include/linux/compat.h                        |   2 +
 include/linux/perf_event.h                    |   6 +-
 include/linux/signal.h                        |   1 +
 include/uapi/asm-generic/siginfo.h            |   6 +-
 include/uapi/linux/perf_event.h               |   5 +-
 include/uapi/linux/signalfd.h                 |   4 +-
 kernel/events/core.c                          | 297 +++++++++++++-----
 kernel/fork.c                                 |   2 +-
 kernel/signal.c                               |  11 +
 tools/include/uapi/linux/perf_event.h         |   5 +-
 tools/perf/tests/Build                        |   1 +
 tools/perf/tests/builtin-test.c               |   5 +
 tools/perf/tests/sigtrap.c                    | 148 +++++++++
 tools/perf/tests/tests.h                      |   1 +
 .../testing/selftests/perf_events/.gitignore  |   3 +
 tools/testing/selftests/perf_events/Makefile  |   6 +
 tools/testing/selftests/perf_events/config    |   1 +
 .../selftests/perf_events/remove_on_exec.c    | 260 +++++++++++++++
 tools/testing/selftests/perf_events/settings  |   1 +
 .../selftests/perf_events/sigtrap_threads.c   | 206 ++++++++++++
 23 files changed, 896 insertions(+), 87 deletions(-)
 create mode 100644 tools/perf/tests/sigtrap.c
 create mode 100644 tools/testing/selftests/perf_events/.gitignore
 create mode 100644 tools/testing/selftests/perf_events/Makefile
 create mode 100644 tools/testing/selftests/perf_events/config
 create mode 100644 tools/testing/selftests/perf_events/remove_on_exec.c
 create mode 100644 tools/testing/selftests/perf_events/settings
 create mode 100644 tools/testing/selftests/perf_events/sigtrap_threads.c
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
#1 lore raw

PATCH v3 1/11 perf: Rework perf_event_exit_event()

From: Peter Zijlstra <[email protected]>

Make perf_event_exit_event() more robust, such that we can use it from other contexts. Specifically the up and coming remove_on_exec.

For this to work we need to address a few issues. Remove_on_exec will not destroy the entire context, so we cannot rely on TASK_TOMBSTONE to disable event_function_call() and we thus have to use perf_remove_from_context().

When using perf_remove_from_context(), there's two races to consider. The first is against close(), where we can have concurrent tear-down of the event. The second is against child_list iteration, which should not find a half baked event.

To address this, teach perf_remove_from_context() to special case !ctx->is_active and about DETACH_CHILD.

---
v3:
* New dependency for series:
https://lkml.kernel.org/r/YFn/[email protected]

---
 include/linux/perf_event.h |   1 +
 kernel/events/core.c       | 142 +++++++++++++++++++++----------------
 2 files changed, 80 insertions(+), 63 deletions(-)
include/linux/perf_event.h +1 -0
index 3f7f89ea5e51..3d478abf411c 100644
@@ -607,6 +607,7 @@ struct swevent_hlist {
 #define PERF_ATTACH_TASK_DATA	0x08
 #define PERF_ATTACH_ITRACE	0x10
 #define PERF_ATTACH_SCHED_CB	0x20
+#define PERF_ATTACH_CHILD	0x40
 
 struct perf_cgroup;
 struct perf_buffer;
kernel/events/core.c +79 -63

209 lines not on this page · raw message

Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
#2 lore raw

Re: [PATCH v3 01/11] perf: Rework perf_event_exit_event()

Quoted 18 lines · Marco Elver

From: Peter Zijlstra <[email protected]>

Make perf_event_exit_event() more robust, such that we can use it from other contexts. Specifically the up and coming remove_on_exec.

For this to work we need to address a few issues. Remove_on_exec will not destroy the entire context, so we cannot rely on TASK_TOMBSTONE to disable event_function_call() and we thus have to use perf_remove_from_context().

When using perf_remove_from_context(), there's two races to consider. The first is against close(), where we can have concurrent tear-down of the event. The second is against child_list iteration, which should not find a half baked event.

To address this, teach perf_remove_from_context() to special case !ctx->is_active and about DETACH_CHILD.

Quoted 7 lines

---
v3:
* New dependency for series:
https://lkml.kernel.org/r/YFn/[email protected]
---

syzkaller found a crash with stack trace pointing at changes in this patch. Can't tell if this is an old issue or introduced in this series.

It looks like task_pid_ptr() wants to access task_struct::signal, but the task_struct pointer is NULL.

Any ideas?

general protection fault, probably for non-canonical address 0xdffffc0000000103: 0000 [#1] PREEMPT SMP KASAN
KASAN: null-ptr-deref in range [0x0000000000000818-0x000000000000081f]
CPU: 2 PID: 15084 Comm: syz-executor.1 Not tainted 5.12.0-rc4+ #5
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-2 04/01/2014
RIP: 0010:task_pid_ptr kernel/pid.c:325 [inline]
RIP: 0010:__task_pid_nr_ns+0x137/0x3e0 kernel/pid.c:500
Code: 8b 75 00 eb 08 e8 59 28 29 00 45 31 f6 31 ff 44 89 fe e8 5c 2c 29 00 45 85 ff 74 49 48 81 c3 20 08 00 00 48 89 d8 48 c1 e8 03 <42> 80 3c 20 00 74 08 48 89 df e8 aa 03 6d 00 48 8b 2b 44 89 fb bf
RSP: 0018:ffffc9000c76f6d0 EFLAGS: 00010007
RAX: 0000000000000103 RBX: 000000000000081f RCX: ffff8880717d8000
RDX: ffff8880717d8000 RSI: 0000000000000001 RDI: 0000000000000000
RBP: 0000000000000001 R08: ffffffff814fe814 R09: fffffbfff1f296b1
R10: fffffbfff1f296b1 R11: 0000000000000000 R12: dffffc0000000000
R13: 1ffff1100e6dfc5c R14: ffff888057fba108 R15: 0000000000000001
FS:  0000000000000000(0000) GS:ffff88802cf00000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007ffcc3b05bc0 CR3: 0000000040ac0000 CR4: 0000000000750ee0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000600
PKRU: 55555554
Call Trace:
 perf_event_pid_type kernel/events/core.c:1412 [inline]
 perf_event_pid kernel/events/core.c:1421 [inline]
 perf_event_read_event kernel/events/core.c:7511 [inline]
 sync_child_event kernel/events/core.c:12521 [inline]
 perf_child_detach kernel/events/core.c:2223 [inline]
 __perf_remove_from_context+0x569/0xd30 kernel/events/core.c:2359
 perf_remove_from_context+0x19d/0x220 kernel/events/core.c:2395
 perf_event_exit_event+0x76/0x950 kernel/events/core.c:12559
 perf_event_exit_task_context kernel/events/core.c:12640 [inline]
 perf_event_exit_task+0x715/0xa40 kernel/events/core.c:12673
 do_exit+0x6c2/0x2290 kernel/exit.c:834
 do_group_exit+0x168/0x2d0 kernel/exit.c:922
 get_signal+0x1734/0x1ef0 kernel/signal.c:2779
 arch_do_signal_or_restart+0x41/0x620 arch/x86/kernel/signal.c:789
 handle_signal_work kernel/entry/common.c:147 [inline]
 exit_to_user_mode_loop kernel/entry/common.c:171 [inline]
 exit_to_user_mode_prepare+0xac/0x1e0 kernel/entry/common.c:208
 irqentry_exit_to_user_mode+0x6/0x40 kernel/entry/common.c:314
 exc_general_protection+0x222/0x370 arch/x86/kernel/traps.c:530
 asm_exc_general_protection+0x1e/0x30 arch/x86/include/asm/idtentry.h:571
#3 lore raw

Re: [PATCH v3 01/11] perf: Rework perf_event_exit_event()

Quoted 27 lines · Marco Elver
Quoted 18 lines · Marco Elver

From: Peter Zijlstra <[email protected]>

Make perf_event_exit_event() more robust, such that we can use it from other contexts. Specifically the up and coming remove_on_exec.

For this to work we need to address a few issues. Remove_on_exec will not destroy the entire context, so we cannot rely on TASK_TOMBSTONE to disable event_function_call() and we thus have to use perf_remove_from_context().

When using perf_remove_from_context(), there's two races to consider. The first is against close(), where we can have concurrent tear-down of the event. The second is against child_list iteration, which should not find a half baked event.

To address this, teach perf_remove_from_context() to special case !ctx->is_active and about DETACH_CHILD.

Quoted 7 lines

---
v3:
* New dependency for series:
https://lkml.kernel.org/r/YFn/[email protected]
---

Quoted 2 lines

syzkaller found a crash with stack trace pointing at changes in this patch. Can't tell if this is an old issue or introduced in this series.

Yay, I found a reproducer. v5.12-rc4 is good, and sadly with this patch only we crash. :-/

Here's a stacktrace with just this patch applied:

| BUG: kernel NULL pointer dereference, address: 00000000000007af
| #PF: supervisor read access in kernel mode
| #PF: error_code(0x0000) - not-present page
| PGD 0 P4D 0
| Oops: 0000 [#1] PREEMPT SMP PTI
| CPU: 7 PID: 465 Comm: a.out Not tainted 5.12.0-rc4+ #25
| Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-2 04/01/2014
| RIP: 0010:task_pid_ptr kernel/pid.c:324 [inline]
| RIP: 0010:__task_pid_nr_ns+0x112/0x240 kernel/pid.c:500
| Code: e8 13 55 07 00 e8 1e a6 0e 00 48 c7 c6 83 1e 0b 81 48 c7 c7 a0 2e d5 82 e8 4b 08 04 00 44 89 e0 5b 5d 41 5c c3 e8 fe a5 0e 00 <48> 8b 85 b0 07 00 00 4a 8d ac e0 98 01 00 00 e9 5a ff ff ff e8 e5
| RSP: 0000:ffffc90001b73a60 EFLAGS: 00010093
| RAX: 0000000000000000 RBX: ffffffff82c69820 RCX: ffffffff810b1eb2
| RDX: ffff888108d143c0 RSI: 0000000000000000 RDI: ffffffff8299ccc6
| RBP: ffffffffffffffff R08: 0000000000000001 R09: 0000000000000000
| R10: ffff888108d14db8 R11: 0000000000000000 R12: 0000000000000001
| R13: ffffffffffffffff R14: ffffffffffffffff R15: ffff888108e05240
| FS:  0000000000000000(0000) GS:ffff88842fdc0000(0000) knlGS:0000000000000000
| CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
| CR2: 00000000000007af CR3: 0000000002c22002 CR4: 0000000000770ee0
| DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
| DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
| PKRU: 55555554
| Call Trace:
|  perf_event_pid_type kernel/events/core.c:1412 [inline]
|  perf_event_pid kernel/events/core.c:1421 [inline]
|  perf_event_read_event+0x78/0x1d0 kernel/events/core.c:7406
|  sync_child_event kernel/events/core.c:12404 [inline]
|  perf_child_detach kernel/events/core.c:2223 [inline]
|  __perf_remove_from_context+0x14d/0x280 kernel/events/core.c:2359
|  perf_remove_from_context+0x9f/0xf0 kernel/events/core.c:2395
|  perf_event_exit_event kernel/events/core.c:12442 [inline]
|  perf_event_exit_task_context kernel/events/core.c:12523 [inline]
|  perf_event_exit_task+0x276/0x4c0 kernel/events/core.c:12556
|  do_exit+0x4cd/0xed0 kernel/exit.c:834
|  do_group_exit+0x4d/0xf0 kernel/exit.c:922
|  get_signal+0x1d2/0xf30 kernel/signal.c:2777
|  arch_do_signal_or_restart+0xf7/0x750 arch/x86/kernel/signal.c:789
|  handle_signal_work kernel/entry/common.c:147 [inline]
|  exit_to_user_mode_loop kernel/entry/common.c:171 [inline]
|  exit_to_user_mode_prepare+0x113/0x190 kernel/entry/common.c:208
|  irqentry_exit_to_user_mode+0x6/0x30 kernel/entry/common.c:314
|  asm_exc_general_protection+0x1e/0x30 arch/x86/include/asm/idtentry.h:571

Attached is a C reproducer of the syzkaller program that crashes us.

Thanks,
-- Marco

// autogenerated by syzkaller (https://github.com/google/syzkaller)
/*
Generated from this syzkaller program:

clone(0x88004400, 0x0, 0x0, 0x0, 0x0)
perf_event_open(&(0x7f00000003c0)={0x4, 0x70, 0x40, 0x1, 0x3, 0x1, 0x0, 0x6, 0x10001, 0x0, 0x0, 0x1, 0x0, 0x1, 0x1, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x1, 0x1, 0x1, 0x1, 0x0, 0x1, 0x1, 0x1, 0x0, 0x1, 0x1, 0x0, 0x1, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x80000001, 0x2, @perf_bp={&(0x7f0000000380), 0xd}, 0x1000, 0x6, 0x0, 0x4, 0x1, 0x4, 0x8}, 0x0, 0xffffffffffffffff, 0xffffffffffffffff, 0x1)
clone(0x8000, &(0x7f0000000200)="3017248985480229c715f01f2776139977f49770d8181077dce816423a929ed5e59bf26ca77f2ba311b783dda29870d621ff2394424d9c799be5fa29f1ee42102645b56fd9727401d2fe52073c20023d4623dd48522d13dff56af96e4d73f53d62f3de841a58436c591733b58072f04a49bd5cf0473e3f568b604959c06365a82e0e1350550271c25298", &(0x7f0000000100), &(0x7f0000000140), &(0x7f00000002c0)="8c0e32ae8f2716cdf998f341eb4ff0b404c7dca07d9e895c109603d3552c42f07c0190860e4c880d03ba867e8d5d738172839bdbe974d38580e5bc8a91713bee4b859c1a4500f61f197d3610ef2f515474d0b302af29f64053899418054cdf0afe2e75f313f92daf84b3f77cdb10d9d002c44bf43d0cb532cce29b249aab4d6e8218e2528c95453d255e31715422b9d3014c35603fa361ec70136322a7366868f53b78b7c369496dc39cf8ea248b7345e378") */

#define _GNU_SOURCE

#include <endian.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>

#define BITMASK(bf_off, bf_len) (((1ull << (bf_len)) - 1) << (bf_off))

#define STORE_BY_BITMASK(type, htobe, addr, val, bf_off, bf_len)               \
  *(type*)(addr) =                                                             \
      htobe((htobe(*(type*)(addr)) & ~BITMASK((bf_off), (bf_len))) |           \
            (((type)(val) << (bf_off)) & BITMASK((bf_off), (bf_len))))

int main(void)
{
syscall(__NR_mmap, 0x1ffff000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul); syscall(__NR_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x32ul, -1, 0ul); syscall(__NR_mmap, 0x21000000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul); syscall(__NR_clone, 0x88004400ul, 0ul, 0ul, 0ul, 0ul); *(uint32_t*)0x200003c0 = 4;
*(uint32_t*)0x200003c4 = 0x70;
*(uint8_t*)0x200003c8 = 0x40;
*(uint8_t*)0x200003c9 = 1;
*(uint8_t*)0x200003ca = 3;
*(uint8_t*)0x200003cb = 1;
*(uint32_t*)0x200003cc = 0;
*(uint64_t*)0x200003d0 = 6;
*(uint64_t*)0x200003d8 = 0x10001;
*(uint64_t*)0x200003e0 = 0;
STORE_BY_BITMASK(uint64_t, , 0x200003e8, 0, 0, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 1, 1, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 0, 2, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 1, 3, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 1, 4, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 0, 5, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 1, 6, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 0, 7, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 0, 8, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 0, 9, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 0, 10, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 1, 11, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 0, 12, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 0, 13, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 0, 14, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 0, 15, 2); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 0, 17, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 0, 18, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 1, 19, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 1, 20, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 1, 21, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 1, 22, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 1, 23, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 0, 24, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 1, 25, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 1, 26, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 1, 27, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 0, 28, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 1, 29, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 1, 30, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 0, 31, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 1, 32, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 0, 33, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 1, 34, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 0, 35, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 0, 36, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 0, 37, 1); STORE_BY_BITMASK(uint64_t, , 0x200003e8, 0, 38, 26); *(uint32_t*)0x200003f0 = 0x80000001; *(uint32_t*)0x200003f4 = 2;
*(uint64_t*)0x200003f8 = 0x20000380; *(uint64_t*)0x20000400 = 0xd;
*(uint64_t*)0x20000408 = 0x1000;
*(uint64_t*)0x20000410 = 6;
*(uint32_t*)0x20000418 = 0;
*(uint32_t*)0x2000041c = 4;
*(uint64_t*)0x20000420 = 1;
*(uint32_t*)0x20000428 = 4;
*(uint16_t*)0x2000042c = 8;
*(uint16_t*)0x2000042e = 0;
syscall(__NR_perf_event_open, 0x200003c0ul, 0, -1ul, -1, 1ul); memcpy(

      (void*)0x20000200,
      "\x30\x17\x24\x89\x85\x48\x02\x29\xc7\x15\xf0\x1f\x27\x76\x13\x99\x77\xf4"
      "\x97\x70\xd8\x18\x10\x77\xdc\xe8\x16\x42\x3a\x92\x9e\xd5\xe5\x9b\xf2\x6c"
      "\xa7\x7f\x2b\xa3\x11\xb7\x83\xdd\xa2\x98\x70\xd6\x21\xff\x23\x94\x42\x4d"
      "\x9c\x79\x9b\xe5\xfa\x29\xf1\xee\x42\x10\x26\x45\xb5\x6f\xd9\x72\x74\x01"
      "\xd2\xfe\x52\x07\x3c\x20\x02\x3d\x46\x23\xdd\x48\x52\x2d\x13\xdf\xf5\x6a"
      "\xf9\x6e\x4d\x73\xf5\x3d\x62\xf3\xde\x84\x1a\x58\x43\x6c\x59\x17\x33\xb5"
      "\x80\x72\xf0\x4a\x49\xbd\x5c\xf0\x47\x3e\x3f\x56\x8b\x60\x49\x59\xc0\x63"
      "\x65\xa8\x2e\x0e\x13\x50\x55\x02\x71\xc2\x52\x98",
      138);
  memcpy(
      (void*)0x200002c0,
      "\x8c\x0e\x32\xae\x8f\x27\x16\xcd\xf9\x98\xf3\x41\xeb\x4f\xf0\xb4\x04\xc7"
      "\xdc\xa0\x7d\x9e\x89\x5c\x10\x96\x03\xd3\x55\x2c\x42\xf0\x7c\x01\x90\x86"
      "\x0e\x4c\x88\x0d\x03\xba\x86\x7e\x8d\x5d\x73\x81\x72\x83\x9b\xdb\xe9\x74"
      "\xd3\x85\x80\xe5\xbc\x8a\x91\x71\x3b\xee\x4b\x85\x9c\x1a\x45\x00\xf6\x1f"
      "\x19\x7d\x36\x10\xef\x2f\x51\x54\x74\xd0\xb3\x02\xaf\x29\xf6\x40\x53\x89"
      "\x94\x18\x05\x4c\xdf\x0a\xfe\x2e\x75\xf3\x13\xf9\x2d\xaf\x84\xb3\xf7\x7c"
      "\xdb\x10\xd9\xd0\x02\xc4\x4b\xf4\x3d\x0c\xb5\x32\xcc\xe2\x9b\x24\x9a\xab"
      "\x4d\x6e\x82\x18\xe2\x52\x8c\x95\x45\x3d\x25\x5e\x31\x71\x54\x22\xb9\xd3"
      "\x01\x4c\x35\x60\x3f\xa3\x61\xec\x70\x13\x63\x22\xa7\x36\x68\x68\xf5\x3b"
      "\x78\xb7\xc3\x69\x49\x6d\xc3\x9c\xf8\xea\x24\x8b\x73\x45\xe3\x78",
      178);
  syscall(__NR_clone, 0x8000ul, 0x20000200ul, 0x20000100ul, 0x20000140ul,
          0x200002c0ul);

return 0;
}

#4 lore raw

Re: [PATCH v3 01/11] perf: Rework perf_event_exit_event()

On Thu, Mar 25, 2021 at 05:17PM +0100, Marco Elver wrote:
[...]

Quoted 9 lines
Quoted 2 lines

syzkaller found a crash with stack trace pointing at changes in this patch. Can't tell if this is an old issue or introduced in this series.

Yay, I found a reproducer. v5.12-rc4 is good, and sadly with this patch only we crash. :-/

Here's a stacktrace with just this patch applied:

| BUG: kernel NULL pointer dereference, address: 00000000000007af

[...]

Quoted 2 lines

| RIP: 0010:task_pid_ptr kernel/pid.c:324 [inline] | RIP: 0010:__task_pid_nr_ns+0x112/0x240 kernel/pid.c:500

[...]

Quoted 12 lines
| Call Trace:
|  perf_event_pid_type kernel/events/core.c:1412 [inline]
|  perf_event_pid kernel/events/core.c:1421 [inline]
|  perf_event_read_event+0x78/0x1d0 kernel/events/core.c:7406
|  sync_child_event kernel/events/core.c:12404 [inline]
|  perf_child_detach kernel/events/core.c:2223 [inline]
|  __perf_remove_from_context+0x14d/0x280 kernel/events/core.c:2359
|  perf_remove_from_context+0x9f/0xf0 kernel/events/core.c:2395
|  perf_event_exit_event kernel/events/core.c:12442 [inline]
|  perf_event_exit_task_context kernel/events/core.c:12523 [inline]
|  perf_event_exit_task+0x276/0x4c0 kernel/events/core.c:12556
|  do_exit+0x4cd/0xed0 kernel/exit.c:834
Quoted 8 lines
|  do_group_exit+0x4d/0xf0 kernel/exit.c:922
|  get_signal+0x1d2/0xf30 kernel/signal.c:2777
|  arch_do_signal_or_restart+0xf7/0x750 arch/x86/kernel/signal.c:789
|  handle_signal_work kernel/entry/common.c:147 [inline]
|  exit_to_user_mode_loop kernel/entry/common.c:171 [inline]
|  exit_to_user_mode_prepare+0x113/0x190 kernel/entry/common.c:208
|  irqentry_exit_to_user_mode+0x6/0x30 kernel/entry/common.c:314
|  asm_exc_general_protection+0x1e/0x30 arch/x86/include/asm/idtentry.h:571

I spun up gdb, and it showed me this:

| #0  perf_event_read_event (event=event@entry=0xffff888107cd5000, task=task@entry=0xffffffffffffffff)
|     at kernel/events/core.c:7397
									^^^ TASK_TOMBSTONE
| #1  0xffffffff811fc9cd in sync_child_event (child_event=0xffff888107cd5000) at kernel/events/core.c:12404
| #2  perf_child_detach (event=0xffff888107cd5000) at kernel/events/core.c:2223
| #3  __perf_remove_from_context (event=event@entry=0xffff888107cd5000, cpuctx=cpuctx@entry=0xffff88842fdf0c00,
|     ctx=ctx@entry=0xffff8881073cb800, info=info@entry=0x3 <fixed_percpu_data+3>) at kernel/events/core.c:2359
| #4  0xffffffff811fcb9f in perf_remove_from_context (event=event@entry=0xffff888107cd5000, flags=flags@entry=3)
|     at kernel/events/core.c:2395
| #5  0xffffffff81204526 in perf_event_exit_event (ctx=0xffff8881073cb800, event=0xffff888107cd5000)
|     at kernel/events/core.c:12442
| #6  perf_event_exit_task_context (ctxn=0, child=0xffff88810531a200) at kernel/events/core.c:12523
| #7  perf_event_exit_task (child=0xffff88810531a200) at kernel/events/core.c:12556
| #8  0xffffffff8108838d in do_exit (code=code@entry=11) at kernel/exit.c:834
| #9  0xffffffff81088e4d in do_group_exit (exit_code=11) at kernel/exit.c:922

and therefore synthesized this fix on top:

kernel/events/core.c +1 -1
index 57de8d436efd..e77294c7e654 100644
@@ -12400,7 +12400,7 @@ static void sync_child_event(struct perf_event *child_event)
 	if (child_event->attr.inherit_stat) {
 		struct task_struct *task = child_event->ctx->task;
 
-		if (task)
+		if (task && task != TASK_TOMBSTONE)
 			perf_event_read_event(child_event, task);
 	}
 

which fixes the problem. My guess is that the parent and child are both racing to exit?

Does that make any sense?

Thanks,
-- Marco

maintainer
#5 lore raw

Re: [PATCH v3 01/11] perf: Rework perf_event_exit_event()

Quoted 18 lines · Marco Elver

and therefore synthesized this fix on top:

kernel/events/core.c +1 -1
index 57de8d436efd..e77294c7e654 100644
@@ -12400,7 +12400,7 @@ static void sync_child_event(struct perf_event *child_event)
 	if (child_event->attr.inherit_stat) {
 		struct task_struct *task = child_event->ctx->task;
 
-		if (task)
+		if (task && task != TASK_TOMBSTONE)
 			perf_event_read_event(child_event, task);
 	}
 

which fixes the problem. My guess is that the parent and child are both racing to exit?

Quoted 1 line

Does that make any sense?

Yes, I think it does. ACK

#6 lore raw

PATCH v3 2/11 perf: Apply PERF_EVENT_IOC_MODIFY_ATTRIBUTES to children

In reply to Marco Elver

As with other ioctls (such as PERF_EVENT_IOC_{ENABLE,DISABLE}), fix up handling of PERF_EVENT_IOC_MODIFY_ATTRIBUTES to also apply to children.

---
 kernel/events/core.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
kernel/events/core.c +21 -1
index 57de8d436efd..37d106837962 100644
@@ -3199,16 +3199,36 @@ static int perf_event_modify_breakpoint(struct perf_event *bp,
 static int perf_event_modify_attr(struct perf_event *event,
 				  struct perf_event_attr *attr)
 {
+	int (*func)(struct perf_event *, struct perf_event_attr *);
+	struct perf_event *child;
+	int err;
+
 	if (event->attr.type != attr->type)
 		return -EINVAL;
 
 	switch (event->attr.type) {
 	case PERF_TYPE_BREAKPOINT:
-		return perf_event_modify_breakpoint(event, attr);
+		func = perf_event_modify_breakpoint;
+		break;
 	default:
 		/* Place holder for future additions. */
 		return -EOPNOTSUPP;
 	}
+
+	WARN_ON_ONCE(event->ctx->parent_ctx);
+
+	mutex_lock(&event->child_mutex);
+	err = func(event, attr);
+	if (err)
+		goto out;
+	list_for_each_entry(child, &event->child_list, child_list) {
+		err = func(child, attr);
+		if (err)
+			goto out;
+	}
+out:
+	mutex_unlock(&event->child_mutex);
+	return err;
 }
 
 static void ctx_sched_out(struct perf_event_context *ctx,
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
#7 lore raw

PATCH v3 3/11 perf: Support only inheriting events if cloned with CLONE_THREAD

In reply to Marco Elver

Adds bit perf_event_attr::inherit_thread, to restricting inheriting events only if the child was cloned with CLONE_THREAD.

This option supports the case where an event is supposed to be process-wide only (including subthreads), but should not propagate beyond the current process's shared environment.

---
v2:
* Add patch to series.

---
 include/linux/perf_event.h      |  5 +++--
 include/uapi/linux/perf_event.h |  3 ++-
 kernel/events/core.c            | 21 ++++++++++++++-------
 kernel/fork.c                   |  2 +-
 4 files changed, 20 insertions(+), 11 deletions(-)
include/linux/perf_event.h +3 -2
index 3d478abf411c..1660039199b2 100644
@@ -958,7 +958,7 @@ extern void __perf_event_task_sched_in(struct task_struct *prev,
 				       struct task_struct *task);
 extern void __perf_event_task_sched_out(struct task_struct *prev,
 					struct task_struct *next);
-extern int perf_event_init_task(struct task_struct *child);
+extern int perf_event_init_task(struct task_struct *child, u64 clone_flags);
 extern void perf_event_exit_task(struct task_struct *child);
 extern void perf_event_free_task(struct task_struct *task);
 extern void perf_event_delayed_put(struct task_struct *task);
@@ -1449,7 +1449,8 @@ perf_event_task_sched_in(struct task_struct *prev,
 static inline void
 perf_event_task_sched_out(struct task_struct *prev,
 			  struct task_struct *next)			{ }
-static inline int perf_event_init_task(struct task_struct *child)	{ return 0; }
+static inline int perf_event_init_task(struct task_struct *child,
+				       u64 clone_flags)			{ return 0; }
 static inline void perf_event_exit_task(struct task_struct *child)	{ }
 static inline void perf_event_free_task(struct task_struct *task)	{ }
 static inline void perf_event_delayed_put(struct task_struct *task)	{ }
include/uapi/linux/perf_event.h +2 -1
index ad15e40d7f5d..813efb65fea8 100644
@@ -389,7 +389,8 @@ struct perf_event_attr {
 				cgroup         :  1, /* include cgroup events */
 				text_poke      :  1, /* include text poke events */
 				build_id       :  1, /* use build id in mmap2 events */
-				__reserved_1   : 29;
+				inherit_thread :  1, /* children only inherit if cloned with CLONE_THREAD */
+				__reserved_1   : 28;
 
 	union {
 		__u32		wakeup_events;	  /* wakeup every n events */
kernel/events/core.c +14 -7
index 37d106837962..224cbcf6125a 100644
@@ -11649,6 +11649,9 @@ static int perf_copy_attr(struct perf_event_attr __user *uattr,
 	    (attr->sample_type & PERF_SAMPLE_WEIGHT_STRUCT))
 		return -EINVAL;
 
+	if (!attr->inherit && attr->inherit_thread)
+		return -EINVAL;
+
 out:
 	return ret;
 
@@ -12869,12 +12872,13 @@ static int
 inherit_task_group(struct perf_event *event, struct task_struct *parent,
 		   struct perf_event_context *parent_ctx,
 		   struct task_struct *child, int ctxn,
-		   int *inherited_all)
+		   u64 clone_flags, int *inherited_all)
 {
 	int ret;
 	struct perf_event_context *child_ctx;
 
-	if (!event->attr.inherit) {
+	if (!event->attr.inherit ||
+	    (event->attr.inherit_thread && !(clone_flags & CLONE_THREAD))) {
 		*inherited_all = 0;
 		return 0;
 	}
@@ -12906,7 +12910,8 @@ inherit_task_group(struct perf_event *event, struct task_struct *parent,
 /*
  * Initialize the perf_event context in task_struct
  */
-static int perf_event_init_context(struct task_struct *child, int ctxn)
+static int perf_event_init_context(struct task_struct *child, int ctxn,
+				   u64 clone_flags)
 {
 	struct perf_event_context *child_ctx, *parent_ctx;
 	struct perf_event_context *cloned_ctx;
@@ -12946,7 +12951,8 @@ static int perf_event_init_context(struct task_struct *child, int ctxn)
 	 */
 	perf_event_groups_for_each(event, &parent_ctx->pinned_groups) {
 		ret = inherit_task_group(event, parent, parent_ctx,
-					 child, ctxn, &inherited_all);
+					 child, ctxn, clone_flags,
+					 &inherited_all);
 		if (ret)
 			goto out_unlock;
 	}
@@ -12962,7 +12968,8 @@ static int perf_event_init_context(struct task_struct *child, int ctxn)
 
 	perf_event_groups_for_each(event, &parent_ctx->flexible_groups) {
 		ret = inherit_task_group(event, parent, parent_ctx,
-					 child, ctxn, &inherited_all);
+					 child, ctxn, clone_flags,
+					 &inherited_all);
 		if (ret)
 			goto out_unlock;
 	}
@@ -13004,7 +13011,7 @@ static int perf_event_init_context(struct task_struct *child, int ctxn)
 /*
  * Initialize the perf_event context in task_struct
  */
-int perf_event_init_task(struct task_struct *child)
+int perf_event_init_task(struct task_struct *child, u64 clone_flags)
 {
 	int ctxn, ret;
 
@@ -13013,7 +13020,7 @@ int perf_event_init_task(struct task_struct *child)
 	INIT_LIST_HEAD(&child->perf_event_list);
 
 	for_each_task_context_nr(ctxn) {
-		ret = perf_event_init_context(child, ctxn);
+		ret = perf_event_init_context(child, ctxn, clone_flags);
 		if (ret) {
 			perf_event_free_task(child);
 			return ret;
kernel/fork.c +1 -1
index 54cc905e5fe0..aeccd7f46ce3 100644
@@ -2078,7 +2078,7 @@ static __latent_entropy struct task_struct *copy_process(
 	if (retval)
 		goto bad_fork_cleanup_policy;
 
-	retval = perf_event_init_task(p);
+	retval = perf_event_init_task(p, clone_flags);
 	if (retval)
 		goto bad_fork_cleanup_policy;
 	retval = audit_alloc(p);
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
#8 lore raw

PATCH v3 4/11 perf: Add support for event removal on exec

In reply to Marco Elver

Adds bit perf_event_attr::remove_on_exec, to support removing an event from a task on exec.

This option supports the case where an event is supposed to be process-wide only, and should not propagate beyond exec, to limit monitoring to the original process image only.

---
v3:
* Rework based on Peter's "perf: Rework perf_event_exit_event()" added to the beginning of the series. Intermediate attempts between v2 and this v3 can be found here:
https://lkml.kernel.org/r/[email protected]

v2:
* Add patch to series.

---
 include/uapi/linux/perf_event.h |  3 +-
 kernel/events/core.c            | 70 +++++++++++++++++++++++++++++----
 2 files changed, 64 insertions(+), 9 deletions(-)
include/uapi/linux/perf_event.h +2 -1
index 813efb65fea8..8c5b9f5ad63f 100644
@@ -390,7 +390,8 @@ struct perf_event_attr {
 				text_poke      :  1, /* include text poke events */
 				build_id       :  1, /* use build id in mmap2 events */
 				inherit_thread :  1, /* children only inherit if cloned with CLONE_THREAD */
-				__reserved_1   : 28;
+				remove_on_exec :  1, /* event is removed from task on exec */
+				__reserved_1   : 27;
 
 	union {
 		__u32		wakeup_events;	  /* wakeup every n events */
kernel/events/core.c +62 -8

92 lines not on this page · raw message

Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
#9 lore raw

PATCH v3 5/11 signal: Introduce TRAP_PERF si_code and si_perf to siginfo

In reply to Marco Elver

Introduces the TRAP_PERF si_code, and associated siginfo_t field si_perf. These will be used by the perf event subsystem to send signals (if requested) to the task where an event occurred.

---
 arch/m68k/kernel/signal.c          |  3 +++
 arch/x86/kernel/signal_compat.c    |  5 ++++-
 fs/signalfd.c                      |  4 ++++
 include/linux/compat.h             |  2 ++
 include/linux/signal.h             |  1 +
 include/uapi/asm-generic/siginfo.h |  6 +++++-
 include/uapi/linux/signalfd.h      |  4 +++-
 kernel/signal.c                    | 11 +++++++++++
 8 files changed, 33 insertions(+), 3 deletions(-)
arch/m68k/kernel/signal.c +3 -0
index 349570f16a78..a4b7ee1df211 100644
@@ -622,6 +622,9 @@ static inline void siginfo_build_tests(void)
 	/* _sigfault._addr_pkey */
 	BUILD_BUG_ON(offsetof(siginfo_t, si_pkey) != 0x12);
 
+	/* _sigfault._perf */
+	BUILD_BUG_ON(offsetof(siginfo_t, si_perf) != 0x10);
+
 	/* _sigpoll */
 	BUILD_BUG_ON(offsetof(siginfo_t, si_band)   != 0x0c);
 	BUILD_BUG_ON(offsetof(siginfo_t, si_fd)     != 0x10);
arch/x86/kernel/signal_compat.c +4 -1
index a5330ff498f0..0e5d0a7e203b 100644
@@ -29,7 +29,7 @@ static inline void signal_compat_build_tests(void)
 	BUILD_BUG_ON(NSIGFPE  != 15);
 	BUILD_BUG_ON(NSIGSEGV != 9);
 	BUILD_BUG_ON(NSIGBUS  != 5);
-	BUILD_BUG_ON(NSIGTRAP != 5);
+	BUILD_BUG_ON(NSIGTRAP != 6);
 	BUILD_BUG_ON(NSIGCHLD != 6);
 	BUILD_BUG_ON(NSIGSYS  != 2);
 
@@ -138,6 +138,9 @@ static inline void signal_compat_build_tests(void)
 	BUILD_BUG_ON(offsetof(siginfo_t, si_pkey) != 0x20);
 	BUILD_BUG_ON(offsetof(compat_siginfo_t, si_pkey) != 0x14);
 
+	BUILD_BUG_ON(offsetof(siginfo_t, si_perf) != 0x18);
+	BUILD_BUG_ON(offsetof(compat_siginfo_t, si_perf) != 0x10);
+
 	CHECK_CSI_OFFSET(_sigpoll);
 	CHECK_CSI_SIZE  (_sigpoll, 2*sizeof(int));
 	CHECK_SI_SIZE   (_sigpoll, 4*sizeof(int));
fs/signalfd.c +4 -0
index 456046e15873..040a1142915f 100644
@@ -134,6 +134,10 @@ static int signalfd_copyinfo(struct signalfd_siginfo __user *uinfo,
 #endif
 		new.ssi_addr_lsb = (short) kinfo->si_addr_lsb;
 		break;
+	case SIL_PERF_EVENT:
+		new.ssi_addr = (long) kinfo->si_addr;
+		new.ssi_perf = kinfo->si_perf;
+		break;
 	case SIL_CHLD:
 		new.ssi_pid    = kinfo->si_pid;
 		new.ssi_uid    = kinfo->si_uid;
include/linux/compat.h +2 -0
index 6e65be753603..c8821d966812 100644
@@ -236,6 +236,8 @@ typedef struct compat_siginfo {
 					char _dummy_pkey[__COMPAT_ADDR_BND_PKEY_PAD];
 					u32 _pkey;
 				} _addr_pkey;
+				/* used when si_code=TRAP_PERF */
+				compat_u64 _perf;
 			};
 		} _sigfault;
 
include/linux/signal.h +1 -0
index 205526c4003a..1e98548d7cf6 100644
@@ -43,6 +43,7 @@ enum siginfo_layout {
 	SIL_FAULT_MCEERR,
 	SIL_FAULT_BNDERR,
 	SIL_FAULT_PKUERR,
+	SIL_PERF_EVENT,
 	SIL_CHLD,
 	SIL_RT,
 	SIL_SYS,
include/uapi/asm-generic/siginfo.h +5 -1
index d2597000407a..d0bb9125c853 100644
@@ -91,6 +91,8 @@ union __sifields {
 				char _dummy_pkey[__ADDR_BND_PKEY_PAD];
 				__u32 _pkey;
 			} _addr_pkey;
+			/* used when si_code=TRAP_PERF */
+			__u64 _perf;
 		};
 	} _sigfault;
 
@@ -155,6 +157,7 @@ typedef struct siginfo {
 #define si_lower	_sifields._sigfault._addr_bnd._lower
 #define si_upper	_sifields._sigfault._addr_bnd._upper
 #define si_pkey		_sifields._sigfault._addr_pkey._pkey
+#define si_perf		_sifields._sigfault._perf
 #define si_band		_sifields._sigpoll._band
 #define si_fd		_sifields._sigpoll._fd
 #define si_call_addr	_sifields._sigsys._call_addr
@@ -253,7 +256,8 @@ typedef struct siginfo {
 #define TRAP_BRANCH     3	/* process taken branch trap */
 #define TRAP_HWBKPT     4	/* hardware breakpoint/watchpoint */
 #define TRAP_UNK	5	/* undiagnosed trap */
-#define NSIGTRAP	5
+#define TRAP_PERF	6	/* perf event with sigtrap=1 */
+#define NSIGTRAP	6
 
 /*
  * There is an additional set of SIGTRAP si_codes used by ptrace
include/uapi/linux/signalfd.h +3 -1
index 83429a05b698..7e333042c7e3 100644
@@ -39,6 +39,8 @@ struct signalfd_siginfo {
 	__s32 ssi_syscall;
 	__u64 ssi_call_addr;
 	__u32 ssi_arch;
+	__u32 __pad3;
+	__u64 ssi_perf;
 
 	/*
 	 * Pad strcture to 128 bytes. Remember to update the
@@ -49,7 +51,7 @@ struct signalfd_siginfo {
 	 * comes out of a read(2) and we really don't want to have
 	 * a compat on read(2).
 	 */
-	__u8 __pad[28];
+	__u8 __pad[16];
 };
 
 
kernel/signal.c +11 -0
index f2a1b898da29..f9351217d391 100644
@@ -1203,6 +1203,7 @@ static inline bool has_si_pid_and_uid(struct kernel_siginfo *info)
 	case SIL_FAULT_MCEERR:
 	case SIL_FAULT_BNDERR:
 	case SIL_FAULT_PKUERR:
+	case SIL_PERF_EVENT:
 	case SIL_SYS:
 		ret = false;
 		break;
@@ -2535,6 +2536,7 @@ static void hide_si_addr_tag_bits(struct ksignal *ksig)
 	case SIL_FAULT_MCEERR:
 	case SIL_FAULT_BNDERR:
 	case SIL_FAULT_PKUERR:
+	case SIL_PERF_EVENT:
 		ksig->info.si_addr = arch_untagged_si_addr(
 			ksig->info.si_addr, ksig->sig, ksig->info.si_code);
 		break;
@@ -3337,6 +3339,10 @@ void copy_siginfo_to_external32(struct compat_siginfo *to,
 #endif
 		to->si_pkey = from->si_pkey;
 		break;
+	case SIL_PERF_EVENT:
+		to->si_addr = ptr_to_compat(from->si_addr);
+		to->si_perf = from->si_perf;
+		break;
 	case SIL_CHLD:
 		to->si_pid = from->si_pid;
 		to->si_uid = from->si_uid;
@@ -3417,6 +3423,10 @@ static int post_copy_siginfo_from_user32(kernel_siginfo_t *to,
 #endif
 		to->si_pkey = from->si_pkey;
 		break;
+	case SIL_PERF_EVENT:
+		to->si_addr = compat_ptr(from->si_addr);
+		to->si_perf = from->si_perf;
+		break;
 	case SIL_CHLD:
 		to->si_pid    = from->si_pid;
 		to->si_uid    = from->si_uid;
@@ -4597,6 +4607,7 @@ static inline void siginfo_buildtime_checks(void)
 	CHECK_OFFSET(si_lower);
 	CHECK_OFFSET(si_upper);
 	CHECK_OFFSET(si_pkey);
+	CHECK_OFFSET(si_perf);
 
 	/* sigpoll */
 	CHECK_OFFSET(si_band);
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
#10 lore raw

PATCH v3 6/11 perf: Add support for SIGTRAP on perf events

In reply to Marco Elver

Adds bit perf_event_attr::sigtrap, which can be set to cause events to send SIGTRAP (with si_code TRAP_PERF) to the task where the event occurred. To distinguish perf events and allow user space to decode si_perf (if set), the event type is set in si_errno.

The primary motivation is to support synchronous signals on perf events in the task where an event (such as breakpoints) triggered.

---
v2:
* Use atomic_set(&event_count, 1), since it must always be 0 in perf_pending_event_disable().
* Implicitly restrict inheriting events if sigtrap, but the child was cloned with CLONE_CLEAR_SIGHAND, because it is not generally safe if the child cleared all signal handlers to continue sending SIGTRAP.

---
 include/uapi/linux/perf_event.h |  3 ++-
 kernel/events/core.c            | 28 +++++++++++++++++++++++++++-
 2 files changed, 29 insertions(+), 2 deletions(-)
include/uapi/linux/perf_event.h +2 -1
index 8c5b9f5ad63f..3a4dbb1688f0 100644
@@ -391,7 +391,8 @@ struct perf_event_attr {
 				build_id       :  1, /* use build id in mmap2 events */
 				inherit_thread :  1, /* children only inherit if cloned with CLONE_THREAD */
 				remove_on_exec :  1, /* event is removed from task on exec */
-				__reserved_1   : 27;
+				sigtrap        :  1, /* send synchronous SIGTRAP on event */
+				__reserved_1   : 26;
 
 	union {
 		__u32		wakeup_events;	  /* wakeup every n events */
kernel/events/core.c +27 -1
index b6434697c516..1e4c949bf75f 100644
@@ -6391,6 +6391,17 @@ void perf_event_wakeup(struct perf_event *event)
 	}
 }
 
+static void perf_sigtrap(struct perf_event *event)
+{
+	struct kernel_siginfo info;
+
+	clear_siginfo(&info);
+	info.si_signo = SIGTRAP;
+	info.si_code = TRAP_PERF;
+	info.si_errno = event->attr.type;
+	force_sig_info(&info);
+}
+
 static void perf_pending_event_disable(struct perf_event *event)
 {
 	int cpu = READ_ONCE(event->pending_disable);
@@ -6400,6 +6411,13 @@ static void perf_pending_event_disable(struct perf_event *event)
 
 	if (cpu == smp_processor_id()) {
 		WRITE_ONCE(event->pending_disable, -1);
+
+		if (event->attr.sigtrap) {
+			atomic_set(&event->event_limit, 1); /* rearm event */
+			perf_sigtrap(event);
+			return;
+		}
+
 		perf_event_disable_local(event);
 		return;
 	}
@@ -11428,6 +11446,9 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
 
 	event->state		= PERF_EVENT_STATE_INACTIVE;
 
+	if (event->attr.sigtrap)
+		atomic_set(&event->event_limit, 1);
+
 	if (task) {
 		event->attach_state = PERF_ATTACH_TASK;
 		/*
@@ -11706,6 +11727,9 @@ static int perf_copy_attr(struct perf_event_attr __user *uattr,
 	if (attr->remove_on_exec && attr->enable_on_exec)
 		return -EINVAL;
 
+	if (attr->sigtrap && !attr->remove_on_exec)
+		return -EINVAL;
+
 out:
 	return ret;
 
@@ -12932,7 +12956,9 @@ inherit_task_group(struct perf_event *event, struct task_struct *parent,
 	struct perf_event_context *child_ctx;
 
 	if (!event->attr.inherit ||
-	    (event->attr.inherit_thread && !(clone_flags & CLONE_THREAD))) {
+	    (event->attr.inherit_thread && !(clone_flags & CLONE_THREAD)) ||
+	    /* Do not inherit if sigtrap and signal handlers were cleared. */
+	    (event->attr.sigtrap && (clone_flags & CLONE_CLEAR_SIGHAND))) {
 		*inherited_all = 0;
 		return 0;
 	}
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
#11 lore raw

Re: [PATCH v3 06/11] perf: Add support for SIGTRAP on perf events

On Wed, Mar 24, 2021 at 12:24PM +0100, Marco Elver wrote:
[...]

kernel/events/core.c void perf_event_wakeup(struct perf_event *event) · line 6397

Quoted 12 lines
kernel/events/core.c +4 -0
index b6434697c516..1e4c949bf75f 100644
@@ -6391,6 +6391,17 @@ void perf_event_wakeup(struct perf_event *event)
 	}
 }
 
+static void perf_sigtrap(struct perf_event *event)
+{
+	struct kernel_siginfo info;
+

I think we need to add something like this here:

kernel/events/core.c +7 -0
index 4b82788fbaab..4fcd6b45ce66 100644
@@ -6395,6 +6395,13 @@ static void perf_sigtrap(struct perf_event *event)
 {
 	struct kernel_siginfo info;
 
+	/*
+	 * This irq_work can race with an exiting task; bail out if sighand has
+	 * already been released in release_task().
+	 */
+	if (!current->sighand)
+		return;
+
 	clear_siginfo(&info);
 	info.si_signo = SIGTRAP;
 	info.si_code = TRAP_PERF;

Because syzkaller was able to produce this:

| general protection fault, probably for non-canonical address 0xdffffc0000000003: 0000 [#1] PREEMPT SMP KASAN
| KASAN: null-ptr-deref in range [0x0000000000000018-0x000000000000001f]
| CPU: 0 PID: 28393 Comm: kworker/u9:4 Not tainted 5.12.0-rc4+ #5
| Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-2 04/01/2014
| RIP: 0010:__lock_acquire+0x87/0x5e60 kernel/locking/lockdep.c:4770
| Code: 84 c0 48 89 7c 24 78 0f 85 10 26 00 00 83 3d 53 64 59 0c 00 0f 84 84 41 00 00 83 3d 72 8a 01 0b 00 74 32 48 89 f8 48 c1 e8 03 <80> 3c 30 00 74 19 48 8b 7c 24 78 e8 79 8b 60 00 48 8b 7c 24 78 48
| RSP: 0018:ffffc90000007c00 EFLAGS: 00010006
| RAX: 0000000000000003 RBX: ffff888048058000 RCX: 0000000000000000
| RDX: 0000000000000000 RSI: dffffc0000000000 RDI: 0000000000000018
| RBP: ffffc90000007da8 R08: 0000000000000001 R09: 0000000000000001
| R10: fffffbfff1b6b27e R11: 0000000000000000 R12: 0000000000000001
| R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000001
| FS:  0000000000000000(0000) GS:ffff88802ce00000(0000) knlGS:0000000000000000
| CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
| CR2: 0000000000970004 CR3: 0000000040d91000 CR4: 0000000000750ef0
| DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
| DR3: 0000000000000000 DR6: 00000000ffff0ff0 DR7: 0000000000000600
| PKRU: 55555554
| Call Trace:
|  <IRQ>
|  lock_acquire+0x126/0x650 kernel/locking/lockdep.c:5510
|  __raw_spin_lock_irqsave include/linux/spinlock_api_smp.h:110 [inline]
|  _raw_spin_lock_irqsave+0x73/0xa0 kernel/locking/spinlock.c:159
|  force_sig_info_to_task+0x65/0x3f0 kernel/signal.c:1322
|  perf_sigtrap kernel/events/core.c:6418 [inline]
|  perf_pending_event_disable kernel/events/core.c:6433 [inline]
|  perf_pending_event+0x46f/0x620 kernel/events/core.c:6475
|  irq_work_single kernel/irq_work.c:153 [inline]
|  irq_work_run_list kernel/irq_work.c:175 [inline]
|  irq_work_run+0x1da/0x640 kernel/irq_work.c:184
|  __sysvec_irq_work+0x62/0x70 arch/x86/kernel/irq_work.c:22
|  sysvec_irq_work+0x8c/0xb0 arch/x86/kernel/irq_work.c:17
|  </IRQ>
|  asm_sysvec_irq_work+0x12/0x20 arch/x86/include/asm/idtentry.h:658
| RIP: 0010:__raw_write_unlock_irq include/linux/rwlock_api_smp.h:268 [inline]
| RIP: 0010:_raw_write_unlock_irq+0x25/0x40 kernel/locking/spinlock.c:343
| Code: aa fd ff 66 90 53 48 89 fb 48 83 c7 18 48 8b 74 24 08 e8 3e 34 04 f8 48 89 df e8 a6 1a 06 f8 e8 21 85 26 f8 fb bf 01 00 00 00 <e8> 56 19 fa f7 65 8b 05 77 65 a9 76 85 c0 74 02 5b c3 e8 2b c1 a7
| RSP: 0018:ffffc9000202fd68 EFLAGS: 00000286
| RAX: 2a7870700b93e400 RBX: ffffffff8c40a040 RCX: ffffffff8ff9cb03
| RDX: 0000000000000000 RSI: 0000000000000001 RDI: 0000000000000001
| RBP: ffff888047b24790 R08: ffffffff817f0f50 R09: fffffbfff1b6b27e
| R10: fffffbfff1b6b27e R11: 0000000000000000 R12: ffff888048058000
| R13: dffffc0000000000 R14: ffff888047b24701 R15: ffff888048058000
|  release_task+0x10bf/0x1360 kernel/exit.c:220
|  exit_notify kernel/exit.c:699 [inline]
|  do_exit+0x19b0/0x2290 kernel/exit.c:845
|  call_usermodehelper_exec_async+0x39c/0x3a0 kernel/umh.c:123
|  ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:294
Quoted 24 lines
kernel/events/core.c +14 -0
+	clear_siginfo(&info);
+	info.si_signo = SIGTRAP;
+	info.si_code = TRAP_PERF;
+	info.si_errno = event->attr.type;
+	force_sig_info(&info);
+}
+
 static void perf_pending_event_disable(struct perf_event *event)
 {
 	int cpu = READ_ONCE(event->pending_disable);
@@ -6400,6 +6411,13 @@ static void perf_pending_event_disable(struct perf_event *event)
 
 	if (cpu == smp_processor_id()) {
 		WRITE_ONCE(event->pending_disable, -1);
+
+		if (event->attr.sigtrap) {
+			atomic_set(&event->event_limit, 1); /* rearm event */
+			perf_sigtrap(event);
+			return;
+		}
+
 		perf_event_disable_local(event);
 		return;
 	}

[...]

maintainer
#12 lore raw

Re: [PATCH v3 06/11] perf: Add support for SIGTRAP on perf events

Quoted 27 lines · Marco Elver

On Wed, Mar 24, 2021 at 12:24PM +0100, Marco Elver wrote:
[...]

kernel/events/core.c void perf_event_wakeup(struct perf_event *event) · line 6397

Quoted 12 lines
kernel/events/core.c +4 -0
index b6434697c516..1e4c949bf75f 100644
@@ -6391,6 +6391,17 @@ void perf_event_wakeup(struct perf_event *event)
 	}
 }
 
+static void perf_sigtrap(struct perf_event *event)
+{
+	struct kernel_siginfo info;
+

I think we need to add something like this here:

kernel/events/core.c +0 -0
index 4b82788fbaab..4fcd6b45ce66 100644
@@ -6395,6 +6395,13 @@ static void perf_sigtrap(struct perf_event *event)
 {
 	struct kernel_siginfo info;
 

kernel/events/core.c static void perf_sigtrap(struct perf_event *event) · line 6407

Quoted 10 lines
kernel/events/core.c +7 -0
+	/*
+	 * This irq_work can race with an exiting task; bail out if sighand has
+	 * already been released in release_task().
+	 */
+	if (!current->sighand)
+		return;
+
 	clear_siginfo(&info);
 	info.si_signo = SIGTRAP;
 	info.si_code = TRAP_PERF;

Urgh.. I'm not entirely sure that check is correct, but I always forget the rules with signal. It could be we ought to be testing PF_EXISTING instead.

But also, I think Jiri Olsa was going to poke around here because all of this is broken on PREEMPT_RT. IIRC the plan was to add yet another stage to the construct. So where today we have:

	<NMI>
		irq_work_queue()
	</NMI>
	...
	<IRQ>
		perf_pending_event()
	</IRQ>

(and we might already have a problem on some architectures where there can be significant time between these due to not having arch_irq_work_raise(), so ideally we ought to double check current in your case)

The idea was, I think to add a task_work(), such that we get:

	<NMI>
		irq_work_queue()
	</NMI>
	...
	<IRQ>
		perf_pending_event()
		  task_work_add()
	</IRQ>
	<ret-to-user>
		run_task_work()
		  ...
		    kill_fasync();
#13 lore raw

Re: [PATCH v3 06/11] perf: Add support for SIGTRAP on perf events

Quoted 6 lines · Peter Zijlstra
Quoted 4 lines · Marco Elver
kernel/events/core.c +0 -0
@@ -6395,6 +6395,13 @@ static void perf_sigtrap(struct perf_event *event)
 {
 	struct kernel_siginfo info;
 
Quoted 6 lines

kernel/events/core.c static void perf_sigtrap(struct perf_event *event) · line 6403

Quoted 6 lines
kernel/events/core.c +6 -0
+	/*
+	 * This irq_work can race with an exiting task; bail out if sighand has
+	 * already been released in release_task().
+	 */
+	if (!current->sighand)
+		return;

This is racy. If "current" has already passed exit_notify(), current->parent can do release_task() and destroy current->sighand right after the check.

Quoted 3 lines

Urgh.. I'm not entirely sure that check is correct, but I always forget the rules with signal. It could be we ought to be testing PF_EXISTING instead.

Agreed, PF_EXISTING check makes more sense in any case, the exiting task can't receive the signal anyway.

Oleg.

#14 lore raw

Re: [PATCH v3 06/11] perf: Add support for SIGTRAP on perf events

Quoted 17 lines · Oleg Nesterov
Quoted 6 lines · Peter Zijlstra
Quoted 4 lines · Marco Elver
kernel/events/core.c +0 -0
@@ -6395,6 +6395,13 @@ static void perf_sigtrap(struct perf_event *event)
 {
    struct kernel_siginfo info;
 
Quoted 6 lines

kernel/events/core.c static void perf_sigtrap(struct perf_event *event) · line 6403

Quoted 6 lines
kernel/events/core.c +6 -0
+   /*
+    * This irq_work can race with an exiting task; bail out if sighand has
+    * already been released in release_task().
+    */
+   if (!current->sighand)
+           return;

This is racy. If "current" has already passed exit_notify(), current->parent can do release_task() and destroy current->sighand right after the check.

Quoted 6 lines
Quoted 3 lines

Urgh.. I'm not entirely sure that check is correct, but I always forget the rules with signal. It could be we ought to be testing PF_EXISTING instead.

Agreed, PF_EXISTING check makes more sense in any case, the exiting task can't receive the signal anyway.

Thanks for confirming. I'll switch to just checking PF_EXITING (PF_EXISTING does not exist :-)).

Thanks,
-- Marco

maintainer
#15 lore raw

Re: [PATCH v3 06/11] perf: Add support for SIGTRAP on perf events

Quoted 22 lines · Marco Elver
Quoted 21 lines · Oleg Nesterov
Quoted 6 lines · Peter Zijlstra
Quoted 4 lines · Marco Elver
kernel/events/core.c +0 -0
@@ -6395,6 +6395,13 @@ static void perf_sigtrap(struct perf_event *event)
 {
    struct kernel_siginfo info;
 
Quoted 6 lines

kernel/events/core.c static void perf_sigtrap(struct perf_event *event) · line 6403

Quoted 6 lines
kernel/events/core.c +6 -0
+   /*
+    * This irq_work can race with an exiting task; bail out if sighand has
+    * already been released in release_task().
+    */
+   if (!current->sighand)
+           return;

This is racy. If "current" has already passed exit_notify(), current->parent can do release_task() and destroy current->sighand right after the check.

Quoted 3 lines

Urgh.. I'm not entirely sure that check is correct, but I always forget the rules with signal. It could be we ought to be testing PF_EXISTING instead.

Quoted 5 lines
Quoted 2 lines

Agreed, PF_EXISTING check makes more sense in any case, the exiting task can't receive the signal anyway.

Thanks for confirming. I'll switch to just checking PF_EXITING (PF_EXISTING does not exist :-)).

Indeed! Typing be hard :-)

#16 lore raw

Re: [PATCH v3 06/11] perf: Add support for SIGTRAP on perf events

In reply to Oleg Nesterov
Quoted 17 lines · Oleg Nesterov
Quoted 6 lines · Peter Zijlstra
Quoted 4 lines · Marco Elver
kernel/events/core.c +0 -0
@@ -6395,6 +6395,13 @@ static void perf_sigtrap(struct perf_event *event)
 {
    struct kernel_siginfo info;
 
Quoted 6 lines

kernel/events/core.c static void perf_sigtrap(struct perf_event *event) · line 6403

Quoted 6 lines
kernel/events/core.c +6 -0
+   /*
+    * This irq_work can race with an exiting task; bail out if sighand has
+    * already been released in release_task().
+    */
+   if (!current->sighand)
+           return;

This is racy. If "current" has already passed exit_notify(), current->parent can do release_task() and destroy current->sighand right after the check.

Quoted 6 lines
Quoted 3 lines

Urgh.. I'm not entirely sure that check is correct, but I always forget the rules with signal. It could be we ought to be testing PF_EXISTING instead.

Agreed, PF_EXISTING check makes more sense in any case, the exiting task can't receive the signal anyway.

So, per off-list discussion, it appears that I should ask to clarify:
PF_EXISTING or PF_EXITING?

It appears that PF_EXISTING is what's being suggested, whereas it has not been mentioned anywhere, nor are its semantics clear. If it is not simply the negation of PF_EXITING, what are its semantics? And why do we need it in the case here (instead of something else that already exists)?

Thanks,
-- Marco

#17 lore raw

Re: [PATCH v3 06/11] perf: Add support for SIGTRAP on perf events

Quoted 3 lines · Marco Elver

So, per off-list discussion, it appears that I should ask to clarify:
PF_EXISTING or PF_EXITING?

Aaaaaaah, sorry Marco.

PF_EXITING, of course.

Oleg.

#18 lore raw

Re: [PATCH v3 06/11] perf: Add support for SIGTRAP on perf events

In reply to Peter Zijlstra
Quoted 4 lines · Peter Zijlstra

(and we might already have a problem on some architectures where there can be significant time between these due to not having arch_irq_work_raise(), so ideally we ought to double check current in your case)

I missed this bit -- just to verify: here we want to check that event->ctx->task == current, in case the the irq_work runs when the current task has already been replaced. Correct?

Thanks,
-- Marco

maintainer
#19 lore raw

Re: [PATCH v3 06/11] perf: Add support for SIGTRAP on perf events

Quoted 10 lines · Marco Elver
Quoted 4 lines · Peter Zijlstra

(and we might already have a problem on some architectures where there can be significant time between these due to not having arch_irq_work_raise(), so ideally we ought to double check current in your case)

I missed this bit -- just to verify: here we want to check that event->ctx->task == current, in case the the irq_work runs when the current task has already been replaced. Correct?

Yeah, just not sure what a decent failure would be, silent ignore seems undesired, maybe WARN and archs that can trigger it get to fix it ?

#20 lore raw

Re: [PATCH v3 06/11] perf: Add support for SIGTRAP on perf events

Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Marco Elver
Quoted 4 lines · Peter Zijlstra

(and we might already have a problem on some architectures where there can be significant time between these due to not having arch_irq_work_raise(), so ideally we ought to double check current in your case)

Quoted 6 lines
Quoted 3 lines

I missed this bit -- just to verify: here we want to check that event->ctx->task == current, in case the the irq_work runs when the current task has already been replaced. Correct?

Yeah, just not sure what a decent failure would be, silent ignore seems undesired, maybe WARN and archs that can trigger it get to fix it ?

I'll go with a WARN and add a comment.

This also revealed there should be a requirement that sigtrap events must be associated with a task (syzkaller managed to trigger the warning for cpu events).

Thanks,
-- Marco

#21 lore raw

PATCH v3 7/11 perf: Add breakpoint information to siginfo on SIGTRAP

In reply to Marco Elver

Encode information from breakpoint attributes into siginfo_t, which helps disambiguate which breakpoint fired.

Note, providing the event fd may be unreliable, since the event may have been modified (via PERF_EVENT_IOC_MODIFY_ATTRIBUTES) between the event triggering and the signal being delivered to user space.

---
v2:
* Add comment about si_perf==0.

---
 kernel/events/core.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
kernel/events/core.c +16 -0
index 1e4c949bf75f..0316d39e8c8f 100644
@@ -6399,6 +6399,22 @@ static void perf_sigtrap(struct perf_event *event)
 	info.si_signo = SIGTRAP;
 	info.si_code = TRAP_PERF;
 	info.si_errno = event->attr.type;
+
+	switch (event->attr.type) {
+	case PERF_TYPE_BREAKPOINT:
+		info.si_addr = (void *)(unsigned long)event->attr.bp_addr;
+		info.si_perf = (event->attr.bp_len << 16) | (u64)event->attr.bp_type;
+		break;
+	default:
+		/*
+		 * No additional info set (si_perf == 0).
+		 *
+		 * Adding new cases for event types to set si_perf to a
+		 * non-constant value must ensure that si_perf != 0.
+		 */
+		break;
+	}
+
 	force_sig_info(&info);
 }
 
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
maintainer
#22 lore raw

Re: [PATCH v3 07/11] perf: Add breakpoint information to siginfo on SIGTRAP

Quoted 20 lines · Marco Elver

Encode information from breakpoint attributes into siginfo_t, which helps disambiguate which breakpoint fired.

Note, providing the event fd may be unreliable, since the event may have been modified (via PERF_EVENT_IOC_MODIFY_ATTRIBUTES) between the event triggering and the signal being delivered to user space.

---
v2:
* Add comment about si_perf==0.

---
 kernel/events/core.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
kernel/events/core.c +0 -0
index 1e4c949bf75f..0316d39e8c8f 100644

kernel/events/core.c static void perf_sigtrap(struct perf_event *event) · line 6406

Quoted 8 lines
kernel/events/core.c +5 -0
@@ -6399,6 +6399,22 @@ static void perf_sigtrap(struct perf_event *event)
 	info.si_signo = SIGTRAP;
 	info.si_code = TRAP_PERF;
 	info.si_errno = event->attr.type;
+
+	switch (event->attr.type) {
+	case PERF_TYPE_BREAKPOINT:
+		info.si_addr = (void *)(unsigned long)event->attr.bp_addr;
+		info.si_perf = (event->attr.bp_len << 16) | (u64)event->attr.bp_type;

Ahh, here's the si_perf user. I wasn't really clear to me what was supposed to be in that field at patch #5 where it was introduced.

Would it perhaps make sense to put the user address of struct perf_event_attr in there instead? (Obviously we'd have to carry it from the syscall to here, but it might be more useful than a random encoding of some bits therefrom).

Then we can also clearly document that's in that field, and it might be more useful for possible other uses.

maintainer
#23 lore raw

Re: [PATCH v3 07/11] perf: Add breakpoint information to siginfo on SIGTRAP

Quoted 33 lines · Peter Zijlstra
Quoted 20 lines · Marco Elver

Encode information from breakpoint attributes into siginfo_t, which helps disambiguate which breakpoint fired.

Note, providing the event fd may be unreliable, since the event may have been modified (via PERF_EVENT_IOC_MODIFY_ATTRIBUTES) between the event triggering and the signal being delivered to user space.

---
v2:
* Add comment about si_perf==0.

---
 kernel/events/core.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
kernel/events/core.c +0 -0
index 1e4c949bf75f..0316d39e8c8f 100644

kernel/events/core.c static void perf_sigtrap(struct perf_event *event) · line 6406

Quoted 8 lines
kernel/events/core.c +5 -0
@@ -6399,6 +6399,22 @@ static void perf_sigtrap(struct perf_event *event)
 	info.si_signo = SIGTRAP;
 	info.si_code = TRAP_PERF;
 	info.si_errno = event->attr.type;
+
+	switch (event->attr.type) {
+	case PERF_TYPE_BREAKPOINT:
+		info.si_addr = (void *)(unsigned long)event->attr.bp_addr;
+		info.si_perf = (event->attr.bp_len << 16) | (u64)event->attr.bp_type;

Ahh, here's the si_perf user. I wasn't really clear to me what was supposed to be in that field at patch #5 where it was introduced.

Quoted 7 lines

Would it perhaps make sense to put the user address of struct perf_event_attr in there instead? (Obviously we'd have to carry it from the syscall to here, but it might be more useful than a random encoding of some bits therefrom).

Then we can also clearly document that's in that field, and it might be more useful for possible other uses.

Something like so...

---

include/linux/perf_event.h +2 -0
@@ -778,6 +778,8 @@ struct perf_event {
 	void *security;
 #endif
 	struct list_head		sb_list;
+
+	struct perf_event_attr		__user *uattr;
 #endif /* CONFIG_PERF_EVENTS */
 };
 
kernel/events/core.c +10 -2
@@ -5652,13 +5652,17 @@ static long _perf_ioctl(struct perf_even
 		return perf_event_query_prog_array(event, (void __user *)arg);
 
 	case PERF_EVENT_IOC_MODIFY_ATTRIBUTES: {
+		struct perf_event_attr __user *uattr;
 		struct perf_event_attr new_attr;
-		int err = perf_copy_attr((struct perf_event_attr __user *)arg,
-					 &new_attr);
+		int err;
 
+		uattr = (struct perf_event_attr __user *)arg;
+		err = perf_copy_attr(uattr, &new_attr);
 		if (err)
 			return err;
 
+		event->uattr = uattr;
+
 		return perf_event_modify_attr(event,  &new_attr);
 	}
 	default:
@@ -6400,6 +6404,8 @@ static void perf_sigtrap(struct perf_eve
 	info.si_signo = SIGTRAP;
 	info.si_code = TRAP_PERF;
 	info.si_errno = event->attr.type;
+	info.si_perf = (unsigned long)event->uattr;
+
 	force_sig_info(&info);
 }
 
@@ -12011,6 +12017,8 @@ SYSCALL_DEFINE5(perf_event_open,
 		goto err_task;
 	}
 
+	event->uattr = attr_uptr;
+
 	if (is_sampling_event(event)) {
 		if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
 			err = -EOPNOTSUPP;
maintainer
#24 lore raw

Re: [PATCH v3 07/11] perf: Add breakpoint information to siginfo on SIGTRAP

Quoted 39 lines · Peter Zijlstra
Quoted 38 lines · Peter Zijlstra
Quoted 20 lines · Marco Elver

Encode information from breakpoint attributes into siginfo_t, which helps disambiguate which breakpoint fired.

Note, providing the event fd may be unreliable, since the event may have been modified (via PERF_EVENT_IOC_MODIFY_ATTRIBUTES) between the event triggering and the signal being delivered to user space.

---
v2:
* Add comment about si_perf==0.

---
 kernel/events/core.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
kernel/events/core.c +0 -0
index 1e4c949bf75f..0316d39e8c8f 100644

kernel/events/core.c static void perf_sigtrap(struct perf_event *event) · line 6406

Quoted 8 lines
kernel/events/core.c +5 -0
@@ -6399,6 +6399,22 @@ static void perf_sigtrap(struct perf_event *event)
 	info.si_signo = SIGTRAP;
 	info.si_code = TRAP_PERF;
 	info.si_errno = event->attr.type;
+
+	switch (event->attr.type) {
+	case PERF_TYPE_BREAKPOINT:
+		info.si_addr = (void *)(unsigned long)event->attr.bp_addr;
+		info.si_perf = (event->attr.bp_len << 16) | (u64)event->attr.bp_type;

Ahh, here's the si_perf user. I wasn't really clear to me what was supposed to be in that field at patch #5 where it was introduced.

Would it perhaps make sense to put the user address of struct perf_event_attr in there instead? (Obviously we'd have to carry it from the syscall to here, but it might be more useful than a random encoding of some bits therefrom).

Quoted 4 lines
Quoted 2 lines

Then we can also clearly document that's in that field, and it might be more useful for possible other uses.

Something like so...

Ok possibly something like so, which also gets the data address right for more cases.

---

include/linux/perf_event.h +2 -0
@@ -778,6 +778,8 @@ struct perf_event {
 	void *security;
 #endif
 	struct list_head		sb_list;
+
+	struct kernel_siginfo 		siginfo;
 #endif /* CONFIG_PERF_EVENTS */
 };
 
kernel/events/core.c +14 -10
@@ -5652,13 +5652,17 @@ static long _perf_ioctl(struct perf_even
 		return perf_event_query_prog_array(event, (void __user *)arg);
 
 	case PERF_EVENT_IOC_MODIFY_ATTRIBUTES: {
+		struct perf_event_attr __user *uattr;
 		struct perf_event_attr new_attr;
-		int err = perf_copy_attr((struct perf_event_attr __user *)arg,
-					 &new_attr);
+		int err;
 
+		uattr = (struct perf_event_attr __user *)arg;
+		err = perf_copy_attr(uattr, &new_attr);
 		if (err)
 			return err;
 
+		event->siginfo.si_perf = (unsigned long)uattr;
+
 		return perf_event_modify_attr(event,  &new_attr);
 	}
 	default:
@@ -6394,13 +6398,7 @@ void perf_event_wakeup(struct perf_event
 
 static void perf_sigtrap(struct perf_event *event)
 {
-	struct kernel_siginfo info;
-
-	clear_siginfo(&info);
-	info.si_signo = SIGTRAP;
-	info.si_code = TRAP_PERF;
-	info.si_errno = event->attr.type;
-	force_sig_info(&info);
+	force_sig_info(&event->siginfo);
 }
 
 static void perf_pending_event_disable(struct perf_event *event)
@@ -6414,8 +6412,8 @@ static void perf_pending_event_disable(s
 		WRITE_ONCE(event->pending_disable, -1);
 
 		if (event->attr.sigtrap) {
-			atomic_set(&event->event_limit, 1); /* rearm event */
 			perf_sigtrap(event);
+			atomic_set_release(&event->event_limit, 1); /* rearm event */
 			return;
 		}
 
@@ -9121,6 +9119,7 @@ static int __perf_event_overflow(struct
 	if (events && atomic_dec_and_test(&event->event_limit)) {
 		ret = 1;
 		event->pending_kill = POLL_HUP;
+		event->siginfo.si_addr = (void *)data->addr;
 
 		perf_event_disable_inatomic(event);
 	}
@@ -12011,6 +12010,11 @@ SYSCALL_DEFINE5(perf_event_open,
 		goto err_task;
 	}
 
+	clear_siginfo(&event->siginfo);
+	event->siginfo.si_signo = SIGTRAP;
+	event->siginfo.si_code = TRAP_PERF;
+	event->siginfo.si_perf = (unsigned long)attr_uptr;
+
 	if (is_sampling_event(event)) {
 		if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
 			err = -EOPNOTSUPP;
maintainer
#25 lore raw

Re: [PATCH v3 07/11] perf: Add breakpoint information to siginfo on SIGTRAP

Quoted 8 lines · Peter Zijlstra
kernel/events/core.c +1 -0
@@ -5652,13 +5652,17 @@ static long _perf_ioctl(struct perf_even
 		return perf_event_query_prog_array(event, (void __user *)arg);
 
 	case PERF_EVENT_IOC_MODIFY_ATTRIBUTES: {
+		struct perf_event_attr __user *uattr;
 		struct perf_event_attr new_attr;

kernel/events/core.c static long _perf_ioctl(struct perf_even · line 5664

Quoted 10 lines
kernel/events/core.c +4 -2
-		int err = perf_copy_attr((struct perf_event_attr __user *)arg,
-					 &new_attr);
+		int err;
 
+		uattr = (struct perf_event_attr __user *)arg;
+		err = perf_copy_attr(uattr, &new_attr);
 		if (err)
 			return err;
 
+		event->siginfo.si_perf = (unsigned long)uattr;

Oh bugger; that wants updating for all children too..

kernel/events/core.c SYSCALL_DEFINE5(perf_event_open, · line 12016

Quoted 12 lines
kernel/events/core.c +5 -0
+
 		return perf_event_modify_attr(event,  &new_attr);
 	}
 	default:
@@ -12011,6 +12010,11 @@ SYSCALL_DEFINE5(perf_event_open,
 		goto err_task;
 	}
 
+	clear_siginfo(&event->siginfo);
+	event->siginfo.si_signo = SIGTRAP;
+	event->siginfo.si_code = TRAP_PERF;
+	event->siginfo.si_perf = (unsigned long)attr_uptr;

And inherit_event() / perf_event_alloc() want to copy/propagate that.

Quoted 3 lines
kernel/events/core.c +0 -0
 	if (is_sampling_event(event)) {
 		if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
 			err = -EOPNOTSUPP;
maintainer
#26 lore raw

Re: [PATCH v3 07/11] perf: Add breakpoint information to siginfo on SIGTRAP

One last try, I'll leave it alone now, I promise :-)

include/linux/perf_event.h +3 -0
@@ -778,6 +778,9 @@ struct perf_event {
 	void *security;
 #endif
 	struct list_head		sb_list;
+
+	unsigned long			si_uattr;
+	unsigned long			si_data;
 #endif /* CONFIG_PERF_EVENTS */
 };
 
kernel/events/core.c +16 -4
@@ -5652,13 +5652,17 @@ static long _perf_ioctl(struct perf_even
 		return perf_event_query_prog_array(event, (void __user *)arg);
 
 	case PERF_EVENT_IOC_MODIFY_ATTRIBUTES: {
+		struct perf_event_attr __user *uattr;
 		struct perf_event_attr new_attr;
-		int err = perf_copy_attr((struct perf_event_attr __user *)arg,
-					 &new_attr);
+		int err;
 
+		uattr = (struct perf_event_attr __user *)arg;
+		err = perf_copy_attr(uattr, &new_attr);
 		if (err)
 			return err;
 
+		event->si_uattr = (unsigned long)uattr;
+
 		return perf_event_modify_attr(event,  &new_attr);
 	}
 	default:
@@ -6399,7 +6403,12 @@ static void perf_sigtrap(struct perf_eve
 	clear_siginfo(&info);
 	info.si_signo = SIGTRAP;
 	info.si_code = TRAP_PERF;
-	info.si_errno = event->attr.type;
+	info.si_addr = (void *)event->si_data;
+
+	info.si_perf = event->si_uattr;
+	if (event->parent)
+		info.si_perf = event->parent->si_uattr;
+
 	force_sig_info(&info);
 }
 
@@ -6414,8 +6423,8 @@ static void perf_pending_event_disable(s
 		WRITE_ONCE(event->pending_disable, -1);
 
 		if (event->attr.sigtrap) {
-			atomic_set(&event->event_limit, 1); /* rearm event */
 			perf_sigtrap(event);
+			atomic_set_release(&event->event_limit, 1); /* rearm event */
 			return;
 		}
 
@@ -9121,6 +9130,7 @@ static int __perf_event_overflow(struct
 	if (events && atomic_dec_and_test(&event->event_limit)) {
 		ret = 1;
 		event->pending_kill = POLL_HUP;
+		event->si_data = data->addr;
 
 		perf_event_disable_inatomic(event);
 	}
@@ -12011,6 +12021,8 @@ SYSCALL_DEFINE5(perf_event_open,
 		goto err_task;
 	}
 
+	event->si_uattr = (unsigned long)attr_uptr;
+
 	if (is_sampling_event(event)) {
 		if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
 			err = -EOPNOTSUPP;
#27 lore raw

Re: [PATCH v3 07/11] perf: Add breakpoint information to siginfo on SIGTRAP

Quoted 2 lines · Peter Zijlstra

One last try, I'll leave it alone now, I promise :-)

This looks like it does what you suggested, thanks! :-)

I'll still need to think about it, because of the potential problem with modify-signal-races and what the user's synchronization story would look like then.

Quoted 75 lines
include/linux/perf_event.h +3 -0
@@ -778,6 +778,9 @@ struct perf_event {
        void *security;
 #endif
        struct list_head                sb_list;
+
+       unsigned long                   si_uattr;
+       unsigned long                   si_data;
 #endif /* CONFIG_PERF_EVENTS */
 };
 
kernel/events/core.c +16 -4
@@ -5652,13 +5652,17 @@ static long _perf_ioctl(struct perf_even
                return perf_event_query_prog_array(event, (void __user *)arg);
 
        case PERF_EVENT_IOC_MODIFY_ATTRIBUTES: {
+               struct perf_event_attr __user *uattr;
                struct perf_event_attr new_attr;
-               int err = perf_copy_attr((struct perf_event_attr __user *)arg,
-                                        &new_attr);
+               int err;
 
+               uattr = (struct perf_event_attr __user *)arg;
+               err = perf_copy_attr(uattr, &new_attr);
                if (err)
                        return err;
 
+               event->si_uattr = (unsigned long)uattr;
+
                return perf_event_modify_attr(event,  &new_attr);
        }
        default:
@@ -6399,7 +6403,12 @@ static void perf_sigtrap(struct perf_eve
        clear_siginfo(&info);
        info.si_signo = SIGTRAP;
        info.si_code = TRAP_PERF;
-       info.si_errno = event->attr.type;
+       info.si_addr = (void *)event->si_data;
+
+       info.si_perf = event->si_uattr;
+       if (event->parent)
+               info.si_perf = event->parent->si_uattr;
+
        force_sig_info(&info);
 }
 
@@ -6414,8 +6423,8 @@ static void perf_pending_event_disable(s
                WRITE_ONCE(event->pending_disable, -1);
 
                if (event->attr.sigtrap) {
-                       atomic_set(&event->event_limit, 1); /* rearm event */
                        perf_sigtrap(event);
+                       atomic_set_release(&event->event_limit, 1); /* rearm event */
                        return;
                }
 
@@ -9121,6 +9130,7 @@ static int __perf_event_overflow(struct
        if (events && atomic_dec_and_test(&event->event_limit)) {
                ret = 1;
                event->pending_kill = POLL_HUP;
+               event->si_data = data->addr;
 
                perf_event_disable_inatomic(event);
        }
@@ -12011,6 +12021,8 @@ SYSCALL_DEFINE5(perf_event_open,
                goto err_task;
        }
 
+       event->si_uattr = (unsigned long)attr_uptr;
+
        if (is_sampling_event(event)) {
                if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
                        err = -EOPNOTSUPP;
#28 lore raw

Re: [PATCH v3 07/11] perf: Add breakpoint information to siginfo on SIGTRAP

Quoted 10 lines · Marco Elver
Quoted 2 lines · Peter Zijlstra

One last try, I'll leave it alone now, I promise :-)

This looks like it does what you suggested, thanks! :-)

I'll still need to think about it, because of the potential problem with modify-signal-races and what the user's synchronization story would look like then.

I agree that this looks inherently racy. The attr can't be allocated on stack, user synchronization may be tricky and expensive. The API may provoke bugs and some users may not even realize the race problem.

One potential alternative is use of an opaque u64 context (if we could shove it into the attr). A user can pass a pointer to the attr in there (makes it equivalent to this proposal), or bit-pack size/type (as we want), pass some sequence number or whatever.

Quoted 75 lines
Quoted 75 lines
include/linux/perf_event.h +3 -0
@@ -778,6 +778,9 @@ struct perf_event {
        void *security;
 #endif
        struct list_head                sb_list;
+
+       unsigned long                   si_uattr;
+       unsigned long                   si_data;
 #endif /* CONFIG_PERF_EVENTS */
 };
 
kernel/events/core.c +16 -4
@@ -5652,13 +5652,17 @@ static long _perf_ioctl(struct perf_even
                return perf_event_query_prog_array(event, (void __user *)arg);
 
        case PERF_EVENT_IOC_MODIFY_ATTRIBUTES: {
+               struct perf_event_attr __user *uattr;
                struct perf_event_attr new_attr;
-               int err = perf_copy_attr((struct perf_event_attr __user *)arg,
-                                        &new_attr);
+               int err;
 
+               uattr = (struct perf_event_attr __user *)arg;
+               err = perf_copy_attr(uattr, &new_attr);
                if (err)
                        return err;
 
+               event->si_uattr = (unsigned long)uattr;
+
                return perf_event_modify_attr(event,  &new_attr);
        }
        default:
@@ -6399,7 +6403,12 @@ static void perf_sigtrap(struct perf_eve
        clear_siginfo(&info);
        info.si_signo = SIGTRAP;
        info.si_code = TRAP_PERF;
-       info.si_errno = event->attr.type;
+       info.si_addr = (void *)event->si_data;
+
+       info.si_perf = event->si_uattr;
+       if (event->parent)
+               info.si_perf = event->parent->si_uattr;
+
        force_sig_info(&info);
 }
 
@@ -6414,8 +6423,8 @@ static void perf_pending_event_disable(s
                WRITE_ONCE(event->pending_disable, -1);
 
                if (event->attr.sigtrap) {
-                       atomic_set(&event->event_limit, 1); /* rearm event */
                        perf_sigtrap(event);
+                       atomic_set_release(&event->event_limit, 1); /* rearm event */
                        return;
                }
 
@@ -9121,6 +9130,7 @@ static int __perf_event_overflow(struct
        if (events && atomic_dec_and_test(&event->event_limit)) {
                ret = 1;
                event->pending_kill = POLL_HUP;
+               event->si_data = data->addr;
 
                perf_event_disable_inatomic(event);
        }
@@ -12011,6 +12021,8 @@ SYSCALL_DEFINE5(perf_event_open,
                goto err_task;
        }
 
+       event->si_uattr = (unsigned long)attr_uptr;
+
        if (is_sampling_event(event)) {
                if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
                        err = -EOPNOTSUPP;
#29 lore raw

Re: [PATCH v3 07/11] perf: Add breakpoint information to siginfo on SIGTRAP

Quoted 10 lines · Dmitry Vyukov
Quoted 9 lines · Peter Zijlstra
Quoted 2 lines

One last try, I'll leave it alone now, I promise :-)

This looks like it does what you suggested, thanks! :-)

I'll still need to think about it, because of the potential problem with modify-signal-races and what the user's synchronization story would look like then.

Quoted 8 lines

I agree that this looks inherently racy. The attr can't be allocated on stack, user synchronization may be tricky and expensive. The API may provoke bugs and some users may not even realize the race problem.

One potential alternative is use of an opaque u64 context (if we could shove it into the attr). A user can pass a pointer to the attr in there (makes it equivalent to this proposal), or bit-pack size/type (as we want), pass some sequence number or whatever.

Just to clarify what I was thinking about, but did not really state:
perf_event_attr_t includes u64 ctx, and we return it back to the user in siginfo_t. Kernel does not treat it in any way. This is a pretty common API pattern in general.

Quoted 75 lines
Quoted 75 lines
Quoted 75 lines
include/linux/perf_event.h +3 -0
@@ -778,6 +778,9 @@ struct perf_event {
        void *security;
 #endif
        struct list_head                sb_list;
+
+       unsigned long                   si_uattr;
+       unsigned long                   si_data;
 #endif /* CONFIG_PERF_EVENTS */
 };
 
kernel/events/core.c +16 -4
@@ -5652,13 +5652,17 @@ static long _perf_ioctl(struct perf_even
                return perf_event_query_prog_array(event, (void __user *)arg);
 
        case PERF_EVENT_IOC_MODIFY_ATTRIBUTES: {
+               struct perf_event_attr __user *uattr;
                struct perf_event_attr new_attr;
-               int err = perf_copy_attr((struct perf_event_attr __user *)arg,
-                                        &new_attr);
+               int err;
 
+               uattr = (struct perf_event_attr __user *)arg;
+               err = perf_copy_attr(uattr, &new_attr);
                if (err)
                        return err;
 
+               event->si_uattr = (unsigned long)uattr;
+
                return perf_event_modify_attr(event,  &new_attr);
        }
        default:
@@ -6399,7 +6403,12 @@ static void perf_sigtrap(struct perf_eve
        clear_siginfo(&info);
        info.si_signo = SIGTRAP;
        info.si_code = TRAP_PERF;
-       info.si_errno = event->attr.type;
+       info.si_addr = (void *)event->si_data;
+
+       info.si_perf = event->si_uattr;
+       if (event->parent)
+               info.si_perf = event->parent->si_uattr;
+
        force_sig_info(&info);
 }
 
@@ -6414,8 +6423,8 @@ static void perf_pending_event_disable(s
                WRITE_ONCE(event->pending_disable, -1);
 
                if (event->attr.sigtrap) {
-                       atomic_set(&event->event_limit, 1); /* rearm event */
                        perf_sigtrap(event);
+                       atomic_set_release(&event->event_limit, 1); /* rearm event */
                        return;
                }
 
@@ -9121,6 +9130,7 @@ static int __perf_event_overflow(struct
        if (events && atomic_dec_and_test(&event->event_limit)) {
                ret = 1;
                event->pending_kill = POLL_HUP;
+               event->si_data = data->addr;
 
                perf_event_disable_inatomic(event);
        }
@@ -12011,6 +12021,8 @@ SYSCALL_DEFINE5(perf_event_open,
                goto err_task;
        }
 
+       event->si_uattr = (unsigned long)attr_uptr;
+
        if (is_sampling_event(event)) {
                if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
                        err = -EOPNOTSUPP;
#30 lore raw

Re: [PATCH v3 07/11] perf: Add breakpoint information to siginfo on SIGTRAP

Quoted 20 lines · Dmitry Vyukov
Quoted 10 lines · Dmitry Vyukov
Quoted 9 lines · Peter Zijlstra
Quoted 2 lines

One last try, I'll leave it alone now, I promise :-)

This looks like it does what you suggested, thanks! :-)

I'll still need to think about it, because of the potential problem with modify-signal-races and what the user's synchronization story would look like then.

Quoted 8 lines

I agree that this looks inherently racy. The attr can't be allocated on stack, user synchronization may be tricky and expensive. The API may provoke bugs and some users may not even realize the race problem.

One potential alternative is use of an opaque u64 context (if we could shove it into the attr). A user can pass a pointer to the attr in there (makes it equivalent to this proposal), or bit-pack size/type (as we want), pass some sequence number or whatever.

Quoted 4 lines

Just to clarify what I was thinking about, but did not really state:
perf_event_attr_t includes u64 ctx, and we return it back to the user in siginfo_t. Kernel does not treat it in any way. This is a pretty common API pattern in general.

Ok, let's go for a new field in perf_event_attr which is copied to si_perf. This gives user space full flexibility to decide what to stick in it, and the kernel does not prescribe some weird encoding or synchronization that user space would have to live with. I'll probably call it perf_event_attr::sig_data, because all si_* things are macros.

Thanks,
-- Marco

#31 lore raw

Re: [PATCH v3 07/11] perf: Add breakpoint information to siginfo on SIGTRAP

In reply to Dmitry Vyukov
Quoted 8 lines · Dmitry Vyukov
Quoted 7 lines · Marco Elver
Quoted 2 lines · Peter Zijlstra

One last try, I'll leave it alone now, I promise :-)

This looks like it does what you suggested, thanks! :-)

Quoted 7 lines
Quoted 3 lines

I'll still need to think about it, because of the potential problem with modify-signal-races and what the user's synchronization story would look like then.

I agree that this looks inherently racy. The attr can't be allocated on stack, user synchronization may be tricky and expensive. The API may provoke bugs and some users may not even realize the race problem.

Yeah, so why cannot we allocate enough space from the signal handler user-space stack and put the attr there, and point to it from sig_info?

The idea would be to create a stable, per-signal snapshot of whatever the perf_attr state is at the moment the event happens and the signal is generated - which is roughly what user-space wants, right?

Thanks,

Ingo

#32 lore raw

Re: [PATCH v3 07/11] perf: Add breakpoint information to siginfo on SIGTRAP

Quoted 19 lines · Ingo Molnar
Quoted 8 lines · Dmitry Vyukov
Quoted 7 lines · Marco Elver
Quoted 2 lines · Peter Zijlstra

One last try, I'll leave it alone now, I promise :-)

This looks like it does what you suggested, thanks! :-)

Quoted 7 lines
Quoted 3 lines

I'll still need to think about it, because of the potential problem with modify-signal-races and what the user's synchronization story would look like then.

I agree that this looks inherently racy. The attr can't be allocated on stack, user synchronization may be tricky and expensive. The API may provoke bugs and some users may not even realize the race problem.

Quoted 7 lines

Yeah, so why cannot we allocate enough space from the signal handler user-space stack and put the attr there, and point to it from sig_info?

The idea would be to create a stable, per-signal snapshot of whatever the perf_attr state is at the moment the event happens and the signal is generated - which is roughly what user-space wants, right?

I certainly couldn't say how feasible this is. Is there infrastructure in place to do this? Or do we have to introduce support for stashing things on the signal stack?

From what we can tell, the most flexible option though appears to be just some user settable opaque data in perf_event_attr, that is copied to siginfo. It'd allow user space to store a pointer or a hash/key, or just encode the relevant information it wants; but could also go further, and add information beyond perf_event_attr, such as things like a signal receiver filter (e.g. task ID or set of threads which should process the signal etc.).

So if there's no strong objection to the additional field in perf_event_attr, I think it'll give us the simplest and most flexible option.

Thanks,
-- Marco

Quoted 3 lines

Thanks,

Ingo

#33 lore raw

Re: [PATCH v3 07/11] perf: Add breakpoint information to siginfo on SIGTRAP

Quoted 21 lines · Marco Elver
Quoted 8 lines

Yeah, so why cannot we allocate enough space from the signal handler user-space stack and put the attr there, and point to it from sig_info?

The idea would be to create a stable, per-signal snapshot of whatever the perf_attr state is at the moment the event happens and the signal is generated - which is roughly what user-space wants, right?

I certainly couldn't say how feasible this is. Is there infrastructure in place to do this? Or do we have to introduce support for stashing things on the signal stack?

From what we can tell, the most flexible option though appears to be just some user settable opaque data in perf_event_attr, that is copied to siginfo. It'd allow user space to store a pointer or a hash/key, or just encode the relevant information it wants; but could also go further, and add information beyond perf_event_attr, such as things like a signal receiver filter (e.g. task ID or set of threads which should process the signal etc.).

Quoted 3 lines

So if there's no strong objection to the additional field in perf_event_attr, I think it'll give us the simplest and most flexible option.

Sounds good to me - it's also probably measurably faster than copying the not-so-small-anymore perf_attr structure.

Thanks,

Ingo

#34 lore raw

Re: [PATCH v3 07/11] perf: Add breakpoint information to siginfo on SIGTRAP

In reply to Peter Zijlstra
Quoted 41 lines · Peter Zijlstra
Quoted 39 lines · Peter Zijlstra
Quoted 38 lines · Peter Zijlstra
Quoted 20 lines · Marco Elver

Encode information from breakpoint attributes into siginfo_t, which helps disambiguate which breakpoint fired.

Note, providing the event fd may be unreliable, since the event may have been modified (via PERF_EVENT_IOC_MODIFY_ATTRIBUTES) between the event triggering and the signal being delivered to user space.

---
v2:
* Add comment about si_perf==0.

---
 kernel/events/core.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
kernel/events/core.c +0 -0
index 1e4c949bf75f..0316d39e8c8f 100644

kernel/events/core.c static void perf_sigtrap(struct perf_event *event) · line 6406

Quoted 8 lines
kernel/events/core.c +5 -0
@@ -6399,6 +6399,22 @@ static void perf_sigtrap(struct perf_event *event)
  info.si_signo = SIGTRAP;
  info.si_code = TRAP_PERF;
  info.si_errno = event->attr.type;
+
+ switch (event->attr.type) {
+ case PERF_TYPE_BREAKPOINT:
+         info.si_addr = (void *)(unsigned long)event->attr.bp_addr;
+         info.si_perf = (event->attr.bp_len << 16) | (u64)event->attr.bp_type;

Ahh, here's the si_perf user. I wasn't really clear to me what was supposed to be in that field at patch #5 where it was introduced.

Would it perhaps make sense to put the user address of struct perf_event_attr in there instead? (Obviously we'd have to carry it from the syscall to here, but it might be more useful than a random encoding of some bits therefrom).

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Then we can also clearly document that's in that field, and it might be more useful for possible other uses.

Something like so...

Ok possibly something like so, which also gets the data address right for more cases.

It'd be nice if this could work. Though I think there's an inherent problem (same as with fd) with trying to pass a reference back to the user, while the user can concurrently modify that reference.

Let's assume that user space creates new copies of perf_event_attr for every version they want, there's still a race where the user modifies an event, and concurrently in another thread a signal arrives. I currently don't see a way to determine when it's safe to free a perf_event_attr or reuse, without there still being a chance that a signal arrives due to some old perf_event_attr. And for our usecase, we really need to know a precise subset out of attr that triggered the event.

So the safest thing I can see is to stash a copy of the relevant information in siginfo, which is how we ended up with encoding bits from perf_event_attr into si_perf.

One way around this I could see is that we know that there's a limited number of combinations of attrs, and the user just creates an instance for every version they want (and hope it doesn't exceed some large number). Of course, for breakpoints, we have bp_addr, but let's assume that si_addr has the right version, so we won't need to access perf_event_attr::bp_addr.

But given the additional complexities, I'm not sure it's worth it. Is there a way to solve the modify-signal-race problem in a nicer way?

Thanks,
-- Marco

#35 lore raw

PATCH v3 8/11 selftests/perf_events: Add kselftest for process-wide sigtrap handling

In reply to Marco Elver

Add a kselftest for testing process-wide perf events with synchronous SIGTRAP on events (using breakpoints). In particular, we want to test that changes to the event propagate to all children, and the SIGTRAPs are in fact synchronously sent to the thread where the event occurred.

Note: The "signal_stress" test case is also added later in the series to perf tool's built-in tests. The test here is more elaborate in that respect, which on one hand avoids bloating the perf tool unnecessarily, but we also benefit from structured tests with TAP-compliant output that the kselftest framework provides.

---
v3:
* Fix for latest libc signal.h.

v2:
* Patch added to series.

---
 .../testing/selftests/perf_events/.gitignore  |   2 +
 tools/testing/selftests/perf_events/Makefile  |   6 +
 tools/testing/selftests/perf_events/config    |   1 +
 tools/testing/selftests/perf_events/settings  |   1 +
 .../selftests/perf_events/sigtrap_threads.c   | 206 ++++++++++++++++++
 5 files changed, 216 insertions(+)
 create mode 100644 tools/testing/selftests/perf_events/.gitignore
 create mode 100644 tools/testing/selftests/perf_events/Makefile
 create mode 100644 tools/testing/selftests/perf_events/config
 create mode 100644 tools/testing/selftests/perf_events/settings
 create mode 100644 tools/testing/selftests/perf_events/sigtrap_threads.c
tools/testing/selftests/perf_events/.gitignore new file +2 -0
new file mode 100644
index 000000000000..4dc43e1bd79c
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+sigtrap_threads
tools/testing/selftests/perf_events/Makefile new file +6 -0
new file mode 100644
index 000000000000..973a2c39ca83
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0
+CFLAGS += -Wl,-no-as-needed -Wall -I../../../../usr/include
+LDFLAGS += -lpthread
+
+TEST_GEN_PROGS := sigtrap_threads
+include ../lib.mk
tools/testing/selftests/perf_events/config new file +1 -0
new file mode 100644
index 000000000000..ba58ff2203e4
@@ -0,0 +1 @@
+CONFIG_PERF_EVENTS=y
tools/testing/selftests/perf_events/settings new file +1 -0
new file mode 100644
index 000000000000..6091b45d226b
@@ -0,0 +1 @@
+timeout=120
tools/testing/selftests/perf_events/sigtrap_threads.c new file +206 -0

206 lines not on this page · raw message

Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
#36 lore raw

PATCH v3 9/11 selftests/perf_events: Add kselftest for remove_on_exec

In reply to Marco Elver

Add kselftest to test that remove_on_exec removes inherited events from child tasks.

---
v3:
* Fix for latest libc signal.h.

v2:
* Add patch to series.

---
 .../testing/selftests/perf_events/.gitignore  |   1 +
 tools/testing/selftests/perf_events/Makefile  |   2 +-
 .../selftests/perf_events/remove_on_exec.c    | 260 ++++++++++++++++++
 3 files changed, 262 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/perf_events/remove_on_exec.c
tools/testing/selftests/perf_events/.gitignore +1 -0
index 4dc43e1bd79c..790c47001e77 100644
@@ -1,2 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0-only
 sigtrap_threads
+remove_on_exec
tools/testing/selftests/perf_events/Makefile +1 -1
index 973a2c39ca83..fcafa5f0d34c 100644
@@ -2,5 +2,5 @@
 CFLAGS += -Wl,-no-as-needed -Wall -I../../../../usr/include
 LDFLAGS += -lpthread
 
-TEST_GEN_PROGS := sigtrap_threads
+TEST_GEN_PROGS := sigtrap_threads remove_on_exec
 include ../lib.mk
tools/testing/selftests/perf_events/remove_on_exec.c new file +260 -0

260 lines not on this page · raw message

Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
#37 lore raw

PATCH v3 10/11 tools headers uapi: Sync tools/include/uapi/linux/perf_event.h

In reply to Marco Elver

Sync tool's uapi to pick up the changes adding inherit_thread, remove_on_exec, and sigtrap fields to perf_event_attr.

---
v3:
* Added to series.

---
 tools/include/uapi/linux/perf_event.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/include/uapi/linux/perf_event.h +4 -1
index ad15e40d7f5d..3a4dbb1688f0 100644
@@ -389,7 +389,10 @@ struct perf_event_attr {
 				cgroup         :  1, /* include cgroup events */
 				text_poke      :  1, /* include text poke events */
 				build_id       :  1, /* use build id in mmap2 events */
-				__reserved_1   : 29;
+				inherit_thread :  1, /* children only inherit if cloned with CLONE_THREAD */
+				remove_on_exec :  1, /* event is removed from task on exec */
+				sigtrap        :  1, /* send synchronous SIGTRAP on event */
+				__reserved_1   : 26;
 
 	union {
 		__u32		wakeup_events;	  /* wakeup every n events */
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
#38 lore raw

PATCH v3 11/11 perf test: Add basic stress test for sigtrap handling

In reply to Marco Elver

Add basic stress test for sigtrap handling as a perf tool built-in test. This allows sanity checking the basic sigtrap functionality from within the perf tool.

Note: A more elaborate kselftest version of this test can also be found in tools/testing/selftests/perf_events/sigtrap_threads.c.

---
v3:
* Added to series (per suggestion from Ian Rogers).

---
 tools/perf/tests/Build          |   1 +
 tools/perf/tests/builtin-test.c |   5 ++
 tools/perf/tests/sigtrap.c      | 148 ++++++++++++++++++++++++++++++++
 tools/perf/tests/tests.h        |   1 +
 4 files changed, 155 insertions(+)
 create mode 100644 tools/perf/tests/sigtrap.c
tools/perf/tests/Build +1 -0
index 650aec19d490..a429c7a02b37 100644
@@ -64,6 +64,7 @@ perf-y += parse-metric.o
 perf-y += pe-file-parsing.o
 perf-y += expand-cgroup.o
 perf-y += perf-time-to-tsc.o
+perf-y += sigtrap.o
 
 $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
 	$(call rule_mkdir)
tools/perf/tests/builtin-test.c +5 -0
index c4b888f18e9c..28a1cb5eaa77 100644
@@ -359,6 +359,11 @@ static struct test generic_tests[] = {
 		.func = test__perf_time_to_tsc,
 		.is_supported = test__tsc_is_supported,
 	},
+	{
+		.desc = "Sigtrap support",
+		.func = test__sigtrap,
+		.is_supported = test__wp_is_supported, /* uses wp for test */
+	},
 	{
 		.func = NULL,
 	},
tools/perf/tests/sigtrap.c new file +148 -0

148 lines not on this page · raw message

tools/perf/tests/tests.h +1 -0
index b85f005308a3..c3f2e2ecbfd6 100644
@@ -127,6 +127,7 @@ int test__parse_metric(struct test *test, int subtest);
 int test__pe_file_parsing(struct test *test, int subtest);
 int test__expand_cgroup_events(struct test *test, int subtest);
 int test__perf_time_to_tsc(struct test *test, int subtest);
+int test__sigtrap(struct test *test, int subtest);
 
 bool test__bp_signal_is_supported(void);
 bool test__bp_account_is_supported(void);
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog