[PATCH 2/4] perf/core: convert call_rcu(free_ctx) to kfree_rcu()

Ran Xiaokai posted 4 patches 2 weeks ago
[PATCH 2/4] perf/core: convert call_rcu(free_ctx) to kfree_rcu()
Posted by Ran Xiaokai 2 weeks ago
From: Ran Xiaokai <ran.xiaokai@zte.com.cn>

The rcu callback free_ctx() simply calls kfree().
It's better to directly call kfree_rcu().

Signed-off-by: Ran Xiaokai <ran.xiaokai@zte.com.cn>
---
 kernel/events/core.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 065f9188b44a..7f4cc9c41bbe 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -1210,14 +1210,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)) {
@@ -1225,7 +1217,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);
 	}
 }
 
-- 
2.17.1
Re: [PATCH 2/4] perf/core: convert call_rcu(free_ctx) to kfree_rcu()
Posted by Peter Zijlstra 1 week, 6 days ago
On Wed, Nov 20, 2024 at 06:47:14AM +0000, Ran Xiaokai wrote:
> From: Ran Xiaokai <ran.xiaokai@zte.com.cn>
> 
> The rcu callback free_ctx() simply calls kfree().
> It's better to directly call kfree_rcu().

Why is it better? 

> Signed-off-by: Ran Xiaokai <ran.xiaokai@zte.com.cn>
> ---
>  kernel/events/core.c | 10 +---------
>  1 file changed, 1 insertion(+), 9 deletions(-)
> 
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 065f9188b44a..7f4cc9c41bbe 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -1210,14 +1210,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)) {
> @@ -1225,7 +1217,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);
>  	}
>  }
>  
> -- 
> 2.17.1
> 
>
Re: [PATCH 2/4] perf/core: convert call_rcu(free_ctx) to kfree_rcu()
Posted by Peter Zijlstra 1 week, 6 days ago
On Wed, Nov 20, 2024 at 10:12:15AM +0100, Peter Zijlstra wrote:
> On Wed, Nov 20, 2024 at 06:47:14AM +0000, Ran Xiaokai wrote:
> > From: Ran Xiaokai <ran.xiaokai@zte.com.cn>
> > 
> > The rcu callback free_ctx() simply calls kfree().
> > It's better to directly call kfree_rcu().
> 
> Why is it better? 

*sigh*, also please don't cross post with a moderated list :-(
Re: [PATCH 2/4] perf/core: convert call_rcu(free_ctx) to kfree_rcu()
Posted by Ran Xiaokai 1 week, 6 days ago
>On Wed, Nov 20, 2024 at 06:47:14AM +0000, Ran Xiaokai wrote:
>> From: Ran Xiaokai <ran.xiaokai@zte.com.cn>
>> 
>> The rcu callback free_ctx() simply calls kfree().
>> It's better to directly call kfree_rcu().
>
>Why is it better? 

Too many of these simple rcu callbacks increase in the kernel size,
and there have been several rounds of cleanups to replace these simple
call_rcu(). So this one is a straggler ?
Besides, no real-world problem was observed.

>> Signed-off-by: Ran Xiaokai <ran.xiaokai@zte.com.cn>
>> ---
>>  kernel/events/core.c | 10 +---------
>>  1 file changed, 1 insertion(+), 9 deletions(-)
>> 
>> diff --git a/kernel/events/core.c b/kernel/events/core.c
>> index 065f9188b44a..7f4cc9c41bbe 100644
>> --- a/kernel/events/core.c
>> +++ b/kernel/events/core.c
>> @@ -1210,14 +1210,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)) {
>> @@ -1225,7 +1217,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);
>>  	}
>>  }
>>  
>> -- 
>> 2.17.1
>> 
>>