[PATCH 3/4] smp: get this_cpu once in smp_call_function

Shrikanth Hegde posted 4 patches 1 week, 3 days ago
[PATCH 3/4] smp: get this_cpu once in smp_call_function
Posted by Shrikanth Hegde 1 week, 3 days ago
Calling smp_processor_id() on:
- In CONFIG_DEBUG_PREEMPT=y, if preemption/irq is disabled, then it does
not print any warning.
- In CONFIG_DEBUG_PREEMPT=n, it doesn't do anything apart from getting
__smp_processor_id

So with both CONFIG_DEBUG_PREEMPT=y/n, in preemption disabled section
it is better to cache the value. It could save a few cycles. Though
tiny, repeated in loop could add up to a small value.

smp_call_function_single/smp_call_function_many_cond is called with
preemption disabled.
So cache the values once.

Signed-off-by: Shrikanth Hegde <sshegde@linux.ibm.com>
---
 kernel/smp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/smp.c b/kernel/smp.c
index f349960f79ca..4c57104d1cd3 100644
--- a/kernel/smp.c
+++ b/kernel/smp.c
@@ -677,7 +677,7 @@ int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
 	csd->func = func;
 	csd->info = info;
 #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
-	csd->node.src = smp_processor_id();
+	csd->node.src = this_cpu;
 	csd->node.dst = cpu;
 #endif
 
@@ -832,7 +832,7 @@ static void smp_call_function_many_cond(const struct cpumask *mask,
 			csd->func = func;
 			csd->info = info;
 #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
-			csd->node.src = smp_processor_id();
+			csd->node.src = this_cpu;
 			csd->node.dst = cpu;
 #endif
 			trace_csd_queue_cpu(cpu, _RET_IP_, func, csd);
-- 
2.47.3
Re: [PATCH 3/4] smp: get this_cpu once in smp_call_function
Posted by Mukesh Kumar Chaurasiya 1 week, 3 days ago
On Tue, Mar 24, 2026 at 01:06:29AM +0530, Shrikanth Hegde wrote:
> Calling smp_processor_id() on:
> - In CONFIG_DEBUG_PREEMPT=y, if preemption/irq is disabled, then it does
> not print any warning.
> - In CONFIG_DEBUG_PREEMPT=n, it doesn't do anything apart from getting
> __smp_processor_id
> 
> So with both CONFIG_DEBUG_PREEMPT=y/n, in preemption disabled section
> it is better to cache the value. It could save a few cycles. Though
> tiny, repeated in loop could add up to a small value.
> 
> smp_call_function_single/smp_call_function_many_cond is called with
> preemption disabled.
> So cache the values once.
> 
> Signed-off-by: Shrikanth Hegde <sshegde@linux.ibm.com>
> ---
>  kernel/smp.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/smp.c b/kernel/smp.c
> index f349960f79ca..4c57104d1cd3 100644
> --- a/kernel/smp.c
> +++ b/kernel/smp.c
> @@ -677,7 +677,7 @@ int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
>  	csd->func = func;
>  	csd->info = info;
>  #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
> -	csd->node.src = smp_processor_id();
> +	csd->node.src = this_cpu;
>  	csd->node.dst = cpu;
>  #endif
>  
> @@ -832,7 +832,7 @@ static void smp_call_function_many_cond(const struct cpumask *mask,
>  			csd->func = func;
>  			csd->info = info;
>  #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
> -			csd->node.src = smp_processor_id();
> +			csd->node.src = this_cpu;
>  			csd->node.dst = cpu;
>  #endif
>  			trace_csd_queue_cpu(cpu, _RET_IP_, func, csd);
> -- 
> 2.47.3
> 
LGTM

Reviewed-by: Mukesh Kumar Chaurasiya (IBM) <mkchauras@gmail.com>
[tip: smp/core] smp: Get this_cpu once in smp_call_function
Posted by tip-bot2 for Shrikanth Hegde 1 week, 1 day ago
The following commit has been merged into the smp/core branch of tip:

Commit-ID:     ec39780d6a9e05e7e427008603b40efef515b775
Gitweb:        https://git.kernel.org/tip/ec39780d6a9e05e7e427008603b40efef515b775
Author:        Shrikanth Hegde <sshegde@linux.ibm.com>
AuthorDate:    Tue, 24 Mar 2026 01:06:29 +05:30
Committer:     Thomas Gleixner <tglx@kernel.org>
CommitterDate: Wed, 25 Mar 2026 20:11:30 +01:00

smp: Get this_cpu once in smp_call_function

smp_call_function_single() and smp_call_function_many_cond() disable
preemption and cache the CPU number via get_cpu().

Use this cached value throughout the function instead of invoking
smp_processor_id() again.

[ tglx: Make the copy&pasta'ed change log match the patch ]

Signed-off-by: Shrikanth Hegde <sshegde@linux.ibm.com>
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
Reviewed-by: Mukesh Kumar Chaurasiya (IBM) <mkchauras@gmail.com>
Link: https://patch.msgid.link/20260323193630.640311-4-sshegde@linux.ibm.com
---
 kernel/smp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/smp.c b/kernel/smp.c
index b179424..bdbf145 100644
--- a/kernel/smp.c
+++ b/kernel/smp.c
@@ -678,7 +678,7 @@ int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
 	csd->func = func;
 	csd->info = info;
 #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
-	csd->node.src = smp_processor_id();
+	csd->node.src = this_cpu;
 	csd->node.dst = cpu;
 #endif
 
@@ -833,7 +833,7 @@ static void smp_call_function_many_cond(const struct cpumask *mask,
 			csd->func = func;
 			csd->info = info;
 #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
-			csd->node.src = smp_processor_id();
+			csd->node.src = this_cpu;
 			csd->node.dst = cpu;
 #endif
 			trace_csd_queue_cpu(cpu, _RET_IP_, func, csd);