Skip to contentSkip to conversation

perf/core: Using kfree_rcu() to simplify the code in put_ctx()

PATCH In review Yang Yingliang 2 participants 3 messages Oct 29, 2022 – Oct 31, 2022 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf/core: Using kfree_rcu() to simplify the code in put_ctx()

After commit 983bd8543b5a ("perf: Rewrite core context handling"), the callback function of call_rcu() just calls a kfree(), so we can use kfree_rcu() instead of call_rcu() + callback function.

---
 kernel/events/core.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
kernel/events/core.c +1 -9
index ebd9ebd53238..9bb274dd2aa6 100644
@@ -1157,14 +1157,6 @@ static void free_task_ctx_data(struct pmu *pmu, void *task_ctx_data)
 		kmem_cache_free(pmu->task_ctx_cache, task_ctx_data);
 }
 
-static void free_ctx(struct rcu_head *head)
-{
-	struct perf_event_context *ctx;
-
-	ctx = container_of(head, struct perf_event_context, rcu_head);
-	kfree(ctx);
-}
-
 static void put_ctx(struct perf_event_context *ctx)
 {
 	if (refcount_dec_and_test(&ctx->refcount)) {
@@ -1172,7 +1164,7 @@ static void put_ctx(struct perf_event_context *ctx)
 			put_ctx(ctx->parent_ctx);
 		if (ctx->task && ctx->task != TASK_TOMBSTONE)
 			put_task_struct(ctx->task);
-		call_rcu(&ctx->rcu_head, free_ctx);
+		kfree_rcu(ctx, rcu_head);
 	}
 }
 
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 29 lines · Yang Yingliang

After commit 983bd8543b5a ("perf: Rewrite core context handling"), the callback function of call_rcu() just calls a kfree(), so we can use kfree_rcu() instead of call_rcu() + callback function.

---
 kernel/events/core.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
kernel/events/core.c +0 -8
index ebd9ebd53238..9bb274dd2aa6 100644
@@ -1157,14 +1157,6 @@ static void free_task_ctx_data(struct pmu *pmu, void *task_ctx_data)
 		kmem_cache_free(pmu->task_ctx_cache, task_ctx_data);
 }
 
-static void free_ctx(struct rcu_head *head)
-{
-	struct perf_event_context *ctx;
-
-	ctx = container_of(head, struct perf_event_context, rcu_head);
-	kfree(ctx);
-}
-
 static void put_ctx(struct perf_event_context *ctx)
 {
 	if (refcount_dec_and_test(&ctx->refcount)) {

kernel/events/core.c static void put_ctx(struct perf_event_context *ctx) · line 1169

Quoted 7 lines
kernel/events/core.c +1 -1
@@ -1172,7 +1164,7 @@ static void put_ctx(struct perf_event_context *ctx)
 			put_ctx(ctx->parent_ctx);
 		if (ctx->task && ctx->task != TASK_TOMBSTONE)
 			put_task_struct(ctx->task);
-		call_rcu(&ctx->rcu_head, free_ctx);
+		kfree_rcu(ctx, rcu_head);
 	}
 }

While valid; I think I prefer the explicit form. This makes it much easier to add some debug code for example.

#2 lore raw

Hi,

Quoted 31 lines · Peter Zijlstra
Quoted 30 lines · Yang Yingliang

After commit 983bd8543b5a ("perf: Rewrite core context handling"), the callback function of call_rcu() just calls a kfree(), so we can use kfree_rcu() instead of call_rcu() + callback function.

---
  kernel/events/core.c | 10 +---------
  1 file changed, 1 insertion(+), 9 deletions(-)
kernel/events/core.c +0 -8
index ebd9ebd53238..9bb274dd2aa6 100644
@@ -1157,14 +1157,6 @@ static void free_task_ctx_data(struct pmu *pmu, void *task_ctx_data)
  		kmem_cache_free(pmu->task_ctx_cache, task_ctx_data);
  }
 
-static void free_ctx(struct rcu_head *head)
-{
-	struct perf_event_context *ctx;
-
-	ctx = container_of(head, struct perf_event_context, rcu_head);
-	kfree(ctx);
-}
-
  static void put_ctx(struct perf_event_context *ctx)
  {
  	if (refcount_dec_and_test(&ctx->refcount)) {
@@ -1172,7 +1164,7 @@ static void put_ctx(struct perf_event_context *ctx)
  			put_ctx(ctx->parent_ctx);
Quoted 8 lines

kernel/events/core.c static void put_ctx(struct perf_event_context *ctx) · line 1169

Quoted 6 lines
kernel/events/core.c +1 -1
  		if (ctx->task && ctx->task != TASK_TOMBSTONE)
  			put_task_struct(ctx->task);
-		call_rcu(&ctx->rcu_head, free_ctx);
+		kfree_rcu(ctx, rcu_head);
  	}
  }

While valid; I think I prefer the explicit form. This makes it much easier to add some debug code for example.

Good point, thanks for replying.

Yang

Quoted 2 lines

.