Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp2262547rwi; Tue, 11 Oct 2022 06:53:00 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4uVMRGvCVCGt6Pc5aG4hahxKAE69jeigddAhrOsCcOpBXStFiwFvH+9YFy53WE9FocNSg9 X-Received: by 2002:a17:907:7287:b0:78d:8f26:5911 with SMTP id dt7-20020a170907728700b0078d8f265911mr14467674ejc.463.1665496380511; Tue, 11 Oct 2022 06:53:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665496380; cv=none; d=google.com; s=arc-20160816; b=SXH1VWlmjNN2oULEZOYlH4dNLcM7iegJXtIslhZsTYvHK1ZJLUn5i8WezHlEXSRPmS A8s0TQ8lg1bFY/s/nx+zVHRnHgUQkZdNJxrIlm9swA9jNhsuZ9qyK8JqbDDdW9j88HnL V1/SbmbyioHqJegANxLuyqE2OR2OswxQNOfCQ4e6VSBh1gP71N/pwK+jBZxFfE+dsOAQ 5HFNVzJIpRQRsafeq+K4TitWm5goOQxARM6MInLwgvgcmQvOhJi+HrTkaPvZr5dT2UgR pc2ncm+8pgFHuWY9hHKFp9bq1Z9oL/u+nYtpee1bfBhp5EL6sUvU/6Lh4a6MwYhP6n15 qApg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=0kl7dlvjnuqfiBoL2flcqsA/Ux8iLqViAUlVpL1x4Uo=; b=LjxxFthBVDSQDXL9hnZMDbAWEf4x12XUZ9tWtMpCzfZOkH5smUO+WtAW+1YWRJ3j1s l1Nra3GJioq1iPpTY9Ucb7btMl9iWteY1z7kF5ONX3g2XW9OyYOfwl3tRGDeWgF5n2/8 v7jhjDW5obN2eEhvIxineNwnh7XQzFrxMtIMyiJ6a4u/EhMUpVOcpR89OB57KFgCyrY5 iDonW/QOgtSqW85hOtfyz8Sptav71maDXP/iLK1Y+EyHSaoIzbAKCI3qJTRIrnoOCjhm jKY6XydkDGHchsBhhlZwg0he8QAPlpHdA2t1EmtxuFwA2Q5ONvKgvrdMFy22jR6lL6cE 7WyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=PMDKwr0K; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dd14-20020a1709069b8e00b0078d286bcb55si12376161ejc.764.2022.10.11.06.52.34; Tue, 11 Oct 2022 06:53:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=PMDKwr0K; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229932AbiJKNHI (ORCPT + 99 others); Tue, 11 Oct 2022 09:07:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52644 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229546AbiJKNHG (ORCPT ); Tue, 11 Oct 2022 09:07:06 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 736B92CE30; Tue, 11 Oct 2022 06:07:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=0kl7dlvjnuqfiBoL2flcqsA/Ux8iLqViAUlVpL1x4Uo=; b=PMDKwr0KWdXbhAaPTSlYSSmTYv mIRgI5rrLsxLj0eUfhOvczUj0+VhBlzjcUYHbk6NdI7Al5eUjP/uSvg79Q5+Jsznybyc9383/SYUv ng18nHoA7ujD0h4YtYCNt1xVNzQ1YDUS9R63z6eWL0Bk8+5/7y6qF+74JHtZldJWX9DI1RX5g8r2K 5102+Mwj1jyeFqD3UvqF0YSfOkiqTwR9tT49PIeLt9SBkkefqpoMKpbMUcKhC+ShVrGWwkIFtop/9 ga/OE6Nm+E1fje6qeCFeFSdhNNt5HzglmD1H5lIx/4tCaAByHdCUrQBOew4zb2b3DVO6+jVUzS7WH mTLEO1kw==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1oiEyI-004yPP-BO; Tue, 11 Oct 2022 13:06:59 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id C2F7830004F; Tue, 11 Oct 2022 15:06:52 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id A693029A09944; Tue, 11 Oct 2022 15:06:52 +0200 (CEST) Date: Tue, 11 Oct 2022 15:06:52 +0200 From: Peter Zijlstra To: Marco Elver Cc: Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, Dmitry Vyukov Subject: Re: [PATCH v2] perf: Fix missing SIGTRAPs Message-ID: References: <20220927121322.1236730-1-elver@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 11, 2022 at 02:58:36PM +0200, Marco Elver wrote: > On Tue, Oct 11, 2022 at 09:44AM +0200, Peter Zijlstra wrote: > > Subject: perf: Fix missing SIGTRAPs > > From: Peter Zijlstra > > Date: Thu Oct 6 15:00:39 CEST 2022 > > > > Marco reported: > > > > Due to the implementation of how SIGTRAP are delivered if > > perf_event_attr::sigtrap is set, we've noticed 3 issues: > > > > 1. Missing SIGTRAP due to a race with event_sched_out() (more > > details below). > > > > 2. Hardware PMU events being disabled due to returning 1 from > > perf_event_overflow(). The only way to re-enable the event is > > for user space to first "properly" disable the event and then > > re-enable it. > > > > 3. The inability to automatically disable an event after a > > specified number of overflows via PERF_EVENT_IOC_REFRESH. > > > > The worst of the 3 issues is problem (1), which occurs when a > > pending_disable is "consumed" by a racing event_sched_out(), observed > > as follows: > > > > CPU0 | CPU1 > > --------------------------------+--------------------------- > > __perf_event_overflow() | > > perf_event_disable_inatomic() | > > pending_disable = CPU0 | ... > > | _perf_event_enable() > > | event_function_call() > > | task_function_call() > > | /* sends IPI to CPU0 */ > > | ... > > __perf_event_enable() +--------------------------- > > ctx_resched() > > task_ctx_sched_out() > > ctx_sched_out() > > group_sched_out() > > event_sched_out() > > pending_disable = -1 > > > > > > perf_pending_event() > > perf_pending_event_disable() > > /* Fails to send SIGTRAP because no pending_disable! */ > > > > > > In the above case, not only is that particular SIGTRAP missed, but also > > all future SIGTRAPs because 'event_limit' is not reset back to 1. > > > > To fix, rework pending delivery of SIGTRAP via IRQ-work by introduction > > of a separate 'pending_sigtrap', no longer using 'event_limit' and > > 'pending_disable' for its delivery. > > > > Additionally; and different to Marco's proposed patch: > > > > - recognise that pending_disable effectively duplicates oncpu for > > the case where it is set. As such, change the irq_work handler to > > use ->oncpu to target the event and use pending_* as boolean toggles. > > > > - observe that SIGTRAP targets the ctx->task, so the context switch > > optimization that carries contexts between tasks is invalid. If > > the irq_work were delayed enough to hit after a context switch the > > SIGTRAP would be delivered to the wrong task. > > > > - observe that if the event gets scheduled out > > (rotation/migration/context-switch/...) the irq-work would be > > insufficient to deliver the SIGTRAP when the event gets scheduled > > back in (the irq-work might still be pending on the old CPU). > > > > Therefore have event_sched_out() convert the pending sigtrap into a > > task_work which will deliver the signal at return_to_user. > > > > Fixes: 97ba62b27867 ("perf: Add support for SIGTRAP on perf events") > > Reported-by: Marco Elver > > Debugged-by: Marco Elver > > Reviewed-by: Marco Elver > Tested-by: Marco Elver > > .. fuzzing, and lots of concurrent sigtrap_threads with this patch: > > https://lore.kernel.org/all/20221011124534.84907-1-elver@google.com/ > > > Signed-off-by: Peter Zijlstra (Intel) > > My original patch also attributed Dmitry: > > Reported-by: Dmitry Vyukov > Debugged-by: Dmitry Vyukov > > ... we all melted our brains on this one. :-) > > Would be good to get the fix into one of the upcoming 6.1-rc. Updated and yes, I'm planning on queueing this in perf/urgent the moment -rc1 happens. Thanks!