From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5631AC43217 for ; Tue, 12 Apr 2022 12:08:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352095AbiDLMID (ORCPT ); Tue, 12 Apr 2022 08:08:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352419AbiDLMC6 (ORCPT ); Tue, 12 Apr 2022 08:02:58 -0400 Received: from mail-yw1-x112c.google.com (mail-yw1-x112c.google.com [IPv6:2607:f8b0:4864:20::112c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D7B18023F for ; Tue, 12 Apr 2022 04:00:09 -0700 (PDT) Received: by mail-yw1-x112c.google.com with SMTP id 00721157ae682-2ebf3746f87so101896377b3.6 for ; Tue, 12 Apr 2022 04:00:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=v2zi2giavZdO2HMDfrc7yK4A4aR/ngLsqIJD3W77LNs=; b=b4v6GqYxeJjlJ1RbEQauF9GEIWimtem9DfI/ea+58BboREvflEEq0M6jErAnAoE93B tYuy5mJ/r4ba02CczLRQd8TmQ7CK9KY/1RfIbv6VC/vU5FzAIsre+zMd2kHi6JeMfPOF 4fBT0F6bq2XsYtIaWNClXMgBpjhBJ9AS7/1/3qS47r77iP3+bY4fHCX9/uoSi8DAvTqG k/jDnzm69qk4dXs/N7H146ttsPkrszUV5cVSdNFLc0OGWTX1yWcUZvfGu8L3L3E878+9 LZbWbMEWL706ypWKJhQXFIcNT9lcoZhbr2KfSuGPorip9ZdCMOWb/sRTFpPLw/dEIfsz NCSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=v2zi2giavZdO2HMDfrc7yK4A4aR/ngLsqIJD3W77LNs=; b=giFUnQNUayzIX00N+ht1i8KtG3nhHpfpwHFAapeCM94gDy8nfL7FKJpuT29FfhD9cW AuxHNYRi9wYOo0xC3YM1tHh+Seet3k58l6BSwYemVrKcBQWRMaiwkG2HrMTutA7/PMeC iuxerKLv9Z9r3qIlNyJ6PT1SJRcd8q6NqogOLcnHQN5pjc1sNutRo1TEc4Qr7/5FGrs9 Qrjni4SDwdE8JGSgkmuIiEL8ItuIv3hfLJz/WdNJEdhNadFe2LuDatSMVWLAvZZ952IT HtwOFFO6nW5T6LvKIHPpAB1ZdUB2AK+3APA5v4JnJrnSBEKqKygG58Hq7QT+YE6HlSRD qOIw== X-Gm-Message-State: AOAM531cp+H5dq4vydWe4zmPSX8uKmXzRDhGSFstf0aP2Jg04soomqNH nrU2yIY+Ttyblvz9M0SOqSGIeL8Mvcla5RCaZhJEKw== X-Google-Smtp-Source: ABdhPJwwqPNdGAbIJgV8Tw3Kjk4tMF7wq4HRILrI/52e/kefI6BLXOkH7KMdW6Q3t6UkDG+3QNDDwa3vKedsVrgXFAE= X-Received: by 2002:a0d:e743:0:b0:2eb:3106:9b32 with SMTP id q64-20020a0de743000000b002eb31069b32mr30496791ywe.512.1649761208158; Tue, 12 Apr 2022 04:00:08 -0700 (PDT) MIME-Version: 1.0 References: <20220404111204.935357-1-elver@google.com> In-Reply-To: From: Marco Elver Date: Tue, 12 Apr 2022 13:00:00 +0200 Message-ID: Subject: Re: [PATCH] signal: Deliver SIGTRAP on perf event asynchronously if blocked To: Dmitry Vyukov , "Eric W. Biederman" , Peter Zijlstra Cc: Thomas Gleixner , Ingo Molnar , x86@kernel.org, linux-arm-kernel@lists.infradead.org, linux-m68k@lists.linux-m68k.org, sparclinux@vger.kernel.org, linux-arch@vger.kernel.org, linux-perf-users@vger.kernel.org, kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 5 Apr 2022 at 15:30, Dmitry Vyukov wrote: > On Mon, 4 Apr 2022 at 13:12, Marco Elver wrote: > > With SIGTRAP on perf events, we have encountered termination of > > processes due to user space attempting to block delivery of SIGTRAP. > > Consider this case: > > > > > > ... > > sigset_t s; > > sigemptyset(&s); > > sigaddset(&s, SIGTRAP | ); > > sigprocmask(SIG_BLOCK, &s, ...); > > ... > > > > > > When the perf event triggers, while SIGTRAP is blocked, force_sig_perf() > > will force the signal, but revert back to the default handler, thus > > terminating the task. > > > > This makes sense for error conditions, but not so much for explicitly > > requested monitoring. However, the expectation is still that signals > > generated by perf events are synchronous, which will no longer be the > > case if the signal is blocked and delivered later. > > > > To give user space the ability to clearly distinguish synchronous from > > asynchronous signals, introduce siginfo_t::si_perf_flags and > > TRAP_PERF_FLAG_ASYNC (opted for flags in case more binary information is > > required in future). > > > > The resolution to the problem is then to (a) no longer force the signal > > (avoiding the terminations), but (b) tell user space via si_perf_flags > > if the signal was synchronous or not, so that such signals can be > > handled differently (e.g. let user space decide to ignore or consider > > the data imprecise). > > > > The alternative of making the kernel ignore SIGTRAP on perf events if > > the signal is blocked may work for some usecases, but likely causes > > issues in others that then have to revert back to interception of > > sigprocmask() (which we want to avoid). [ A concrete example: when using > > breakpoint perf events to track data-flow, in a region of code where > > signals are blocked, data-flow can no longer be tracked accurately. > > When a relevant asynchronous signal is received after unblocking the > > signal, the data-flow tracking logic needs to know its state is > > imprecise. ] > > > > Link: https://lore.kernel.org/all/Yjmn%2FkVblV3TdoAq@elver.google.com/ > > Fixes: 97ba62b27867 ("perf: Add support for SIGTRAP on perf events") > > Reported-by: Dmitry Vyukov > > Signed-off-by: Marco Elver > > Tested-by: Dmitry Vyukov > > I've tested delivery of SIGTRAPs when it's blocked with sigprocmask, > it does not kill the process now. > > And tested the case where previously I was getting infinite recursion > and stack overflow (SIGTRAP handler causes another SIGTRAP recursively > before being able to detect recursion and return). With this patch it > can be handled by blocking recursive SIGTRAPs (!SA_NODEFER). Thanks! Should there be any further comments, please shout. Thanks, -- Marco