[01/15] tick/nohz: Remove duplicate between tick_nohz_switch_to_nohz() and tick_setup_sched_timer()

Message ID 20240124170459.24850-2-frederic@kernel.org
State New
Headers
Series timers/nohz cleanups and hotplug reorganization |

Commit Message

Frederic Weisbecker Jan. 24, 2024, 5:04 p.m. UTC
  From: Peng Liu <liupeng17@lenovo.com>

The ts->sched_timer initialization work of tick_nohz_switch_to_nohz()
is almost the same as that of tick_setup_sched_timer(), so adjust the
latter to get it reused by tick_nohz_switch_to_nohz().

This also makes low-res mode sched_timer benefit from the tick skew
boot option.

Signed-off-by: Peng Liu <liupeng17@lenovo.com>
Signed-off-by: Frederic Weisbecker <frederic@kernel.org>
---
 kernel/time/hrtimer.c    |  2 +-
 kernel/time/tick-sched.c | 38 +++++++++++++++++---------------------
 kernel/time/tick-sched.h |  2 +-
 3 files changed, 19 insertions(+), 23 deletions(-)
  

Comments

Thomas Gleixner Jan. 25, 2024, 9:12 a.m. UTC | #1
On Wed, Jan 24 2024 at 18:04, Frederic Weisbecker wrote:
>  
>  /**
>   * tick_setup_sched_timer - setup the tick emulation timer
> + * @mode: tick_nohz_mode to setup for
>   */
> -void tick_setup_sched_timer(void)
> +void tick_setup_sched_timer(int mode)
>  {
>  	struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
> -	ktime_t now = ktime_get();
>  
>  	/* Emulate tick processing via per-CPU hrtimers: */
>  	hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
> -	ts->sched_timer.function = tick_nohz_highres_handler;
> +#ifdef CONFIG_HIGH_RES_TIMERS
> +	if (mode == NOHZ_MODE_HIGHRES)
> +		ts->sched_timer.function = tick_nohz_highres_handler;
> +#endif

Adding

#define tick_nohz_highres_handler NULL

for CONFIG_HIGH_RES_TIMERS=n somewhere allows you to write that as:

	if (IS_ENABLED(CONFIG_HIGH_RES_TIMERS) && mode == NOHZ_MODE_HIGHRES)
		ts->sched_timer.function = tick_nohz_highres_handler;
    
> +	hrtimer_forward_now(&ts->sched_timer, TICK_NSEC);
> +	if (mode == NOHZ_MODE_HIGHRES)

Wants a (IS_ENABLED(HIGRES) && mode == ...) no?

Thanks,

        tglx
  
Frederic Weisbecker Jan. 25, 2024, 11:57 a.m. UTC | #2
On Thu, Jan 25, 2024 at 10:12:20AM +0100, Thomas Gleixner wrote:
> On Wed, Jan 24 2024 at 18:04, Frederic Weisbecker wrote:
> >  
> >  /**
> >   * tick_setup_sched_timer - setup the tick emulation timer
> > + * @mode: tick_nohz_mode to setup for
> >   */
> > -void tick_setup_sched_timer(void)
> > +void tick_setup_sched_timer(int mode)
> >  {
> >  	struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
> > -	ktime_t now = ktime_get();
> >  
> >  	/* Emulate tick processing via per-CPU hrtimers: */
> >  	hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
> > -	ts->sched_timer.function = tick_nohz_highres_handler;
> > +#ifdef CONFIG_HIGH_RES_TIMERS
> > +	if (mode == NOHZ_MODE_HIGHRES)
> > +		ts->sched_timer.function = tick_nohz_highres_handler;
> > +#endif
> 
> Adding
> 
> #define tick_nohz_highres_handler NULL
> 
> for CONFIG_HIGH_RES_TIMERS=n somewhere allows you to write that as:
> 
> 	if (IS_ENABLED(CONFIG_HIGH_RES_TIMERS) && mode == NOHZ_MODE_HIGHRES)
> 		ts->sched_timer.function = tick_nohz_highres_handler;

So the next patch does:

if (mode == NOHZ_MODE_HIGHRES)
    ts->sched_timer.function = tick_nohz_highres_handler;

Because that condition only happens in CONFIG_HIGH_RES_TIMERS anyway
and the function field exists in any case.

I can move that to the current patch though.

>     
> > +	hrtimer_forward_now(&ts->sched_timer, TICK_NSEC);
> > +	if (mode == NOHZ_MODE_HIGHRES)
> 
> Wants a (IS_ENABLED(HIGRES) && mode == ...) no?

Hmm, indeed hrtimer_start_range_ns() called by hrtimer_start_expires()
is only defined if CONFIG_HIGH_RES_TIMERS=y. I'll resend.

Thanks.

> 
> Thanks,
> 
>         tglx
  
Thomas Gleixner Jan. 25, 2024, 1:34 p.m. UTC | #3
On Thu, Jan 25 2024 at 12:57, Frederic Weisbecker wrote:
> On Thu, Jan 25, 2024 at 10:12:20AM +0100, Thomas Gleixner wrote:
>> Adding
>> 
>> #define tick_nohz_highres_handler NULL
>> 
>> for CONFIG_HIGH_RES_TIMERS=n somewhere allows you to write that as:
>> 
>> 	if (IS_ENABLED(CONFIG_HIGH_RES_TIMERS) && mode == NOHZ_MODE_HIGHRES)
>> 		ts->sched_timer.function = tick_nohz_highres_handler;
>
> So the next patch does:
>
> if (mode == NOHZ_MODE_HIGHRES)
>     ts->sched_timer.function = tick_nohz_highres_handler;
>
> Because that condition only happens in CONFIG_HIGH_RES_TIMERS anyway
> and the function field exists in any case.

Right, but the function does not exist for HRES=n, so you need the
#define ... NULL. The IS_ENABLED() allows the compiler to eliminate the
code for HRES=n completely.

Thanks,

        tglx
  
Frederic Weisbecker Jan. 25, 2024, 2:35 p.m. UTC | #4
On Thu, Jan 25, 2024 at 02:34:06PM +0100, Thomas Gleixner wrote:
> On Thu, Jan 25 2024 at 12:57, Frederic Weisbecker wrote:
> > On Thu, Jan 25, 2024 at 10:12:20AM +0100, Thomas Gleixner wrote:
> >> Adding
> >> 
> >> #define tick_nohz_highres_handler NULL
> >> 
> >> for CONFIG_HIGH_RES_TIMERS=n somewhere allows you to write that as:
> >> 
> >> 	if (IS_ENABLED(CONFIG_HIGH_RES_TIMERS) && mode == NOHZ_MODE_HIGHRES)
> >> 		ts->sched_timer.function = tick_nohz_highres_handler;
> >
> > So the next patch does:
> >
> > if (mode == NOHZ_MODE_HIGHRES)
> >     ts->sched_timer.function = tick_nohz_highres_handler;
> >
> > Because that condition only happens in CONFIG_HIGH_RES_TIMERS anyway
> > and the function field exists in any case.
> 
> Right, but the function does not exist for HRES=n, so you need the
> #define ... NULL. The IS_ENABLED() allows the compiler to eliminate the
> code for HRES=n completely.

Ah but the next patch moves tick_nohz_highres_handler out of CONFIG_HRES
(to soon become tick_nohz_handler()). But anyway I can add the IS_ENABLED()
to remove dead code.

Thanks.

> 
> Thanks,
> 
>         tglx
  

Patch

diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
index 760793998cdd..355b5a957f7f 100644
--- a/kernel/time/hrtimer.c
+++ b/kernel/time/hrtimer.c
@@ -746,7 +746,7 @@  static void hrtimer_switch_to_hres(void)
 	base->hres_active = 1;
 	hrtimer_resolution = HIGH_RES_NSEC;
 
-	tick_setup_sched_timer();
+	tick_setup_sched_timer(NOHZ_MODE_HIGHRES);
 	/* "Retrigger" the interrupt to get things going */
 	retrigger_next_event(NULL);
 }
diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
index d2501673028d..e90dbb7ae70a 100644
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -1433,9 +1433,6 @@  static inline void tick_nohz_activate(struct tick_sched *ts, int mode)
  */
 static void tick_nohz_switch_to_nohz(void)
 {
-	struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
-	ktime_t next;
-
 	if (!tick_nohz_enabled)
 		return;
 
@@ -1444,16 +1441,9 @@  static void tick_nohz_switch_to_nohz(void)
 
 	/*
 	 * Recycle the hrtimer in 'ts', so we can share the
-	 * hrtimer_forward_now() function with the highres code.
+	 * highres code.
 	 */
-	hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
-	/* Get the next period */
-	next = tick_init_jiffy_update();
-
-	hrtimer_set_expires(&ts->sched_timer, next);
-	hrtimer_forward_now(&ts->sched_timer, TICK_NSEC);
-	tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
-	tick_nohz_activate(ts, NOHZ_MODE_LOWRES);
+	tick_setup_sched_timer(NOHZ_MODE_LOWRES);
 }
 
 static inline void tick_nohz_irq_enter(void)
@@ -1532,7 +1522,9 @@  static enum hrtimer_restart tick_nohz_highres_handler(struct hrtimer *timer)
 
 	return HRTIMER_RESTART;
 }
