Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp940870lqp; Thu, 21 Mar 2024 23:52:40 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUg6t0zEb4R1tr5Wrd8dAGE/I7pPh5WthDUq3K6xXta3LKp4Doldu+4hyNePTl3pJaFF8EvGEyzoXhB+B5y92xRKjl+/03tERfQszD77Q== X-Google-Smtp-Source: AGHT+IEs6CDjlge0HpgFgDiVHZZkAsOxja8CAHhAwte40I27VTpbkhvzbbkX5/FH7eG0WkCjlBJn X-Received: by 2002:a05:622a:14cb:b0:430:eb2e:e5c5 with SMTP id u11-20020a05622a14cb00b00430eb2ee5c5mr1194712qtx.33.1711090360306; Thu, 21 Mar 2024 23:52:40 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711090360; cv=pass; d=google.com; s=arc-20160816; b=htO2v4Tnt8kl9VvzO7VaanKDw5J6PFWp69pPk0zDKY0ewaQgzwpZt5Cn7E1xQTnr2U wpQNJnDSQ5ebEvntETh2MYsetF9NN4PjBtb0xsPR/Cl0YAD4Kkbev0WchEzBtGoCddsA DzXnfGh5mb9ytXC8z9TEWHvCuQdRTf2C+lHUIM45+rZwxQjIr0wuILlNVsINIozbpkRb q0Fsyc5ztqrZ3uO6lWKs5R+mTytmNvC5doIawGOICm9aasNt8TQ3OD0ZGBZAHFtGgjtO kPStH0Deo3k/6Cj/YxXnXD+Gf2cCY/+Z2pdUTQVzEAAsycHq77w0zjvnSLJZrsX5UtPD KXyg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:dkim-signature:dkim-signature:from; bh=hVWkTfLyeena7uooSCx+qKpvfluWfb5gR012eo5Qaig=; fh=shuUCmE+hQsLem2SGJGjn0zib0LKOC3uV8i8/kUZ968=; b=MAxS9S+ydzXECBArD6XKfYy/rf4LCSBSIFWd/4gkmCNEa1APDHJCbvZ5tspn7F7SaD lpFpJBxRTkt5lO1kO01Mv212NggKDE7i3zKH3bDRh5YBMWPXAqO4SXFlZmmo5wVlwRLP yTaKlit/7skRhX0KM6/DL+BJtM/Kp1b3gfsRVGLSLnudslmU2Z7iBbT7Ktm9fNVt/RDw s2f2Bqjp2VZU3EgkjT1/OZEhe72RiJqS5rg+aZTrelulS2T620esmqFajvDVTvWR9U4j BcFmzy7Wb81TSZlO3qUJjdIuaMrOuLUzt5C+ANT4ADde4LkB7t0llVwBBQvRcunYgrFG UNZw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=QRnaIk3Y; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-111032-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-111032-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id o11-20020a05622a138b00b0042f159aa86fsi1319660qtk.236.2024.03.21.23.52.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 23:52:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-111032-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=QRnaIk3Y; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-111032-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-111032-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id F27191C23908 for ; Fri, 22 Mar 2024 06:52:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B271413ADD; Fri, 22 Mar 2024 06:52:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="QRnaIk3Y"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="srG5idyu" Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CE15B12E56; Fri, 22 Mar 2024 06:52:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711090337; cv=none; b=JGnME1JSbCQmfTYNxqHz21guCy2gmNdqv34t6h7yqrRwpVe9GH2tWWn7lxHSUDKCho95tjFN5tTd1uu0sX0pKmh4uLofwyF7GyMcC5/0ieLexIPqgDmPqO9onUOCJ9kdupOTrzqtju7+9YMNZAleUmfUluv3eAnmshkdYdkmwnQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711090337; c=relaxed/simple; bh=SshC32YEexUo3g/CT1Jab1UAR6HgOFlH0X5KoqCdP8U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UG8E0fhIRIw2lZZJxk9dsvM/xF9yVnuqE+J6DMsb335DeuRgaEfVrxnKe1AHQHmR4pEwzcMsTtAJcLexmA5HvMP95p1lE8GJgMpobb0I8UYtr4M491hrgKgcGKDJhZtfKSJTlomc6Xa6fBTIc/08SUC7KfqueCVM/mp9QZbUEEQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=QRnaIk3Y; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=srG5idyu; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de From: Sebastian Andrzej Siewior DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1711090333; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hVWkTfLyeena7uooSCx+qKpvfluWfb5gR012eo5Qaig=; b=QRnaIk3YqfUgMyeXsAuTSZuaIWsD908AqkeCTTHWHgcsVlDIgZGaRhleqYEKzD1Yim06sc jiIJ/fqpiDLAwBWfrVDaZzRQPlmkdAuhQ35VMiZ66jw919erMFZR/9ByqE05HkbA6/jsEu t2XJc0iMN2ONveBsL2JZswXoAZ2TRdJM3/RTug/uiD/zPy/0tZ3yB5r7wzMqirUwA/74KW CEhAOpipsbxlfINBUXJKiRWtt7bAXWSviwykRbnzK1lHtvro9aajdRgoW3k8O4z79Lqe4x 4ZJmvlk1jZyKQDQhD92XTD1ZMgEhvo7xGfOHcBSDglJxN7vcoyyaZBnMVclBAw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1711090333; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hVWkTfLyeena7uooSCx+qKpvfluWfb5gR012eo5Qaig=; b=srG5idyuqejdKcOeTBbac3C2XmruqocMTd0cS/ZaY7fqkK4vpIS+oaz6dxTSTkNVzHIa4j 6h/0LWBRY4jRg0Bw== To: linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Adrian Hunter , Alexander Shishkin , Arnaldo Carvalho de Melo , Ian Rogers , Ingo Molnar , Jiri Olsa , Marco Elver , Mark Rutland , Namhyung Kim , Peter Zijlstra , Thomas Gleixner , Sebastian Andrzej Siewior , Arnaldo Carvalho de Melo Subject: [PATCH v3 2/4] perf: Enqueue SIGTRAP always via task_work. Date: Fri, 22 Mar 2024 07:48:22 +0100 Message-ID: <20240322065208.60456-3-bigeasy@linutronix.de> In-Reply-To: <20240322065208.60456-1-bigeasy@linutronix.de> References: <20240322065208.60456-1-bigeasy@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable A signal is delivered by raising irq_work() which works from any context including NMI. irq_work() can be delayed if the architecture does not provide an interrupt vector. In order not to lose a signal, the signal is injected via task_work during event_sched_out(). Instead going via irq_work, the signal could be added directly via task_work. The signal is sent to current and can be enqueued on its return path to userland instead of triggering irq_work. A dummy IRQ is required in the NMI case to ensure the task_work is handled before returning to user land. For this irq_work is used. An alternative would be just raising an interrupt like arch_send_call_function_single_ipi(). During testing with `remove_on_exec' it become visible that the event can be enqueued via NMI during execve(). The task_work must not be kept because free_event() will complain later. Also the new task will not have a sighandler installed. Queue signal via task_work. Remove perf_event::pending_sigtrap and and use perf_event::pending_work instead. Raise irq_work in the NMI case for a dummy interrupt. Remove the task_work if the event is freed. Tested-by: Marco Elver Tested-by: Arnaldo Carvalho de Melo Signed-off-by: Sebastian Andrzej Siewior --- include/linux/perf_event.h | 3 +- kernel/events/core.c | 58 ++++++++++++++++++++++---------------- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index d2a15c0c6f8a9..24ac6765146c7 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -781,7 +781,6 @@ struct perf_event { unsigned int pending_wakeup; unsigned int pending_kill; unsigned int pending_disable; - unsigned int pending_sigtrap; unsigned long pending_addr; /* SIGTRAP */ struct irq_work pending_irq; struct callback_head pending_task; @@ -959,7 +958,7 @@ struct perf_event_context { struct rcu_head rcu_head; =20 /* - * Sum (event->pending_sigtrap + event->pending_work) + * Sum (event->pending_work + event->pending_work) * * The SIGTRAP is targeted at ctx->task, as such it won't do changing * that until the signal is delivered. diff --git a/kernel/events/core.c b/kernel/events/core.c index c7a0274c662c8..e0b2da8de485f 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -2283,21 +2283,6 @@ event_sched_out(struct perf_event *event, struct per= f_event_context *ctx) state =3D PERF_EVENT_STATE_OFF; } =20 - if (event->pending_sigtrap) { - bool dec =3D true; - - event->pending_sigtrap =3D 0; - if (state !=3D PERF_EVENT_STATE_OFF && - !event->pending_work) { - event->pending_work =3D 1; - dec =3D false; - WARN_ON_ONCE(!atomic_long_inc_not_zero(&event->refcount)); - task_work_add(current, &event->pending_task, TWA_RESUME); - } - if (dec) - local_dec(&event->ctx->nr_pending); - } - perf_event_set_state(event, state); =20 if (!is_software_event(event)) @@ -6741,11 +6726,6 @@ static void __perf_pending_irq(struct perf_event *ev= ent) * Yay, we hit home and are in the context of the event. */ if (cpu =3D=3D smp_processor_id()) { - if (event->pending_sigtrap) { - event->pending_sigtrap =3D 0; - perf_sigtrap(event); - local_dec(&event->ctx->nr_pending); - } if (event->pending_disable) { event->pending_disable =3D 0; perf_event_disable_local(event); @@ -9592,14 +9572,23 @@ static int __perf_event_overflow(struct perf_event = *event, =20 if (regs) pending_id =3D hash32_ptr((void *)instruction_pointer(regs)) ?: 1; - if (!event->pending_sigtrap) { - event->pending_sigtrap =3D pending_id; + if (!event->pending_work) { + event->pending_work =3D pending_id; local_inc(&event->ctx->nr_pending); - irq_work_queue(&event->pending_irq); + WARN_ON_ONCE(!atomic_long_inc_not_zero(&event->refcount)); + task_work_add(current, &event->pending_task, TWA_RESUME); + /* + * The NMI path returns directly to userland. The + * irq_work is raised as a dummy interrupt to ensure + * regular return path to user is taken and task_work + * is processed. + */ + if (in_nmi()) + irq_work_queue(&event->pending_irq); } else if (event->attr.exclude_kernel && valid_sample) { /* * Should not be able to return to user space without - * consuming pending_sigtrap; with exceptions: + * consuming pending_work; with exceptions: * * 1. Where !exclude_kernel, events can overflow again * in the kernel without returning to user space. @@ -9609,7 +9598,7 @@ static int __perf_event_overflow(struct perf_event *e= vent, * To approximate progress (with false negatives), * check 32-bit hash of the current IP. */ - WARN_ON_ONCE(event->pending_sigtrap !=3D pending_id); + WARN_ON_ONCE(event->pending_work !=3D pending_id); } =20 event->pending_addr =3D 0; @@ -13049,6 +13038,13 @@ static void sync_child_event(struct perf_event *ch= ild_event) &parent_event->child_total_time_running); } =20 +static bool task_work_cb_match(struct callback_head *cb, void *data) +{ + struct perf_event *event =3D container_of(cb, struct perf_event, pending_= task); + + return event =3D=3D data; +} + static void perf_event_exit_event(struct perf_event *event, struct perf_event_context = *ctx) { @@ -13088,6 +13084,18 @@ perf_event_exit_event(struct perf_event *event, st= ruct perf_event_context *ctx) * Kick perf_poll() for is_event_hup(); */ perf_event_wakeup(parent_event); + /* + * Cancel pending task_work and update counters if it has not + * yet been delivered to userland. free_event() expects the + * reference counter at one and keeping the event around until + * the task returns to userland can be a unexpected if there is + * no signal handler registered. + */ + if (event->pending_work && + task_work_cancel_match(current, task_work_cb_match, event)) { + put_event(event); + local_dec(&event->ctx->nr_pending); + } free_event(event); put_event(parent_event); return; --=20 2.43.0