Skip to contentSkip to conversation

hrtimer: avoid retrigger_next_event IPI

PATCH Superseded Marcelo Tosatti 5 participants 19 messages Apr 7, 2021 – Apr 19, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hrtimer: avoid retrigger_next_event IPI

Setting the realtime clock triggers an IPI to all CPUs to reprogram hrtimers.

However, only base, boottime and tai clocks have their offsets updated (and therefore potentially require a reprogram).

If the CPU is a nohz_full one, check if it only has monotonic active timers, and in that case update the realtime base offsets, skipping the IPI.

This reduces interruptions to nohz_full CPUs.

kernel/time/hrtimer.c +56 -2
index 743c852e10f2..b42b1a434b22 100644
@@ -853,6 +853,28 @@ static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
 	tick_program_event(expires, 1);
 }
 
+#define CLOCK_SET_BASES ((1U << HRTIMER_BASE_REALTIME)|		\
+			 (1U << HRTIMER_BASE_REALTIME_SOFT)|	\
+			 (1U << HRTIMER_BASE_BOOTTIME)|		\
+			 (1U << HRTIMER_BASE_BOOTTIME_SOFT)|	\
+			 (1U << HRTIMER_BASE_TAI)|		\
+			 (1U << HRTIMER_BASE_TAI_SOFT))
+
+static bool need_reprogram_timer(struct hrtimer_cpu_base *cpu_base)
+{
+	unsigned int active = 0;
+
+	if (!cpu_base->softirq_activated)
+		active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
+
+	active = active | (cpu_base->active_bases & HRTIMER_ACTIVE_HARD);
+
+	if ((active & CLOCK_SET_BASES) == 0)
+		return false;
+
+	return true;
+}
+
 /*
  * Clock realtime was set
  *
@@ -867,9 +889,41 @@ static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
 void clock_was_set(void)
 {
 #ifdef CONFIG_HIGH_RES_TIMERS
-	/* Retrigger the CPU local events everywhere */
-	on_each_cpu(retrigger_next_event, NULL, 1);
+	cpumask_var_t mask;
+	int cpu;
+
+	if (!tick_nohz_full_enabled()) {
+		/* Retrigger the CPU local events everywhere */
+		on_each_cpu(retrigger_next_event, NULL, 1);
+		goto set_timerfd;
+	}
+
+	if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
+		on_each_cpu(retrigger_next_event, NULL, 1);
+		goto set_timerfd;
+	}
+
+	/* Avoid interrupting nohz_full CPUs if possible */
+	preempt_disable();
+	for_each_online_cpu(cpu) {
+		if (tick_nohz_full_cpu(cpu)) {
+			unsigned long flags;
+			struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
+
+			raw_spin_lock_irqsave(&cpu_base->lock, flags);
+			if (need_reprogram_timer(cpu_base))
+				cpumask_set_cpu(cpu, mask);
+			else
+				hrtimer_update_base(cpu_base);
+			raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
+		}
+	}
+
+	smp_call_function_many(mask, retrigger_next_event, NULL, 1);
+	preempt_enable();
+	free_cpumask_var(mask);
 #endif
+set_timerfd:
 	timerfd_clock_was_set();
 }
 
#1 lore raw
Thank you for the patch! Perhaps something to improve:

Hi Marcelo,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on tip/timers/core]
[also build test WARNING on linux/master linus/master v5.12-rc6 next-20210407] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Marcelo-Tosatti/hrtimer-avoid-retrigger_next_event-IPI/20210407-233005
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git d4c7c28806616809e3baa0b7cd8c665516b2726d
config: arm64-randconfig-r032-20210407 (attached as .config)
compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project c060945b23a1c54d4b2a053ff4b093a2277b303d)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm64 cross compiling tool for clang build
# apt-get install binutils-aarch64-linux-gnu
# https://github.com/0day-ci/linux/commit/defd4db9d63d1f16e3e21862bd9c9105a9f6a7e9
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Marcelo-Tosatti/hrtimer-avoid-retrigger_next_event-IPI/20210407-233005 git checkout defd4db9d63d1f16e3e21862bd9c9105a9f6a7e9
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=arm64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

kernel/time/hrtimer.c:120:21: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
[CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
^~~~~~~~~~~~~~~~~~~~~
kernel/time/hrtimer.c:118:27: note: previous initialization is here

           [0 ... MAX_CLOCKS - 1]  = HRTIMER_MAX_CLOCK_BASES,
                                     ^~~~~~~~~~~~~~~~~~~~~~~
   kernel/time/hrtimer.c:121:22: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
           [CLOCK_MONOTONIC]       = HRTIMER_BASE_MONOTONIC,
                                     ^~~~~~~~~~~~~~~~~~~~~~
   kernel/time/hrtimer.c:118:27: note: previous initialization is here
           [0 ... MAX_CLOCKS - 1]  = HRTIMER_MAX_CLOCK_BASES,
                                     ^~~~~~~~~~~~~~~~~~~~~~~
   kernel/time/hrtimer.c:122:21: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
           [CLOCK_BOOTTIME]        = HRTIMER_BASE_BOOTTIME,
                                     ^~~~~~~~~~~~~~~~~~~~~
   kernel/time/hrtimer.c:118:27: note: previous initialization is here
           [0 ... MAX_CLOCKS - 1]  = HRTIMER_MAX_CLOCK_BASES,
                                     ^~~~~~~~~~~~~~~~~~~~~~~
   kernel/time/hrtimer.c:123:17: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
           [CLOCK_TAI]             = HRTIMER_BASE_TAI,
                                     ^~~~~~~~~~~~~~~~
   kernel/time/hrtimer.c:118:27: note: previous initialization is here
           [0 ... MAX_CLOCKS - 1]  = HRTIMER_MAX_CLOCK_BASES,
                                     ^~~~~~~~~~~~~~~~~~~~~~~
>> kernel/time/hrtimer.c:944:1: warning: unused label 'set_timerfd' [-Wunused-label]
   set_timerfd:
   ^~~~~~~~~~~~
   kernel/time/hrtimer.c:147:20: warning: unused function 'is_migration_base' [-Wunused-function]
   static inline bool is_migration_base(struct hrtimer_clock_base *base)
                      ^
   kernel/time/hrtimer.c:650:19: warning: unused function 'hrtimer_hres_active' [-Wunused-function]
   static inline int hrtimer_hres_active(void)
                     ^
   kernel/time/hrtimer.c:881:13: warning: unused function 'need_reprogram_timer' [-Wunused-function]
   static bool need_reprogram_timer(struct hrtimer_cpu_base *cpu_base)
               ^
   kernel/time/hrtimer.c:1793:20: warning: unused function '__hrtimer_peek_ahead_timers' [-Wunused-function]
   static inline void __hrtimer_peek_ahead_timers(void) { }
                      ^

9 warnings generated.

vim +/set_timerfd +944 kernel/time/hrtimer.c

895
896 /*
897 * Clock realtime was set
898 *
899 * Change the offset of the realtime clock vs. the monotonic
900 * clock.
901 *
902 * We might have to reprogram the high resolution timer interrupt. On
903 * SMP we call the architecture specific code to retrigger _all_ high
904 * resolution timer interrupts. On UP we just disable interrupts and
905 * call the high resolution interrupt code.
906 */
907 void clock_was_set(void)
908 {
909 #ifdef CONFIG_HIGH_RES_TIMERS
910 cpumask_var_t mask;
911 int cpu;
912
913 if (!tick_nohz_full_enabled()) {
914 /* Retrigger the CPU local events everywhere */
915 on_each_cpu(retrigger_next_event, NULL, 1);
916 goto set_timerfd;
917 }
918
919 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
920 on_each_cpu(retrigger_next_event, NULL, 1);
921 goto set_timerfd;
922 }
923
924 /* Avoid interrupting nohz_full CPUs if possible */
925 preempt_disable();
926 for_each_online_cpu(cpu) {
927 if (tick_nohz_full_cpu(cpu)) {
928 unsigned long flags;
929 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
930
931 raw_spin_lock_irqsave(&cpu_base->lock, flags);
932 if (need_reprogram_timer(cpu_base))
933 cpumask_set_cpu(cpu, mask);
934 else
935 hrtimer_update_base(cpu_base);
936 raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
937 }
938 }
939
940 smp_call_function_many(mask, retrigger_next_event, NULL, 1);
941 preempt_enable();
942 free_cpumask_var(mask);
943 #endif

 > 944	set_timerfd:

945 timerfd_clock_was_set();
946 }
947

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#2 lore raw
In reply to Marcelo Tosatti
Quoted 72 lines · Marcelo Tosatti

