Skip to contentSkip to conversation

clocksource init adjustments (fix bug #7426)

PATCH In review john stultz 4 participants 7 messages Feb 23, 2007 – Mar 4, 2007

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

PATCH 1/1 clocksource init adjustments (fix bug #7426)

This patch resolves the issue found here:
http://bugme.osdl.org/show_bug.cgi?id=7426

The basic summary is:
Currently we register most of i386/x86_64 clocksources at module_init time. Then we enable clocksource selection at late_initcall time. This causes some problems for drivers that use gettimeofday for init calibration routines (specifically the es1968 driver in this case), where durring module_init, the only clocksource available is the low-res jiffies clocksource. This may cause slight calibration errors, due to the small sampling time used.

It should be noted that drivers that require fine grained time may not function on architectures that do not have better then jiffies resolution timekeeping (there are a few). However, this does not discount the reasonable need for such fine-grained timekeeping at init time.

Thus the solution here is to register clocksources earlier (ideally when the hardware is being initialized), and then we enable clocksource selection at fs_initcall (before device_initcall).

This patch should probably get some testing time in -mm, since clocksource selection is one of the most important issues for correct timekeeping, and I've only been able to test this on a few of my own boxes.

thanks
-john

 arch/i386/kernel/hpet.c       |   80 ++++++++++++---------------
 arch/i386/kernel/i8253.c      |    2 
 arch/i386/kernel/setup.c      |    1 
 arch/i386/kernel/time.c       |    1 
 arch/i386/kernel/tsc.c        |   83 ++++++++++++----------------
 arch/x86_64/kernel/hpet.c     |  122 +++++++++++++++++++-----------------------
 arch/x86_64/kernel/tsc.c      |    7 --
 drivers/clocksource/acpi_pm.c |    5 +
 drivers/clocksource/cyclone.c |    2 
 kernel/time/clocksource.c     |    8 +-
 10 files changed, 145 insertions(+), 166 deletions(-)

linux-2.6.21-rc1_adjust-clocksource-selection_C7.patch
============================================

arch/i386/kernel/hpet.c +37 -43
index e1006b7..f3ab61e 100644
@@ -201,12 +201,30 @@ static int hpet_next_event(unsigned long
 }
 
 /*
+ * Clock source related code
+ */
+static cycle_t read_hpet(void)
+{
+	return (cycle_t)hpet_readl(HPET_COUNTER);
+}
+
+static struct clocksource clocksource_hpet = {
+	.name		= "hpet",
+	.rating		= 250,
+	.read		= read_hpet,
+	.mask		= HPET_MASK,
+	.shift		= HPET_SHIFT,
+	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
+};
+
+/*
  * Try to setup the HPET timer
  */
 int __init hpet_enable(void)
 {
 	unsigned long id;
 	uint64_t hpet_freq;
+	u64 tmp;
 
 	if (!is_hpet_capable())
 		return 0;
@@ -253,6 +271,25 @@ #endif
 	/* Start the counter */
 	hpet_start_counter();
 
+	/* Initialize and register HPET clocksource
+	 *
+	 * hpet period is in femto seconds per cycle
+	 * so we need to convert this to ns/cyc units
+	 * aproximated by mult/2^shift
+	 *
+	 *  fsec/cyc * 1nsec/1000000fsec = nsec/cyc = mult/2^shift
+	 *  fsec/cyc * 1ns/1000000fsec * 2^shift = mult
+	 *  fsec/cyc * 2^shift * 1nsec/1000000fsec = mult
+	 *  (fsec/cyc << shift)/1000000 = mult
+	 *  (hpet_period << shift)/FSEC_PER_NSEC = mult
+	 */
+	tmp = (u64)hpet_period << HPET_SHIFT;
+	do_div(tmp, FSEC_PER_NSEC);
+	clocksource_hpet.mult = (u32)tmp;
+
+	clocksource_register(&clocksource_hpet);
+
+
 	if (id & HPET_ID_LEGSUP) {
 		hpet_enable_int();
 		hpet_reserve_platform_timers(id);
@@ -273,49 +310,6 @@ out_nohpet:
 	return 0;
 }
 
-/*
- * Clock source related code
- */
-static cycle_t read_hpet(void)
-{
-	return (cycle_t)hpet_readl(HPET_COUNTER);
-}
-
-static struct clocksource clocksource_hpet = {
-	.name		= "hpet",
-	.rating		= 250,
-	.read		= read_hpet,
-	.mask		= HPET_MASK,
-	.shift		= HPET_SHIFT,
-	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
-};
-
-static int __init init_hpet_clocksource(void)
-{
-	u64 tmp;
-
-	if (!hpet_virt_address)
-		return -ENODEV;
-
-	/*
-	 * hpet period is in femto seconds per cycle
-	 * so we need to convert this to ns/cyc units
-	 * aproximated by mult/2^shift
-	 *
-	 *  fsec/cyc * 1nsec/1000000fsec = nsec/cyc = mult/2^shift
-	 *  fsec/cyc * 1ns/1000000fsec * 2^shift = mult
-	 *  fsec/cyc * 2^shift * 1nsec/1000000fsec = mult
-	 *  (fsec/cyc << shift)/1000000 = mult
-	 *  (hpet_period << shift)/FSEC_PER_NSEC = mult
-	 */
-	tmp = (u64)hpet_period << HPET_SHIFT;
-	do_div(tmp, FSEC_PER_NSEC);
-	clocksource_hpet.mult = (u32)tmp;
-
-	return clocksource_register(&clocksource_hpet);
-}
-
-module_init(init_hpet_clocksource);
 
 #ifdef CONFIG_HPET_EMULATE_RTC
 
arch/i386/kernel/i8253.c +1 -1
index a6bc7bb..5cbb776 100644
@@ -195,4 +195,4 @@ static int __init init_pit_clocksource(v
 	clocksource_pit.mult = clocksource_hz2mult(CLOCK_TICK_RATE, 20);
 	return clocksource_register(&clocksource_pit);
 }
-module_init(init_pit_clocksource);
+arch_initcall(init_pit_clocksource);
arch/i386/kernel/setup.c +0 -1
index 122623d..698c24f 100644
@@ -657,5 +657,4 @@ #elif defined(CONFIG_DUMMY_CONSOLE)
 	conswitchp = &dummy_con;
 #endif
 #endif
-	tsc_init();
 }
arch/i386/kernel/time.c +1 -0
index a535005..5be29fb 100644
@@ -271,5 +271,6 @@ static void __init hpet_time_init(void)
 
 void __init time_init(void)
 {
+	tsc_init();
 	late_time_init = hpet_time_init;
 }
arch/i386/kernel/tsc.c +37 -46

99 lines not on this page · raw message

arch/x86_64/kernel/hpet.c +57 -65

146 lines not on this page · raw message

arch/x86_64/kernel/tsc.c +2 -5
index 8958318..1a0edbb 100644
@@ -210,7 +210,7 @@ void mark_tsc_unstable(void)
 }
 EXPORT_SYMBOL_GPL(mark_tsc_unstable);
 
-static int __init init_tsc_clocksource(void)
+void __init init_tsc_clocksource(void)
 {
 	if (!notsc) {
 		clocksource_tsc.mult = clocksource_khz2mult(cpu_khz,
@@ -218,9 +218,6 @@ static int __init init_tsc_clocksource(v
 		if (check_tsc_unstable())
 			clocksource_tsc.rating = 0;
 
-		return clocksource_register(&clocksource_tsc);
+		clocksource_register(&clocksource_tsc);
 	}
-	return 0;
 }
-
-module_init(init_tsc_clocksource);
drivers/clocksource/acpi_pm.c +4 -1
index ccaa6a3..d42060e 100644
@@ -214,4 +214,7 @@ pm_good:
 	return clocksource_register(&clocksource_acpi_pm);
 }
 
-module_init(init_acpi_pm_clocksource);
+/* We use fs_initcall because we want the PCI fixups to have run
+ * but we still need to load before device_initcall
+ */
+fs_initcall(init_acpi_pm_clocksource);
drivers/clocksource/cyclone.c +1 -1
index 4f3925c..1bde303 100644
@@ -116,4 +116,4 @@ static int __init init_cyclone_clocksour
 	return clocksource_register(&clocksource_cyclone);
 }
 
-module_init(init_cyclone_clocksource);
+arch_initcall(init_cyclone_clocksource);
kernel/time/clocksource.c +5 -3
index 193a079..5b0e46b 100644
@@ -55,16 +55,18 @@ static DEFINE_SPINLOCK(clocksource_lock)
 static char override_name[32];
 static int finished_booting;
 
-/* clocksource_done_booting - Called near the end of bootup
+/* clocksource_done_booting - Called near the end of core bootup
  *
- * Hack to avoid lots of clocksource churn at boot time
+ * Hack to avoid lots of clocksource churn at boot time.
+ * We use fs_initcall because we want this to start before
+ * device_initcall but after subsys_initcall.
  */
 static int __init clocksource_done_booting(void)
 {
 	finished_booting = 1;
 	return 0;
 }
-late_initcall(clocksource_done_booting);
+fs_initcall(clocksource_done_booting);
 
 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
 static LIST_HEAD(watchdog_list);
#1 lore raw
Quoted 18 lines · On Thu, 22 Feb 2007 16:13:02 -0800 john stultz <[email protected]> wrote:

This patch resolves the issue found here:
http://bugme.osdl.org/show_bug.cgi?id=7426

The basic summary is:
Currently we register most of i386/x86_64 clocksources at module_init time. Then we enable clocksource selection at late_initcall time. This causes some problems for drivers that use gettimeofday for init calibration routines (specifically the es1968 driver in this case), where durring module_init, the only clocksource available is the low-res jiffies clocksource. This may cause slight calibration errors, due to the small sampling time used.

It should be noted that drivers that require fine grained time may not function on architectures that do not have better then jiffies resolution timekeeping (there are a few). However, this does not discount the reasonable need for such fine-grained timekeeping at init time.

Quoted 8 lines

Thus the solution here is to register clocksources earlier (ideally when the hardware is being initialized), and then we enable clocksource selection at fs_initcall (before device_initcall).

This patch should probably get some testing time in -mm, since clocksource selection is one of the most important issues for correct timekeeping, and I've only been able to test this on a few of my own boxes.

This doornails my Nocona box early in boot:
http://userweb.kernel.org/~akpm/s5000431.jpg

Slab isn't ready yet - time_init()->hpet_arch_init() is called before start_kernel() has run kmem_cache_init().

#2 lore raw
Quoted 10 lines · Andrew Morton
Quoted 8 lines · On Thu, 22 Feb 2007 16:13:02 -0800 john stultz <[email protected]> wrote:

Thus the solution here is to register clocksources earlier (ideally when the hardware is being initialized), and then we enable clocksource selection at fs_initcall (before device_initcall).

This patch should probably get some testing time in -mm, since clocksource selection is one of the most important issues for correct timekeeping, and I've only been able to test this on a few of my own boxes.

Quoted 5 lines

This doornails my Nocona box early in boot:
http://userweb.kernel.org/~akpm/s5000431.jpg

Slab isn't ready yet - time_init()->hpet_arch_init() is called before start_kernel() has run kmem_cache_init().

Oh! Sorry! Yea, looking at it more the ioremap isn't actually necessary, as we can use hpet_readl() instead of re-calculating the hpet base address pointer.

I'll fix this up (and find an HPET enabled x86_64 box to test it on) and get a patch to you shortly.

Sorry again!
-john

#3 lore raw

From: john stultz <[email protected]>
Date: Fri, 02 Mar 2007 11:58:11 -0800

Quoted 6 lines

Oh! Sorry! Yea, looking at it more the ioremap isn't actually necessary, as we can use hpet_readl() instead of re-calculating the hpet base address pointer.

I'll fix this up (and find an HPET enabled x86_64 box to test it on) and get a patch to you shortly.

Not to pressure you John but I'd really like to see this go in soon, it does fix real bugs such as the Radeon FB issue I pointed out the other week.

Once this is in I can integrate sparc64 clocksource/clockevent support.

Thanks!

#4 lore raw

PATCH 1/1 clocksource init adjustments (fix bug #7426)

Quoted 7 lines · David Miller

From: john stultz <[email protected]>
Date: Fri, 02 Mar 2007 11:58:11 -0800

Quoted 4 lines

Oh! Sorry! Yea, looking at it more the ioremap isn't actually necessary, as we can use hpet_readl() instead of re-calculating the hpet base address pointer.

Quoted 6 lines
Quoted 2 lines

I'll fix this up (and find an HPET enabled x86_64 box to test it on) and get a patch to you shortly.

Not to pressure you John but I'd really like to see this go in soon, it does fix real bugs such as the Radeon FB issue I pointed out the other week.

Here's my second try at this. This time I caught three bugs from the last patch (all in x86_64):
1) Calling ioremap too early in the HPET code (which is unnecessary)
2) hpet_period local variable aliasing
3) forgot to re-add call to init_tsc_clocksource()

