All of lore.kernel.org
 help / color / mirror / Atom feed
* [Patch] Fix the possibility of insane return value of hpet_calibrate() against SMI.
@ 2009-03-13  5:00 Yasunori Goto
  2009-03-14  8:23 ` Rik van Riel
  2009-03-15 19:56 ` Paul Gortmaker
  0 siblings, 2 replies; 18+ messages in thread
From: Yasunori Goto @ 2009-03-13  5:00 UTC (permalink / raw)
  To: clemens; +Cc: Linux Kernel ML, robert.picco, venkatesh.pallipadi, vojtech,
	mingo

Hello.

I think there is a possibility that HPET driver will return
insane value due to a SMI interruption (or switching guests by hypervisor).
I found it by reviewing, and I would like to fix it.

Current HPET driver calibrates the adjustment value
by calculation the elapse time in CPU busy loop. 
However this way is too dangerous against SMI interruption.

Here is the calibration code in hpet_calibrate()

 701 static unsigned long hpet_calibrate(struct hpets *hpetp)
             :
             :
 728         do {
 729                 m = read_counter(&hpet->hpet_mc);
 730                 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
 731         } while (i++, (m - start) < count);
 732 
 733         local_irq_restore(flags);
 734 
 735         return (m - start) / i;

If SMI interruption occurs between 728 to 731, then return value will be
bigger value than correct one. (SMI is not able to be controlled by OS.)


This patch is a simple solution to fix it.
hpet_calibrate() is called 5 times, and one of them is expected as
correct value.

Thanks.


---

hpet_calibrate() has a possibility of miss-calibration due to SMI.
If SMI interrupts in the while loop of calibration, then return value
will be big. This changes it tries 5 times and get minimum value as
correct value.

Signed-off-by: Yasunori Goto <y-goto@jp.fujitsu.com>

---

Index: hpet_test/drivers/char/hpet.c
===================================================================
--- hpet_test.orig/drivers/char/hpet.c	2008-12-04 16:24:02.000000000 +0900
+++ hpet_test/drivers/char/hpet.c	2008-12-04 16:34:59.000000000 +0900
@@ -713,7 +713,7 @@
  */
 #define	TICK_CALIBRATE	(1000UL)
 
-static unsigned long hpet_calibrate(struct hpets *hpetp)
+static unsigned long __hpet_calibrate(struct hpets *hpetp)
 {
 	struct hpet_timer __iomem *timer = NULL;
 	unsigned long t, m, count, i, flags, start;
@@ -750,6 +750,17 @@
 	return (m - start) / i;
 }
 
+static unsigned long hpet_calibrate(struct hpets *hpetp)
+{
+	unsigned long ret = ~0UL, i;
+
+	/* Try 5 times to remove impact of SMI.*/
+	for (i = 0; i < 5; i++)
+		ret = min(ret, __hpet_calibrate(hpetp));
+
+	return ret;
+}
+
 int hpet_alloc(struct hpet_data *hdp)
 {
 	u64 cap, mcfg;

-- 
Yasunori Goto 



^ permalink raw reply	[flat|nested] 18+ messages in thread
* [Patch] Fix the possibility of insane return value of hpet_calibrate() against SMI.
@ 2008-12-04 10:34 Yasunori Goto
  0 siblings, 0 replies; 18+ messages in thread
From: Yasunori Goto @ 2008-12-04 10:34 UTC (permalink / raw)
  To: clemens; +Cc: Linux Kernel ML, tglx, mingo

Hello.

I think there is a possibility that hpet_calibrate() will return 
an insane value when SMI interrupts.

 701 static unsigned long hpet_calibrate(struct hpets *hpetp)
             :
             :
 728         do {
 729                 m = read_counter(&hpet->hpet_mc);
 730                 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
 731         } while (i++, (m - start) < count);
 732 
 733         local_irq_restore(flags);
 734 
 735         return (m - start) / i;

If SMI interrupts between 728 to 731, then return value will be
bigger value than correct one. This is the fix for it. 

I found it by just reviewing about SMI and the codes of timer calibration.
But, I've not encountered this issue, and this issue is very difficult
to produce. So, if I'm something wrong, sorry for noise.

Thanks.


---

hpet_calibrate() has a possibility of miss-calibration due to SMI.
If SMI interrupts in the while loop of calibration, then return value
will be big. This changes it tries 3 times and get minimum value.

Signed-off-by: Yasunori Goto <y-goto@jp.fujitsu.com>

---

Index: hpet_test/drivers/char/hpet.c
===================================================================
--- hpet_test.orig/drivers/char/hpet.c	2008-12-04 16:24:02.000000000 +0900
+++ hpet_test/drivers/char/hpet.c	2008-12-04 16:34:59.000000000 +0900
@@ -713,7 +713,7 @@
  */
 #define	TICK_CALIBRATE	(1000UL)
 
-static unsigned long hpet_calibrate(struct hpets *hpetp)
+static unsigned long __hpet_calibrate(struct hpets *hpetp)
 {
 	struct hpet_timer __iomem *timer = NULL;
 	unsigned long t, m, count, i, flags, start;
@@ -750,6 +750,17 @@
 	return (m - start) / i;
 }
 
+static unsigned long hpet_calibrate(struct hpets *hpetp)
+{
+	unsigned long ret = ~0UL, i;
+
+	/* Try 3 times to remove impact of SMI.*/
+	for (i = 0; i < 3; i++)
+		ret = min(ret, __hpet_calibrate(hpetp));
+
+	return ret;
+}
+
 int hpet_alloc(struct hpet_data *hdp)
 {
 	u64 cap, mcfg;

-- 
Yasunori Goto 



^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2009-03-30 21:26 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-03-13  5:00 [Patch] Fix the possibility of insane return value of hpet_calibrate() against SMI Yasunori Goto
2009-03-14  8:23 ` Rik van Riel
2009-03-15 19:56 ` Paul Gortmaker
2009-03-16  2:34   ` Yasunori Goto
2009-03-16  8:59     ` Vojtech Pavlik
2009-03-16  9:52     ` Clemens Ladisch
2009-03-17 10:12       ` Yasunori Goto
2009-03-17 13:12         ` Paul Gortmaker
2009-03-18  0:45           ` Yasunori Goto
2009-03-18  2:47             ` [Patch] Fix the possibility of insane return value of hpet_calibrate() against SMI. (take 2) Yasunori Goto
2009-03-18  7:44               ` Clemens Ladisch
2009-03-18  8:25                 ` Vojtech Pavlik
2009-03-18 13:55               ` Paul Gortmaker
2009-03-18 15:11                 ` Clemens Ladisch
2009-03-18 15:32                   ` Paul Gortmaker
2009-03-30 21:06               ` Andrew Morton
2009-03-30 21:23                 ` Paul Gortmaker
  -- strict thread matches above, loose matches on Subject: below --
2008-12-04 10:34 [Patch] Fix the possibility of insane return value of hpet_calibrate() against SMI Yasunori Goto

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.