Setting the realtime clock triggers an IPI to all CPUs to reprogram hrtimers.

However, only base, boottime and tai clocks have their offsets updated (and therefore potentially require a reprogram).

If the CPU is a nohz_full one, check if it only has monotonic active timers, and in that case update the realtime base offsets, skipping the IPI.

This reduces interruptions to nohz_full CPUs.

kernel/time/hrtimer.c +40 -2
index 743c852e10f2..b42b1a434b22 100644
@@ -853,6 +853,28 @@ static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
 	tick_program_event(expires, 1);
 }
 
+#define CLOCK_SET_BASES ((1U << HRTIMER_BASE_REALTIME)|		\
+			 (1U << HRTIMER_BASE_REALTIME_SOFT)|	\
+			 (1U << HRTIMER_BASE_BOOTTIME)|		\
+			 (1U << HRTIMER_BASE_BOOTTIME_SOFT)|	\
+			 (1U << HRTIMER_BASE_TAI)|		\
+			 (1U << HRTIMER_BASE_TAI_SOFT))
+
+static bool need_reprogram_timer(struct hrtimer_cpu_base *cpu_base)
+{
+	unsigned int active = 0;
+
+	if (!cpu_base->softirq_activated)
+		active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
+
+	active = active | (cpu_base->active_bases & HRTIMER_ACTIVE_HARD);
+
+	if ((active & CLOCK_SET_BASES) == 0)
+		return false;
+
+	return true;
+}
+
 /*
  * Clock realtime was set
  *
@@ -867,9 +889,41 @@ static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
 void clock_was_set(void)
 {
 #ifdef CONFIG_HIGH_RES_TIMERS
-	/* Retrigger the CPU local events everywhere */
-	on_each_cpu(retrigger_next_event, NULL, 1);
+	cpumask_var_t mask;
+	int cpu;
+
+	if (!tick_nohz_full_enabled()) {
+		/* Retrigger the CPU local events everywhere */
+		on_each_cpu(retrigger_next_event, NULL, 1);
+		goto set_timerfd;
+	}
+
+	if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
+		on_each_cpu(retrigger_next_event, NULL, 1);
+		goto set_timerfd;
+	}
+
+	/* Avoid interrupting nohz_full CPUs if possible */
+	preempt_disable();
+	for_each_online_cpu(cpu) {
+		if (tick_nohz_full_cpu(cpu)) {

kernel/time/hrtimer.c static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram) · line 919

Quoted 10 lines
kernel/time/hrtimer.c +10 -0
+			unsigned long flags;
+			struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
+
+			raw_spin_lock_irqsave(&cpu_base->lock, flags);
+			if (need_reprogram_timer(cpu_base))
+				cpumask_set_cpu(cpu, mask);
+			else
+				hrtimer_update_base(cpu_base);
+			raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
+		}

You forgot to add the housekeeping CPUs to the mask.
As for the need_reprogram_timer() trick, I'll rather defer to Thomas review...

Thanks.

Quoted 11 lines
kernel/time/hrtimer.c +6 -0
+	}
+
+	smp_call_function_many(mask, retrigger_next_event, NULL, 1);
+	preempt_enable();
+	free_cpumask_var(mask);
 #endif
+set_timerfd:
 	timerfd_clock_was_set();
 }
 
#3 lore raw
Quoted 24 lines · Frederic Weisbecker
Quoted 23 lines · Marcelo Tosatti

Setting the realtime clock triggers an IPI to all CPUs to reprogram hrtimers.

However, only base, boottime and tai clocks have their offsets updated (and therefore potentially require a reprogram).

If the CPU is a nohz_full one, check if it only has monotonic active timers, and in that case update the realtime base offsets, skipping the IPI.

This reduces interruptions to nohz_full CPUs.

kernel/time/hrtimer.c +0 -0
index 743c852e10f2..b42b1a434b22 100644
@@ -853,6 +853,28 @@ static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
 	tick_program_event(expires, 1);
 }
 
Quoted 13 lines

kernel/time/hrtimer.c static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram) · line 868

Quoted 13 lines
kernel/time/hrtimer.c +13 -0
+#define CLOCK_SET_BASES ((1U << HRTIMER_BASE_REALTIME)|		\
+			 (1U << HRTIMER_BASE_REALTIME_SOFT)|	\
+			 (1U << HRTIMER_BASE_BOOTTIME)|		\
+			 (1U << HRTIMER_BASE_BOOTTIME_SOFT)|	\
+			 (1U << HRTIMER_BASE_TAI)|		\
+			 (1U << HRTIMER_BASE_TAI_SOFT))
+
+static bool need_reprogram_timer(struct hrtimer_cpu_base *cpu_base)
+{
+	unsigned int active = 0;
+
+	if (!cpu_base->softirq_activated)
+		active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;

If cpu_base->softirq_activated == 1, should IPI as well.

Quoted 39 lines
Quoted 39 lines
kernel/time/hrtimer.c +30 -2
+	active = active | (cpu_base->active_bases & HRTIMER_ACTIVE_HARD);
+
+	if ((active & CLOCK_SET_BASES) == 0)
+		return false;
+
+	return true;
+}
+
 /*
  * Clock realtime was set
  *
@@ -867,9 +889,41 @@ static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
 void clock_was_set(void)
 {
 #ifdef CONFIG_HIGH_RES_TIMERS
-	/* Retrigger the CPU local events everywhere */
-	on_each_cpu(retrigger_next_event, NULL, 1);
+	cpumask_var_t mask;
+	int cpu;
+
+	if (!tick_nohz_full_enabled()) {
+		/* Retrigger the CPU local events everywhere */
+		on_each_cpu(retrigger_next_event, NULL, 1);
+		goto set_timerfd;
+	}
+
+	if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
+		on_each_cpu(retrigger_next_event, NULL, 1);
+		goto set_timerfd;
+	}
+
+	/* Avoid interrupting nohz_full CPUs if possible */
+	preempt_disable();
+	for_each_online_cpu(cpu) {
+		if (tick_nohz_full_cpu(cpu)) {
+			unsigned long flags;
+			struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
+
+			raw_spin_lock_irqsave(&cpu_base->lock, flags);
Quoted 8 lines

kernel/time/hrtimer.c static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram) · line 919

Quoted 6 lines
kernel/time/hrtimer.c +6 -0
+			if (need_reprogram_timer(cpu_base))
+				cpumask_set_cpu(cpu, mask);
+			else
+				hrtimer_update_base(cpu_base);
+			raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
+		}

