Skip to contentSkip to conversation

clocksource/timekeeping cleanup

PATCH In review Daniel Walker 3 participants 16 messages Jan 23, 2007

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

cover clocksource/timekeeping cleanup

This patchset represents the most stable clocksource changes in my tree. Also John (and others) have reviewed these changes a few times. I think it's all acceptable.

Daniel

#1 lore raw

PATCH 1/12 timekeeping: create kernel/time/timekeeping.c

Move the generic timekeeping code from kernel/timer.c to kernel/time/timekeeping.c . This requires some glue code which is added to the include/linux/timekeeping.h header.

---
 include/linux/clocksource.h |    3 
 include/linux/timekeeping.h |   19 +
 kernel/time/Makefile        |    2 
 kernel/time/clocksource.c   |    3 
 kernel/time/timekeeping.c   |  639 ++++++++++++++++++++++++++++++++++++++++++++
 kernel/timer.c              |  630 -------------------------------------------
 6 files changed, 663 insertions(+), 633 deletions(-)
include/linux/clocksource.h +3 -0
Index: linux-2.6.19/include/linux/clocksource.h
===================================================================
@@ -18,6 +18,9 @@
 /* clocksource cycle base type */
 typedef u64 cycle_t;
 
+/* XXX - Would like a better way for initializing curr_clocksource */
+extern struct clocksource clocksource_jiffies;
+
 /**
  * struct clocksource - hardware abstraction for a free running counter
  *	Provides mostly state-free accessors to the underlying hardware.
include/linux/timekeeping.h new file +19 -0
Index: linux-2.6.19/include/linux/timekeeping.h
===================================================================
@@ -0,0 +1,19 @@
+#ifndef _LINUX_TIMEKEEPING_H
+#define _LINUX_TIMEKEEPING_H
+
+#include <linux/clocksource.h>
+
+extern void update_wall_time(void);
+
+#ifdef CONFIG_GENERIC_TIME
+
+extern struct clocksource *clock;
+
+#else /* CONFIG_GENERIC_TIME */
+static inline int change_clocksource(void)
+{
+	return 0;
+}
+#endif /* !CONFIG_GENERIC_TIME */
+
+#endif /* _LINUX_TIMEKEEPING_H */
kernel/time/Makefile +1 -1
Index: linux-2.6.19/kernel/time/Makefile
===================================================================
@@ -1,4 +1,4 @@
-obj-y += ntp.o clocksource.o jiffies.o timer_list.o
+obj-y += ntp.o clocksource.o jiffies.o timer_list.o timekeeping.o
 
 obj-$(CONFIG_GENERIC_CLOCKEVENTS)	+= clockevents.o
 obj-$(CONFIG_TIMER_STATS)		+= timer_stats.o
kernel/time/clocksource.c +0 -3
Index: linux-2.6.19/kernel/time/clocksource.c
===================================================================
@@ -29,9 +29,6 @@
 #include <linux/init.h>
 #include <linux/module.h>
 
-/* XXX - Would like a better way for initializing curr_clocksource */
-extern struct clocksource clocksource_jiffies;
-
 /*[Clocksource internal variables]---------
  * curr_clocksource:
  *	currently selected clocksource. Initialized to clocksource_jiffies.
kernel/time/timekeeping.c new file +639 -0

639 lines not on this page · raw message

kernel/timer.c +1 -629

648 lines not on this page · raw message

#2 lore raw

PATCH 2/12 clocksource: rating sorted list

In reply to Daniel Walker

Converts the original plain list into a sorted list based on the clock rating. Later in my tree this allows some of the variables to be dropped since the highest rated clock is always at the front of the list. This also does some other nice things like allow the sysfs files to print the clocks in a more interesting order. It's forward looking.

---
 arch/i386/kernel/tsc.c      |    2 
 arch/x86_64/kernel/tsc.c    |    2 
 include/linux/clocksource.h |    9 ++-
 kernel/time/clocksource.c   |  132 +++++++++++++++++++++++++++++---------------
 4 files changed, 97 insertions(+), 48 deletions(-)
arch/i386/kernel/tsc.c +1 -1
Index: linux-2.6.19/arch/i386/kernel/tsc.c
===================================================================
@@ -361,7 +361,7 @@ static int tsc_update_callback(void)
 	/* check to see if we should switch to the safe clocksource: */
 	if (clocksource_tsc.rating != 0 && check_tsc_unstable()) {
 		clocksource_tsc.rating = 0;
-		clocksource_reselect();
+		clocksource_rating_change(&clocksource_tsc);
 		change = 1;
 	}
 
