[PATCH] timers: Replace get_timer_this_cpu_base() with get_timer_cpu_base()

Jinjie Ruan posted 1 patch 2 weeks ago
kernel/time/timer.c | 16 +---------------
1 file changed, 1 insertion(+), 15 deletions(-)
[PATCH] timers: Replace get_timer_this_cpu_base() with get_timer_cpu_base()
Posted by Jinjie Ruan 2 weeks ago
get_timer_this_cpu_base() is only used in __mod_timer() where the timer
base lock is already held via raw_spin_lock_irqsave().  In this context,
preemption is disabled and the CPU number cannot change, making
raw_smp_processor_id() safe to use.

Since get_timer_cpu_base() provides identical functionality using
raw_smp_processor_id(), replace get_timer_this_cpu_base() with
get_timer_cpu_base() to eliminate code duplication.

No functional change intended.

Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
 kernel/time/timer.c | 16 +---------------
 1 file changed, 1 insertion(+), 15 deletions(-)

diff --git a/kernel/time/timer.c b/kernel/time/timer.c
index 1f2364126894..5a827e09cb8f 100644
--- a/kernel/time/timer.c
+++ b/kernel/time/timer.c
@@ -925,20 +925,6 @@ static inline struct timer_base *get_timer_cpu_base(u32 tflags, u32 cpu)
 	return per_cpu_ptr(&timer_bases[index], cpu);
 }
 
-static inline struct timer_base *get_timer_this_cpu_base(u32 tflags)
-{
-	int index = tflags & TIMER_PINNED ? BASE_LOCAL : BASE_GLOBAL;
-
-	/*
-	 * If the timer is deferrable and NO_HZ_COMMON is set then we need
-	 * to use the deferrable base.
-	 */
-	if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && (tflags & TIMER_DEFERRABLE))
-		index = BASE_DEF;
-
-	return this_cpu_ptr(&timer_bases[index]);
-}
-
 static inline struct timer_base *get_timer_base(u32 tflags)
 {
 	return get_timer_cpu_base(tflags, tflags & TIMER_CPUMASK);
@@ -1098,7 +1084,7 @@ __mod_timer(struct timer_list *timer, unsigned long expires, unsigned int option
 	if (!ret && (options & MOD_TIMER_PENDING_ONLY))
 		goto out_unlock;
 
-	new_base = get_timer_this_cpu_base(timer->flags);
+	new_base = get_timer_cpu_base(timer->flags, raw_smp_processor_id());
 
 	if (base != new_base) {
 		/*
-- 
2.34.1
Re: [PATCH] timers: Replace get_timer_this_cpu_base() with get_timer_cpu_base()
Posted by Frederic Weisbecker 1 week, 3 days ago
Le Fri, Jan 23, 2026 at 04:47:15PM +0800, Jinjie Ruan a écrit :
> get_timer_this_cpu_base() is only used in __mod_timer() where the timer
> base lock is already held via raw_spin_lock_irqsave().  In this context,
> preemption is disabled and the CPU number cannot change, making
> raw_smp_processor_id() safe to use.
> 
> Since get_timer_cpu_base() provides identical functionality using
> raw_smp_processor_id(), replace get_timer_this_cpu_base() with
> get_timer_cpu_base() to eliminate code duplication.
> 
> No functional change intended.
> 
> Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
> ---
>  kernel/time/timer.c | 16 +---------------
>  1 file changed, 1 insertion(+), 15 deletions(-)
> 
> diff --git a/kernel/time/timer.c b/kernel/time/timer.c
> index 1f2364126894..5a827e09cb8f 100644
> --- a/kernel/time/timer.c
> +++ b/kernel/time/timer.c
> @@ -925,20 +925,6 @@ static inline struct timer_base *get_timer_cpu_base(u32 tflags, u32 cpu)
>  	return per_cpu_ptr(&timer_bases[index], cpu);
>  }
>  
> -static inline struct timer_base *get_timer_this_cpu_base(u32 tflags)
> -{
> -	int index = tflags & TIMER_PINNED ? BASE_LOCAL : BASE_GLOBAL;
> -
> -	/*
> -	 * If the timer is deferrable and NO_HZ_COMMON is set then we need
> -	 * to use the deferrable base.
> -	 */
> -	if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && (tflags & TIMER_DEFERRABLE))
> -		index = BASE_DEF;
> -
> -	return this_cpu_ptr(&timer_bases[index]);
> -}
> -
>  static inline struct timer_base *get_timer_base(u32 tflags)
>  {
>  	return get_timer_cpu_base(tflags, tflags & TIMER_CPUMASK);
> @@ -1098,7 +1084,7 @@ __mod_timer(struct timer_list *timer, unsigned long expires, unsigned int option
>  	if (!ret && (options & MOD_TIMER_PENDING_ONLY))
>  		goto out_unlock;
>  
> -	new_base = get_timer_this_cpu_base(timer->flags);
> +	new_base = get_timer_cpu_base(timer->flags, raw_smp_processor_id());

Ok but this should be smp_processor_id() since we expect IRQs to be disabled.

Thanks.

>  
>  	if (base != new_base) {
>  		/*
> -- 
> 2.34.1
> 

-- 
Frederic Weisbecker
SUSE Labs