You forgot to add the housekeeping CPUs to the mask.

So people are using:

console=tty0 console=ttyS0,115200n8 skew_tick=1 nohz=on rcu_nocbs=8-31 tuned.non_isolcpus=000000ff intel_pstate=disable nosoftlockup tsc=nowatchdog intel_iommu=on iommu=pt isolcpus=managed_irq,8-31 systemd.cpu_affinity=0,1,2,3,4,5,6,7 default_hugepagesz=1G hugepagesz=2M hugepages=128 nohz_full=8-31

And using the nohz_full= CPUs (or subsets of nohz_full= CPUs) in two modes:

Either "generic non-isolated applications"
(with load-balancing enabled for those CPUs), or for latency sensitive applications. And switching between the modes.

In this case, it would only be possible to check for housekeeping CPUs of type MANAGED_IRQ, which would be strange.

Quoted 3 lines

As for the need_reprogram_timer() trick, I'll rather defer to Thomas review...

Thanks.

Thanks!

Quoted 12 lines
Quoted 12 lines
kernel/time/hrtimer.c +6 -0
+	}
+
+	smp_call_function_many(mask, retrigger_next_event, NULL, 1);
+	preempt_enable();
+	free_cpumask_var(mask);
 #endif
+set_timerfd:
 	timerfd_clock_was_set();
 }
 
#4 lore raw
In reply to Marcelo Tosatti
Quoted 4 lines · Marcelo Tosatti

Setting the realtime clock triggers an IPI to all CPUs to reprogram hrtimers.

However, only base, boottime and tai clocks have their offsets updated

base clock? And why boottime? Boottime is not affected by a clock realtime set. It's clock REALTIME and TAI, nothing else.

Quoted 11 lines
kernel/time/hrtimer.c +11 -0
+#define CLOCK_SET_BASES ((1U << HRTIMER_BASE_REALTIME)|		\
+			 (1U << HRTIMER_BASE_REALTIME_SOFT)|	\
+			 (1U << HRTIMER_BASE_BOOTTIME)|		\
+			 (1U << HRTIMER_BASE_BOOTTIME_SOFT)|	\
+			 (1U << HRTIMER_BASE_TAI)|		\
+			 (1U << HRTIMER_BASE_TAI_SOFT))
+
+static bool need_reprogram_timer(struct hrtimer_cpu_base *cpu_base)
+{
+	unsigned int active = 0;
+

kernel/time/hrtimer.c static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram) · line 876

Quoted 10 lines
kernel/time/hrtimer.c +10 -0
+	if (!cpu_base->softirq_activated)
+		active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
+
+	active = active | (cpu_base->active_bases & HRTIMER_ACTIVE_HARD);
+
+	if ((active & CLOCK_SET_BASES) == 0)
+		return false;
+
+	return true;
+}

Errm.

Quoted 5 lines
kernel/time/hrtimer.c +5 -0
+	/* Avoid interrupting nohz_full CPUs if possible */
+	preempt_disable();
+	for_each_online_cpu(cpu) {
+		if (tick_nohz_full_cpu(cpu)) {
+			unsigned long flags;

kernel/time/hrtimer.c static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram) · line 920

Quoted 10 lines
kernel/time/hrtimer.c +10 -0
+			struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
+
+			raw_spin_lock_irqsave(&cpu_base->lock, flags);
+			if (need_reprogram_timer(cpu_base))
+				cpumask_set_cpu(cpu, mask);
+			else
+				hrtimer_update_base(cpu_base);
+			raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
+		}
+	}

How is that supposed to be correct?

CPU0 CPU1

clock_was_set() hrtimer_start(CLOCK_REALTIME)

  if (!active_mask[CPU1] & XXX)
  	continue;
                                active_mask |= REALTIME;

---> fail because that newly started timer is on the old offset.

Thanks,

tglx

#5 lore raw

+CC Anna-Maria.

Quoted 7 lines · Thomas Gleixner
Quoted 4 lines · Marcelo Tosatti

Setting the realtime clock triggers an IPI to all CPUs to reprogram hrtimers.

However, only base, boottime and tai clocks have their offsets updated

base clock?

Heh...

Quoted 2 lines

And why boottime? Boottime is not affected by a clock realtime set. It's clock REALTIME and TAI, nothing else.

OK!

Quoted 5 lines
Quoted 5 lines
kernel/time/hrtimer.c +5 -0
+#define CLOCK_SET_BASES ((1U << HRTIMER_BASE_REALTIME)|		\
+			 (1U << HRTIMER_BASE_REALTIME_SOFT)|	\
+			 (1U << HRTIMER_BASE_BOOTTIME)|		\
+			 (1U << HRTIMER_BASE_BOOTTIME_SOFT)|	\
+			 (1U << HRTIMER_BASE_TAI)|		\
Quoted 8 lines

kernel/time/hrtimer.c static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram) · line 868

Quoted 8 lines
kernel/time/hrtimer.c +8 -0
+			 (1U << HRTIMER_BASE_TAI_SOFT))
+
+static bool need_reprogram_timer(struct hrtimer_cpu_base *cpu_base)
+{
+	unsigned int active = 0;
+
+	if (!cpu_base->softirq_activated)
+		active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;

Again, if (cpu_base->softirq_activated), need to IPI (will resend).

Quoted 9 lines

kernel/time/hrtimer.c static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram) · line 876

Quoted 7 lines
kernel/time/hrtimer.c +7 -0
+	active = active | (cpu_base->active_bases & HRTIMER_ACTIVE_HARD);
+
+	if ((active & CLOCK_SET_BASES) == 0)
+		return false;
+
+	return true;
+}

Errm.

What?

Quoted 20 lines
Quoted 5 lines
kernel/time/hrtimer.c +5 -0
+	/* Avoid interrupting nohz_full CPUs if possible */
+	preempt_disable();
+	for_each_online_cpu(cpu) {
+		if (tick_nohz_full_cpu(cpu)) {
+			unsigned long flags;

kernel/time/hrtimer.c static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram) · line 920

Quoted 10 lines
kernel/time/hrtimer.c +10 -0
+			struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
+
+			raw_spin_lock_irqsave(&cpu_base->lock, flags);
+			if (need_reprogram_timer(cpu_base))
+				cpumask_set_cpu(cpu, mask);
+			else
+				hrtimer_update_base(cpu_base);
+			raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
+		}
+	}

How is that supposed to be correct?

CPU0 CPU1

Quoted 7 lines