I boot tested on two x86_64 boxes (one ACPI PM and the other HPET). However it probably should still go through a bit of testing in -mm to make sure all the quirks are shaken out.

thanks
-john

This patch resolves the issue found here:
http://bugme.osdl.org/show_bug.cgi?id=7426

The basic summary is:
Currently we register most of i386/x86_64 clocksources at module_init time. Then we enable clocksource selection at late_initcall time. This causes some problems for drivers that use gettimeofday for init calibration routines (specifically the es1968 driver in this case), where durring module_init, the only clocksource available is the low-res jiffies clocksource. This may cause slight calibration errors, due to the small sampling time used.

It should be noted that drivers that require fine grained time may not function on architectures that do not have better then jiffies resolution timekeeping (there are a few). However, this does not discount the reasonable need for such fine-grained timekeeping at init time.

Thus the solution here is to register clocksources earlier (ideally when the hardware is being initialized), and then we enable clocksource selection at fs_initcall (before device_initcall).

This patch should probably get some testing time in -mm, since clocksource selection is one of the most important issues for correct timekeeping, and I've only been able to test this on a few of my own boxes.

 arch/i386/kernel/hpet.c       |   80 ++++++++++++++----------------
 arch/i386/kernel/i8253.c      |    2 
 arch/i386/kernel/setup.c      |    1 
 arch/i386/kernel/time.c       |    1 
 arch/i386/kernel/tsc.c        |   83 ++++++++++++++-----------------
 arch/x86_64/kernel/hpet.c     |  109 ++++++++++++++++--------------------------
 arch/x86_64/kernel/time.c     |    2 
 arch/x86_64/kernel/tsc.c      |    7 --
 drivers/clocksource/acpi_pm.c |    5 +
 drivers/clocksource/cyclone.c |    2 
 include/asm-x86_64/tsc.h      |    1 
 kernel/time/clocksource.c     |    8 +--
 12 files changed, 135 insertions(+), 166 deletions(-)