arch/x86_64/kernel/tsc.c +1 -1
Index: linux-2.6.19/arch/x86_64/kernel/tsc.c
===================================================================
@@ -218,7 +218,7 @@ static int tsc_update_callback(void)
 	/* check to see if we should switch to the safe clocksource: */
 	if (clocksource_tsc.rating != 50 && check_tsc_unstable()) {
 		clocksource_tsc.rating = 50;
-		clocksource_reselect();
+		clocksource_rating_change(&clocksource_tsc);
 		change = 1;
 	}
 	return change;
include/linux/clocksource.h +6 -3
Index: linux-2.6.19/include/linux/clocksource.h
===================================================================
@@ -12,6 +12,9 @@
 #include <linux/timex.h>
 #include <linux/time.h>
 #include <linux/list.h>
+#include <linux/plist.h>
+#include <linux/sysdev.h>
+
 #include <asm/div64.h>
 #include <asm/io.h>
 
@@ -183,9 +186,9 @@ static inline void clocksource_calculate
 
 
 /* used to install a new clocksource */
-int clocksource_register(struct clocksource*);
-void clocksource_reselect(void);
-struct clocksource* clocksource_get_next(void);
+extern struct clocksource *clocksource_get_next(void);
+extern int clocksource_register(struct clocksource*);
+extern void clocksource_rating_change(struct clocksource*);
 
 #ifdef CONFIG_GENERIC_TIME_VSYSCALL
 extern void update_vsyscall(struct timespec *ts, struct clocksource *c);
kernel/time/clocksource.c +89 -43

207 lines not on this page · raw message

#3 lore raw

PATCH 3/12 clocksource: arm initialize list value

In reply to Daniel Walker

Update arch/arm/ with list initialization.

---
 arch/arm/mach-imx/time.c      |    1 +
 arch/arm/mach-ixp4xx/common.c |    1 +
 arch/arm/mach-netx/time.c     |    1 +
 arch/arm/mach-pxa/time.c      |    1 +
 4 files changed, 4 insertions(+)
arch/arm/mach-imx/time.c +1 -0
Index: linux-2.6.19/arch/arm/mach-imx/time.c
===================================================================
@@ -87,6 +87,7 @@ static struct clocksource clocksource_im
 	.read		= imx_get_cycles,
 	.mask		= 0xFFFFFFFF,
 	.shift 		= 20,
+	.list		= LIST_HEAD_INIT(clocksource_imx.list),
 	.is_continuous 	= 1,
 };
 
arch/arm/mach-ixp4xx/common.c +1 -0
Index: linux-2.6.19/arch/arm/mach-ixp4xx/common.c
===================================================================
@@ -396,6 +396,7 @@ static struct clocksource clocksource_ix
 	.mask		= CLOCKSOURCE_MASK(32),
 	.shift 		= 20,
 	.is_continuous 	= 1,
+	.list		= LIST_HEAD_INIT(clocksource_ixp4xx.list),
 };
 
 unsigned long ixp4xx_timer_freq = FREQ;
arch/arm/mach-netx/time.c +1 -0
Index: linux-2.6.19/arch/arm/mach-netx/time.c
===================================================================
@@ -62,6 +62,7 @@ static struct clocksource clocksource_ne
 	.read		= netx_get_cycles,
 	.mask		= CLOCKSOURCE_MASK(32),
 	.shift 		= 20,
+	.list		= LIST_HEAD_INIT(clocksource_netx.list),
 	.is_continuous 	= 1,
 };
 
arch/arm/mach-pxa/time.c +1 -0
Index: linux-2.6.19/arch/arm/mach-pxa/time.c
===================================================================
@@ -112,6 +112,7 @@ static struct clocksource clocksource_px
 	.read           = pxa_get_cycles,
 	.mask           = CLOCKSOURCE_MASK(32),
 	.shift          = 20,
+	.list		= LIST_HEAD_INIT(clocksource_pxa.list),
 	.is_continuous  = 1,
 };
 
#4 lore raw

PATCH 4/12 clocksource: avr32 initialize list value

In reply to Daniel Walker

Update arch/avre32/ with list initialization.

---
 arch/avr32/kernel/time.c |    1 +
 1 file changed, 1 insertion(+)
arch/avr32/kernel/time.c +1 -0
Index: linux-2.6.19/arch/avr32/kernel/time.c
===================================================================
@@ -37,6 +37,7 @@ static struct clocksource clocksource_av
 	.read		= read_cycle_count,
 	.mask		= CLOCKSOURCE_MASK(32),
 	.shift		= 16,