clock_was_set() hrtimer_start(CLOCK_REALTIME)

  if (!active_mask[CPU1] & XXX)
  	continue;
                                active_mask |= REALTIME;

---> fail because that newly started timer is on the old offset.

CPU0 CPU1

clock_was_set()

							Case-1: CPU-1 grabs base->lock before CPU-0:
							CPU-0 sees active_mask[CPU1] and IPIs.

							base = lock_hrtimer_base(timer, &flags);
        						if (__hrtimer_start_range_ns(timer, tim, ...
                						hrtimer_reprogram(timer, true);

        						unlock_hrtimer_base(timer, &flags);

raw_spin_lock_irqsave(&cpu_base->lock, flags);
if (need_reprogram_timer(cpu_base))

        cpumask_set_cpu(cpu, mask);
else
        hrtimer_update_base(cpu_base);

raw_spin_unlock_irqrestore(&cpu_base->lock, flags);

							Case-2: CPU-1 grabs base->lock after CPU-0:
							CPU-0 will have updated the offsets remotely.

							base = lock_hrtimer_base(timer, &flags);
        						if (__hrtimer_start_range_ns(timer, tim, ...
                						hrtimer_reprogram(timer, true);

        						unlock_hrtimer_base(timer, &flags);

No?

#6 lore raw
Quoted 35 lines · Marcelo Tosatti
Quoted 2 lines · Thomas Gleixner

On Wed, Apr 07 2021 at 10:53, Marcelo Tosatti wrote:
---> fail because that newly started timer is on the old offset.

CPU0 CPU1

clock_was_set()

							Case-1: CPU-1 grabs base->lock before CPU-0:
							CPU-0 sees active_mask[CPU1] and IPIs.

							base = lock_hrtimer_base(timer, &flags);
        						if (__hrtimer_start_range_ns(timer, tim, ...
                						hrtimer_reprogram(timer, true);

        						unlock_hrtimer_base(timer, &flags);

raw_spin_lock_irqsave(&cpu_base->lock, flags);
if (need_reprogram_timer(cpu_base))

        cpumask_set_cpu(cpu, mask);
else
        hrtimer_update_base(cpu_base);

raw_spin_unlock_irqrestore(&cpu_base->lock, flags);

							Case-2: CPU-1 grabs base->lock after CPU-0:
							CPU-0 will have updated the offsets remotely.

							base = lock_hrtimer_base(timer, &flags);
        						if (__hrtimer_start_range_ns(timer, tim, ...
                						hrtimer_reprogram(timer, true);

        						unlock_hrtimer_base(timer, &flags);
Quoted 1 line

No?

Yeah, you're right. I misread the loop logic.

Can we please make that unconditional independent of nohz full. There is no reason to special case it.

Thanks,

tglx

#7 lore raw

PATCH v2 1/1 hrtimer: avoid retrigger_next_event IPI

Setting the realtime clock triggers an IPI to all CPUs to reprogram hrtimers.

However, only realtime and TAI clocks have their offsets updated (and therefore potentially require a reprogram).

Check if it only has monotonic active timers, and in that case update the realtime and TAI base offsets remotely, skipping the IPI.

This reduces interruptions to latency sensitive applications.

---

v2:
- Only REALTIME and TAI bases are affected by offset-to-monotonic changes (Thomas).
- Don't special case nohz_full CPUs (Thomas).

kernel/time/hrtimer.c +46 -2
index 5c9d968187ae..be21b85c679d 100644
@@ -871,6 +871,28 @@ static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
 	tick_program_event(expires, 1);
 }
 
+#define CLOCK_SET_BASES ((1U << HRTIMER_BASE_REALTIME)|		\
+			 (1U << HRTIMER_BASE_REALTIME_SOFT)|	\
+			 (1U << HRTIMER_BASE_TAI)|		\
+			 (1U << HRTIMER_BASE_TAI_SOFT))
+
+static bool need_reprogram_timer(struct hrtimer_cpu_base *cpu_base)
+{
+	unsigned int active = 0;
+
+	if (cpu_base->softirq_activated)
+		return true;
+
+	active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
+
+	active = active | (cpu_base->active_bases & HRTIMER_ACTIVE_HARD);
+
+	if ((active & CLOCK_SET_BASES) == 0)
+		return false;
+
+	return true;
+}
+
 /*
  * Clock realtime was set
  *
@@ -885,9 +907,31 @@ static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
 void clock_was_set(void)
 {
 #ifdef CONFIG_HIGH_RES_TIMERS
-	/* Retrigger the CPU local events everywhere */
-	on_each_cpu(retrigger_next_event, NULL, 1);
+	cpumask_var_t mask;
+	int cpu;
+
+	if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
+		on_each_cpu(retrigger_next_event, NULL, 1);
+		goto set_timerfd;
+	}
+
+	/* Avoid interrupting CPUs if possible */
+	preempt_disable();
+	for_each_online_cpu(cpu) {
+		unsigned long flags;
+		struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
+
+		raw_spin_lock_irqsave(&cpu_base->lock, flags);
+		if (need_reprogram_timer(cpu_base))
+			cpumask_set_cpu(cpu, mask);
+		raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
+	}
+
+	smp_call_function_many(mask, retrigger_next_event, NULL, 1);
+	preempt_enable();
+	free_cpumask_var(mask);
 #endif
+set_timerfd:
 	timerfd_clock_was_set();
 }
 
#8 lore raw

Marcelo,

Quoted 2 lines · Marcelo Tosatti

Setting the realtime clock triggers an IPI to all CPUs to reprogram hrtimers.

s/hrtimers/clock event device/

Quoted 4 lines

However, only realtime and TAI clocks have their offsets updated (and therefore potentially require a reprogram).

Check if it only has monotonic active timers, and in that case

boottime != monotonic

Quoted 1 line

update the realtime and TAI base offsets remotely, skipping the IPI.

Something like this perhaps:

Instead of sending an IPI unconditionally, check each per CPU hrtimer base whether it has active timers in the CLOCK_REALTIME and CLOCK_TAI bases. If that's not the case, update the realtime and TAI base offsets remotely and skip the IPI. This ensures that any subsequently armed timers on CLOCK_REALTIME and CLOCK_TAI are evaluated with the correct offsets.

Hmm?

Quoted 1 line

+#define CLOCK_SET_BASES ((1U << HRTIMER_BASE_REALTIME)| \

Missing space between ) and |

Quoted 9 lines
kernel/time/hrtimer.c +9 -0
+			 (1U << HRTIMER_BASE_REALTIME_SOFT)|	\
+			 (1U << HRTIMER_BASE_TAI)|		\
+			 (1U << HRTIMER_BASE_TAI_SOFT))
+
+static bool need_reprogram_timer(struct hrtimer_cpu_base *cpu_base)
+{
+	unsigned int active = 0;
+
+	if (cpu_base->softirq_activated)

kernel/time/hrtimer.c static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram) · line 893

Quoted 10 lines
kernel/time/hrtimer.c +10 -0
+		return true;
+
+	active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
+
+	active = active | (cpu_base->active_bases & HRTIMER_ACTIVE_HARD);
+
+	if ((active & CLOCK_SET_BASES) == 0)
+		return false;
+
+	return true;

That's a pretty elaborate way of writing:

       return (cpu_base->active_bases & CLOCK_SET_BASES) != 0;
Quoted 11 lines
kernel/time/hrtimer.c +2 -2
+}
+
 /*
  * Clock realtime was set
  *
@@ -885,9 +907,31 @@ static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
 void clock_was_set(void)
 {
 #ifdef CONFIG_HIGH_RES_TIMERS
-	/* Retrigger the CPU local events everywhere */
-	on_each_cpu(retrigger_next_event, NULL, 1);

kernel/time/hrtimer.c static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram) · line 919

