From nobody Sun Feb 8 17:42:06 2026 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 7F0BE79E5; Fri, 8 Mar 2024 17:58:21 +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=1709920703; cv=none; b=X3cnZ5aSdNs967pbI+oUrXX7CgmujHsTCn23m4ANBu+D95NZW+1+Nqt9zZTQevCGiNWUpFvd7Gs3LlXdnj39GF3uTt5gyRc7zoddaRskOyWWavIU4z3OLBH/3+Lz3pBZDE07KaifyMakXZNlRLlHxiBK+uZB+rvT8jutLRxEmmE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709920703; c=relaxed/simple; bh=1x/H3KVUUkvLDmgpGS1Nke9B7aI3HIltrKV+AEtaWUg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HWIIBRbE6Wlwtplbd9LT+lY0YDKggmYkDZS4bAWgsL5OsK06Wpgm5k7j8kAlsBbLATHhq8gj+V8ssO8SG/V98nxnDZzfJahhai/Hk3YzZsaXy2tffl2kn16HZ599Vk+mPCnLnlrjNrCMdv5a4k2N1tGha4sJswHViTsjnDjBkjs= 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=v4OmJ48C; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ziHz1O9T; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="v4OmJ48C"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ziHz1O9T" From: Sebastian Andrzej Siewior DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1709920699; 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=9Hqyi2fLhMWja1cb9pKy7mpQ/l/tY6lU3b5qleZavj0=; b=v4OmJ48CGNYbFrppyGlC2jevaEK7plTwqLa485cAKMYLlosrVridqFwI5yxacNF9se2O7b 5NK7Htc5j7G7lkiDU4c+ZJmsDJmT1CPKU9Y21+qC/lA6fxxovNTNYUrCs67QqCrEoNeJMM WUwn5nl52erfDuoU6gBJCKRrwhICWQLeTYHwkMcmiuYdaaRKKiX8V0/BmY2tIW9jQOX7S6 xE5VzwUn0fTEoqr78KPmQv1VGw6l4Pyx0rKU8wSo9mcy8lvavd90qM4sw42xtCpkqugiBP RZlSNW6S13I0g1dHXc2/mgtUn0NdtbQUCqVlR/8c5YUGVe18Ez7VaWOeJWf1IA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1709920699; 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=9Hqyi2fLhMWja1cb9pKy7mpQ/l/tY6lU3b5qleZavj0=; b=ziHz1O9TIToov9AHtRlOrWFr0J5dlqqZrDDkGxrMnNg4PuEw92OkwV/rwvtnBUqoEVDeGt HfhYub59FMz4CxCg== 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 , Mark Rutland , Namhyung Kim , Peter Zijlstra , Thomas Gleixner , Sebastian Andrzej Siewior Subject: [PATCH 1/4] perf: Move irq_work_queue() where the event is prepared. Date: Fri, 8 Mar 2024 18:51:27 +0100 Message-ID: <20240308175810.2894694-2-bigeasy@linutronix.de> In-Reply-To: <20240308175810.2894694-1-bigeasy@linutronix.de> References: <20240308175810.2894694-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 Content-Type: text/plain; charset="utf-8" Only if perf_event::pending_sigtrap is zero, the irq_work accounted by increminging perf_event::nr_pending. The member perf_event::pending_addr might be overwritten by a subsequent event if the signal was not yet delivered and is expected. The irq_work will not be enqeueued again because it has a check to be only enqueued once. Move irq_work_queue() to where the counter is incremented and perf_event::pending_sigtrap is set to make it more obvious that the irq_work is scheduled once. Signed-off-by: Sebastian Andrzej Siewior --- kernel/events/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index f0f0f71213a1d..c7a0274c662c8 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -9595,6 +9595,7 @@ static int __perf_event_overflow(struct perf_event *e= vent, if (!event->pending_sigtrap) { event->pending_sigtrap =3D pending_id; local_inc(&event->ctx->nr_pending); + irq_work_queue(&event->pending_irq); } else if (event->attr.exclude_kernel && valid_sample) { /* * Should not be able to return to user space without @@ -9614,7 +9615,6 @@ static int __perf_event_overflow(struct perf_event *e= vent, event->pending_addr =3D 0; if (valid_sample && (data->sample_flags & PERF_SAMPLE_ADDR)) event->pending_addr =3D data->addr; - irq_work_queue(&event->pending_irq); } =20 READ_ONCE(event->overflow_handler)(event, data, regs); --=20 2.43.0 From nobody Sun Feb 8 17:42:06 2026 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 D9DF340861; Fri, 8 Mar 2024 17:58:21 +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=1709920703; cv=none; b=uTVpdh5gEF1APwVVge1sRzomCXnul3QFUv8oNn793Y2c4DkxSm5wgEaU2hIZs7WO5dGdn2aQUNbomoT+drOd9FLu+70olJSOkAEVK5KxqDSDFL4RLCw+CmBwCsQVrYk0r9Zrtg+HTY0QRCQwRrcSok5qr6ulwIgsW/obmWCu3dE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709920703; c=relaxed/simple; bh=qgIOhCi1AT5/KkPlVlTKGgFIpu/HZ3VsmyhH2xLz0Ws=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MktAfAZmXFfXDYAYlwYAjba3Q4Bql7MHtp69yrkGKkluXc+D2RMP9VcU5SI2VgfGkW1OTzanB+DZJPegAwg3NtdJelS/UDL00ZC9lL2JX6SnhRLEbhsW3IEmsfHqBG5wp8jK4Bo8fbX8WP+dVHMV0kjcV3En61MBa1EZuBj4TCA= 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=JV4ty8dQ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=YJBdep7/; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="JV4ty8dQ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="YJBdep7/" From: Sebastian Andrzej Siewior DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1709920700; 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=xk1CSY2j/Du8Z3nH9cehrGYPSezPuuWTJQyDu/5RHlc=; b=JV4ty8dQlJElZcsroYiiZ914/yQt/Yer5CzQbRo9i2nuCjUCqZ64EIAXcOo+ox0ut4x/bd rosSSDROA4j4m4SxQRXWMAq2wt44UmwVtcV2SuAb0gKGdCXdmLnRXv8FHl6+3AeJIp4ePY 2/YTljWk1UeNjPA1EdSVVJBU7EnSVxJ3MMbxjU6Om4PbKtTmKwMWwFBUrrMNKsx4alkuRm lek+kHr3+GcT9BFQpFoqn/y5eRNmOoMDcCu9KtSHuKr5e8bGLbsUF/t0N+zD6NNGJ3oWgQ aTNe54hxf7ApwH85TYXY3bYI1Kgcdl7/aG4JpY9ncUkUxIVPLNcII9hKzXGnzg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1709920700; 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=xk1CSY2j/Du8Z3nH9cehrGYPSezPuuWTJQyDu/5RHlc=; b=YJBdep7/6jVdWfH49/GIFla8t1NJkLGvNkRNgK3L5DM5IGlxgxVukz6HUXU/ORoOHf+BoX zgAuKav8usy12mDg== 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 , Mark Rutland , Namhyung Kim , Peter Zijlstra , Thomas Gleixner , Sebastian Andrzej Siewior Subject: [PATCH 2/4] perf: Enqueue SIGTRAP always via task_work. Date: Fri, 8 Mar 2024 18:51:28 +0100 Message-ID: <20240308175810.2894694-3-bigeasy@linutronix.de> In-Reply-To: <20240308175810.2894694-1-bigeasy@linutronix.de> References: <20240308175810.2894694-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 Content-Type: text/plain; charset="utf-8" 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 triggering irq_work. Queue signal via task_work. Remove perf_event::pending_sigtrap and and use perf_event::pending_work instead. Signed-off-by: Sebastian Andrzej Siewior --- include/linux/perf_event.h | 3 +-- kernel/events/core.c | 31 ++++++------------------------- 2 files changed, 7 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..e4a85d6d2d685 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,15 @@ 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); } 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 +9590,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; --=20 2.43.0 From nobody Sun Feb 8 17:42:06 2026 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 9585540873; Fri, 8 Mar 2024 17:58:22 +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=1709920704; cv=none; b=YyUXUtzjMIXL9YostI4shNWH2QX4KVKuI+TpuOWUTtvc4kYcBLsfdCQa+EzD6G1WH7CyDjUUW/lawCo1dslMnzZxJySg9gnFNCKMJi9OzuygpnND77xZlemftMEHUJj8bF5rE2u2V50DgqLIicg/8PInT/zKzPQVjZLTHbYevPc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709920704; c=relaxed/simple; bh=a9DDZehV50x4XWLbjHcWEPL3CKFvwBk4n2wqrhoJtSw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IwT/D3MAW90prpD6n1/j6KVMNwhOp/IsorBRPqHQWrUA5lcoMKIlhPU/l6S/wGD709GYAMqKkqM2Fzu4ib7wzW6ipoEEySJ633m8ySpfJXMJ8thQczfKMGZw/JQM64D1kbZeKdtSc4jEYJ2zUCqVYZ+KecIxxOLaB1qGJmqMQ9E= 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=MmosQXkY; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=W+3UTTpN; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="MmosQXkY"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="W+3UTTpN" From: Sebastian Andrzej Siewior DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1709920700; 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=KGVbVzwGMjptyOBEmR5tRxK9Ir4UTrX3Zgcyetw1CtU=; b=MmosQXkYlxRCAsXVqGKBRH/btOd/i92AdfGb544G35qRBb5twLYQnwRLbU8a/2sto/mUFv iziiUykfNmCAgz8aWx9fQ8c4nRxQhJFdo/oOek5madvrXaBfJJV8d9qcj6+Uc92HE8kOdn nkosZrwSfefNBrHufniIEHsCbwsSwfnPjt0EXEhtvDRqT53Cx6lgxZz2urZ/R7IPMHS/hK 97+2X5aICCsv3v/7RuOwI060vStie0c4iHJHX8hF77M3wSF3P690P6mb/YzhrEYaikqaVu CeUYMNXtJfyjRaHHS9ozqTolPLEA9n6CHVasKk4q4sIaQvvkjml9Vcx/tFW9GA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1709920700; 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=KGVbVzwGMjptyOBEmR5tRxK9Ir4UTrX3Zgcyetw1CtU=; b=W+3UTTpNM5/kfWuzo8x4EEE8dijzJ4b58X9Llh3oW3Ba+M8lq5AZ4FMKvPOkgt0BiwBZ2V ipwsVQRqqkGhTZCA== 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 , Mark Rutland , Namhyung Kim , Peter Zijlstra , Thomas Gleixner , Sebastian Andrzej Siewior Subject: [PATCH 3/4] perf: Remove perf_swevent_get_recursion_context() from perf_pending_task(). Date: Fri, 8 Mar 2024 18:51:29 +0100 Message-ID: <20240308175810.2894694-4-bigeasy@linutronix.de> In-Reply-To: <20240308175810.2894694-1-bigeasy@linutronix.de> References: <20240308175810.2894694-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 Content-Type: text/plain; charset="utf-8" perf_swevent_get_recursion_context() is supposed to avoid recursion. This requires to remain on the same CPU in order to decrement/ increment the same counter. This is done by using preempt_disable(). Having preemption disabled while sending a signal leads to locking problems on PREEMPT_RT because sighand, a spinlock_t, becomes a sleeping lock. This callback runs in task context and currently delivers only a signal to "itself". Any kind of recusrion protection in this context is not required. Remove recursion protection in perf_pending_task(). Signed-off-by: Sebastian Andrzej Siewior --- kernel/events/core.c | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index e4a85d6d2d685..240e9b7dfd3e9 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -6785,14 +6785,6 @@ static void perf_pending_irq(struct irq_work *entry) static void perf_pending_task(struct callback_head *head) { struct perf_event *event =3D container_of(head, struct perf_event, pendin= g_task); - int rctx; - - /* - * If we 'fail' here, that's OK, it means recursion is already disabled - * and we won't recurse 'further'. - */ - preempt_disable_notrace(); - rctx =3D perf_swevent_get_recursion_context(); =20 if (event->pending_work) { event->pending_work =3D 0; @@ -6800,10 +6792,6 @@ static void perf_pending_task(struct callback_head *= head) local_dec(&event->ctx->nr_pending); } =20 - if (rctx >=3D 0) - perf_swevent_put_recursion_context(rctx); - preempt_enable_notrace(); - put_event(event); } =20 --=20 2.43.0 From nobody Sun Feb 8 17:42:06 2026 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 958854317C; Fri, 8 Mar 2024 17:58:22 +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=1709920704; cv=none; b=LoOsyb0SuySqasycSixYpGK9nPSdPY8KLxAyYBwJ8g4SiRVWhswbH2c7HcjgneoH06ygB839d7iASDiOMc8ZIUHEV414XwSigQc1ix3T8wkWCZOOqy8k9wov7XaW/LnOwx9fYraXL4VaYLlgdTYlnNf+Hg8beua7+AQxHY6bkBs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709920704; c=relaxed/simple; bh=v9oRDDeUcHqrkEUP8VzYFKDyX5c150rKdyAPyGd2O3o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HNuSsUyNV9TNjBgJZqqtmbxSPNMULcmF/t/tlKIUsbk3b/yWwFbeR7OSW8zTJADoKSF+wwn8Risq0eLiuzFa2VZvhRF3jVwYEkJ+fLQLCTQn9LZJBhor2CGtesYuA6LPBo+cTEUWio/DDr/3PLkx4cSA3tr7w6aJqUbjhKV7ZJ8= 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=rgG0Ytod; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=4oPOgEtH; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="rgG0Ytod"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="4oPOgEtH" From: Sebastian Andrzej Siewior DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1709920700; 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=aAKGV/1Hf8Gj+QyTGEf6WcavYV4PkwpLQlTxkKtAWUU=; b=rgG0YtodymkbClX9mzuGLYkdWdAwBNNNQhB/4wlg6MHVYgySMKBYXZek+/fAVkUb7k4eiT JfhIjiE4pkyaCYn530RsAR1vENxcJcZF1/qMmjR23mLq4BWqbfeNRNTbWiSe+viDL23iTY i1n2l2Bvry2ba1EU3dAWWzyUPIGXST+rNblxY1CBsRHEOej5lLpt5zj+Wir7JJ1E/8Y/5M QWulzh6kLLOdj1pNnv7XMZmw9Ssg6JNw/nT46sk++cMeoLgwVdTf7erfJq18EiodlWAEpI 4Ywm8b4lVK4d8GVG3Wh9E/kKDZ9vIcbbVs00jJigE9mqzG724oGZqjm38Rjozw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1709920700; 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=aAKGV/1Hf8Gj+QyTGEf6WcavYV4PkwpLQlTxkKtAWUU=; b=4oPOgEtHn3jB+Ul9uwEDG9+2lX/AOGqgv5y1750Mo6UawJKNuSVDIneQOgWTn9DRvx1BGY ioHiI/83oPl9O8Cg== 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 , Mark Rutland , Namhyung Kim , Peter Zijlstra , Thomas Gleixner , Sebastian Andrzej Siewior Subject: [PATCH 4/4] perf: Split __perf_pending_irq() out of perf_pending_irq() Date: Fri, 8 Mar 2024 18:51:30 +0100 Message-ID: <20240308175810.2894694-5-bigeasy@linutronix.de> In-Reply-To: <20240308175810.2894694-1-bigeasy@linutronix.de> References: <20240308175810.2894694-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 Content-Type: text/plain; charset="utf-8" perf_pending_irq() invokes perf_event_wakeup() and __perf_pending_irq(). The former is in charge of waking any tasks which wait to be woken up while the latter disables perf-events. The irq_work perf_pending_irq(), while this an irq_work, the callback is invoked in thread context on PREEMPT_RT. This is needed because all the waking functions (wake_up_all(), kill_fasync()) acquire sleep locks which must not be used with disabled interrupts. Disabling events, as done by __perf_pending_irq(), expects a hardirq context and disabled interrupts. This requirement is not fulfilled on PREEMPT_RT. Split functionality based on perf_event::pending_disable into irq_work named `pending_disable_irq' and invoke it in hardirq context on PREEMPT_RT. Rename the split out callback to perf_pending_disable(). Signed-off-by: Sebastian Andrzej Siewior --- include/linux/perf_event.h | 1 + kernel/events/core.c | 29 ++++++++++++++++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 24ac6765146c7..c1c6600541657 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -783,6 +783,7 @@ struct perf_event { unsigned int pending_disable; unsigned long pending_addr; /* SIGTRAP */ struct irq_work pending_irq; + struct irq_work pending_disable_irq; struct callback_head pending_task; unsigned int pending_work; =20 diff --git a/kernel/events/core.c b/kernel/events/core.c index 240e9b7dfd3e9..eef7df33eea4d 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -2449,7 +2449,7 @@ static void __perf_event_disable(struct perf_event *e= vent, * hold the top-level event's child_mutex, so any descendant that * goes to exit will block in perf_event_exit_event(). * - * When called from perf_pending_irq it's OK because event->ctx + * When called from perf_pending_disable it's OK because event->ctx * is the current context on this CPU and preemption is disabled, * hence we can't get into perf_event_task_sched_out for this context. */ @@ -2489,7 +2489,7 @@ EXPORT_SYMBOL_GPL(perf_event_disable); void perf_event_disable_inatomic(struct perf_event *event) { event->pending_disable =3D 1; - irq_work_queue(&event->pending_irq); + irq_work_queue(&event->pending_disable_irq); } =20 #define MAX_INTERRUPTS (~0ULL) @@ -5175,6 +5175,7 @@ static void perf_addr_filters_splice(struct perf_even= t *event, static void _free_event(struct perf_event *event) { irq_work_sync(&event->pending_irq); + irq_work_sync(&event->pending_disable_irq); =20 unaccount_event(event); =20 @@ -6711,7 +6712,7 @@ static void perf_sigtrap(struct perf_event *event) /* * Deliver the pending work in-event-context or follow the context. */ -static void __perf_pending_irq(struct perf_event *event) +static void __perf_pending_disable(struct perf_event *event) { int cpu =3D READ_ONCE(event->oncpu); =20 @@ -6749,11 +6750,26 @@ static void __perf_pending_irq(struct perf_event *e= vent) * irq_work_queue(); // FAILS * * irq_work_run() - * perf_pending_irq() + * perf_pending_disable() * * But the event runs on CPU-B and wants disabling there. */ - irq_work_queue_on(&event->pending_irq, cpu); + irq_work_queue_on(&event->pending_disable_irq, cpu); +} + +static void perf_pending_disable(struct irq_work *entry) +{ + struct perf_event *event =3D container_of(entry, struct perf_event, pendi= ng_disable_irq); + int rctx; + + /* + * If we 'fail' here, that's OK, it means recursion is already disabled + * and we won't recurse 'further'. + */ + rctx =3D perf_swevent_get_recursion_context(); + __perf_pending_disable(event); + if (rctx >=3D 0) + perf_swevent_put_recursion_context(rctx); } =20 static void perf_pending_irq(struct irq_work *entry) @@ -6776,8 +6792,6 @@ static void perf_pending_irq(struct irq_work *entry) perf_event_wakeup(event); } =20 - __perf_pending_irq(event); - if (rctx >=3D 0) perf_swevent_put_recursion_context(rctx); } @@ -11904,6 +11918,7 @@ perf_event_alloc(struct perf_event_attr *attr, int = cpu, =20 init_waitqueue_head(&event->waitq); init_irq_work(&event->pending_irq, perf_pending_irq); + event->pending_disable_irq =3D IRQ_WORK_INIT_HARD(perf_pending_disable); init_task_work(&event->pending_task, perf_pending_task); =20 mutex_init(&event->mmap_mutex); --=20 2.43.0