============================================

arch/i386/kernel/hpet.c +37 -43
index e1006b7..f3ab61e 100644
@@ -201,12 +201,30 @@ static int hpet_next_event(unsigned long
 }
 
 /*
+ * Clock source related code
+ */
+static cycle_t read_hpet(void)
+{
+	return (cycle_t)hpet_readl(HPET_COUNTER);
+}
+
+static struct clocksource clocksource_hpet = {
+	.name		= "hpet",
+	.rating		= 250,
+	.read		= read_hpet,
+	.mask		= HPET_MASK,
+	.shift		= HPET_SHIFT,
+	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
+};
+
+/*
  * Try to setup the HPET timer
  */
 int __init hpet_enable(void)
 {
 	unsigned long id;
 	uint64_t hpet_freq;
+	u64 tmp;
 
 	if (!is_hpet_capable())
 		return 0;
@@ -253,6 +271,25 @@ #endif
 	/* Start the counter */
 	hpet_start_counter();
 
+	/* Initialize and register HPET clocksource
+	 *
+	 * hpet period is in femto seconds per cycle
+	 * so we need to convert this to ns/cyc units
+	 * aproximated by mult/2^shift
+	 *
+	 *  fsec/cyc * 1nsec/1000000fsec = nsec/cyc = mult/2^shift
+	 *  fsec/cyc * 1ns/1000000fsec * 2^shift = mult
+	 *  fsec/cyc * 2^shift * 1nsec/1000000fsec = mult
+	 *  (fsec/cyc << shift)/1000000 = mult
+	 *  (hpet_period << shift)/FSEC_PER_NSEC = mult
+	 */
+	tmp = (u64)hpet_period << HPET_SHIFT;
+	do_div(tmp, FSEC_PER_NSEC);
+	clocksource_hpet.mult = (u32)tmp;
+
+	clocksource_register(&clocksource_hpet);
+
+
 	if (id & HPET_ID_LEGSUP) {
 		hpet_enable_int();
 		hpet_reserve_platform_timers(id);
@@ -273,49 +310,6 @@ out_nohpet:
 	return 0;
 }
 
-/*
- * Clock source related code
- */
-static cycle_t read_hpet(void)
-{
-	return (cycle_t)hpet_readl(HPET_COUNTER);
-}
-
-static struct clocksource clocksource_hpet = {
-	.name		= "hpet",
-	.rating		= 250,
-	.read		= read_hpet,
-	.mask		= HPET_MASK,
-	.shift		= HPET_SHIFT,
-	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
-};
-
-static int __init init_hpet_clocksource(void)
-{
-	u64 tmp;
-
-	if (!hpet_virt_address)
-		return -ENODEV;
-
-	/*
-	 * hpet period is in femto seconds per cycle
-	 * so we need to convert this to ns/cyc units
-	 * aproximated by mult/2^shift
-	 *
-	 *  fsec/cyc * 1nsec/1000000fsec = nsec/cyc = mult/2^shift
-	 *  fsec/cyc * 1ns/1000000fsec * 2^shift = mult
-	 *  fsec/cyc * 2^shift * 1nsec/1000000fsec = mult
-	 *  (fsec/cyc << shift)/1000000 = mult
-	 *  (hpet_period << shift)/FSEC_PER_NSEC = mult
-	 */
-	tmp = (u64)hpet_period << HPET_SHIFT;
-	do_div(tmp, FSEC_PER_NSEC);
-	clocksource_hpet.mult = (u32)tmp;
-
-	return clocksource_register(&clocksource_hpet);
-}
-
-module_init(init_hpet_clocksource);
 
 #ifdef CONFIG_HPET_EMULATE_RTC
 
arch/i386/kernel/i8253.c +1 -1
index a6bc7bb..5cbb776 100644
@@ -195,4 +195,4 @@ static int __init init_pit_clocksource(v
 	clocksource_pit.mult = clocksource_hz2mult(CLOCK_TICK_RATE, 20);
 	return clocksource_register(&clocksource_pit);
 }
-module_init(init_pit_clocksource);
+arch_initcall(init_pit_clocksource);
arch/i386/kernel/setup.c +0 -1
index 122623d..698c24f 100644
@@ -657,5 +657,4 @@ #elif defined(CONFIG_DUMMY_CONSOLE)
 	conswitchp = &dummy_con;
 #endif
 #endif
-	tsc_init();
 }