+	.list		= LIST_HEAD_INIT(clocksource_avr32.list),
 	.is_continuous	= 1,
 };
 
#5 lore raw

Re: [PATCH 04/12] clocksource: avr32 initialize list value

Quoted 3 lines · Daniel Walker

Update arch/avre32/ with list initialization.

Looks good to me, although I suppose it wouldn't hurt to include <linux/list.h> as well since there seems to be an ongoing effort to reduce the number of files that are included implicitly through other headers. Might save us from some trouble later.

I've closed your support request, so you shouldn't expect a reply from [email protected] (I was going to reply through the support system, but I couldn't figure out how to prevent it from top-quoting.) Probably best if you send patches directly to me at [email protected], but the support address is a nice fallback if I'm unable to respond for some reason.

Haavard

#6 lore raw

Re: [PATCH 04/12] clocksource: avr32 initialize list value

Quoted 9 lines · Haavard Skinnemoen
Quoted 3 lines · Daniel Walker

Update arch/avre32/ with list initialization.

Looks good to me, although I suppose it wouldn't hurt to include <linux/list.h> as well since there seems to be an ongoing effort to reduce the number of files that are included implicitly through other headers. Might save us from some trouble later.

It shouldn't be outright implicit since it's included in clocksource.h . That has to be included before you can create a clocksource. I could see adding list.h if it wasn't clear which include was bringing it in.

Quoted 6 lines

I've closed your support request, so you shouldn't expect a reply from [email protected] (I was going to reply through the support system, but I couldn't figure out how to prevent it from top-quoting.) Probably best if you send patches directly to me at [email protected], but the support address is a nice fallback if I'm unable to respond for some reason.

I got that address from the maintainers file,

AVR32 ARCHITECTURE

P:      Atmel AVR32 Support Team
M:      [email protected]
P:      Haavard Skinnemoen
M:      [email protected]
W:      http://www.atmel.com/products/AVR32/
W:      http://avr32linux.org/
W:      http://avrfreaks.net/
S:      Supported

I just picked the top address .. If it makes sense you might want to consider dropping that address from the maintainers file. It seems a little formal for submitting patches. In the future I'll send them to you.

Daniel

#7 lore raw

Re: [PATCH 04/12] clocksource: avr32 initialize list value

Quoted 6 lines · On Tue, 23 Jan 2007 05:02:49 -0800 Daniel Walker <[email protected]> wrote:
Quoted 5 lines · Haavard Skinnemoen
Quoted 3 lines · Daniel Walker

Update arch/avre32/ with list initialization.

Quoted 8 lines
Quoted 4 lines

Looks good to me, although I suppose it wouldn't hurt to include <linux/list.h> as well since there seems to be an ongoing effort to reduce the number of files that are included implicitly through other headers. Might save us from some trouble later.

It shouldn't be outright implicit since it's included in clocksource.h . That has to be included before you can create a clocksource. I could see adding list.h if it wasn't clear which include was bringing it in.

Ah, of course. Just disregard that suggestion, then.

Quoted 19 lines
Quoted 6 lines

I've closed your support request, so you shouldn't expect a reply from [email protected] (I was going to reply through the support system, but I couldn't figure out how to prevent it from top-quoting.) Probably best if you send patches directly to me at [email protected], but the support address is a nice fallback if I'm unable to respond for some reason.

I got that address from the maintainers file,

AVR32 ARCHITECTURE

P:      Atmel AVR32 Support Team
M:      [email protected]
P:      Haavard Skinnemoen
M:      [email protected]
W:      http://www.atmel.com/products/AVR32/
W:      http://avr32linux.org/
W:      http://avrfreaks.net/
S:      Supported
Quoted 4 lines

I just picked the top address .. If it makes sense you might want to consider dropping that address from the maintainers file. It seems a little formal for submitting patches. In the future I'll send them to you.

Right. I guess we should at least switch the order so that I'm the first one on the list. I'll have a chat with the support guys, maybe we should just drop that address from MAINTAINERS.

Håvard

#8 lore raw

PATCH 5/12 clocksource: mips initialize list value

In reply to Daniel Walker

Update arch/mips/ with list initialization.

---
 arch/mips/kernel/time.c |    1 +
 1 file changed, 1 insertion(+)
arch/mips/kernel/time.c +1 -0
Index: linux-2.6.19/arch/mips/kernel/time.c
===================================================================
@@ -307,6 +307,7 @@ static unsigned int __init calibrate_hpt
 struct clocksource clocksource_mips = {
 	.name		= "MIPS",
 	.mask		= 0xffffffff,
+	.list		= LIST_HEAD_INIT(clocksource_mips.list),
 	.is_continuous	= 1,
 };
 
#9 lore raw

PATCH 6/12 clocksource: i386 initialize list value

In reply to Daniel Walker

Update arch/i386/ with list initialization.

---
 arch/i386/kernel/hpet.c  |    1 +
 arch/i386/kernel/i8253.c |    1 +
 arch/i386/kernel/tsc.c   |    1 +
 3 files changed, 3 insertions(+)
arch/i386/kernel/hpet.c +1 -0
Index: linux-2.6.19/arch/i386/kernel/hpet.c
===================================================================
@@ -282,6 +282,7 @@ static struct clocksource clocksource_hp
 	.mask		= HPET_MASK,
 	.shift		= HPET_SHIFT,
 	.is_continuous	= 1,
+	.list		= LIST_HEAD_INIT(clocksource_hpet.list),
 };
 
 static int __init init_hpet_clocksource(void)
arch/i386/kernel/i8253.c +1 -0
Index: linux-2.6.19/arch/i386/kernel/i8253.c
===================================================================
@@ -177,6 +177,7 @@ static struct clocksource clocksource_pi
 	.mask	= CLOCKSOURCE_MASK(32),
 	.mult	= 0,
 	.shift	= 20,
+	.list	= LIST_HEAD_INIT(clocksource_pit.list),
 };
 
 static int __init init_pit_clocksource(void)
arch/i386/kernel/tsc.c +1 -0
Index: linux-2.6.19/arch/i386/kernel/tsc.c
===================================================================
@@ -352,6 +352,7 @@ static struct clocksource clocksource_ts
 	.shift			= 22,
 	.update_callback	= tsc_update_callback,
 	.is_continuous		= 1,
+	.list			= LIST_HEAD_INIT(clocksource_tsc.list),
 };
 
 static int tsc_update_callback(void)
