From patchwork Thu Nov 24 09:43:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 25411 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp3292013wrr; Thu, 24 Nov 2022 01:48:10 -0800 (PST) X-Google-Smtp-Source: AA0mqf7ReQ4stBjVDtqJioS4xowjXttZDYzyBK/0FPa+b2vcPspYKqfQT5Ue7lDZFq+S7+3jA7d5 X-Received: by 2002:aa7:d551:0:b0:468:fb2c:6c8c with SMTP id u17-20020aa7d551000000b00468fb2c6c8cmr27574427edr.340.1669283290165; Thu, 24 Nov 2022 01:48:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669283290; cv=none; d=google.com; s=arc-20160816; b=FZmrcNgvNaPVomI92YGKY8p79IkFYDOGoyqeMsIIZREsYSVr8XrFklTVCcLsJkYbYw 7t0O/siwPquTP6IVN5o2nN6qm1Z0N72iYeSHZnLDZ0KhJlyUYlDBdBCBUagmyXi890mp 1AzGumjQiNxLGXmBBlYyFPApqvL2d7ElZ19GFVLrD8Vvy2pcUlbouGzt5Mwg0xwS1DQb CBQInG2b9/MjURjCvUmgTx6fvnDofuZHH6DtZi7BmapO8pFC7EYh7N7AbZBzlQTOXXj8 RUO6dtKfnJcg2nMryQX1AkoQAMKMLlI3RR2Z+KMcfoKdVJXCbZA1g0nGj8tUplvfPnE5 yXCw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=3QBRCQ+BHT2z0qtsJ++jNMKm5qLGoOdudDBsHBJOE78=; b=xHQIWec+VCf/04DhakN6d0Ea/op1ERycDDPAZ9rxn2hfOXXpokk/hdlBUYCG/6H7AK v+kMLVPa+PRIdjbZ1ouzIJkZy/WPwHCzpjgufpgoj0zecIi1JfTUmSL/vSpPuSgp/Dr0 9VRaWkQMQTmpN0C0H0i+h52A+F0V7ub0yJbZ0MDK+RxcrRXyXBj8WgjH1qdXM2DlHvZH bP1SXrUv5oPlO3dGe3q8Aw2cSC9try7GOBjwPQPghz18dPE4HHsTZVwwFZAECWszLpoo F05CFvE4nqXNsWF/v1qYsfiCokdGNumXdfCt4Btf2DDIMAuqAw3JtcZ+9hC0dGEnVse0 P+6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=NKvUw7oG; dkim=neutral (no key) header.i=@linutronix.de; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jg8-20020a170907970800b0078dcddc1b8csi501087ejc.788.2022.11.24.01.47.43; Thu, 24 Nov 2022 01:48:10 -0800 (PST) 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=@linutronix.de header.s=2020 header.b=NKvUw7oG; dkim=neutral (no key) header.i=@linutronix.de; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229916AbiKXJnS (ORCPT + 99 others); Thu, 24 Nov 2022 04:43:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52634 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229915AbiKXJnN (ORCPT ); Thu, 24 Nov 2022 04:43:13 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8F3DDD22A2; Thu, 24 Nov 2022 01:43:05 -0800 (PST) Date: Thu, 24 Nov 2022 09:43:02 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1669282984; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3QBRCQ+BHT2z0qtsJ++jNMKm5qLGoOdudDBsHBJOE78=; b=NKvUw7oGOkJrmwWdlBtiOmXpOSp0RmdIsmmU8jkx1nFZnI2nI6jTzLWoC5JQTRRhziWIUQ y/dAnb/F+ZW56WWLraSkrzlvQAlcc5+0K6R5BJCUA22VhIfxGRIefdwieXTlyMPF2RW5nT RYtox3p1H1KzMUY1OpnCxTluA6RgxbqxI0O2bwhfj/HDCbSNyGoSbJZzgA52xpFAE4+VB/ Vvg2SL5hAbNfb2trAPly6vFseopmnlK8XbkhpdQduWioum2jnxgmMqEkgkAEh7Y+z5LzPK 0ipcID4gcz0C/vMTgUFbolpSTBVMWggKGVl5DnE4cRm0qrmI5zlGoeRjTHfHhQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1669282984; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3QBRCQ+BHT2z0qtsJ++jNMKm5qLGoOdudDBsHBJOE78=; b=Z1YL6gq1rY5mU0MQ8Jsm3k7gWHs7aSE3zEQArJsflOPTzfa1ycalc3tOw7mP72dElNPn4t 4Ihq5B5qaKmXOgBA== From: "tip-bot2 for Peter Zijlstra" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: perf/urgent] perf: Consider OS filter fail Cc: Pengfei Xu , "Peter Zijlstra (Intel)" , Marco Elver , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: References: MIME-Version: 1.0 Message-ID: <166928298250.4906.513715216575584648.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails 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_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750370395681606417?= X-GMAIL-MSGID: =?utf-8?q?1750370395681606417?= The following commit has been merged into the perf/urgent branch of tip: Commit-ID: 030a976efae83f7b6593afb11a8254d42f9290fe Gitweb: https://git.kernel.org/tip/030a976efae83f7b6593afb11a8254d42f9290fe Author: Peter Zijlstra AuthorDate: Sat, 19 Nov 2022 10:45:54 +08:00 Committer: Peter Zijlstra CommitterDate: Thu, 24 Nov 2022 10:12:23 +01:00 perf: Consider OS filter fail Some PMUs (notably the traditional hardware kind) have boundary issues with the OS filter. Specifically, it is possible for perf_event_attr::exclude_kernel=1 events to trigger in-kernel due to SKID or errata. This can upset the sigtrap logic some and trigger the WARN. However, if this invalid sample is the first we must not loose the SIGTRAP, OTOH if it is the second, it must not override the pending_addr with a (possibly) invalid one. Fixes: ca6c21327c6a ("perf: Fix missing SIGTRAPs") Reported-by: Pengfei Xu Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Marco Elver Tested-by: Pengfei Xu Link: https://lkml.kernel.org/r/Y3hDYiXwRnJr8RYG@xpf.sh.intel.com --- kernel/events/core.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index f2bb27e..9d15d2d 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -9273,6 +9273,19 @@ int perf_event_account_interrupt(struct perf_event *event) return __perf_event_account_interrupt(event, 1); } +static inline bool sample_is_allowed(struct perf_event *event, struct pt_regs *regs) +{ + /* + * Due to interrupt latency (AKA "skid"), we may enter the + * kernel before taking an overflow, even if the PMU is only + * counting user events. + */ + if (event->attr.exclude_kernel && !user_mode(regs)) + return false; + + return true; +} + /* * Generic event overflow handling, sampling. */ @@ -9306,6 +9319,13 @@ static int __perf_event_overflow(struct perf_event *event, } if (event->attr.sigtrap) { + /* + * The desired behaviour of sigtrap vs invalid samples is a bit + * tricky; on the one hand, one should not loose the SIGTRAP if + * it is the first event, on the other hand, we should also not + * trigger the WARN or override the data address. + */ + bool valid_sample = sample_is_allowed(event, regs); unsigned int pending_id = 1; if (regs) @@ -9313,7 +9333,7 @@ static int __perf_event_overflow(struct perf_event *event, if (!event->pending_sigtrap) { event->pending_sigtrap = pending_id; local_inc(&event->ctx->nr_pending); - } else if (event->attr.exclude_kernel) { + } else if (event->attr.exclude_kernel && valid_sample) { /* * Should not be able to return to user space without * consuming pending_sigtrap; with exceptions: @@ -9330,7 +9350,7 @@ static int __perf_event_overflow(struct perf_event *event, } event->pending_addr = 0; - if (data->sample_flags & PERF_SAMPLE_ADDR) + if (valid_sample && (data->sample_flags & PERF_SAMPLE_ADDR)) event->pending_addr = data->addr; irq_work_queue(&event->pending_irq); }