From nobody Sun Jun 14 03:57:30 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 4E9F7283FCF for ; Mon, 4 May 2026 07:16:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777878960; cv=none; b=OGPdVKt6PJODchynNvWuXu4FPH8tvWqASx/D0oGy6OoTLs4os/llDp9ryYa/V0g6aMTArgpgk2/Z9VDmWn2/h8vonQWRTtxq9gb80x04eIKlIMlYGH3l9nGsxb89hMtSPnJXiZuV0ppftnP3oDfa0NXa8xRv4Azlz4fQInyg0aE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777878960; c=relaxed/simple; bh=LDZHscmke4t4lS74Tof9zHZYvVaxmfWG1c201Uhe340=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=DpArO+vNNFAUZta6V5C6YTYH/LevAa10b7micPuECSrp+ZT5IqYIZ8RoFeT3WVma1GYiZWZDKgQa947Pgq/BWxVxSi0C3eP27vhvDDzAcWb91LRHjE38jpqEX621rLMdDZzzP+uZWXPt9BtmCcZuQiFf3peDiqfpGeOMYYVC15E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FLmi+fIk; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FLmi+fIk" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C7417C2BCB8; Mon, 4 May 2026 07:15:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1777878960; bh=LDZHscmke4t4lS74Tof9zHZYvVaxmfWG1c201Uhe340=; h=From:To:Cc:Subject:Date:From; b=FLmi+fIkukQtS9FAzNvl7p/z+Td44k312Gpr5aLEEjTmxoapUj7/778SXeUsptJVr XTSMBB4uX/sxweRfcgb13qBZyang6L2wrynQWPtrTFDv4p8XGMRzul9a3a8nQ1hdE9 vcj303/bE1axyI9yCxp5fTGGxDaDTVeozPwfnu0lz09Y/wfTEblAoZV6scxilTxqmF Q8PWeyxVOi3hJ7KY+j7V4IM9DeZdEZShwXcjsnRTyyAhjNucvaT8UIwKp2ce28lq2M Mc10R8b7pM7u4qyy57d8nejWLubXd5BdSUVzqeuTYOenlG8vGZJCHmw+69iDUv+mEV 2p6+JPzz4D8rg== From: Namhyung Kim To: Peter Zijlstra , Ingo Molnar Cc: Mark Rutland , Alexander Shishkin , Arnaldo Carvalho de Melo , LKML Subject: [PATCH] perf/core: Fix refcount leak in attach_perf_ctx_data() Date: Mon, 4 May 2026 00:15:51 -0700 Message-ID: <20260504071551.99479-1-namhyung@kernel.org> X-Mailer: git-send-email 2.53.0 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" The attach_perf_ctx_data() can race on global and !global cases. The global case is protected by global_ctx_data_rwsem and shares a single reference count using perf_ctx_data.global field. But when it races with !global case, it may miss to set the global field and result in a reference count leak. CPU1 CPU2 ---------------------------------------------------------------- attach_task_ctx_data(global=3D1) attach_task_ctx_data(global=3D0) cd1 =3D alloc_perf_ctx_data() cd2 =3D alloc_perf_ctx_data() try_cmpxchg() // ok // task->perf_ctx_data =3D cd2 try_cmpxchg() // fail; old =3D cd2; global =3D 0 refcount_inc_not_zero() // cd2->refcount++; free_perf_ctx_data() // cd1 Then later detach_global_ctx_data() will see the data but it's not marked as global, so it won't call detach_task_ctx_data(). Assisted-by: Sashiko.dev:Gemini-3.1-pro Signed-off-by: Namhyung Kim --- kernel/events/core.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/events/core.c b/kernel/events/core.c index 6d1f8bad7e1c5210..939436a168ba5ec2 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -5445,6 +5445,8 @@ attach_task_ctx_data(struct task_struct *task, struct= kmem_cache *ctx_cache, } =20 if (refcount_inc_not_zero(&old->refcount)) { + if (global) + old->global =3D true; free_perf_ctx_data(cd); /* unused */ return 0; } --=20 2.53.0