Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp35710pxf; Wed, 24 Mar 2021 20:07:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxj6oakikd5GSukgeV3/+Ut+z7fXgbz+BoY+zD6s1FiHiIkgOJHzCoRWsVJHu7XklN1Dpm1 X-Received: by 2002:a05:6402:488:: with SMTP id k8mr6628906edv.233.1616641676874; Wed, 24 Mar 2021 20:07:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616641676; cv=none; d=google.com; s=arc-20160816; b=Js4Ag/AmPHr2caexXLbOJHZwXkR/QbnuXPGWPPAI3vCepc8aDUzLXWfxavznROgwMj meQJd+5t7H90P/TlKBu9OsdP8EwmcxrRhxOae4C4ebGwik+TUNx5zigixun9vPdqTYKw DST62CTTCYClMNMY/vONdGEusxcGtpQFrjsoJbK9eMeId4AKB6ES9rzV1cC45svcZpYU iIML8VxNQWN2tDdKDt1jUabOBFae1DI4C0KKWEteciGTog4OZLNroZaQiYN4iaEn23lu XvewUPoRyGZL0Wr5OOjV4GY26Zye78IgAYRbXLdJBZ8d1rqATpJ94UY2rcMNPEAnRt+b qIew== 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=tUH1uT1xGENlL23VeLmAGT6RQ/hIIGv8URJxekLD7KA=; b=K/oNpdb3fQG8mPlQpcHzlo7QRAM925ohndltwCs29+ycdGqXOT760z28+eMx9wiykg /bxBEPR5IKHlBY+zziaN0pjRmYhtdpybCcGpeUCnaphbp2DEHRQjuAskkIc5s76wqY1t n9gxWPXortL5gCCLcXzvsTTF2e7IortOowvDgK10YeRcyiybHjTR2DrT34eJ+Ai9y3PC f8uzuGM3i/HH8e/bN0DL+frkX4zNrXjuZLr9qDzz/aJHs4n/fv8ggUv6IMtrGySyjYQs GAmqHfpyy0KAzNtn3cIhPWKWasFbFQcVYPgih4EzeI7uH83Mt6+FAS9letrYjuOsr5dV 0gGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=NQINk5lc; 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 y13si3243007edv.220.2021.03.24.20.07.33; Wed, 24 Mar 2021 20:07:56 -0700 (PDT) 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=NQINk5lc; 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 S235536AbhCXNs1 (ORCPT + 99 others); Wed, 24 Mar 2021 09:48:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40890 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235647AbhCXNrz (ORCPT ); Wed, 24 Mar 2021 09:47:55 -0400 Received: from mail-ot1-x336.google.com (mail-ot1-x336.google.com [IPv6:2607:f8b0:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D9C5C0613DF for ; Wed, 24 Mar 2021 06:47:55 -0700 (PDT) Received: by mail-ot1-x336.google.com with SMTP id h6-20020a0568300346b02901b71a850ab4so23058856ote.6 for ; Wed, 24 Mar 2021 06:47:55 -0700 (PDT) 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=tUH1uT1xGENlL23VeLmAGT6RQ/hIIGv8URJxekLD7KA=; b=NQINk5lciTHNfSmo0k4fLx5pcbEjTXfE1KI1AXj30vGqCN2lu5TqFPeK/1DUwA9bBf RRiDPZVJQkYd8iVT6pr3WD2Et13X50D4cLcdCSyws+n4HgeQuHX2sSFaDtMjnBmnfRYy Lsq/HbdcFYQTeKcCn/w7YxXyJFFRPS9rX5cHwBOP2yXMFX8NEmTJ26zeGY2UpfToe37h wC2Uw40HuR0jm60iWO7yCUiTFmaJ/kGdjLvKhf0niMRxrUF+Z2f4VHIxjsCBFDPxh7Yp Lr43KsTzjVBS8tDGIL9/HjNZxVXTRfiKkD1MplT+cSgG3jNy+Y7on3f7r/zuPCz5WBPO QtEA== 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=tUH1uT1xGENlL23VeLmAGT6RQ/hIIGv8URJxekLD7KA=; b=oRnUQyMloRNGL8wNX+qdSNvaRR6LfkAmiuICGRbcTyAc7graVK4UoUfJokYhiCVZxE LykiH8lmYRi7BOe338P2KrjIGz+vVA0CyrDWmj5b0+s4bKT1kO5oVKGFrJK2liNN/d4o 7362ok5v4OhFYO5aR3U7YZyMUYUcRCLdL2DTLVzd+nUgQlzvg01RAVc1B35J/HPrpWle iHHbpcIsvr5w3lXbOKwMBVyQuNvjroEoNIiac5J1Bv3dEMFXbhQTsJaOxiPgsp56RdOp UJ75EH8tHeTqEeN7MkDEtIV1A7XqTEBNgnBrPBsOPksT3ejlllMXiwQzGSmW9BdcvImr nlKA== X-Gm-Message-State: AOAM532fQF+rZgLvjOTcgHAGkiY3Kz0GPzNY4SrwCB8o+nPuEEDzHmnN kCyJJeQn3k4fQP6rcbhjRJgCsMuTZIc8JyyxOR19Yw== X-Received: by 2002:a9d:5508:: with SMTP id l8mr3422469oth.233.1616593674497; Wed, 24 Mar 2021 06:47:54 -0700 (PDT) MIME-Version: 1.0 References: <20210324112503.623833-1-elver@google.com> <20210324112503.623833-8-elver@google.com> In-Reply-To: From: Marco Elver Date: Wed, 24 Mar 2021 14:47:43 +0100 Message-ID: Subject: Re: [PATCH v3 07/11] perf: Add breakpoint information to siginfo on SIGTRAP To: Peter Zijlstra Cc: Alexander Shishkin , Arnaldo Carvalho de Melo , Ingo Molnar , Jiri Olsa , Mark Rutland , Namhyung Kim , Thomas Gleixner , Alexander Potapenko , Al Viro , Arnd Bergmann , Christian Brauner , Dmitry Vyukov , Jann Horn , Jens Axboe , Matt Morehouse , Peter Collingbourne , Ian Rogers , kasan-dev , linux-arch , linux-fsdevel , LKML , "the arch/x86 maintainers" , "open list:KERNEL SELFTEST FRAMEWORK" Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 24 Mar 2021 at 14:21, Peter Zijlstra wrote: > > On Wed, Mar 24, 2021 at 02:01:56PM +0100, Peter Zijlstra wrote: > > On Wed, Mar 24, 2021 at 01:53:48PM +0100, Peter Zijlstra wrote: > > > On Wed, Mar 24, 2021 at 12:24:59PM +0100, Marco Elver wrote: > > > > 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. > > > > > > > > Signed-off-by: Marco Elver > > > > --- > > > > v2: > > > > * Add comment about si_perf==0. > > > > --- > > > > kernel/events/core.c | 16 ++++++++++++++++ > > > > 1 file changed, 16 insertions(+) > > > > > > > > diff --git a/kernel/events/core.c b/kernel/events/core.c > > > > index 1e4c949bf75f..0316d39e8c8f 100644 > > > > --- a/kernel/events/core.c > > > > +++ b/kernel/events/core.c > > > > @@ -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. > > > > 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