+#endif /* HIGH_RES_TIMERS */
 
+#if defined CONFIG_NO_HZ_COMMON || defined CONFIG_HIGH_RES_TIMERS
 static int sched_skew_tick;
 
 static int __init skew_tick(char *str)
@@ -1545,15 +1537,18 @@  early_param("skew_tick", skew_tick);
 
 /**
  * tick_setup_sched_timer - setup the tick emulation timer
+ * @mode: tick_nohz_mode to setup for
  */
-void tick_setup_sched_timer(void)
+void tick_setup_sched_timer(int mode)
 {
 	struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
-	ktime_t now = ktime_get();
 
 	/* Emulate tick processing via per-CPU hrtimers: */
 	hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
-	ts->sched_timer.function = tick_nohz_highres_handler;
+#ifdef CONFIG_HIGH_RES_TIMERS
+	if (mode == NOHZ_MODE_HIGHRES)
+		ts->sched_timer.function = tick_nohz_highres_handler;
+#endif
 
 	/* Get the next period (per-CPU) */
 	hrtimer_set_expires(&ts->sched_timer, tick_init_jiffy_update());
@@ -1566,13 +1561,14 @@  void tick_setup_sched_timer(void)
 		hrtimer_add_expires_ns(&ts->sched_timer, offset);
 	}
 