Quoted 10 lines
kernel/time/hrtimer.c +10 -0
+	cpumask_var_t mask;
+	int cpu;
+
+	if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
+		on_each_cpu(retrigger_next_event, NULL, 1);
+		goto set_timerfd;
+	}
+
+	/* Avoid interrupting CPUs if possible */
+	preempt_disable();

That preempt disable is only required around the function call, for the loop cpus_read_lock() is sufficient.

Quoted 5 lines
kernel/time/hrtimer.c +5 -0
+	for_each_online_cpu(cpu) {
+		unsigned long flags;
+		struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
+
+		raw_spin_lock_irqsave(&cpu_base->lock, flags);

kernel/time/hrtimer.c static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram) · line 934

Quoted 10 lines
kernel/time/hrtimer.c +9 -0
+		if (need_reprogram_timer(cpu_base))
+			cpumask_set_cpu(cpu, mask);
+		raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
+	}
+
+	smp_call_function_many(mask, retrigger_next_event, NULL, 1);
+	preempt_enable();
+	free_cpumask_var(mask);
 #endif
+set_timerfd:

My brain compiler tells me that with CONFIG_HIGH_RES_TIMERS=n a real compiler will emit a warning about a defined, but unused label....

kernel/time/hrtimer.c static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram) · line 936

Quoted 2 lines
kernel/time/hrtimer.c +0 -0
 	timerfd_clock_was_set();
 }

Thanks,

tglx

#9 lore raw

PATCH v3 1/1 hrtimer: avoid retrigger_next_event IPI

In reply to Marcelo Tosatti

Setting the realtime clock triggers an IPI to all CPUs to reprogram the clock event device.

However, only realtime and TAI clocks have their offsets updated (and therefore potentially require a reprogram).

Instead of sending an IPI unconditionally, check each per CPU hrtimer base whether it has active timers in the CLOCK_REALTIME and CLOCK_TAI bases. If that's not the case, update the realtime and TAI base offsets remotely and skip the IPI. This ensures that any subsequently armed timers on CLOCK_REALTIME and CLOCK_TAI are evaluated with the correct offsets.

---

v3:
- Nicer changelog (Thomas).
- Code style fixes (Thomas).
- Compilation warning with CONFIG_HIGH_RES_TIMERS=n (Thomas).
- Shrink preemption disabled section (Thomas).

v2:
- Only REALTIME and TAI bases are affected by offset-to-monotonic changes (Thomas).
- Don't special case nohz_full CPUs (Thomas).

kernel/time/hrtimer.c +44 -2
index 5c9d968187ae..dd9c0d2f469f 100644
@@ -871,6 +871,24 @@ static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
 	tick_program_event(expires, 1);
 }
 
+#define CLOCK_SET_BASES ((1U << HRTIMER_BASE_REALTIME) |	\
+			 (1U << HRTIMER_BASE_REALTIME_SOFT) |	\
+			 (1U << HRTIMER_BASE_TAI) |		\
+			 (1U << HRTIMER_BASE_TAI_SOFT))
+
+static bool need_reprogram_timer(struct hrtimer_cpu_base *cpu_base)
+{
+	unsigned int active = 0;
+
+	if (cpu_base->softirq_activated)
+		return true;
+
+	active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
+	active = active | (cpu_base->active_bases & HRTIMER_ACTIVE_HARD);
+
+	return (cpu_base->active_bases & CLOCK_SET_BASES) != 0;
+}
+
 /*
  * Clock realtime was set
  *
@@ -885,8 +903,32 @@ static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
 void clock_was_set(void)
 {
 #ifdef CONFIG_HIGH_RES_TIMERS
-	/* Retrigger the CPU local events everywhere */
-	on_each_cpu(retrigger_next_event, NULL, 1);
+	cpumask_var_t mask;
+	int cpu;
+
+	if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
+		on_each_cpu(retrigger_next_event, NULL, 1);
+		goto set_timerfd;
+	}
+
+	/* Avoid interrupting CPUs if possible */
+	cpus_read_lock();
+	for_each_online_cpu(cpu) {
+		unsigned long flags;
+		struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
+
+		raw_spin_lock_irqsave(&cpu_base->lock, flags);
+		if (need_reprogram_timer(cpu_base))
+			cpumask_set_cpu(cpu, mask);
+		raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
+	}
+
+	preempt_disable();
+	smp_call_function_many(mask, retrigger_next_event, NULL, 1);
+	preempt_enable();
+	cpus_read_unlock();
+	free_cpumask_var(mask);
+set_timerfd:
 #endif
 	timerfd_clock_was_set();
 }
#10 lore raw

kernel/time/hrtimer.c static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram) · line 887

Quoted 9 lines · Marcelo Tosatti
kernel/time/hrtimer.c +9 -0
+static bool need_reprogram_timer(struct hrtimer_cpu_base *cpu_base)
+{
+	unsigned int active = 0;
+
+	if (cpu_base->softirq_activated)
+		return true;
+
+	active = cpu_base->active_bases & HRTIMER_ACTIVE_SOFT;
+	active = active | (cpu_base->active_bases & HRTIMER_ACTIVE_HARD);

It's not entirely clear to me what that 'active' variable business above is doing and why it's needed at all. But I might be missing something.

Quoted 2 lines
kernel/time/hrtimer.c +2 -0
+	return (cpu_base->active_bases & CLOCK_SET_BASES) != 0;
+}
#11 lore raw

PATCH v4 1/1 hrtimer: avoid retrigger_next_event IPI

Setting the realtime clock triggers an IPI to all CPUs to reprogram the clock event device.

However, only realtime and TAI clocks have their offsets updated (and therefore potentially require a reprogram).

Instead of sending an IPI unconditionally, check each per CPU hrtimer base whether it has active timers in the CLOCK_REALTIME and CLOCK_TAI bases. If that's not the case, update the realtime and TAI base offsets remotely and skip the IPI. This ensures that any subsequently armed timers on CLOCK_REALTIME and CLOCK_TAI are evaluated with the correct offsets.

---

v4:
- Drop unused code (Thomas).

v3:
- Nicer changelog (Thomas).
- Code style fixes (Thomas).
- Compilation warning with CONFIG_HIGH_RES_TIMERS=n (Thomas).
- Shrink preemption disabled section (Thomas).

v2:
- Only REALTIME and TAI bases are affected by offset-to-monotonic changes (Thomas).
- Don't special case nohz_full CPUs (Thomas).

kernel/time/hrtimer.c +39 -2
index 5c9d968187ae..e228c0a0c98f 100644
@@ -871,6 +871,19 @@ static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
 	tick_program_event(expires, 1);
 }
 