arch/i386/kernel/time.c +1 -0
index a535005..5be29fb 100644
@@ -271,5 +271,6 @@ static void __init hpet_time_init(void)
 
 void __init time_init(void)
 {
+	tsc_init();
 	late_time_init = hpet_time_init;
 }
arch/i386/kernel/tsc.c +37 -46

99 lines not on this page · raw message

arch/x86_64/kernel/hpet.c +44 -65

133 lines not on this page · raw message

arch/x86_64/kernel/time.c +2 -0
index c9addcf..75d73a9 100644
@@ -358,6 +358,8 @@ void __init time_init(void)
 	set_cyc2ns_scale(cpu_khz);
 	printk(KERN_INFO "time.c: Detected %d.%03d MHz processor.\n",
 		cpu_khz / 1000, cpu_khz % 1000);
+	init_tsc_clocksource();
+
 	setup_irq(0, &irq0);
 }
 
arch/x86_64/kernel/tsc.c +2 -5
index 8958318..1a0edbb 100644
@@ -210,7 +210,7 @@ void mark_tsc_unstable(void)
 }
 EXPORT_SYMBOL_GPL(mark_tsc_unstable);
 
-static int __init init_tsc_clocksource(void)
+void __init init_tsc_clocksource(void)
 {
 	if (!notsc) {
 		clocksource_tsc.mult = clocksource_khz2mult(cpu_khz,
@@ -218,9 +218,6 @@ static int __init init_tsc_clocksource(v
 		if (check_tsc_unstable())
 			clocksource_tsc.rating = 0;
 
-		return clocksource_register(&clocksource_tsc);
+		clocksource_register(&clocksource_tsc);
 	}
-	return 0;
 }
-
-module_init(init_tsc_clocksource);
drivers/clocksource/acpi_pm.c +4 -1
index ccaa6a3..d42060e 100644
@@ -214,4 +214,7 @@ pm_good:
 	return clocksource_register(&clocksource_acpi_pm);
 }
 
-module_init(init_acpi_pm_clocksource);
+/* We use fs_initcall because we want the PCI fixups to have run
+ * but we still need to load before device_initcall
+ */
+fs_initcall(init_acpi_pm_clocksource);
drivers/clocksource/cyclone.c +1 -1
index 4f3925c..1bde303 100644
@@ -116,4 +116,4 @@ static int __init init_cyclone_clocksour
 	return clocksource_register(&clocksource_cyclone);
 }
 
