[PATCH 2/3] perf/core: Try to allocate task_ctx_data quickly

Namhyung Kim posted 3 patches 1 month, 2 weeks ago
[PATCH 2/3] perf/core: Try to allocate task_ctx_data quickly
Posted by Namhyung Kim 1 month, 2 weeks ago
The attach_global_ctx_data() has O(N^2) algorithm to allocate the
context data for each thread.  This caused perfomance problems on large
systems with O(100k) threads.

Because kmalloc(GFP_KERNEL) can go sleep it cannot be called under the
RCU lock.  So let's try with GFP_NOWAIT first so that it can proceed in
normal cases.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 kernel/events/core.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index b8498e9891e21c18..5b05a71edeb47955 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -5489,6 +5489,13 @@ attach_global_ctx_data(struct kmem_cache *ctx_cache)
 					cd = NULL;
 			}
 			if (!cd) {
+				/*
+				 * Try to allocate context quickly before
+				 * traversing the whole thread list again.
+				 */
+				if (!attach_task_ctx_data(p, ctx_cache, true,
+							  GFP_NOWAIT))
+					continue;
 				get_task_struct(p);
 				goto alloc;
 			}
-- 
2.53.0.273.g2a3d683680-goog
[tip: perf/core] perf/core: Try to allocate task_ctx_data quickly
Posted by tip-bot2 for Namhyung Kim 1 month ago
The following commit has been merged into the perf/core branch of tip:

Commit-ID:     bec2ee2390c95ed0c44494340464e69e79802e4a
Gitweb:        https://git.kernel.org/tip/bec2ee2390c95ed0c44494340464e69e79802e4a
Author:        Namhyung Kim <namhyung@kernel.org>
AuthorDate:    Wed, 11 Feb 2026 14:32:20 -08:00
Committer:     Peter Zijlstra <peterz@infradead.org>
CommitterDate: Fri, 27 Feb 2026 16:40:21 +01:00

perf/core: Try to allocate task_ctx_data quickly

The attach_global_ctx_data() has O(N^2) algorithm to allocate the
context data for each thread.  This caused perfomance problems on large
systems with O(100k) threads.

Because kmalloc(GFP_KERNEL) can go sleep it cannot be called under the
RCU lock.  So let's try with GFP_NOWAIT first so that it can proceed in
normal cases.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://patch.msgid.link/20260211223222.3119790-3-namhyung@kernel.org
---
 kernel/events/core.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 90b0c93..d357714 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -5489,6 +5489,12 @@ again:
 					cd = NULL;
 			}
 			if (!cd) {
+				/*
+				 * Try to allocate context quickly before
+				 * traversing the whole thread list again.
+				 */
+				if (!attach_task_ctx_data(p, ctx_cache, true, GFP_NOWAIT))
+					continue;
 				get_task_struct(p);
 				goto alloc;
 			}