+#define CLOCK_SET_BASES ((1U << HRTIMER_BASE_REALTIME) |	\
+			 (1U << HRTIMER_BASE_REALTIME_SOFT) |	\
+			 (1U << HRTIMER_BASE_TAI) |		\
+			 (1U << HRTIMER_BASE_TAI_SOFT))
+
+static bool need_reprogram_timer(struct hrtimer_cpu_base *cpu_base)
+{
+	if (cpu_base->softirq_activated)
+		return true;
+
+	return (cpu_base->active_bases & CLOCK_SET_BASES) != 0;
+}
+
 /*
  * Clock realtime was set
  *
@@ -885,8 +898,32 @@ static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
 void clock_was_set(void)
 {
 #ifdef CONFIG_HIGH_RES_TIMERS
-	/* Retrigger the CPU local events everywhere */
-	on_each_cpu(retrigger_next_event, NULL, 1);
+	cpumask_var_t mask;
+	int cpu;
+
+	if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
+		on_each_cpu(retrigger_next_event, NULL, 1);
+		goto set_timerfd;
+	}
+
+	/* Avoid interrupting CPUs if possible */
+	cpus_read_lock();
+	for_each_online_cpu(cpu) {
+		unsigned long flags;
+		struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
+
+		raw_spin_lock_irqsave(&cpu_base->lock, flags);
+		if (need_reprogram_timer(cpu_base))
+			cpumask_set_cpu(cpu, mask);
+		raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
+	}
+
+	preempt_disable();
+	smp_call_function_many(mask, retrigger_next_event, NULL, 1);
+	preempt_enable();
+	cpus_read_unlock();
+	free_cpumask_var(mask);
+set_timerfd:
 #endif
 	timerfd_clock_was_set();
 }
#12 lore raw

PATCH v5 1/1 hrtimer: avoid retrigger_next_event IPI

Setting the realtime clock triggers an IPI to all CPUs to reprogram the clock event device.

However, only realtime and TAI clocks have their offsets updated (and therefore potentially require a reprogram).

Instead of sending an IPI unconditionally, check each per CPU hrtimer base whether it has active timers in the CLOCK_REALTIME and CLOCK_TAI bases. If that's not the case, update the realtime and TAI base offsets remotely and skip the IPI. This ensures that any subsequently armed timers on CLOCK_REALTIME and CLOCK_TAI are evaluated with the correct offsets.

---

v5:
- Add missing hrtimer_update_base (Peter Xu).

v4:
- Drop unused code (Thomas).

v3:
- Nicer changelog (Thomas).
- Code style fixes (Thomas).
- Compilation warning with CONFIG_HIGH_RES_TIMERS=n (Thomas).
- Shrink preemption disabled section (Thomas).

v2:
- Only REALTIME and TAI bases are affected by offset-to-monotonic changes (Thomas).
- Don't special case nohz_full CPUs (Thomas).

kernel/time/hrtimer.c +41 -2
index 5c9d968187ae..06fcc272e28d 100644
@@ -871,6 +871,19 @@ static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
 	tick_program_event(expires, 1);
 }
 
+#define CLOCK_SET_BASES ((1U << HRTIMER_BASE_REALTIME) |	\
+			 (1U << HRTIMER_BASE_REALTIME_SOFT) |	\
+			 (1U << HRTIMER_BASE_TAI) |		\
+			 (1U << HRTIMER_BASE_TAI_SOFT))
+
+static bool need_reprogram_timer(struct hrtimer_cpu_base *cpu_base)
+{
+	if (cpu_base->softirq_activated)
+		return true;
+
+	return (cpu_base->active_bases & CLOCK_SET_BASES) != 0;
+}
+
 /*
  * Clock realtime was set
  *
@@ -885,8 +898,34 @@ static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
 void clock_was_set(void)
 {
 #ifdef CONFIG_HIGH_RES_TIMERS
-	/* Retrigger the CPU local events everywhere */
-	on_each_cpu(retrigger_next_event, NULL, 1);
+	cpumask_var_t mask;
+	int cpu;
+
+	if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
+		on_each_cpu(retrigger_next_event, NULL, 1);
+		goto set_timerfd;
+	}
+
+	/* Avoid interrupting CPUs if possible */
+	cpus_read_lock();
+	for_each_online_cpu(cpu) {
+		unsigned long flags;
+		struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
+
+		raw_spin_lock_irqsave(&cpu_base->lock, flags);
+		if (need_reprogram_timer(cpu_base))
+			cpumask_set_cpu(cpu, mask);
+		else
+			hrtimer_update_base(cpu_base);
+		raw_spin_unlock_irqrestore(&cpu_base->lock, flags);
+	}
+
+	preempt_disable();
+	smp_call_function_many(mask, retrigger_next_event, NULL, 1);
+	preempt_enable();
+	cpus_read_unlock();
+	free_cpumask_var(mask);
+set_timerfd:
 #endif
 	timerfd_clock_was_set();
 }
#13 lore raw

Hi, all,

Quoted 41 lines · Marcelo Tosatti

Setting the realtime clock triggers an IPI to all CPUs to reprogram the clock event device.

However, only realtime and TAI clocks have their offsets updated (and therefore potentially require a reprogram).

Instead of sending an IPI unconditionally, check each per CPU hrtimer base whether it has active timers in the CLOCK_REALTIME and CLOCK_TAI bases. If that's not the case, update the realtime and TAI base offsets remotely and skip the IPI. This ensures that any subsequently armed timers on CLOCK_REALTIME and CLOCK_TAI are evaluated with the correct offsets.

---

v5:
- Add missing hrtimer_update_base (Peter Xu).

v4:
- Drop unused code (Thomas).

v3:
- Nicer changelog (Thomas).
- Code style fixes (Thomas).
- Compilation warning with CONFIG_HIGH_RES_TIMERS=n (Thomas).
- Shrink preemption disabled section (Thomas).

v2:
- Only REALTIME and TAI bases are affected by offset-to-monotonic changes (Thomas).
- Don't special case nohz_full CPUs (Thomas).

kernel/time/hrtimer.c +0 -0
index 5c9d968187ae..06fcc272e28d 100644
@@ -871,6 +871,19 @@ static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
 	tick_program_event(expires, 1);
 }

kernel/time/hrtimer.c static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram) · line 882

Quoted 10 lines
kernel/time/hrtimer.c +9 -0
 
+#define CLOCK_SET_BASES ((1U << HRTIMER_BASE_REALTIME) |	\
+			 (1U << HRTIMER_BASE_REALTIME_SOFT) |	\
+			 (1U << HRTIMER_BASE_TAI) |		\
+			 (1U << HRTIMER_BASE_TAI_SOFT))
+
+static bool need_reprogram_timer(struct hrtimer_cpu_base *cpu_base)
+{
+	if (cpu_base->softirq_activated)
+		return true;

A pure question on whether this check is needed...

Here even if softirq_activated==1 (as softirq is going to happen), as long as (cpu_base->active_bases & CLOCK_SET_BASES)==0, shouldn't it already mean that "yes indeed clock was set, but no need to kick this cpu as no relevant timer"? As that question seems to be orthogonal to whether a softirq is going to trigger on that cpu.

Thanks,

Quoted 3 lines
kernel/time/hrtimer.c +3 -0
+
+	return (cpu_base->active_bases & CLOCK_SET_BASES) != 0;
+}
Signature · 1 line
Peter Xu
#14 lore raw
Quoted 14 lines · Peter Xu