-module_init(init_cyclone_clocksource);
+arch_initcall(init_cyclone_clocksource);
include/asm-x86_64/tsc.h +1 -0
index 9a0a368..26c3e98 100644
@@ -55,6 +55,7 @@ #endif
 extern void tsc_init(void);
 extern void mark_tsc_unstable(void);
 extern int unsynchronized_tsc(void);
+extern void init_tsc_clocksource(void);
 
 /*
  * Boot-time check whether the TSCs are synchronized across
kernel/time/clocksource.c +5 -3
index 193a079..5b0e46b 100644
@@ -55,16 +55,18 @@ static DEFINE_SPINLOCK(clocksource_lock)
 static char override_name[32];
 static int finished_booting;
 
-/* clocksource_done_booting - Called near the end of bootup
+/* clocksource_done_booting - Called near the end of core bootup
  *
- * Hack to avoid lots of clocksource churn at boot time
+ * Hack to avoid lots of clocksource churn at boot time.
+ * We use fs_initcall because we want this to start before
+ * device_initcall but after subsys_initcall.
  */
 static int __init clocksource_done_booting(void)
 {
 	finished_booting = 1;
 	return 0;
 }
-late_initcall(clocksource_done_booting);
+fs_initcall(clocksource_done_booting);
 
 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
 static LIST_HEAD(watchdog_list);
