Skip to contentSkip to conversation

Dynamic Tick: Enabling longer sleep times on 32-bit machines

PATCH In review Jon Hunter 3 participants 12 messages Aug 18, 2009 – Nov 13, 2009

Subsystems (3)
lore
Share & tools
lore

Jon Hunter

author
#0 lore raw

cover Dynamic Tick: Enabling longer sleep times on 32-bit machines

From: Jon Hunter <[email protected]>

This is a resend of the patch series shown here:
http://www.spinics.net/lists/kernel/msg891029.html

This patch series has been rebase on the linux-2.6-tip timers/core branch per request from Thomas Gleixner.

This patch series ensures that the wrapping of the clocksource will not be missed if the kernel sleeps for longer periods and allows 32-bit machines to sleep for longer than 2.15 seconds.

Jon Hunter (2):
Dynamic Tick: Prevent clocksource wrapping during idle Dynamic Tick: Allow 32-bit machines to sleep for more than 2.15
seconds

 include/linux/clockchips.h  |    6 ++--
 include/linux/clocksource.h |    2 +
 include/linux/time.h        |    1 +
 kernel/hrtimer.c            |    2 +-
 kernel/time/clockevents.c   |   10 ++++----
 kernel/time/clocksource.c   |   47 +++++++++++++++++++++++++++++++++++
 kernel/time/tick-oneshot.c  |    2 +-
 kernel/time/tick-sched.c    |   57 ++++++++++++++++++++++++++++++++----------
 kernel/time/timekeeping.c   |   11 ++++++++
 kernel/time/timer_list.c    |    4 +-
 10 files changed, 116 insertions(+), 26 deletions(-)
author
#1 lore raw

PATCH 1/2 Dynamic Tick: Prevent clocksource wrapping during idle

From: Jon Hunter <[email protected]>

The dynamic tick allows the kernel to sleep for periods longer than a single tick. This patch prevents that the kernel from sleeping for a period longer than the maximum time that the current clocksource can count. This ensures that the kernel will not lose track of time. This patch adds a function called "clocksource_max_deferment()" that calculates the maximum time the kernel can sleep for a given clocksource and function called "timekeeping_max_deferment()" that returns maximum time the kernel can sleep for the current clocksource.

---
 include/linux/clocksource.h |    2 +
 include/linux/time.h        |    1 +
 kernel/time/clocksource.c   |   47 +++++++++++++++++++++++++++++++++++
 kernel/time/tick-sched.c    |   57 ++++++++++++++++++++++++++++++++----------
 kernel/time/timekeeping.c   |   11 ++++++++
 5 files changed, 104 insertions(+), 14 deletions(-)