kernel/time/hrtimer.c static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram) · line 882

Quoted 10 lines · Marcelo Tosatti
kernel/time/hrtimer.c +9 -0
+#define CLOCK_SET_BASES ((1U << HRTIMER_BASE_REALTIME) |	\
+			 (1U << HRTIMER_BASE_REALTIME_SOFT) |	\
+			 (1U << HRTIMER_BASE_TAI) |		\
+			 (1U << HRTIMER_BASE_TAI_SOFT))
+
+static bool need_reprogram_timer(struct hrtimer_cpu_base *cpu_base)
+{
+	if (cpu_base->softirq_activated)
+		return true;

A pure question on whether this check is needed...

Quoted 5 lines

Here even if softirq_activated==1 (as softirq is going to happen), as long as (cpu_base->active_bases & CLOCK_SET_BASES)==0, shouldn't it already mean that "yes indeed clock was set, but no need to kick this cpu as no relevant timer"? As that question seems to be orthogonal to whether a softirq is going to trigger on that cpu.

That's correct and it's not any different from firing the IPI because in both cases the update happens with the base lock of the CPU in question held. And if there are no active timers in any of the affected bases, then there is no need to reevaluate the next expiry because the offset update does not affect any armed timers. It just makes sure that the next enqueu of a timer on such a base will see the the correct offset.

I'll just zap it.

Thanks,

tglx

#15 lore raw
Quoted 21 lines · Thomas Gleixner
Quoted 14 lines · Peter Xu

kernel/time/hrtimer.c static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram) · line 882

Quoted 10 lines · Marcelo Tosatti
kernel/time/hrtimer.c +9 -0
+#define CLOCK_SET_BASES ((1U << HRTIMER_BASE_REALTIME) |	\
+			 (1U << HRTIMER_BASE_REALTIME_SOFT) |	\
+			 (1U << HRTIMER_BASE_TAI) |		\
+			 (1U << HRTIMER_BASE_TAI_SOFT))
+
+static bool need_reprogram_timer(struct hrtimer_cpu_base *cpu_base)
+{
+	if (cpu_base->softirq_activated)
+		return true;

A pure question on whether this check is needed...

Quoted 5 lines

Here even if softirq_activated==1 (as softirq is going to happen), as long as (cpu_base->active_bases & CLOCK_SET_BASES)==0, shouldn't it already mean that "yes indeed clock was set, but no need to kick this cpu as no relevant timer"? As that question seems to be orthogonal to whether a softirq is going to trigger on that cpu.

Quoted 8 lines

That's correct and it's not any different from firing the IPI because in both cases the update happens with the base lock of the CPU in question held. And if there are no active timers in any of the affected bases, then there is no need to reevaluate the next expiry because the offset update does not affect any armed timers. It just makes sure that the next enqueu of a timer on such a base will see the the correct offset.

I'll just zap it.

But the whole thing is still wrong in two aspects:

1) BOOTTIME can be one of the affected clocks when sleep time (suspended time) is injected because that uses the same mechanism.

Sorry for missing that earlier when I asked to remove it, but that's trivial to fix by adding the BOOTTIME base back.

2) What's worse is that on resume this might break because that mechanism is also used to enforce the reprogramming of the clock event devices and there we cannot be selective on clock bases.

I need to dig deeper into that because suspend/resume has changed a lot over time, so this might be just a historical leftover. But without proper analysis we might end up with subtle and hard to debug wreckage.

Thanks,

tglx

#16 lore raw
Quoted 43 lines · Thomas Gleixner
Quoted 21 lines · Thomas Gleixner
Quoted 14 lines · Peter Xu

kernel/time/hrtimer.c static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram) · line 882

Quoted 10 lines · Marcelo Tosatti
kernel/time/hrtimer.c +9 -0
+#define CLOCK_SET_BASES ((1U << HRTIMER_BASE_REALTIME) |	\
+			 (1U << HRTIMER_BASE_REALTIME_SOFT) |	\
+			 (1U << HRTIMER_BASE_TAI) |		\
+			 (1U << HRTIMER_BASE_TAI_SOFT))
+
+static bool need_reprogram_timer(struct hrtimer_cpu_base *cpu_base)
+{
+	if (cpu_base->softirq_activated)
+		return true;

A pure question on whether this check is needed...

Quoted 5 lines

Here even if softirq_activated==1 (as softirq is going to happen), as long as (cpu_base->active_bases & CLOCK_SET_BASES)==0, shouldn't it already mean that "yes indeed clock was set, but no need to kick this cpu as no relevant timer"? As that question seems to be orthogonal to whether a softirq is going to trigger on that cpu.

Quoted 8 lines

That's correct and it's not any different from firing the IPI because in both cases the update happens with the base lock of the CPU in question held. And if there are no active timers in any of the affected bases, then there is no need to reevaluate the next expiry because the offset update does not affect any armed timers. It just makes sure that the next enqueu of a timer on such a base will see the the correct offset.

I'll just zap it.

But the whole thing is still wrong in two aspects:

1) BOOTTIME can be one of the affected clocks when sleep time (suspended time) is injected because that uses the same mechanism.

Sorry for missing that earlier when I asked to remove it, but that's trivial to fix by adding the BOOTTIME base back.

2) What's worse is that on resume this might break because that mechanism is also used to enforce the reprogramming of the clock event devices and there we cannot be selective on clock bases.

Quoted 8 lines

I need to dig deeper into that because suspend/resume has changed a lot over time, so this might be just a historical leftover. But without proper analysis we might end up with subtle and hard to debug wreckage.

Thanks,

tglx

Thomas,

There is no gain in avoiding the IPIs for the suspend/resume case (since suspending is a large interruption anyway). To avoid the potential complexity (and associated bugs), one option would be to NOT skip IPIs for the resume case.

Sending -v6 with that (and other suggestions/fixes).

#17 lore raw

PATCH v6 1/1 hrtimer: avoid retrigger_next_event IPI

In reply to Marcelo Tosatti

Setting the realtime clock triggers an IPI to all CPUs to reprogram the clock event device.

However, only realtime and TAI clocks have their offsets updated (and therefore potentially require a reprogram).

Instead of sending an IPI unconditionally, check each per CPU hrtimer base whether it has active timers in the CLOCK_REALTIME and CLOCK_TAI bases. If that's not the case, update the realtime and TAI base offsets remotely and skip the IPI. This ensures that any subsequently armed timers on CLOCK_REALTIME and CLOCK_TAI are evaluated with the correct offsets.

---

v6:
- Do not take softirq_raised into account (Peter Xu).
- Include BOOTTIME as base that requires IPI (Thomas).
- Unconditional reprogram on resume path, since there is nothing to gain in such path anyway.

v5:
- Add missing hrtimer_update_base (Peter Xu).

v4:
- Drop unused code (Thomas).

v3:
- Nicer changelog (Thomas).
- Code style fixes (Thomas).
- Compilation warning with CONFIG_HIGH_RES_TIMERS=n (Thomas).
- Shrink preemption disabled section (Thomas).

