Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp144167pxf; Wed, 10 Mar 2021 02:43:54 -0800 (PST) X-Google-Smtp-Source: ABdhPJz1AcQEk9SV6TI58MnwNk1uISRztm2sNXK5/qAWy99e/PvN1Zs2tWlCM8zCialRgG40h1NB X-Received: by 2002:a05:6402:1a3c:: with SMTP id be28mr2500232edb.125.1615373034344; Wed, 10 Mar 2021 02:43:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615373034; cv=none; d=google.com; s=arc-20160816; b=UsiM5kuGFL+PEf3uPxckObXSg7eZjfsqf4C9GQ13tO5TNXfJPzA52lYCdoRZnqH++b PZusPbL73lgzLBJkVKqGXwyEh7nZ6tVTzwWAknaTOkI0Cl9OLl0JlwOp1YoCpG1a8viZ sG9vzm47RWQRxcbX8Y8/vmyOCogkg5KpmkoXnOnxJbwqPCEeRsgH9yJsijjbnoNmUhwO xmawH5NyqGlztOHC9ZhzMlpxzO5iKVri+MwGaz6bKZljWmTh8QjkNPeaLVz141OH/iO8 RyaPVlVWWzOA+Pk5nPXRCwfH2V8zpSe7B95Y/ymHznz7l2bqsunIIH2r4OaEPyvLR583 9NLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:mime-version:message-id:date :dkim-signature; bh=CXDGXPLv4zBuXju/CY+pJKsC5gho63hQizGsKGKZIlo=; b=BeALA0w0Zsy2maKv/3TQ223K8whkRsPI1Gav5wCzbeiRYJQgMbAtnQGRaB5rZqjLlE ZcTmmW2iR1G4YhXexvsKTXhGa/gmJtJnKPdJD6AiMCifxdHRrQRkCxi44QHBZyCTdgOX Ul88uFrOY3tMlI7fW3zu0oObCw38lVFLxNmJ4xKeJnA9crWWOqOQW+yakWo6G2ARAYhz vPNoiBatRHKEaT7bEUp94Qghnvrzq22jbJc8c05J9xATE9dHGyzJU1jDVkD2DF/UT2SE 7RnrwV7lq+5c7or2Xzx5cUzITlS9lcD/PnZtceotEHXhcrmteWZ8kD/6ZYJPsZpfzCRn KkPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=s8QQMW2L; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n14si41959edo.179.2021.03.10.02.43.32; Wed, 10 Mar 2021 02:43:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=s8QQMW2L; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232041AbhCJKmW (ORCPT + 99 others); Wed, 10 Mar 2021 05:42:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38566 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231958AbhCJKlv (ORCPT ); Wed, 10 Mar 2021 05:41:51 -0500 Received: from mail-qv1-xf49.google.com (mail-qv1-xf49.google.com [IPv6:2607:f8b0:4864:20::f49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7F915C061761 for ; Wed, 10 Mar 2021 02:41:51 -0800 (PST) Received: by mail-qv1-xf49.google.com with SMTP id d15so12310296qvn.16 for ; Wed, 10 Mar 2021 02:41:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=CXDGXPLv4zBuXju/CY+pJKsC5gho63hQizGsKGKZIlo=; b=s8QQMW2LW/UVYVavbb8dseqF5ki4rCkHLP26JLijl8mjtrb2/K9xsn1pRuxTKkCI4A WGyH7yeNfeUXgTkHZRLKKruj4dvA8zqeAw8g9DEKxuPrIaGljnESUIgjFlOM+bCXz4tn c9bFCJzuS2ISr9LWzCXLNx+Wcm+zwSSUKC22+8M7ayZ+u40rra70SNM/qZC8LntQ8M+x LfbEJhGnYNOCsOoYIBKkY22AYCekDUpQLyniHJOCTXmbR2NjqIWEI/Q7LXtMgkQdT9PA pBMfOEGap4OIRTFxX57bGhAxB9ytAltLxLxoTVs8/M5KXEc0dKDW6iG4+bhnhlJ2Cq5H n5tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=CXDGXPLv4zBuXju/CY+pJKsC5gho63hQizGsKGKZIlo=; b=eCbnp1QgjkKmvh9nhcs7+wQ2J74NzQ6ytt64TlT+K5awCNfiTxFrmjOJvQMtdDSuc8 enubapolSpM1lJYaXu4eeG/vwluxCy44eFIge+4J/LO4fpQcdaWqIcDVv3urVXMTMBlt yAuzVUBl1J9ZxNVG+Pkeu3eNy9hA/vA/bK1ACx/7ayAqnTOXF7mghIKaiP4yuwIYb5GB XeA3DVUVheV/ZGj7iLp9UkDhDjOcrAaLgVosngF8cuJJQ3SZZzo5svS7WOzXkk3s4xD/ BpLzRnJrO3XpXJgL563XhpIw3WTmdjfvbGgS77kVprBLSs38s3OOZfSDVpIBU3araA+Q 1bsw== X-Gm-Message-State: AOAM531MWDiRF9c83nmMHmzWAZW76JEgCnIQzCWN97t3eI9UsBruf+5c W15O1PPdGOfENrB5MFR2IzefWf4naA== X-Received: from elver.muc.corp.google.com ([2a00:79e0:15:13:e995:ac0b:b57c:49a4]) (user=elver job=sendgmr) by 2002:a05:6214:2262:: with SMTP id gs2mr2155838qvb.32.1615372910698; Wed, 10 Mar 2021 02:41:50 -0800 (PST) Date: Wed, 10 Mar 2021 11:41:31 +0100 Message-Id: <20210310104139.679618-1-elver@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.30.1.766.gb4fecdf3b7-goog Subject: [PATCH RFC v2 0/8] Add support for synchronous signals on perf events From: Marco Elver To: elver@google.com, peterz@infradead.org, alexander.shishkin@linux.intel.com, acme@kernel.org, mingo@redhat.com, jolsa@redhat.com, mark.rutland@arm.com, namhyung@kernel.org, tglx@linutronix.de Cc: glider@google.com, viro@zeniv.linux.org.uk, arnd@arndb.de, christian@brauner.io, dvyukov@google.com, jannh@google.com, axboe@kernel.dk, mascasa@google.com, pcc@google.com, irogers@google.com, kasan-dev@googlegroups.com, linux-arch@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org, linux-kselftest@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 two kselftests at the end of the series. The kselftests verify and stress test the basic functionality. The discussion at [1] led to the changes proposed in this series. The approach taken in patch "Add support for SIGTRAP on perf events" to use 'event_limit' to trigger the signal was kindly suggested by Peter Zijlstra in [2]. [1] https://lore.kernel.org/lkml/CACT4Y+YPrXGw+AtESxAgPyZ84TYkNZdP0xpocX2jwVAbZD=-XQ@mail.gmail.com/ [2] https://lore.kernel.org/lkml/YBv3rAT566k+6zjg@hirez.programming.kicks-ass.net/ Motivation and example uses: 1. Our immediate motivation is low-overhead sampling-based race detection for user space [3]. 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). [3] https://llvm.org/devmtg/2020-09/slides/Morehouse-GWP-Tsan.pdf 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. 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. --- v2: * 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: https://lkml.kernel.org/r/20210223143426.2412737-1-elver@google.com Marco Elver (8): perf/core: Apply PERF_EVENT_IOC_MODIFY_ATTRIBUTES to children perf/core: Support only inheriting events if cloned with CLONE_THREAD perf/core: Add support for event removal on exec signal: Introduce TRAP_PERF si_code and si_perf to siginfo perf/core: Add support for SIGTRAP on perf events perf/core: Add breakpoint information to siginfo on SIGTRAP selftests/perf: Add kselftest for process-wide sigtrap handling selftests/perf: Add kselftest for remove_on_exec 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 | 5 +- 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 | 130 ++++++++- kernel/fork.c | 2 +- kernel/signal.c | 11 + .../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 | 256 ++++++++++++++++++ tools/testing/selftests/perf_events/settings | 1 + .../selftests/perf_events/sigtrap_threads.c | 202 ++++++++++++++ 18 files changed, 632 insertions(+), 15 deletions(-) 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 -- 2.30.1.766.gb4fecdf3b7-goog