Skip to contentSkip to conversation

Enforce valid shift values in clocksource_register()

PATCH In review John Stultz 2 participants 4 messages Nov 26, 2008 – Nov 27, 2008 Timekeeping, clocksource core, NTP, alarmtimer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Enforce valid shift values in clocksource_register()

Thomas noted that some of the timekeeping core assumes clocksource shift values will be less then 32. However, we do nothing to enforce such limits.

This patch prints a warning if a clocksource with an invalid shift value is attempted to be registered, and will refuse to register the clocksource

I could not find any clocksources that use 32 or higher for a shift value, so this should be only a preventive warning for future clocksource writers.

kernel/time/clocksource.c +9 -2
index 9ed2eec..4a3bb13 100644
@@ -318,7 +318,7 @@ static int clocksource_enqueue(struct clocksource *c)
  * clocksource_register - Used to install new clocksources
  * @t:		clocksource to be registered
  *
- * Returns -EBUSY if registration fails, zero otherwise.
+ * Returns -EBUSY or -EINVAL if registration fails, zero otherwise.
  */
 int clocksource_register(struct clocksource *c)
 {
@@ -327,7 +327,14 @@ int clocksource_register(struct clocksource *c)
 
 	/* save mult_orig on registration */
 	c->mult_orig = c->mult;
-
+	if (c->shift >= 32) {
+		printk(KERN_WARNING "===============================\n");
+		printk(KERN_WARNING "WARNING: Cannot register %s clocksource\n",
+					c->name);
+		printk(KERN_WARNING "The shift value must be less then 32\n");
+		printk(KERN_WARNING "===============================\n");
+		return -EINVAL;
+	}
 	spin_lock_irqsave(&clocksource_lock, flags);
 	ret = clocksource_enqueue(c);
 	if (!ret)
#1 lore raw
Quoted 29 lines · John Stultz

Thomas noted that some of the timekeeping core assumes clocksource shift values will be less then 32. However, we do nothing to enforce such limits.

This patch prints a warning if a clocksource with an invalid shift value is attempted to be registered, and will refuse to register the clocksource

I could not find any clocksources that use 32 or higher for a shift value, so this should be only a preventive warning for future clocksource writers.

kernel/time/clocksource.c +1 -1
index 9ed2eec..4a3bb13 100644
@@ -318,7 +318,7 @@ static int clocksource_enqueue(struct clocksource *c)
  * clocksource_register - Used to install new clocksources
  * @t:		clocksource to be registered
  *
- * Returns -EBUSY if registration fails, zero otherwise.
+ * Returns -EBUSY or -EINVAL if registration fails, zero otherwise.
  */
 int clocksource_register(struct clocksource *c)
 {
@@ -327,7 +327,14 @@ int clocksource_register(struct clocksource *c)
 

kernel/time/clocksource.c int clocksource_register(struct clocksource *c) · line 336

Quoted 10 lines
kernel/time/clocksource.c +7 -1
 	/* save mult_orig on registration */
 	c->mult_orig = c->mult;
-
+	if (c->shift >= 32) {
+		printk(KERN_WARNING "===============================\n");
+		printk(KERN_WARNING "WARNING: Cannot register %s clocksource\n",
+					c->name);
+		printk(KERN_WARNING "The shift value must be less then 32\n");
+		printk(KERN_WARNING "===============================\n");
+		return -EINVAL;

Just setting the shift value to 31 along with a WARN_ON() should be enough. We don't need to kill the clocksource in that case, as this can be nasty when it happens in the early boot code where we dont have any output.

Thanks,

tglx

#2 lore raw
Quoted 31 lines · Thomas Gleixner
Quoted 29 lines · John Stultz

Thomas noted that some of the timekeeping core assumes clocksource shift values will be less then 32. However, we do nothing to enforce such limits.

This patch prints a warning if a clocksource with an invalid shift value is attempted to be registered, and will refuse to register the clocksource

I could not find any clocksources that use 32 or higher for a shift value, so this should be only a preventive warning for future clocksource writers.

kernel/time/clocksource.c +1 -1
index 9ed2eec..4a3bb13 100644
@@ -318,7 +318,7 @@ static int clocksource_enqueue(struct clocksource *c)
  * clocksource_register - Used to install new clocksources
  * @t:		clocksource to be registered
  *
- * Returns -EBUSY if registration fails, zero otherwise.
+ * Returns -EBUSY or -EINVAL if registration fails, zero otherwise.
  */
 int clocksource_register(struct clocksource *c)
 {
@@ -327,7 +327,14 @@ int clocksource_register(struct clocksource *c)
 
Quoted 15 lines

kernel/time/clocksource.c int clocksource_register(struct clocksource *c) · line 336

Quoted 10 lines
kernel/time/clocksource.c +7 -1
 	/* save mult_orig on registration */
 	c->mult_orig = c->mult;
-
+	if (c->shift >= 32) {
+		printk(KERN_WARNING "===============================\n");
+		printk(KERN_WARNING "WARNING: Cannot register %s clocksource\n",
+					c->name);
+		printk(KERN_WARNING "The shift value must be less then 32\n");
+		printk(KERN_WARNING "===============================\n");
+		return -EINVAL;

Just setting the shift value to 31 along with a WARN_ON() should be enough. We don't need to kill the clocksource in that case, as this can be nasty when it happens in the early boot code where we dont have any output.

Well, we can't tweak the shift value without changing the mult. And that seemed a bit overreaching (but might be safe). I'll give it a shot.

Also early boot we should have the jiffies clocksource around, so unless I'm forgetting something, I don't think it has early boot concerns.

thanks
-john

#3 lore raw
Quoted 15 lines · John Stultz
Quoted 13 lines · Thomas Gleixner

kernel/time/clocksource.c int clocksource_register(struct clocksource *c) · line 336

Quoted 7 lines · John Stultz
kernel/time/clocksource.c +7 -0
+	if (c->shift >= 32) {
+		printk(KERN_WARNING "===============================\n");
+		printk(KERN_WARNING "WARNING: Cannot register %s clocksource\n",
+					c->name);
+		printk(KERN_WARNING "The shift value must be less then 32\n");
+		printk(KERN_WARNING "===============================\n");
+		return -EINVAL;

Just setting the shift value to 31 along with a WARN_ON() should be enough. We don't need to kill the clocksource in that case, as this can be nasty when it happens in the early boot code where we dont have any output.

Quoted 5 lines

Well, we can't tweak the shift value without changing the mult. And that seemed a bit overreaching (but might be safe). I'll give it a shot.

Also early boot we should have the jiffies clocksource around, so unless I'm forgetting something, I don't think it has early boot concerns.

Fair enough. I forgot that we do a late handover from jiffies to the real clocksource. Still a WARN_ON is more prominent and more useful for developers to get to the root cause. Setting the shift to 31 will result in wrong timer values but not brick the box. So espcially for code, which calculates the values this might be worthwhile.

Thanks,

tglx