From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 9E5E01E9B37 for ; Mon, 15 Jun 2026 05:11:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781500304; cv=none; b=BLqTIjXIZOxElmAYnXiD99qwl/ZSogexhudaTOWZ5Qo/TUiMa1jtW6UBE/I+/FbAWl6u3XkSj/DqmR2319WQvKN10KhRPkbU+yKBbiEHTvQPtlb2I0phVORZSlobchfFWBuvPC1z8PcNAPcYyV+bWgL8EyITJjnD1/4pgbmF2xE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781500304; c=relaxed/simple; bh=QQ+5/4bykmrDrQTP4Pif8dobi+3LJr8oSG+mx0gLpLw=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=GfoLHJXhj8Vecv4MXbGVIjJimCNi2F8y9k4VLtSdLK8/CqUDHkHDQClZgu9x3cPUey4GPLN92bOFifAu3qUE/6eNBmwviG0D7y3w0h5otUZpVgXLbowXgxkNhm7CHwDwjkf6Ln9gmY7RVsAgLUlOS/mRzps9ySgUAQNQWqRy8pQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Koca08Ld; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Koca08Ld" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EEA851F000E9; Mon, 15 Jun 2026 05:11:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1781500303; bh=06vcmXAzlCFJnBwKvv3xrAOZHe7zJk2NhU/qFsEj36s=; h=From:To:Cc:Subject:Date; b=Koca08Ldtp9v0azYf8CsKqyuaxrv2A90AsYKzE1WmVoTDQPcQGEXO3PGc3YgLitCP I5wLJbBc4/6Cb8AWLxJOTdcWhlSjYdhVWyw95XJSLzpkX5o3VndhGJQAv/unHl9nAo x1zPHlQf9vadm8+NiaNOOkEKASkd70elxdERzVLwm/qF4dQLESRJDAGjhYLA8CdItQ fKqlT24n8Jike0KTe4xYTHTK2nytEAFMELILv/QArSdfqI9inXnzrVV0YgPFQV4YPf GvlE4laCXgsQbFUH8LzPruaTdDzylNnNDY4EX3XKsua6dI1ALPj4e48P0TP0OhA7e3 9mlhOqDgmqx5w== From: Namhyung Kim To: Peter Zijlstra , Ingo Molnar Cc: Mark Rutland , Alexander Shishkin , Arnaldo Carvalho de Melo , LKML Subject: [PATCH RESEND] perf/core: Fix a refcount leak in attach_perf_ctx_data() Date: Sun, 14 Jun 2026 22:11:37 -0700 Message-ID: <20260615051137.14977-1-namhyung@kernel.org> X-Mailer: git-send-email 2.54.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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=1) attach_task_ctx_data(global=0) cd1 = alloc_perf_ctx_data() cd2 = alloc_perf_ctx_data() try_cmpxchg() // ok // task->perf_ctx_data = cd2 try_cmpxchg() // fail; old = cd2; global = 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(). Fixes: 506e64e710ff ("perf: attach/detach PMU specific 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 7935d5663944ee1c..dc598e3822d062d2 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, } if (refcount_inc_not_zero(&old->refcount)) { + if (global) + old->global = true; free_perf_ctx_data(cd); /* unused */ return 0; } -- 2.54.0