-	hrtimer_forward(&ts->sched_timer, now, TICK_NSEC);
-	hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED_HARD);
-	tick_nohz_activate(ts, NOHZ_MODE_HIGHRES);
+	hrtimer_forward_now(&ts->sched_timer, TICK_NSEC);
+	if (mode == NOHZ_MODE_HIGHRES)
+		hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED_HARD);
+	else
+		tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
+	tick_nohz_activate(ts, mode);
 }
-#endif /* HIGH_RES_TIMERS */
 
-#if defined CONFIG_NO_HZ_COMMON || defined CONFIG_HIGH_RES_TIMERS
 void tick_cancel_sched_timer(int cpu)
 {
 	struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
@@ -1589,7 +1585,7 @@  void tick_cancel_sched_timer(int cpu)
 	ts->idle_sleeptime = idle_sleeptime;
 	ts->iowait_sleeptime = iowait_sleeptime;
 }
-#endif
+#endif /* CONFIG_NO_HZ_COMMON || CONFIG_HIGH_RES_TIMERS */
 
 /*
  * Async notification about clocksource changes
diff --git a/kernel/time/tick-sched.h b/kernel/time/tick-sched.h
index 5ed5a9d41d5a..35808bbb8a47 100644
--- a/kernel/time/tick-sched.h
+++ b/kernel/time/tick-sched.h
@@ -102,7 +102,7 @@  struct tick_sched {
 
 extern struct tick_sched *tick_get_tick_sched(int cpu);
 
-extern void tick_setup_sched_timer(void);
+extern void tick_setup_sched_timer(int mode);
 #if defined CONFIG_NO_HZ_COMMON || defined CONFIG_HIGH_RES_TIMERS
 extern void tick_cancel_sched_timer(int cpu);
 #else