#10 lore raw

PATCH 7/12 clocksource: x86_64 initialize list value

In reply to Daniel Walker

Update arch/x86_64/ with list initialization.

---
 arch/x86_64/kernel/hpet.c |    1 +
 arch/x86_64/kernel/tsc.c  |    1 +
 2 files changed, 2 insertions(+)
arch/x86_64/kernel/hpet.c +1 -0
Index: linux-2.6.19/arch/x86_64/kernel/hpet.c
===================================================================
@@ -472,6 +472,7 @@ struct clocksource clocksource_hpet = {
 	.shift		= HPET_SHIFT,
 	.is_continuous	= 1,
 	.vread		= vread_hpet,
+	.list		= LIST_HEAD_INIT(clocksource_hpet.list),
 };
 
 static int __init init_hpet_clocksource(void)
arch/x86_64/kernel/tsc.c +1 -0
Index: linux-2.6.19/arch/x86_64/kernel/tsc.c
===================================================================
@@ -209,6 +209,7 @@ static struct clocksource clocksource_ts
 	.update_callback	= tsc_update_callback,
 	.is_continuous		= 1,
 	.vread			= vread_tsc,
+	.list			= LIST_HEAD_INIT(clocksource_tsc.list),
 };
 
 static int tsc_update_callback(void)
#11 lore raw

PATCH 8/12 clocksource: driver initialize list value

In reply to Daniel Walker

Update drivers/clocksource/ with list initialization.

---
 drivers/clocksource/acpi_pm.c    |    1 +
 drivers/clocksource/cyclone.c    |    1 +
 drivers/clocksource/scx200_hrt.c |    1 +
 3 files changed, 3 insertions(+)
drivers/clocksource/acpi_pm.c +1 -0
Index: linux-2.6.19/drivers/clocksource/acpi_pm.c
===================================================================
@@ -74,6 +74,7 @@ static struct clocksource clocksource_ac
 	.mult		= 0, /*to be caluclated*/
 	.shift		= 22,
 	.is_continuous	= 1,
+	.list		= LIST_HEAD_INIT(clocksource_acpi_pm.list),
 };
 
 
drivers/clocksource/cyclone.c +1 -0
Index: linux-2.6.19/drivers/clocksource/cyclone.c
===================================================================
@@ -32,6 +32,7 @@ static struct clocksource clocksource_cy
 	.mult		= 10,
 	.shift		= 0,
 	.is_continuous	= 1,
+	.list		= LIST_HEAD_INIT(clocksource_cyclone.list),
 };
 
 static int __init init_cyclone_clocksource(void)