include/linux/clocksource.h +2 -0
index 9ea40ff..09ed7f1 100644
@@ -151,6 +151,7 @@ extern u64 timecounter_cyc2time(struct timecounter *tc,
  *			subtraction of non 64 bit counters
  * @mult:		cycle to nanosecond multiplier
  * @shift:		cycle to nanosecond divisor (power of two)
+ * @max_idle_ns:	max idle time permitted by the clocksource (nsecs)
  * @flags:		flags describing special properties
  * @vread:		vsyscall based read
  * @resume:		resume function for the clocksource, if necessary
@@ -168,6 +169,7 @@ struct clocksource {
 	cycle_t mask;
 	u32 mult;
 	u32 shift;
+	s64 max_idle_ns;
 	unsigned long flags;
 	cycle_t (*vread)(void);
 	void (*resume)(void);
include/linux/time.h +1 -0
index f505988..e68a480 100644
@@ -146,6 +146,7 @@ extern void monotonic_to_bootbased(struct timespec *ts);
 
 extern struct timespec timespec_trunc(struct timespec t, unsigned gran);
 extern int timekeeping_valid_for_hres(void);
+extern s64 timekeeping_max_deferment(void);
 extern void update_wall_time(void);
 extern void update_xtime_cache(u64 nsec);
 extern void timekeeping_leap_insert(int leapsecond);
kernel/time/clocksource.c +47 -0

59 lines not on this page · raw message

kernel/time/tick-sched.c +43 -14

88 lines not on this page · raw message

kernel/time/timekeeping.c +11 -0
index 15e06de..2e57251 100644
@@ -487,6 +487,17 @@ int timekeeping_valid_for_hres(void)
 }
 
 /**
+ * timekeeping_max_deferment - Returns max time the clocksource can be deferred
+ *
+ * IMPORTANT: Caller must observe xtime_lock via read_seqbegin/read_seqretry
+ * to ensure that the clocksource does not change!
+ */
+s64 timekeeping_max_deferment(void)
+{
+	return timekeeper.clock->max_idle_ns;
+}
+
+/**
  * read_persistent_clock -  Return time from the persistent clock.
  *
  * Weak dummy function for arches that do not yet support it.
Signature · 1 line
1.6.0.4
author
#2 lore raw

PATCH 2/2 Dynamic Tick: Allow 32-bit machines to sleep for more than 2.15 seconds

From: Jon Hunter <[email protected]>

In the dynamic tick code, "max_delta_ns" (member of the "clock_event_device" structure) represents the maximum sleep time that can occur between timer events in nanoseconds.

The variable, "max_delta_ns", is defined as an unsigned long which is a 32-bit integer for 32-bit machines and a 64-bit integer for 64-bit machines (if -m64 option is used for gcc). The value of max_delta_ns is set by calling the function "clockevent_delta2ns()" which returns a maximum value of LONG_MAX. For a 32-bit machine LONG_MAX is equal to 0x7fffffff and in nanoseconds this equates to ~2.15 seconds. Hence, the maximum sleep time for a 32-bit machine is ~2.15 seconds, where as for a 64-bit machine it will be many years.

This patch changes the type of max_delta_ns to be "unsigned long long" instead of "unsigned long" so that this variable is a 64-bit type for both 32-bit and 64-bit machines. It also changes the maximum value returned by clockevent_delta2ns() to LLONG_MAX. Hence this allows a 32-bit machine to sleep for longer than ~2.15 seconds. Please note that this patch also changes "min_delta_ns" to be "unsigned long long" too and although this is probably unnecessary, it makes the patch simpler.

---
 include/linux/clockchips.h |    6 +++---
 kernel/hrtimer.c           |    2 +-
 kernel/time/clockevents.c  |   10 +++++-----
 kernel/time/tick-oneshot.c |    2 +-
 kernel/time/timer_list.c   |    4 ++--
 5 files changed, 12 insertions(+), 12 deletions(-)
include/linux/clockchips.h +3 -3
index 3a1dbba..8154bc6 100644
@@ -77,8 +77,8 @@ enum clock_event_nofitiers {
 struct clock_event_device {
 	const char		*name;
 	unsigned int		features;
-	unsigned long		max_delta_ns;
-	unsigned long		min_delta_ns;
+	unsigned long long	max_delta_ns;
+	unsigned long long	min_delta_ns;
 	unsigned long		mult;
 	int			shift;
 	int			rating;
@@ -116,7 +116,7 @@ static inline unsigned long div_sc(unsigned long ticks, unsigned long nsec,
 }
 
 /* Clock event layer functions */
-extern unsigned long clockevent_delta2ns(unsigned long latch,
+extern unsigned long long clockevent_delta2ns(unsigned long latch,
 					 struct clock_event_device *evt);
 extern void clockevents_register_device(struct clock_event_device *dev);
 
kernel/hrtimer.c +1 -1
index e2f91ec..2043e78 100644
@@ -1198,7 +1198,7 @@ hrtimer_interrupt_hanging(struct clock_event_device *dev,
 	force_clock_reprogram = 1;
 	dev->min_delta_ns = (unsigned long)try_time.tv64 * 3;
 	printk(KERN_WARNING "hrtimer: interrupt too slow, "
-		"forcing clock min delta to %lu ns\n", dev->min_delta_ns);
+		"forcing clock min delta to %llu ns\n", dev->min_delta_ns);
 }
 /*
  * High resolution timer interrupt
kernel/time/clockevents.c +5 -5
index a6dcd67..6db410f 100644
@@ -37,10 +37,10 @@ static DEFINE_SPINLOCK(clockevents_lock);
  *
  * Math helper, returns latch value converted to nanoseconds (bound checked)
  */
-unsigned long clockevent_delta2ns(unsigned long latch,
+unsigned long long clockevent_delta2ns(unsigned long latch,
 				  struct clock_event_device *evt)
 {
-	u64 clc = ((u64) latch << evt->shift);
+	unsigned long long clc = ((unsigned long long) latch << evt->shift);
 
 	if (unlikely(!evt->mult)) {
 		evt->mult = 1;
@@ -50,10 +50,10 @@ unsigned long clockevent_delta2ns(unsigned long latch,
 	do_div(clc, evt->mult);
 	if (clc < 1000)
 		clc = 1000;
-	if (clc > LONG_MAX)
-		clc = LONG_MAX;
+	if (clc > LLONG_MAX)
+		clc = LLONG_MAX;
 
-	return (unsigned long) clc;
+	return clc;
 }
 EXPORT_SYMBOL_GPL(clockevent_delta2ns);
 
kernel/time/tick-oneshot.c +1 -1
index a96c0e2..327d4ed 100644
@@ -50,7 +50,7 @@ int tick_dev_program_event(struct clock_event_device *dev, ktime_t expires,
 				dev->min_delta_ns += dev->min_delta_ns >> 1;
 
 			printk(KERN_WARNING
-			       "CE: %s increasing min_delta_ns to %lu nsec\n",
+			       "CE: %s increasing min_delta_ns to %llu nsec\n",
 			       dev->name ? dev->name : "?",
 			       dev->min_delta_ns << 1);
 
kernel/time/timer_list.c +2 -2
index a999b92..3bf30b4 100644
@@ -204,8 +204,8 @@ print_tickdevice(struct seq_file *m, struct tick_device *td, int cpu)
 		return;
 	}
 	SEQ_printf(m, "%s\n", dev->name);
-	SEQ_printf(m, " max_delta_ns:   %lu\n", dev->max_delta_ns);
-	SEQ_printf(m, " min_delta_ns:   %lu\n", dev->min_delta_ns);
+	SEQ_printf(m, " max_delta_ns:   %llu\n", dev->max_delta_ns);
+	SEQ_printf(m, " min_delta_ns:   %llu\n", dev->min_delta_ns);
 	SEQ_printf(m, " mult:           %lu\n", dev->mult);
 	SEQ_printf(m, " shift:          %d\n", dev->shift);
 	SEQ_printf(m, " mode:           %d\n", dev->mode);
Signature · 1 line
1.6.0.4
#3 lore raw

Re: [PATCH 2/2] Dynamic Tick: Allow 32-bit machines to sleep for more than 2.15 seconds

include/linux/clockchips.h enum clock_event_nofitiers { · line 81

Quoted 12 lines · Jon Hunter
include/linux/clockchips.h +2 -2
index 3a1dbba..8154bc6 100644
@@ -77,8 +77,8 @@ enum clock_event_nofitiers {
 struct clock_event_device {
 	const char		*name;
 	unsigned int		features;
-	unsigned long		max_delta_ns;
-	unsigned long		min_delta_ns;
+	unsigned long long	max_delta_ns;
+	unsigned long long	min_delta_ns;

Can we please use u64 for this ?

Thanks,

tglx

author
#4 lore raw

Re: [PATCH 2/2] Dynamic Tick: Allow 32-bit machines to sleep for more than 2.15 seconds

Quoted 15 lines · Thomas Gleixner

include/linux/clockchips.h enum clock_event_nofitiers { · line 81

Quoted 12 lines · Jon Hunter
include/linux/clockchips.h +2 -2
index 3a1dbba..8154bc6 100644
@@ -77,8 +77,8 @@ enum clock_event_nofitiers {
 struct clock_event_device {
 	const char		*name;
 	unsigned int		features;
-	unsigned long		max_delta_ns;
-	unsigned long		min_delta_ns;
+	unsigned long long	max_delta_ns;
+	unsigned long long	min_delta_ns;

Can we please use u64 for this ?

John brought this up as well. There was some discussion sometime back about this. I did get some feedback that u64 was a different type between ppc64 and x86-64 which was causing problems with printk. The above variables are also used with printk in the kernel today.

See the following email:
http://marc.info/?l=linux-kernel&m=124041426203283&w=2

I am not sure if this is still the case and safer to stick with long-long for now. Let me know your thoughts.

Cheers
Jon

#5 lore raw

[tip:timers/core] nohz: Allow 32-bit machines to sleep for more than 2.15 seconds

In reply to Jon Hunter
Commit-ID:  97813f2fe77804a4464564c75ba8d8826377feea
Gitweb:     http://git.kernel.org/tip/97813f2fe77804a4464564c75ba8d8826377feea
Author:     Jon Hunter <[email protected]>
AuthorDate: Tue, 18 Aug 2009 12:45:11 -0500
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Fri, 13 Nov 2009 20:46:24 +0100

nohz: Allow 32-bit machines to sleep for more than 2.15 seconds

In the dynamic tick code, "max_delta_ns" (member of the "clock_event_device" structure) represents the maximum sleep time that can occur between timer events in nanoseconds.

The variable, "max_delta_ns", is defined as an unsigned long which is a 32-bit integer for 32-bit machines and a 64-bit integer for 64-bit machines (if -m64 option is used for gcc). The value of max_delta_ns is set by calling the function "clockevent_delta2ns()" which returns a maximum value of LONG_MAX. For a 32-bit machine LONG_MAX is equal to 0x7fffffff and in nanoseconds this equates to ~2.15 seconds. Hence, the maximum sleep time for a 32-bit machine is ~2.15 seconds, where as for a 64-bit machine it will be many years.

This patch changes the type of max_delta_ns to be "u64" instead of "unsigned long" so that this variable is a 64-bit type for both 32-bit and 64-bit machines. It also changes the maximum value returned by clockevent_delta2ns() to KTIME_MAX. Hence this allows a 32-bit machine to sleep for longer than ~2.15 seconds. Please note that this patch also changes "min_delta_ns" to be "u64" too and although this is unnecessary, it makes the patch simpler as it avoids to fixup all callers of clockevent_delta2ns().

[ tglx: changed "unsigned long long" to u64 as we use this data type
through out the time code ]

LKML-Reference: <[email protected]>

---
 include/linux/clockchips.h |    8 ++++----
 kernel/hrtimer.c           |    3 ++-
 kernel/time/clockevents.c  |   11 +++++------
 kernel/time/tick-oneshot.c |    4 ++--
 kernel/time/timer_list.c   |    6 ++++--
 5 files changed, 17 insertions(+), 15 deletions(-)
include/linux/clockchips.h +4 -4
index 4d438b0..0cf725b 100644
@@ -77,8 +77,8 @@ enum clock_event_nofitiers {
 struct clock_event_device {
 	const char		*name;
 	unsigned int		features;
-	unsigned long		max_delta_ns;
-	unsigned long		min_delta_ns;
+	u64			max_delta_ns;
+	u64			min_delta_ns;
 	u32			mult;
 	u32			shift;
 	int			rating;
@@ -116,8 +116,8 @@ static inline unsigned long div_sc(unsigned long ticks, unsigned long nsec,
 }
 
 /* Clock event layer functions */
-extern unsigned long clockevent_delta2ns(unsigned long latch,
-					 struct clock_event_device *evt);
+extern u64 clockevent_delta2ns(unsigned long latch,
+			       struct clock_event_device *evt);
 extern void clockevents_register_device(struct clock_event_device *dev);
 
 extern void clockevents_exchange_device(struct clock_event_device *old,
kernel/hrtimer.c +2 -1
index 6d70204..c215b74 100644
@@ -1240,7 +1240,8 @@ hrtimer_interrupt_hanging(struct clock_event_device *dev,
 	force_clock_reprogram = 1;
 	dev->min_delta_ns = (unsigned long)try_time.tv64 * 3;
 	printk(KERN_WARNING "hrtimer: interrupt too slow, "
-		"forcing clock min delta to %lu ns\n", dev->min_delta_ns);
+	       "forcing clock min delta to %llu ns\n",
+	       (unsigned long long) dev->min_delta_ns);
 }
 /*
  * High resolution timer interrupt
kernel/time/clockevents.c +5 -6
index 620b58a..05e8aee 100644
@@ -37,10 +37,9 @@ static DEFINE_SPINLOCK(clockevents_lock);
  *
  * Math helper, returns latch value converted to nanoseconds (bound checked)
  */
-unsigned long clockevent_delta2ns(unsigned long latch,
-				  struct clock_event_device *evt)
+u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt)
 {
-	u64 clc = ((u64) latch << evt->shift);
+	u64 clc = (u64) latch << evt->shift;
 
 	if (unlikely(!evt->mult)) {
 		evt->mult = 1;
@@ -50,10 +49,10 @@ unsigned long clockevent_delta2ns(unsigned long latch,
 	do_div(clc, evt->mult);
 	if (clc < 1000)
 		clc = 1000;
-	if (clc > LONG_MAX)
-		clc = LONG_MAX;
+	if (clc > KTIME_MAX)
+		clc = KTIME_MAX;
 
-	return (unsigned long) clc;
+	return clc;
 }
 EXPORT_SYMBOL_GPL(clockevent_delta2ns);
 
kernel/time/tick-oneshot.c +2 -2
index a96c0e2..0a8a213 100644
@@ -50,9 +50,9 @@ int tick_dev_program_event(struct clock_event_device *dev, ktime_t expires,
 				dev->min_delta_ns += dev->min_delta_ns >> 1;
 
 			printk(KERN_WARNING
-			       "CE: %s increasing min_delta_ns to %lu nsec\n",
+			       "CE: %s increasing min_delta_ns to %llu nsec\n",
 			       dev->name ? dev->name : "?",
-			       dev->min_delta_ns << 1);
+			       (unsigned long long) dev->min_delta_ns << 1);
 
 			i = 0;
 		}
kernel/time/timer_list.c +4 -2
index fa00da1..665c76e 100644
@@ -204,8 +204,10 @@ print_tickdevice(struct seq_file *m, struct tick_device *td, int cpu)
 		return;
 	}
 	SEQ_printf(m, "%s\n", dev->name);
-	SEQ_printf(m, " max_delta_ns:   %lu\n", dev->max_delta_ns);
-	SEQ_printf(m, " min_delta_ns:   %lu\n", dev->min_delta_ns);
+	SEQ_printf(m, " max_delta_ns:   %llu\n",
+		   (unsigned long long) dev->max_delta_ns);
+	SEQ_printf(m, " min_delta_ns:   %llu\n",
+		   (unsigned long long) dev->min_delta_ns);
 	SEQ_printf(m, " mult:           %u\n", dev->mult);
 	SEQ_printf(m, " shift:          %u\n", dev->shift);
 	SEQ_printf(m, " mode:           %d\n", dev->mode);
#6 lore raw

Re: [PATCH 1/2] Dynamic Tick: Prevent clocksource wrapping during idle

In reply to Jon Hunter
Quoted 34 lines · Jon Hunter

From: Jon Hunter <[email protected]>

The dynamic tick allows the kernel to sleep for periods longer than a single tick. This patch prevents that the kernel from sleeping for a period longer than the maximum time that the current clocksource can count. This ensures that the kernel will not lose track of time. This patch adds a function called "clocksource_max_deferment()" that calculates the maximum time the kernel can sleep for a given clocksource and function called "timekeeping_max_deferment()" that returns maximum time the kernel can sleep for the current clocksource.

---
 include/linux/clocksource.h |    2 +
 include/linux/time.h        |    1 +
 kernel/time/clocksource.c   |   47 +++++++++++++++++++++++++++++++++++
 kernel/time/tick-sched.c    |   57 ++++++++++++++++++++++++++++++++----------
 kernel/time/timekeeping.c   |   11 ++++++++
 5 files changed, 104 insertions(+), 14 deletions(-)
include/linux/clocksource.h +1 -0
index 9ea40ff..09ed7f1 100644
@@ -151,6 +151,7 @@ extern u64 timecounter_cyc2time(struct timecounter *tc,
  *			subtraction of non 64 bit counters
  * @mult:		cycle to nanosecond multiplier
  * @shift:		cycle to nanosecond divisor (power of two)
+ * @max_idle_ns:	max idle time permitted by the clocksource (nsecs)
  * @flags:		flags describing special properties
  * @vread:		vsyscall based read
  * @resume:		resume function for the clocksource, if necessary

include/linux/clocksource.h struct clocksource { · line 172

Quoted 4 lines
include/linux/clocksource.h +1 -0
@@ -168,6 +169,7 @@ struct clocksource {
 	cycle_t mask;
 	u32 mult;
 	u32 shift;
+	s64 max_idle_ns;

I don't think we should move this to the clocksource. That should go into the new struct timekeeper and initialized when a clocksource is selected for timekeeping.

Quoted 18 lines
kernel/time/tick-sched.c +3 -0
index e0f59a2..7a98e90 100644
@@ -217,6 +217,7 @@ void tick_nohz_stop_sched_tick(int inidle)
 	ktime_t last_update, expires, now;
 	struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev;
 	int cpu;
+	s64 time_delta, max_time_delta;
 
 	local_irq_save(flags);
 
@@ -270,6 +271,18 @@ void tick_nohz_stop_sched_tick(int inidle)
 		seq = read_seqbegin(&xtime_lock);
 		last_update = last_jiffies_update;
 		last_jiffies = jiffies;
+
+		/*

kernel/time/tick-sched.c void tick_nohz_stop_sched_tick(int inidle) · line 285

Quoted 10 lines
kernel/time/tick-sched.c +10 -0
+		 * On SMP we really should only care for the CPU which
+		 * has the do_timer duty assigned. All other CPUs can
+		 * sleep as long as they want.
+		 */
+		if (cpu == tick_do_timer_cpu ||
+				tick_do_timer_cpu == TICK_DO_TIMER_NONE)
+			max_time_delta = timekeeping_max_deferment();
+		else
+			max_time_delta = KTIME_MAX;
+

Is it worth the extra check instead of always using timekeeping_max_deferment() ?

Quoted 26 lines
kernel/time/tick-sched.c +14 -5
 	} while (read_seqretry(&xtime_lock, seq));
 
 	/* Get the next timer wheel timer */
@@ -289,11 +302,30 @@ void tick_nohz_stop_sched_tick(int inidle)
 	if ((long)delta_jiffies >= 1) {
 
 		/*
-		* calculate the expiry time for the next timer wheel
-		* timer
-		*/
-		expires = ktime_add_ns(last_update, tick_period.tv64 *
-				   delta_jiffies);
+		 * calculate the expiry time for the next timer wheel
+		 * timer. delta_jiffies >= NEXT_TIMER_MAX_DELTA signals
+		 * that there is no timer pending or at least extremely
+		 * far into the future (12 days for HZ=1000). In this
+		 * case we set the expiry to the end of time.
+		 */
+		if (likely(delta_jiffies < NEXT_TIMER_MAX_DELTA)) {
+
+			/*
+			 * Calculate the time delta for the next timer event.
+			 * If the time delta exceeds the maximum time delta
+			 * permitted by the current clocksource then adjust
+			 * the time delta accordingly to ensure the
+			 * clocksource does not wrap.

kernel/time/tick-sched.c void tick_nohz_stop_sched_tick(int inidle) · line 328

Quoted 10 lines
kernel/time/tick-sched.c +10 -0
+			 */
+			time_delta = tick_period.tv64 * delta_jiffies;
+
+			if (time_delta > max_time_delta)
+				time_delta = max_time_delta;
+
+			expires = ktime_add_ns(last_update, time_delta);
+		} else {
+			expires.tv64 = KTIME_MAX;
+		}

This looks incorrect. You set expires to KTIME_MAX when no timer is pending, but that defeats the purpose of this patch. When we hit this code path and the next interrupt comes in after the timekeeping clocksource wrapped we are bust.

Thanks,

tglx

author
#7 lore raw

Re: [PATCH 1/2] Dynamic Tick: Prevent clocksource wrapping during idle

Quoted 110 lines · Thomas Gleixner
Quoted 34 lines · Jon Hunter

From: Jon Hunter <[email protected]>

The dynamic tick allows the kernel to sleep for periods longer than a single tick. This patch prevents that the kernel from sleeping for a period longer than the maximum time that the current clocksource can count. This ensures that the kernel will not lose track of time. This patch adds a function called "clocksource_max_deferment()" that calculates the maximum time the kernel can sleep for a given clocksource and function called "timekeeping_max_deferment()" that returns maximum time the kernel can sleep for the current clocksource.

---
 include/linux/clocksource.h |    2 +
 include/linux/time.h        |    1 +
 kernel/time/clocksource.c   |   47 +++++++++++++++++++++++++++++++++++
 kernel/time/tick-sched.c    |   57 ++++++++++++++++++++++++++++++++----------
 kernel/time/timekeeping.c   |   11 ++++++++
 5 files changed, 104 insertions(+), 14 deletions(-)
include/linux/clocksource.h +1 -0
index 9ea40ff..09ed7f1 100644
@@ -151,6 +151,7 @@ extern u64 timecounter_cyc2time(struct timecounter *tc,
  *			subtraction of non 64 bit counters
  * @mult:		cycle to nanosecond multiplier
  * @shift:		cycle to nanosecond divisor (power of two)
+ * @max_idle_ns:	max idle time permitted by the clocksource (nsecs)
  * @flags:		flags describing special properties
  * @vread:		vsyscall based read
  * @resume:		resume function for the clocksource, if necessary

include/linux/clocksource.h struct clocksource { · line 172

Quoted 4 lines
include/linux/clocksource.h +1 -0
@@ -168,6 +169,7 @@ struct clocksource {
 	cycle_t mask;
 	u32 mult;
 	u32 shift;
+	s64 max_idle_ns;

I don't think we should move this to the clocksource. That should go into the new struct timekeeper and initialized when a clocksource is selected for timekeeping.

Quoted 18 lines
kernel/time/tick-sched.c +3 -0
index e0f59a2..7a98e90 100644
@@ -217,6 +217,7 @@ void tick_nohz_stop_sched_tick(int inidle)
 	ktime_t last_update, expires, now;
 	struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev;
 	int cpu;
+	s64 time_delta, max_time_delta;
 
 	local_irq_save(flags);
 
@@ -270,6 +271,18 @@ void tick_nohz_stop_sched_tick(int inidle)
 		seq = read_seqbegin(&xtime_lock);
 		last_update = last_jiffies_update;
 		last_jiffies = jiffies;
+
+		/*

kernel/time/tick-sched.c void tick_nohz_stop_sched_tick(int inidle) · line 285

Quoted 10 lines
kernel/time/tick-sched.c +10 -0
+		 * On SMP we really should only care for the CPU which
+		 * has the do_timer duty assigned. All other CPUs can
+		 * sleep as long as they want.
+		 */
+		if (cpu == tick_do_timer_cpu ||
+				tick_do_timer_cpu == TICK_DO_TIMER_NONE)
+			max_time_delta = timekeeping_max_deferment();
+		else
+			max_time_delta = KTIME_MAX;
+

Is it worth the extra check instead of always using timekeeping_max_deferment() ?

Quoted 33 lines
kernel/time/tick-sched.c +21 -5
 	} while (read_seqretry(&xtime_lock, seq));
 
 	/* Get the next timer wheel timer */
@@ -289,11 +302,30 @@ void tick_nohz_stop_sched_tick(int inidle)
 	if ((long)delta_jiffies >= 1) {
 
 		/*
-		* calculate the expiry time for the next timer wheel
-		* timer
-		*/
-		expires = ktime_add_ns(last_update, tick_period.tv64 *
-				   delta_jiffies);
+		 * calculate the expiry time for the next timer wheel
+		 * timer. delta_jiffies >= NEXT_TIMER_MAX_DELTA signals
+		 * that there is no timer pending or at least extremely
+		 * far into the future (12 days for HZ=1000). In this
+		 * case we set the expiry to the end of time.
+		 */
+		if (likely(delta_jiffies < NEXT_TIMER_MAX_DELTA)) {
+
+			/*
+			 * Calculate the time delta for the next timer event.
+			 * If the time delta exceeds the maximum time delta
+			 * permitted by the current clocksource then adjust
+			 * the time delta accordingly to ensure the
+			 * clocksource does not wrap.
+			 */
+			time_delta = tick_period.tv64 * delta_jiffies;
+
+			if (time_delta > max_time_delta)
+				time_delta = max_time_delta;
+
+			expires = ktime_add_ns(last_update, time_delta);
Quoted 8 lines

kernel/time/tick-sched.c void tick_nohz_stop_sched_tick(int inidle) · line 328

Quoted 3 lines
kernel/time/tick-sched.c +3 -0
+		} else {
+			expires.tv64 = KTIME_MAX;
+		}

This looks incorrect. You set expires to KTIME_MAX when no timer is pending, but that defeats the purpose of this patch. When we hit this code path and the next interrupt comes in after the timekeeping clocksource wrapped we are bust.

Right, so this is a bit of a grey area for me. When I first started looking at this I was questioning the purpose of the following code that exists today in the tick_nohz_stop_sched_tick() function:

	/*
	 * delta_jiffies >= NEXT_TIMER_MAX_DELTA signals that
	 * there is no timer pending or at least extremly far
	 * into the future (12 days for HZ=1000). In this case
	 * we simply stop the tick timer:
	 */
	if (unlikely(delta_jiffies >= NEXT_TIMER_MAX_DELTA)) {
		ts->idle_expires.tv64 = KTIME_MAX;
		if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
			hrtimer_cancel(&ts->sched_timer);
			goto out;
	}

The above code checks to see delta_jiffies is greater than NEXT_TIMER_MAX_DELTA, if so then sets expires to KTIME_MAX and disables the timer. I had questioned this a few months ago, but I don't think that John and I knew the history here. So for right or wrong, I left this code alone. In the above patch it is still do the same thing if delta_jiffies is indeed greater than NEXT_TIMER_MAX_DELTA.

If you agree that this code is not needed and that in the case where we have no timers we should simply make the next timer event always occur in max_time_delta ns later, then I can re-work it to do this.

Thanks
Jon

#8 lore raw

[tip:timers/core] nohz: Prevent clocksource wrapping during idle

In reply to Jon Hunter
Commit-ID:  98962465ed9e6ea99c38e0af63fe1dcb5a79dc25
Gitweb:     http://git.kernel.org/tip/98962465ed9e6ea99c38e0af63fe1dcb5a79dc25
Author:     Jon Hunter <[email protected]>
AuthorDate: Tue, 18 Aug 2009 12:45:10 -0500
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Fri, 13 Nov 2009 20:46:24 +0100

nohz: Prevent clocksource wrapping during idle

The dynamic tick allows the kernel to sleep for periods longer than a single tick, but it does not limit the sleep time currently. In the worst case the kernel could sleep longer than the wrap around time of the time keeping clock source which would result in losing track of time.

Prevent this by limiting it to the safe maximum sleep time of the current time keeping clock source. The value is calculated when the clock source is registered.

[ tglx: simplified the code a bit and massaged the commit msg ]

LKML-Reference: <[email protected]>

---
 include/linux/clocksource.h |    2 +
 include/linux/time.h        |    1 +
 kernel/time/clocksource.c   |   44 ++++++++++++++++++++++++++++++++++++
 kernel/time/tick-sched.c    |   52 +++++++++++++++++++++++++++++++-----------
 kernel/time/timekeeping.c   |   11 +++++++++
 5 files changed, 96 insertions(+), 14 deletions(-)
include/linux/clocksource.h +2 -0
index f57f882..279c547 100644
@@ -151,6 +151,7 @@ extern u64 timecounter_cyc2time(struct timecounter *tc,
  *			subtraction of non 64 bit counters
  * @mult:		cycle to nanosecond multiplier
  * @shift:		cycle to nanosecond divisor (power of two)
+ * @max_idle_ns:	max idle time permitted by the clocksource (nsecs)
  * @flags:		flags describing special properties
  * @vread:		vsyscall based read
  * @resume:		resume function for the clocksource, if necessary
@@ -168,6 +169,7 @@ struct clocksource {
 	cycle_t mask;
 	u32 mult;
 	u32 shift;
+	u64 max_idle_ns;
 	unsigned long flags;
 	cycle_t (*vread)(void);
 	void (*resume)(void);
include/linux/time.h +1 -0
index fe04e5e..6e026e4 100644
@@ -148,6 +148,7 @@ extern void monotonic_to_bootbased(struct timespec *ts);
 
 extern struct timespec timespec_trunc(struct timespec t, unsigned gran);
 extern int timekeeping_valid_for_hres(void);
+extern u64 timekeeping_max_deferment(void);
 extern void update_wall_time(void);
 extern void update_xtime_cache(u64 nsec);
 extern void timekeeping_leap_insert(int leapsecond);
kernel/time/clocksource.c +44 -0

56 lines not on this page · raw message

kernel/time/tick-sched.c +38 -14

83 lines not on this page · raw message

kernel/time/timekeeping.c +11 -0
index 96b3f0d..5d4d423 100644
@@ -478,6 +478,17 @@ int timekeeping_valid_for_hres(void)
 }
 
 /**
+ * timekeeping_max_deferment - Returns max time the clocksource can be deferred
+ *
+ * Caller must observe xtime_lock via read_seqbegin/read_seqretry to
+ * ensure that the clocksource does not change!
+ */
+u64 timekeeping_max_deferment(void)
+{
+	return timekeeper.clock->max_idle_ns;
+}
+
+/**
  * read_persistent_clock -  Return time from the persistent clock.
  *
  * Weak dummy function for arches that do not yet support it.
#9 lore raw
In reply to Jon Hunter
Quoted 8 lines · Jon Hunter

From: Jon Hunter <[email protected]>

This is a resend of the patch series shown here:
http://www.spinics.net/lists/kernel/msg891029.html

This patch series has been rebase on the linux-2.6-tip timers/core branch per request from Thomas Gleixner.

Quoted 8 lines

This patch series ensures that the wrapping of the clocksource will not be missed if the kernel sleeps for longer periods and allows 32-bit machines to sleep for longer than 2.15 seconds.

Jon Hunter (2):
 Dynamic Tick: Prevent clocksource wrapping during idle Dynamic Tick: Allow 32-bit machines to sleep for more than 2.15 seconds

I could have sworn this was in mainline by now, but I recently was looking for the code and can't find it there or in -tip either.

Thomas, are they just hiding somewhere I can't find?

Jon, you've been terribly patient and great about resubmitting these patches over and over. If I'm not just being crazy and missing these patches in front of my nose, are you still willing to submit them again? I think they'll be quite useful as folks start pushing the NOHZ idle times out.

thanks
-john

author
#10 lore raw
Quoted 10 lines · john stultz

I could have sworn this was in mainline by now, but I recently was looking for the code and can't find it there or in -tip either.

Thomas, are they just hiding somewhere I can't find?

Jon, you've been terribly patient and great about resubmitting these patches over and over. If I'm not just being crazy and missing these patches in front of my nose, are you still willing to submit them again? I think they'll be quite useful as folks start pushing the NOHZ idle times out.

Absolutely! It is still on my to-do list, but unfortunately, I got busy with a couple other things.

With regard to the last patch set I submitted for this, Thomas had an issue with one of the patches. I understand the concern, but I am not sure which would be the preferred way to handle this. See the below thread:

http://marc.info/?l=linux-kernel&m=125062817124381&w=2

If you or Thomas have any feedback on this, I could re-work the patch against the latest kernel tree.

Cheers
Jon

#11 lore raw
Quoted 15 lines · Jon Hunter
Quoted 10 lines · john stultz

I could have sworn this was in mainline by now, but I recently was looking for the code and can't find it there or in -tip either.

Thomas, are they just hiding somewhere I can't find?

Jon, you've been terribly patient and great about resubmitting these patches over and over. If I'm not just being crazy and missing these patches in front of my nose, are you still willing to submit them again? I think they'll be quite useful as folks start pushing the NOHZ idle times out.

Absolutely! It is still on my to-do list, but unfortunately, I got busy with a couple other things.

Quoted 8 lines

With regard to the last patch set I submitted for this, Thomas had an issue with one of the patches. I understand the concern, but I am not sure which would be the preferred way to handle this. See the below thread:

http://marc.info/?l=linux-kernel&m=125062817124381&w=2

If you or Thomas have any feedback on this, I could re-work the patch against the latest kernel tree.

Ok. I think Thomas is right there, setting the expiration to max_time_delta makes the most sense. Honestly I suspect we don't ever hit that case in the current code (no timers for 12 days), so its probably an untested code path as it stands.

thanks
-john