#5 lore raw

From: john stultz <[email protected]>
Date: Fri, 02 Mar 2007 15:24:00 -0800

Quoted 21 lines · David Miller
Quoted 7 lines

From: john stultz <[email protected]>
Date: Fri, 02 Mar 2007 11:58:11 -0800

Quoted 4 lines

Oh! Sorry! Yea, looking at it more the ioremap isn't actually necessary, as we can use hpet_readl() instead of re-calculating the hpet base address pointer.

Quoted 6 lines
Quoted 2 lines

I'll fix this up (and find an HPET enabled x86_64 box to test it on) and get a patch to you shortly.

Not to pressure you John but I'd really like to see this go in soon, it does fix real bugs such as the Radeon FB issue I pointed out the other week.

Here's my second try at this. This time I caught three bugs from the last patch (all in x86_64):
1) Calling ioremap too early in the HPET code (which is unnecessary)
2) hpet_period local variable aliasing
3) forgot to re-add call to init_tsc_clocksource()

Quoted 3 lines

I boot tested on two x86_64 boxes (one ACPI PM and the other HPET). However it probably should still go through a bit of testing in -mm to make sure all the quirks are shaken out.

Agreed and it looks good to me after going over it a few times.

#6 lore raw
In reply to john stultz
Quoted 4 lines · john stultz

Thus the solution here is to register clocksources earlier (ideally when the hardware is being initialized), and then we enable clocksource selection at fs_initcall (before device_initcall).

When I was doing this in my tree I found that registering clocksources earlier would always break dynamic tick .. If dynamic tick switches itself on during boot up then it usually hangs the system .. Did you test this patch with that option enabled? (I haven't had a chance too..)

Daniel