v2:
- Only REALTIME and TAI bases are affected by offset-to-monotonic changes (Thomas).
- Don't special case nohz_full CPUs (Thomas).

include/linux/hrtimer.h +3 -3
index bb5e7b0a4274..14a6e449b221 100644
@@ -318,7 +318,7 @@ struct clock_event_device;
 
 extern void hrtimer_interrupt(struct clock_event_device *dev);
 
-extern void clock_was_set_delayed(void);
+extern void clock_was_set_delayed(bool force_reprogram);
 
 extern unsigned int hrtimer_resolution;
 
@@ -326,7 +326,7 @@ extern unsigned int hrtimer_resolution;
 
 #define hrtimer_resolution	(unsigned int)LOW_RES_NSEC
 
-static inline void clock_was_set_delayed(void) { }
+static inline void clock_was_set_delayed(bool force_reprogram) { }
 
 #endif
 
@@ -351,7 +351,7 @@ hrtimer_expires_remaining_adjusted(const struct hrtimer *timer)
 						    timer->base->get_time());
 }
 
-extern void clock_was_set(void);
+extern void clock_was_set(bool);
 #ifdef CONFIG_TIMERFD
 extern void timerfd_clock_was_set(void);
 #else
kernel/time/hrtimer.c +61 -7

103 lines not on this page · raw message

kernel/time/timekeeping.c +5 -5
index 6aee5768c86f..3fef237267bd 100644
@@ -1323,7 +1323,7 @@ int do_settimeofday64(const struct timespec64 *ts)
 	raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
 
 	/* signal hrtimers about time change */
-	clock_was_set();
+	clock_was_set(false);
 
 	if (!ret)
 		audit_tk_injoffset(ts_delta);
@@ -1371,7 +1371,7 @@ static int timekeeping_inject_offset(const struct timespec64 *ts)
 	raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
 
 	/* signal hrtimers about time change */
-	clock_was_set();
+	clock_was_set(false);
 
 	return ret;
 }
@@ -1736,7 +1736,7 @@ void timekeeping_inject_sleeptime64(const struct timespec64 *delta)
 	raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
 
 	/* signal hrtimers about time change */
-	clock_was_set();
+	clock_was_set(true);
 }
 #endif
 
@@ -2187,7 +2187,7 @@ static void timekeeping_advance(enum timekeeping_adv_mode mode)
 	raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
 	if (clock_set)
 		/* Have to call _delayed version, since in irq context*/
-		clock_was_set_delayed();
+		clock_was_set_delayed(false);
 }
 
 /**
@@ -2425,7 +2425,7 @@ int do_adjtimex(struct __kernel_timex *txc)
 		timekeeping_advance(TK_ADV_FREQ);
 
 	if (tai != orig_tai)
-		clock_was_set();
+		clock_was_set(false);
 
 	ntp_notify_cmos_timer();
 
#18 lore raw
Quoted 62 lines · Marcelo Tosatti
kernel/time/hrtimer.c +29 -6
+static void clock_was_set_force_reprogram_work(struct work_struct *work)
+{
+	clock_was_set(true);
+}
+
+static DECLARE_WORK(hrtimer_force_reprogram_work, clock_was_set_force_reprogram_work);
+
+
 static void clock_was_set_work(struct work_struct *work)
 {
-	clock_was_set();
+	clock_was_set(false);
 }
 
 static DECLARE_WORK(hrtimer_work, clock_was_set_work);
@@ -769,9 +777,12 @@ static DECLARE_WORK(hrtimer_work, clock_was_set_work);
  * Called from timekeeping and resume code to reprogram the hrtimer
  * interrupt device on all cpus.
  */
-void clock_was_set_delayed(void)
+void clock_was_set_delayed(bool force_reprogram)
 {
-	schedule_work(&hrtimer_work);
+	if (force_reprogram)
+		schedule_work(&hrtimer_force_reprogram_work);
+	else
+		schedule_work(&hrtimer_work);
 }
 
 #else
@@ -871,6 +882,18 @@ static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
 	tick_program_event(expires, 1);
 }
 
+#define CLOCK_SET_BASES ((1U << HRTIMER_BASE_REALTIME) |	\
+			 (1U << HRTIMER_BASE_REALTIME_SOFT) |	\
+			 (1U << HRTIMER_BASE_TAI) |		\
+			 (1U << HRTIMER_BASE_TAI_SOFT) |	\
+			 (1U << HRTIMER_BASE_BOOTTIME) |	\
+			 (1U << HRTIMER_BASE_BOOTTIME_SOFT))
+
+static bool need_reprogram_timer(struct hrtimer_cpu_base *cpu_base)
+{
+	return (cpu_base->active_bases & CLOCK_SET_BASES) != 0;
+}
+
 /*
  * Clock realtime was set
  *
@@ -882,11 +905,42 @@ static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram)
  * resolution timer interrupts. On UP we just disable interrupts and
  * call the high resolution interrupt code.
  */
-void clock_was_set(void)
+void clock_was_set(bool force_reprogram)
 {
 #ifdef CONFIG_HIGH_RES_TIMERS
-	/* Retrigger the CPU local events everywhere */
-	on_each_cpu(retrigger_next_event, NULL, 1);
+	cpumask_var_t mask;
+	int cpu;

kernel/time/hrtimer.c static void hrtimer_reprogram(struct hrtimer *timer, bool reprogram) · line 922

Quoted 10 lines
kernel/time/hrtimer.c +10 -0
+
+	if (force_reprogram == true) {
+		on_each_cpu(retrigger_next_event, NULL, 1);
+		goto set_timerfd;
+	}
+
+	if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
+		on_each_cpu(retrigger_next_event, NULL, 1);
+		goto set_timerfd;
+	}

This is really horrible and the proper thing to do is to seperate the s2idle/resume related functionality which allows to do some other cleanups as well.

I already started to look at that over the weekend, but got stuck due to a couple of correctness issues I found with the whole clock_was_set() mechanism while looking. That stuff needs to be fixed first.

Back to the above. For the s2idle resume path there is no reason for an IPI at all. It's just that way because it has been bolted on the existing machinery.

So today it does:

   tick_unfreeze() {
     if (first_cpu_to_unfreeze()) {
        timekeeping_resume();
          tick_resume();
            tick_resume_local();
          clock_was_set_delayed();
     } else {
        tick_resume_local();
     }
   }

Then after everything is unfrozen including workqueues the delayed clock_was_set() runs and does the IPI dance. That's just silly.

We can be smarter than that:

   tick_unfreeze() {
     if (first_cpu_to_unfreeze())
        timekeeping_resume();
          tick_resume();
            tick_resume_local();
              hrtimer_resume_local();
     } else {
        tick_resume_local();
          hrtimer_resume_local();
     }
   }

See? No IPI required at all and no magic force argument and as a bonus we get also understandable code.

Splitting this properly allows to be smarter about the affected clocks because most of the clock_was_set() events only care about CLOCK_REALTIME and CLOCK_TAI and just the sleep time injection affects CLOCK_BOOTTIME as well.

There are a few other things which can be done to avoid even more IPIs, but let me address the correctness issues of clock_was_set() first.

Thanks,

tglx