public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Gabriel Shahrouzi <gshahrouzi@gmail.com>,
	syzbot+ff3aa851d46ab82953a3@syzkaller.appspotmail.com,
	Ingo Molnar <mingo@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Ravi Bangoria <ravi.bangoria@amd.com>,
	Kan Liang <kan.liang@linux.intel.com>,
	Oleg Nesterov <oleg@redhat.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Sasha Levin <sashal@kernel.org>,
	mingo@redhat.com, acme@kernel.org, namhyung@kernel.org,
	linux-perf-users@vger.kernel.org
Subject: [PATCH AUTOSEL 6.6 05/24] perf/core: Fix WARN_ON(!ctx) in __free_event() for partial init
Date: Mon, 14 Apr 2025 09:29:38 -0400	[thread overview]
Message-ID: <20250414132957.680250-5-sashal@kernel.org> (raw)
In-Reply-To: <20250414132957.680250-1-sashal@kernel.org>

From: Gabriel Shahrouzi <gshahrouzi@gmail.com>

[ Upstream commit 0ba3a4ab76fd3367b9cb680cad70182c896c795c ]

Move the get_ctx(child_ctx) call and the child_event->ctx assignment to
occur immediately after the child event is allocated. Ensure that
child_event->ctx is non-NULL before any subsequent error path within
inherit_event calls free_event(), satisfying the assumptions of the
cleanup code.

Details:

There's no clear Fixes tag, because this bug is a side-effect of
multiple interacting commits over time (up to 15 years old), not
a single regression.

The code initially incremented refcount then assigned context
immediately after the child_event was created. Later, an early
validity check for child_event was added before the
refcount/assignment. Even later, a WARN_ON_ONCE() cleanup check was
added, assuming event->ctx is valid if the pmu_ctx is valid.
The problem is that the WARN_ON_ONCE() could trigger after the initial
check passed but before child_event->ctx was assigned, violating its
precondition. The solution is to assign child_event->ctx right after
its initial validation. This ensures the context exists for any
subsequent checks or cleanup routines, resolving the WARN_ON_ONCE().

To resolve it, defer the refcount update and child_event->ctx assignment
directly after child_event->pmu_ctx is set but before checking if the
parent event is orphaned. The cleanup routine depends on
event->pmu_ctx being non-NULL before it verifies event->ctx is
non-NULL. This also maintains the author's original intent of passing
in child_ctx to find_get_pmu_context before its refcount/assignment.

[ mingo: Expanded the changelog from another email by Gabriel Shahrouzi. ]