drivers/clocksource/scx200_hrt.c +1 -0
Index: linux-2.6.19/drivers/clocksource/scx200_hrt.c
===================================================================
@@ -58,6 +58,7 @@ static struct clocksource cs_hrt = {
 	.read		= read_hrt,
 	.mask		= CLOCKSOURCE_MASK(32),
 	.is_continuous	= 1,
+	.list		= LIST_HEAD_INIT(cs_hrt.list),
 	/* mult, shift are set based on mhz27 flag */
 };
 
#12 lore raw

PATCH 9/12 clocksource: initialize list value

In reply to Daniel Walker

A change to clocksource initialization. If the list field is initialized it allows clocksource_register to complete faster since it doesn't have to scan the list of clocks doing strcmp on each looking for duplicates.

---
 kernel/time/clocksource.c |    3 +--
 kernel/time/jiffies.c     |    1 +
 2 files changed, 2 insertions(+), 2 deletions(-)
kernel/time/clocksource.c +1 -2
Index: linux-2.6.19/kernel/time/clocksource.c
===================================================================
@@ -186,12 +186,11 @@ int clocksource_register(struct clocksou
 	unsigned long flags;
 
 	spin_lock_irqsave(&clocksource_lock, flags);
-	if (unlikely(!list_empty(&c->list) && __is_registered(c->name))) {
+	if (unlikely(!list_empty(&c->list))) {
 		printk("register_clocksource: Cannot register %s clocksource. "
 		       "Already registered!", c->name);
 		ret = -EBUSY;
 	} else {
-		INIT_LIST_HEAD(&c->list);
  		__sorted_list_add(c);
 		/* scan the registered clocksources, and pick the best one */
 		next_clocksource = select_clocksource();
kernel/time/jiffies.c +1 -0
Index: linux-2.6.19/kernel/time/jiffies.c
===================================================================
@@ -63,6 +63,7 @@ struct clocksource clocksource_jiffies =
 	.mult		= NSEC_PER_JIFFY << JIFFIES_SHIFT, /* details above */
 	.shift		= JIFFIES_SHIFT,
 	.is_continuous	= 0, /* tick based, not free running */
+	.list		= LIST_HEAD_INIT(clocksource_jiffies.list),
 };
 
 static int __init init_jiffies_clocksource(void)
#13 lore raw

PATCH 10/12 clocksource: add block notifier

In reply to Daniel Walker

Adds a call back interface for register/rating change events. This is also used later in this series to signal other interesting events.

---
 include/linux/clocksource.h |   37 +++++++++++++++++++++++++++++++++++++
 include/linux/timekeeping.h |    3 +++
 kernel/time/clocksource.c   |   10 ++++++++++
 3 files changed, 50 insertions(+)
include/linux/clocksource.h +37 -0
Index: linux-2.6.19/include/linux/clocksource.h
===================================================================
@@ -14,6 +14,7 @@
 #include <linux/list.h>
 #include <linux/plist.h>
 #include <linux/sysdev.h>
+#include <linux/notifier.h>
 
 #include <asm/div64.h>
 #include <asm/io.h>
@@ -24,6 +25,42 @@ typedef u64 cycle_t;
 /* XXX - Would like a better way for initializing curr_clocksource */
 extern struct clocksource clocksource_jiffies;
 
+/*
+ * Allows inlined calling for notifier routines.
+ */
+extern struct atomic_notifier_head clocksource_list_notifier;
+
+/*
+ * Block notifier flags.
+ */
+#define CLOCKSOURCE_NOTIFY_REGISTER	1
+#define CLOCKSOURCE_NOTIFY_RATING	2
+#define CLOCKSOURCE_NOTIFY_FREQ		4
+
+/**
+ * clocksource_notifier_register - Registers a list change notifier
+ * @nb:		pointer to a notifier block
+ *
+ * Returns zero always.
+ */
+static inline int clocksource_notifier_register(struct notifier_block *nb)
+{
+	return atomic_notifier_chain_register(&clocksource_list_notifier, nb);
+}
+
+/**
+ * clocksource_freq_change - Allows notification of dynamic frequency changes.
+ *
+ * Signals that a clocksource is dynamically changing it's frequency.
+ * This could happen if a clocksource becomes more/less stable.
+ */
+static inline void clocksource_freq_change(struct clocksource *c)
+{
+	atomic_notifier_call_chain(&clocksource_list_notifier,
+				   CLOCKSOURCE_NOTIFY_FREQ, c);
+}
+
+
 /**
  * struct clocksource - hardware abstraction for a free running counter
  *	Provides mostly state-free accessors to the underlying hardware.
include/linux/timekeeping.h +3 -0
Index: linux-2.6.19/include/linux/timekeeping.h
===================================================================
@@ -14,6 +14,9 @@ static inline int change_clocksource(voi
 {
 	return 0;
 }
+
+static inline void change_clocksource(void) { }
+
 #endif /* !CONFIG_GENERIC_TIME */
 
 #endif /* _LINUX_TIMEKEEPING_H */
kernel/time/clocksource.c +10 -0
Index: linux-2.6.19/kernel/time/clocksource.c
===================================================================
@@ -49,6 +49,8 @@ static DEFINE_SPINLOCK(clocksource_lock)
 static char override_name[32];
 static int finished_booting;
 
+ATOMIC_NOTIFIER_HEAD(clocksource_list_notifier);
+
 /* clocksource_done_booting - Called near the end of bootup
  *
  * Hack to avoid lots of clocksource churn at boot time
@@ -196,6 +198,10 @@ int clocksource_register(struct clocksou
 		next_clocksource = select_clocksource();
 	}
 	spin_unlock_irqrestore(&clocksource_lock, flags);
+
+	atomic_notifier_call_chain(&clocksource_list_notifier,
+ 				   CLOCKSOURCE_NOTIFY_REGISTER, c);
+
 	return ret;
 }
 EXPORT_SYMBOL(clocksource_register);
@@ -224,6 +230,10 @@ void clocksource_rating_change(struct cl
 
 	next_clocksource = select_clocksource();
 	spin_unlock_irqrestore(&clocksource_lock, flags);
+
+	atomic_notifier_call_chain(&clocksource_list_notifier,
+				   CLOCKSOURCE_NOTIFY_RATING, c);
+
 }
 EXPORT_SYMBOL(clocksource_rating_change);
 
#14 lore raw

PATCH 11/12 clocksource: remove update_callback

In reply to Daniel Walker

Uses the block notifier to replace the functionality of update_callback(). update_callback() was a special case specifically for the tsc, but including it in the clocksource structure duplicated it needlessly for other clocks.

---
 arch/i386/kernel/tsc.c      |   35 ++++++++++-------------------------
 arch/x86_64/kernel/tsc.c    |   19 +++++--------------
 include/linux/clocksource.h |    2 --
 include/linux/timekeeping.h |    1 +
 kernel/time/timekeeping.c   |   32 ++++++++++++++++++++++++++++++--
 5 files changed, 46 insertions(+), 43 deletions(-)
arch/i386/kernel/tsc.c +10 -25
Index: linux-2.6.19/arch/i386/kernel/tsc.c
===================================================================
@@ -51,8 +51,7 @@ static int __init tsc_setup(char *str)
 __setup("notsc", tsc_setup);
 
 /*
- * code to mark and check if the TSC is unstable
- * due to cpufreq or due to unsynced TSCs
+ * Flag that denotes an unstable tsc and check function.
  */
 static int tsc_unstable;
 
@@ -61,12 +60,6 @@ static inline int check_tsc_unstable(voi
 	return tsc_unstable;
 }
 
-void mark_tsc_unstable(void)
-{
-	tsc_unstable = 1;
-}
-EXPORT_SYMBOL_GPL(mark_tsc_unstable);
-
 /* Accellerators for sched_clock()
  * convert from cycles(64bits) => nanoseconds (64bits)
  *  basic equation:
@@ -180,6 +173,7 @@ int recalibrate_cpu_khz(void)
 	if (cpu_has_tsc) {
 		cpu_khz = calculate_cpu_khz();
 		tsc_khz = cpu_khz;
+		mark_tsc_unstable();
 		cpu_data[0].loops_per_jiffy =
 			cpufreq_scale(cpu_data[0].loops_per_jiffy,
 					cpu_khz_old, cpu_khz);
@@ -332,7 +326,6 @@ core_initcall(cpufreq_tsc);
 /* clock source code */
 
 static unsigned long current_tsc_khz = 0;
-static int tsc_update_callback(void);
 
 static cycle_t read_tsc(void)
 {
@@ -350,32 +343,24 @@ static struct clocksource clocksource_ts
 	.mask			= CLOCKSOURCE_MASK(64),
 	.mult			= 0, /* to be set */
 	.shift			= 22,
-	.update_callback	= tsc_update_callback,
 	.is_continuous		= 1,
 	.list			= LIST_HEAD_INIT(clocksource_tsc.list),
 };
 
-static int tsc_update_callback(void)
+/*
+ * code to mark if the TSC is unstable
+ * due to cpufreq or due to unsynced TSCs
+ */
+void mark_tsc_unstable(void)
 {
-	int change = 0;
-
 	/* check to see if we should switch to the safe clocksource: */
-	if (clocksource_tsc.rating != 0 && check_tsc_unstable()) {
+	if (unlikely(!tsc_unstable && clocksource_tsc.rating != 0)) {
 		clocksource_tsc.rating = 0;
 		clocksource_rating_change(&clocksource_tsc);
-		change = 1;
-	}
-
-	/* only update if tsc_khz has changed: */
-	if (current_tsc_khz != tsc_khz) {
-		current_tsc_khz = tsc_khz;
-		clocksource_tsc.mult = clocksource_khz2mult(current_tsc_khz,
-							clocksource_tsc.shift);
-		change = 1;
 	}
-
-	return change;
+	tsc_unstable = 1;
 }
+EXPORT_SYMBOL_GPL(mark_tsc_unstable);
 
 static int __init dmi_mark_tsc_unstable(struct dmi_system_id *d)
 {
arch/x86_64/kernel/tsc.c +5 -14
Index: linux-2.6.19/arch/x86_64/kernel/tsc.c
===================================================================
@@ -47,11 +47,6 @@ static inline int check_tsc_unstable(voi
 	return tsc_unstable;
 }
 
-void mark_tsc_unstable(void)
-{
-	tsc_unstable = 1;
-}
-EXPORT_SYMBOL_GPL(mark_tsc_unstable);
 
 #ifdef CONFIG_CPU_FREQ
 
@@ -185,8 +180,6 @@ __setup("notsc", notsc_setup);
 
 /* clock source code: */
 
-static int tsc_update_callback(void);
-
 static cycle_t read_tsc(void)
 {
 	cycle_t ret = (cycle_t)get_cycles_sync();
@@ -206,24 +199,22 @@ static struct clocksource clocksource_ts
 	.mask			= (cycle_t)-1,
 	.mult			= 0, /* to be set */
 	.shift			= 22,
-	.update_callback	= tsc_update_callback,
 	.is_continuous		= 1,
 	.vread			= vread_tsc,
 	.list			= LIST_HEAD_INIT(clocksource_tsc.list),
 };
 
-static int tsc_update_callback(void)
+void mark_tsc_unstable(void)
 {
-	int change = 0;
-
 	/* check to see if we should switch to the safe clocksource: */
-	if (clocksource_tsc.rating != 50 && check_tsc_unstable()) {
+	if (unlikely(!tsc_unstable && clocksource_tsc.rating != 50)) {
 		clocksource_tsc.rating = 50;
 		clocksource_rating_change(&clocksource_tsc);
-		change = 1;
 	}
-	return change;
+
+	tsc_unstable = 1;
 }
+EXPORT_SYMBOL_GPL(mark_tsc_unstable);
 
 static int __init init_tsc_clocksource(void)
 {
include/linux/clocksource.h +0 -2
Index: linux-2.6.19/include/linux/clocksource.h
===================================================================
@@ -87,7 +87,6 @@ static inline void clocksource_freq_chan
  *			subtraction of non 64 bit counters
  * @mult:		cycle to nanosecond multiplier
  * @shift:		cycle to nanosecond divisor (power of two)
- * @update_callback:	called when safe to alter clocksource values
  * @is_continuous:	defines if clocksource is free-running.
  * @vread:		vsyscall based read
  * @cycle_interval:	Used internally by timekeeping core, please ignore.
@@ -101,7 +100,6 @@ struct clocksource {
 	cycle_t mask;
 	u32 mult;
 	u32 shift;
-	int (*update_callback)(void);
 	int is_continuous;
 	cycle_t (*vread)(void);
 
include/linux/timekeeping.h +1 -0
Index: linux-2.6.19/include/linux/timekeeping.h
===================================================================
@@ -16,6 +16,7 @@ static inline int change_clocksource(voi
 }
 
 static inline void change_clocksource(void) { }
+static inline void timekeeping_init_notifier(void) { }
 
 #endif /* !CONFIG_GENERIC_TIME */
 
kernel/time/timekeeping.c +30 -2
Index: linux-2.6.19/kernel/time/timekeeping.c
===================================================================
@@ -19,6 +19,7 @@ static unsigned long timekeeping_suspend
  * Clock used for timekeeping
  */
 struct clocksource *clock = &clocksource_jiffies;
+atomic_t clock_recalc_interval = ATOMIC_INIT(0);
 
 /*
  * The current time
@@ -169,8 +170,9 @@ static int change_clocksource(void)
 		printk(KERN_INFO "Time: %s clocksource has been installed.\n",
 		       clock->name);
 		return 1;
-	} else if (clock->update_callback) {
-		return clock->update_callback();
+	} else if (unlikely(atomic_read(&clock_recalc_interval))) {
+		atomic_set(&clock_recalc_interval, 0);
+		return 1;
 	}
 	return 0;
 }
@@ -192,6 +194,29 @@ int timekeeping_is_continuous(void)
 
 	return ret;
 }
+
+static int
+clocksource_callback(struct notifier_block *nb, unsigned long op, void *c)
+{
+	if (c == clock && op == CLOCKSOURCE_NOTIFY_FREQ &&
+	    !atomic_read(&clock_recalc_interval))
+		atomic_inc(&clock_recalc_interval);
+
+	return 0;
+}
+
+static struct notifier_block clocksource_nb = {
+	.notifier_call = clocksource_callback,
+};
+
+void __init timekeeping_init_notifier(void)
+{
+	/*
+	 * Needs to be done early, incase a clock is unstable
+	 * early.
+	 */
+	clocksource_notifier_register(&clocksource_nb);
+}
 #endif /* CONFIG_GENERIC_TIME */
 
 /**
@@ -230,6 +255,9 @@ void __init timekeeping_init(void)
 		-xtime.tv_sec, -xtime.tv_nsec);
 
 	write_sequnlock_irqrestore(&xtime_lock, flags);
+
+	timekeeping_init_notifier();
+
 }
 
 
#15 lore raw

PATCH 12/12 clocksource: atomic signals

In reply to Daniel Walker

Modifies the way clocks are switched to in the timekeeping code. The original code would constantly monitor the clocksource list checking for newly added clocksources. I modified this by using atomic types to signal when a new clock is added. This allows the operation to be used only when it's needed.

The fast path is also reduced to checking a single atomic value.

---
 include/linux/clocksource.h |    5 ++++
 include/linux/timekeeping.h |   10 --------
 kernel/time/clocksource.c   |    6 +++++
 kernel/time/timekeeping.c   |   51 +++++++++++++++++++++++++++++++-------------
 4 files changed, 49 insertions(+), 23 deletions(-)
include/linux/clocksource.h +5 -0
Index: linux-2.6.19/include/linux/clocksource.h
===================================================================
@@ -26,6 +26,11 @@ typedef u64 cycle_t;
 extern struct clocksource clocksource_jiffies;
 
 /*
+ * Atomic signal that is specific to timekeeping.
+ */
+extern atomic_t clock_check;
+
+/*
  * Allows inlined calling for notifier routines.
  */
 extern struct atomic_notifier_head clocksource_list_notifier;
include/linux/timekeeping.h +1 -9
Index: linux-2.6.19/include/linux/timekeeping.h
===================================================================
@@ -5,15 +5,7 @@
 
 extern void update_wall_time(void);
 
-#ifdef CONFIG_GENERIC_TIME
-
-extern struct clocksource *clock;
-
-#else /* CONFIG_GENERIC_TIME */
-static inline int change_clocksource(void)
-{
-	return 0;
-}
+#ifndef CONFIG_GENERIC_TIME
 
 static inline void change_clocksource(void) { }
 static inline void timekeeping_init_notifier(void) { }
kernel/time/clocksource.c +6 -0
Index: linux-2.6.19/kernel/time/clocksource.c
===================================================================
@@ -50,6 +50,7 @@ static char override_name[32];
 static int finished_booting;
 
 ATOMIC_NOTIFIER_HEAD(clocksource_list_notifier);
+atomic_t clock_check = ATOMIC_INIT(0);
 
 /* clocksource_done_booting - Called near the end of bootup
  *
@@ -58,6 +59,8 @@ ATOMIC_NOTIFIER_HEAD(clocksource_list_no
 static int __init clocksource_done_booting(void)
 {
 	finished_booting = 1;
+	/* Check for a new clock now */
+	atomic_inc(&clock_check);
 	return 0;
 }
 
@@ -291,6 +294,9 @@ static ssize_t sysfs_override_clocksourc
 	/* try to select it: */
 	next_clocksource = select_clocksource();
 
+	/* Signal that there is a new clocksource */
+	atomic_inc(&clock_check);
+
 	spin_unlock_irq(&clocksource_lock);
 
 	return ret;
kernel/time/timekeeping.c +37 -14

98 lines not on this page · raw message