Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2673562pxb; Tue, 23 Feb 2021 12:37:24 -0800 (PST) X-Google-Smtp-Source: ABdhPJzGxA+gEBRP8Wnrqap5GML8wrcFWfFwYeoQBybHxx25pFqeoYrWqq6y1+Ogqu6XoS5Ka3R1 X-Received: by 2002:aa7:dd49:: with SMTP id o9mr29848752edw.14.1614112643813; Tue, 23 Feb 2021 12:37:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614112643; cv=none; d=google.com; s=arc-20160816; b=een/66u+nya9q5QgcH5hcdNMI/cALYcnk1qRwW9FvxVGUgyh5l0dvrSBoHN6Lm6b69 Col4xwTVM82tsGh0vqpL1jhtayQMvsHSGKFtR343pup/jKZih3S46/m1YKexUzqFtU1Q IN/EtOm2qBt5mqWArwdjZ+YfgRXt+dl4SjuOsOUlnSE52RAvPuTqwBD5y50qP9r5g6U0 s+0q5BrAbja7lhTYwmhXlcqvC4P2XI552ajcjepTRDppGxlASq/rm2xhluaz8kptB657 O1fUVIQvLCELm4RPP4luRV2ReiwdQf7iGDIQjLO6Gyvk8rdsMkhvRWfZInSCL9l5rD+Q cJIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=64chh81MrDhbGsEgUYAQK1O320WsxsfdzdSvZe/81Lc=; b=aSJB38owqwZ+itgd1+qPVtT64lPIrTe79MUFc797kc2Y1nfQqG82VSlROMxVNmCU7R 2cwOumovrv62d0+wdcAv/xhvm5tpPvLiae7BkM2NXQtVNBfzZGN1ftjCou4W1/wDc3T/ vTQ3rztm7UZkd0wa7n4v1NUTOgioP7fOXyIOjYqom2NVG7lpzGPe1wKDMx2Aj8bB418P PHqG5UmSAa0PSB/gtEbCpot3zfhOxAbMPwqYpOFSk/ybtiCLhsAJqHMOo7kJW5k+gh5I I6pBoEaSsFpgLrdH6VAitoolRuuoW84ziJMvjyMssIEl72spP3SS/DMsQh754j22cd6q rhUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=FMcvcTOt; 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 el22si14838275ejc.714.2021.02.23.12.36.59; Tue, 23 Feb 2021 12:37:23 -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=FMcvcTOt; 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 S232634AbhBWSOi (ORCPT + 99 others); Tue, 23 Feb 2021 13:14:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233115AbhBWSOg (ORCPT ); Tue, 23 Feb 2021 13:14:36 -0500 Received: from mail-qv1-xf2d.google.com (mail-qv1-xf2d.google.com [IPv6:2607:f8b0:4864:20::f2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25C67C06174A for ; Tue, 23 Feb 2021 10:13:56 -0800 (PST) Received: by mail-qv1-xf2d.google.com with SMTP id s12so7562490qvq.4 for ; Tue, 23 Feb 2021 10:13:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=64chh81MrDhbGsEgUYAQK1O320WsxsfdzdSvZe/81Lc=; b=FMcvcTOtJbk9S1OVOWzfwDI5hEJZtOOmWU/jx2w96FB7sbM4dTOUaL9uiX7+DbzOjh SckAfTThgCrNq9MZdWyLykL8A8V2tpZq2WKyEu+IEcuT9vYdy42OJfU0uY5KQmUvOc14 IWOySySdL1dIZtnKCoAktLqbu5etA0LNHAujHL8fGQQx/khRZpqpAlO/3G9ifKZOSe7p U/V1t1C2ssrd12UzPv6Tc8TI1ZQmeQL4SBk29m7M3GzQuqVAIvMNKithrg9NzgJ+wPvt p3t+iw8zwvxfIY5nhwTyV9xO8IgyaZDSCJqvoTzizj2jdUg5IQ/zrKmsPyS0Q5cSX+pn /DxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=64chh81MrDhbGsEgUYAQK1O320WsxsfdzdSvZe/81Lc=; b=MQXQZ5XgwqRQLEGagWtWfIVwSqYz/OzehXmRvXJaiDxbh/bshRXtN9tA2T7wHpAcrI JiB8ziC3IAzni6EU2S/dwBgXZKjQj/wrxSO5eKs0czdrKCdODYbT6LALYn1UoMNcnlJz 58XVyOpn8ZambCrtnrpDHcw0kMl0xhmWMe58FBX2I3GO/GCgz2ZeRU7eQXUW0WmuXmFt 6T1FVB5Bt9H4wmBjPTm+4J/SUrGDN8M4GG9AbbYChcbQ1R2QN7VR7YjZLSGI/oDge/8A loIofc8fA4HyygKJaztvvNWlPWfXDMsSm0tbRz7grxpLwXsu2yHxe/I4e+fRfBLtqAAd YtHg== X-Gm-Message-State: AOAM533lSxWUjNXi+oFKPmtJGbHVQ0ANclSLXkgLCnY9RyarsTQ7oYEQ 6bF4kbsKqrIjyDtnJQvgf3u/8BjNxVf37UswTcFBjA== X-Received: by 2002:a0c:8304:: with SMTP id j4mr26498875qva.18.1614104034885; Tue, 23 Feb 2021 10:13:54 -0800 (PST) MIME-Version: 1.0 References: <20210223143426.2412737-1-elver@google.com> <20210223143426.2412737-4-elver@google.com> In-Reply-To: <20210223143426.2412737-4-elver@google.com> From: Dmitry Vyukov Date: Tue, 23 Feb 2021 19:13:42 +0100 Message-ID: Subject: Re: [PATCH RFC 3/4] perf/core: Add support for SIGTRAP on perf events To: Marco Elver Cc: Peter Zijlstra , Alexander Shishkin , Arnaldo Carvalho de Melo , Ingo Molnar , Jiri Olsa , Mark Rutland , Namhyung Kim , Thomas Gleixner , Alexander Potapenko , Al Viro , Arnd Bergmann , Christian Brauner , Jann Horn , Jens Axboe , Matt Morehouse , Peter Collingbourne , Ian Rogers , kasan-dev , linux-arch , linux-fsdevel , LKML , linux-m68k@lists.linux-m68k.org, "the arch/x86 maintainers" Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Feb 23, 2021 at 3:34 PM Marco Elver wrote: > > 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. > > Link: https://lore.kernel.org/lkml/YBv3rAT566k+6zjg@hirez.programming.kicks-ass.net/ > Suggested-by: Peter Zijlstra > Signed-off-by: Marco Elver > --- > include/uapi/linux/perf_event.h | 3 ++- > kernel/events/core.c | 21 +++++++++++++++++++++ > 2 files changed, 23 insertions(+), 1 deletion(-) > > diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h > index ad15e40d7f5d..b9cc6829a40c 100644 > --- a/include/uapi/linux/perf_event.h > +++ b/include/uapi/linux/perf_event.h > @@ -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; > + sigtrap : 1, /* send synchronous SIGTRAP on event */ > + __reserved_1 : 28; > > union { > __u32 wakeup_events; /* wakeup every n events */ > diff --git a/kernel/events/core.c b/kernel/events/core.c > index 37a8297be164..8718763045fd 100644 > --- a/kernel/events/core.c > +++ b/kernel/events/core.c > @@ -6288,6 +6288,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); > @@ -6297,6 +6308,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_inc(&event->event_limit); /* rearm event */ We send the signal to the current task. Can this fire outside of the current task context? E.g. in interrupt/softirq/etc? And then we will send the signal to the current task. Watchpoint can be set to userspace address and then something asynchronous (some IO completion) that does not belong to this task access the userspace address (is this possible?). But watchpoints can also be set to kernel addresses, then another context can definitely access it. (1) can this happen? maybe perf context is somehow disabled when !in_task()? (2) if yes, what is the desired behavior? > + perf_sigtrap(event); > + return; > + } > + > perf_event_disable_local(event); > return; > } > @@ -11325,6 +11343,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; > /* > -- > 2.30.0.617.g56c4b15f3c-goog >