Reported-by: syzbot+ff3aa851d46ab82953a3@syzkaller.appspotmail.com
Signed-off-by: Gabriel Shahrouzi <gshahrouzi@gmail.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Link: https://lore.kernel.org/r/20250405203036.582721-1-gshahrouzi@gmail.com
Closes: https://syzkaller.appspot.com/bug?extid=ff3aa851d46ab82953a3
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 kernel/events/core.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index b710976fb01b1..987807b1040ae 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -13419,6 +13419,9 @@ inherit_event(struct perf_event *parent_event,
 	if (IS_ERR(child_event))
 		return child_event;
 
+	get_ctx(child_ctx);
+	child_event->ctx = child_ctx;
+
 	pmu_ctx = find_get_pmu_context(child_event->pmu, child_ctx, child_event);
 	if (IS_ERR(pmu_ctx)) {
 		free_event(child_event);
@@ -13441,8 +13444,6 @@ inherit_event(struct perf_event *parent_event,
 		return NULL;
 	}
 
-	get_ctx(child_ctx);
-
 	/*
 	 * Make the child state follow the state of the parent event,
 	 * not its attr.disabled bit.  We hold the parent's mutex,
@@ -13463,7 +13464,6 @@ inherit_event(struct perf_event *parent_event,
 		local64_set(&hwc->period_left, sample_period);
 	}
 
-	child_event->ctx = child_ctx;
 	child_event->overflow_handler = parent_event->overflow_handler;
 	child_event->overflow_handler_context
 		= parent_event->overflow_handler_context;
-- 
2.39.5


  parent reply	other threads:[~2025-04-14 13:30 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-14 13:29 [PATCH AUTOSEL 6.6 01/24] KVM: s390: Don't use %pK through tracepoints Sasha Levin
2025-04-14 13:29 ` [PATCH AUTOSEL 6.6 02/24] KVM: s390: Don't use %pK through debug printing Sasha Levin
2025-04-14 13:29 ` [PATCH AUTOSEL 6.6 03/24] udmabuf: fix a buf size overflow issue during udmabuf creation Sasha Levin
2025-04-14 13:29 ` [PATCH AUTOSEL 6.6 04/24] selftests: ublk: fix test_stripe_04 Sasha Levin
2025-04-14 13:29 ` Sasha Levin [this message]
2025-04-14 13:29 ` [PATCH AUTOSEL 6.6 06/24] xen: Change xen-acpi-processor dom0 dependency Sasha Levin
2025-04-14 13:29 ` [PATCH AUTOSEL 6.6 07/24] nvme: requeue namespace scan on missed AENs Sasha Levin
2025-04-14 13:29 ` [PATCH AUTOSEL 6.6 08/24] ACPI: EC: Set ec_no_wakeup for Lenovo Go S Sasha Levin
2025-04-14 13:29 ` [PATCH AUTOSEL 6.6 09/24] ACPI PPTT: Fix coding mistakes in a couple of sizeof() calls Sasha Levin
2025-04-14 13:29 ` [PATCH AUTOSEL 6.6 10/24] nvme: re-read ANA log page after ns scan completes Sasha Levin
2025-04-14 13:29 ` [PATCH AUTOSEL 6.6 11/24] nvme: multipath: fix return value of nvme_available_path Sasha Levin
2025-04-14 13:29 ` [PATCH AUTOSEL 6.6 12/24] objtool: Stop UNRET validation on UD2 Sasha Levin
2025-04-14 13:29 ` [PATCH AUTOSEL 6.6 13/24] gpiolib: of: Move Atmel HSMCI quirk up out of the regulator comment Sasha Levin
2025-04-14 13:29 ` [PATCH AUTOSEL 6.6 14/24] selftests/mincore: Allow read-ahead pages to reach the end of the file Sasha Levin
2025-04-14 13:29 ` [PATCH AUTOSEL 6.6 15/24] x86/bugs: Use SBPB in write_ibpb() if applicable Sasha Levin
2025-04-14 13:29 ` [PATCH AUTOSEL 6.6 16/24] x86/bugs: Don't fill RSB on VMEXIT with eIBRS+retpoline Sasha Levin
2025-04-14 13:29 ` [PATCH AUTOSEL 6.6 17/24] x86/bugs: Don't fill RSB on context switch with eIBRS Sasha Levin
2025-04-14 13:29 ` [PATCH AUTOSEL 6.6 18/24] nvmet-fc: take tgtport reference only once Sasha Levin
2025-04-14 13:29 ` [PATCH AUTOSEL 6.6 19/24] nvmet-fc: put ref when assoc->del_work is already scheduled Sasha Levin
2025-04-14 13:29 ` [PATCH AUTOSEL 6.6 20/24] net_sched: sch_sfq: use a temporary work area for validating configuration Sasha Levin
2025-04-14 13:29 ` [PATCH AUTOSEL 6.6 21/24] cifs: Fix encoding of SMB1 Session Setup Kerberos Request in non-UNICODE mode Sasha Levin
2025-04-14 13:29 ` [PATCH AUTOSEL 6.6 22/24] timekeeping: Add a lockdep override in tick_freeze() Sasha Levin
2025-04-14 13:29 ` [PATCH AUTOSEL 6.6 23/24] cifs: Fix querying of WSL CHR and BLK reparse points over SMB1 Sasha Levin
2025-04-14 13:29 ` [PATCH AUTOSEL 6.6 24/24] ext4: make block validity check resistent to sb bh corruption Sasha Levin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250414132957.680250-5-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=acme@kernel.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=gshahrouzi@gmail.com \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    --cc=ravi.bangoria@amd.com \
    --cc=stable@vger.kernel.org \
    --cc=syzbot+ff3aa851d46ab82953a3@syzkaller.appspotmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox