Skip to contentSkip to conversation

BugFix: grub menu gets stuck due to unreliable rdtsc instruction.

PATCH In review Duan Yayong via Grub-devel 2 participants 14 messages Nov 11, 2024 – Feb 26, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 BugFix: grub menu gets stuck due to unreliable rdtsc instruction.

This patch is used to fix grub menu gets stuck in server
AC poweron/poweroff stress test of x86_64, which is reproduced with 1/200 ratio. The root cause analysis as below:

Q. What's the code logic?
A. "grub_tsc_init" function will init tsc by setting grub_tsc_rate, which call stack is:
grub_tsc_init -> grub_tsc_calibrate_from_pmtimer -> grub_divmod64
Among, "grub_divmod64" function needs "tsc_diff" as the second parameter. In "grub_pmtimer_wait_count_tsc", we will call "grub_get_tsc" function to get time stamp counter value to assign to "start_tsc" variable, and get into "while(1)" loop space to get "end_tsc" variable value with same function, after 3580 ticks, return "end_tsc - start_tsc". Actually, "rdtsc" instruction will be called in "grub_get_tsc", but "rdtsc" instruction is not reliable(for the reason see the next question), which will cause "tsc_diff" to be a very big number larger than (1UL << 32) or a negative number, so that grub_tsc_rate will be zero. When "run_menu" function is startup, and calls "grub_tsc_get_time_ms" function to get current time to check if timeout time reach, at this time, "grub_tsc_get_time_ms" function will return zero due to zero "grub_tsc_rate" variable, then grub menu gets stuck...

Q. What's the difference between rdtsc and rdtscp instructions in x86_64 architecture? Here is more explanations from Intel Manual per check with Intel OS expert as below:

A. The RDTSC instruction is not a serializing instruction. It does not necessarily wait until all previous instructions have been executedbefore reading the counter. Similarly, subsequent instructions may begin execution before the read operation is performed. The following items may guide software seeking to order executions of RDTSC:
- If software requires RDTSC to be executed only after all previouss instructions have executed and all previous loads are globally visible, 1 it can execute LFENCE immediately before RDTSC.
- If software requires RDTSC to be executed only after all previouss instructions have executed and all previous loads and stores areglobally visible, it can execute the sequence MFENCE;LFENCE immediately before RDTSC.
- If software requires RDTSC to be executed prior to execution of any sulbsequent instruction (including any memory accesses), it can execute the sequence LFENCE immediately after RDTSC.

A. The RDTSCP instruction is not a serializing instruction, but it does wait until all previous instructions have executed and all previous loads are globally visible. 1 But it does not wait for previous stores to be globally visible, and subsequent instructions may begin execution before the read operation is performed. The following items may guide softwareseeking to order executions of RDTSCP:
- If software requires RDTSCP to be executed only after all previous stores are globally visible, it can execute MFENCE immediatelybefore RDTSCP.
- If software requires RDTSCP to be executed prior to execution of any subsequent instruction (including any memory accesses), itcan execute LFENCE immediately after RDTSCP.

Q. Why do we do this fix?

A. Changing "rdtsc" instruction to "rdtscp" to make sure do read counter operation after previous instruction have executed and all previous loads are globally visiable, that means we keep current and previous instruction serializing; add "lfence" load barrier instruction after "rdrscp" instruction, that means we keep current and subsequent instrution serializing. So we must get correct tsc value in this timeout check scenario.

A. Adding "if (grub_tsc_rate == 0)" judgement just in case other unknown instruction exception, so that "grub_tsc_calibrate_from_pit||grub_tsc_calibrate_from_efi" getting "grub_tsc_rate" methods have a opportunity to be performed but causing grub menu stucking.

A. After this change, we cannot reproduce this issue via 1060 AC poweron/poweroff stress test cycles.

---
 grub-core/kern/i386/tsc_pmtimer.c | 11 +++++++++++
 include/grub/i386/tsc.h           |  8 ++++----
 2 files changed, 15 insertions(+), 4 deletions(-)
grub-core/kern/i386/tsc_pmtimer.c +11 -0
index 5c03c510a..04f7c2006 100644
@@ -143,5 +143,16 @@ grub_tsc_calibrate_from_pmtimer (void)
   if (tsc_diff == 0)
     return 0;
   grub_tsc_rate = grub_divmod64 ((1ULL << 32), tsc_diff, 0);
+  /*
+    Specifically, when the tsc_diff (end_tsc - start_tsc) is greater than (1ULL << 32),
+    the result of grub_divmod64() becomes zero, causing grub_tsc_rate to always be zero.
+    As a result, grub_tsc_get_time_ms() consistently returns zero, and the grub menu
+    countdown gets stuck. To resolve this, we return 0 to proceed to the next calibration
+    function when grub_tsc_rate is zero.
+  */
+  if (grub_tsc_rate == 0) {
+    return 0;
+  }
+
   return 1;
 }
include/grub/i386/tsc.h +4 -4
index 947e62fa1..543de93e0 100644
@@ -44,10 +44,10 @@ grub_get_tsc (void)
   /* The CPUID instruction is a 'serializing' instruction, and
      avoids out-of-order execution of the RDTSC instruction. */
   grub_cpuid (0,a,b,c,d);
-  /* Read TSC value.  We cannot use "=A", since this would use
-     %rax on x86_64. */
-  asm volatile ("rdtsc":"=a" (lo), "=d" (hi));
-
+  /*
+    Use the rdtscp command instead of RDTSC to provide better ordering guarantees.
+  */
+  asm volatile ("rdtscp\n\t""lfence\n\t":"=a"(lo), "=d"(hi));
   return (((grub_uint64_t) hi) << 32) | lo;
 }
 
Signature · 7 lines
2.39.5 (Apple Git-154)


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw
Quoted 8 lines · Duan Yayong via Grub-devel

This patch is used to fix grub menu gets stuck in server
AC poweron/poweroff stress test of x86_64, which is reproduced with 1/200 ratio. The root cause analysis as below:

Q. What's the code logic?
A. "grub_tsc_init" function will init tsc by setting grub_tsc_rate, which call stack is:
grub_tsc_init -> grub_tsc_calibrate_from_pmtimer -> grub_divmod64
Among, "grub_divmod64" function needs "tsc_diff" as the second parameter. In "grub_pmtimer_wait_count_tsc", we will call "grub_get_tsc" function to get time stamp counter value to assign to "start_tsc" variable, and get into "while(1)" loop space to get "end_tsc" variable value with same function, after 3580 ticks, return "end_tsc - start_tsc". Actually, "rdtsc" instruction will be called in "grub_get_tsc", but "rdtsc" instruction is not reliable(for the reason see the next question), which will cause "tsc_diff" to be a very big number larger than (1UL << 32) or a negative number, so that grub_tsc_rate will be zero. When "run_menu" function is startup, and calls "grub_tsc_get_time_ms" function to get current time to check if timeout time reach, at this time, "grub_tsc_get_time_ms" function will return zero due to zero "grub_tsc_rate" variable, then grub menu gets stuck...

Please wrap lines in the commit message. Now it is unreadable. FYI, lines can be (a bit) longer than 80 chars...

Quoted 4 lines

Q. What's the difference between rdtsc and rdtscp instructions in x86_64 architecture? Here is more explanations from Intel Manual per check with Intel OS expert as below:

Could you point exact paragraph(s) explaining this in Intel SDM? How this applies to the AMD? Additionally, it seems to me we should check for rdtscp instruction availability and fallback to rdtsc/lfence when it is not present.

Quoted 30 lines

A. The RDTSC instruction is not a serializing instruction. It does not necessarily wait until all previous instructions have been executedbefore reading the counter. Similarly, subsequent instructions may begin execution before the read operation is performed. The following items may guide software seeking to order executions of RDTSC:
- If software requires RDTSC to be executed only after all previouss instructions have executed and all previous loads are globally visible, 1 it can execute LFENCE immediately before RDTSC.
- If software requires RDTSC to be executed only after all previouss instructions have executed and all previous loads and stores areglobally visible, it can execute the sequence MFENCE;LFENCE immediately before RDTSC.
- If software requires RDTSC to be executed prior to execution of any sulbsequent instruction (including any memory accesses), it can execute the sequence LFENCE immediately after RDTSC.

A. The RDTSCP instruction is not a serializing instruction, but it does wait until all previous instructions have executed and all previous loads are globally visible. 1 But it does not wait for previous stores to be globally visible, and subsequent instructions may begin execution before the read operation is performed. The following items may guide softwareseeking to order executions of RDTSCP:
- If software requires RDTSCP to be executed only after all previous stores are globally visible, it can execute MFENCE immediatelybefore RDTSCP.
- If software requires RDTSCP to be executed prior to execution of any subsequent instruction (including any memory accesses), itcan execute LFENCE immediately after RDTSCP.

Q. Why do we do this fix?

A. Changing "rdtsc" instruction to "rdtscp" to make sure do read counter operation after previous instruction have executed and all previous loads are globally visiable, that means we keep current and previous instruction serializing; add "lfence" load barrier instruction after "rdrscp" instruction, that means we keep current and subsequent instrution serializing. So we must get correct tsc value in this timeout check scenario.

A. Adding "if (grub_tsc_rate == 0)" judgement just in case other unknown instruction exception, so that "grub_tsc_calibrate_from_pit||grub_tsc_calibrate_from_efi" getting "grub_tsc_rate" methods have a opportunity to be performed but causing grub menu stucking.

A. After this change, we cannot reproduce this issue via 1060 AC poweron/poweroff stress test cycles.

---
 grub-core/kern/i386/tsc_pmtimer.c | 11 +++++++++++
 include/grub/i386/tsc.h           |  8 ++++----
 2 files changed, 15 insertions(+), 4 deletions(-)
grub-core/kern/i386/tsc_pmtimer.c +0 -0
index 5c03c510a..04f7c2006 100644

grub-core/kern/i386/tsc_pmtimer.c grub_tsc_calibrate_from_pmtimer (void) · line 152

Quoted 10 lines
grub-core/kern/i386/tsc_pmtimer.c +7 -0
@@ -143,5 +143,16 @@ grub_tsc_calibrate_from_pmtimer (void)
   if (tsc_diff == 0)
     return 0;
   grub_tsc_rate = grub_divmod64 ((1ULL << 32), tsc_diff, 0);
+  /*
+    Specifically, when the tsc_diff (end_tsc - start_tsc) is greater than (1ULL << 32),
+    the result of grub_divmod64() becomes zero, causing grub_tsc_rate to always be zero.
+    As a result, grub_tsc_get_time_ms() consistently returns zero, and the grub menu
+    countdown gets stuck. To resolve this, we return 0 to proceed to the next calibration
+    function when grub_tsc_rate is zero.
+  */

Wrong coding style for comment [1]. Please fix it...

grub-core/kern/i386/tsc_pmtimer.c grub_tsc_calibrate_from_pmtimer (void) · line 156

Quoted 4 lines
grub-core/kern/i386/tsc_pmtimer.c +4 -0
+  if (grub_tsc_rate == 0) {
+    return 0;
+  }
+

I think the grub-core/kern/i386/tsc_pmtimer.c fix should be in separate patch.

Quoted 7 lines
grub-core/kern/i386/tsc_pmtimer.c +0 -0
   return 1;
 }
include/grub/i386/tsc.h +0 -0
index 947e62fa1..543de93e0 100644

include/grub/i386/tsc.h grub_get_tsc (void) · line 49

Quoted 10 lines
include/grub/i386/tsc.h +3 -4
@@ -44,10 +44,10 @@ grub_get_tsc (void)
   /* The CPUID instruction is a 'serializing' instruction, and
      avoids out-of-order execution of the RDTSC instruction. */
   grub_cpuid (0,a,b,c,d);
-  /* Read TSC value.  We cannot use "=A", since this would use
-     %rax on x86_64. */
-  asm volatile ("rdtsc":"=a" (lo), "=d" (hi));
-
+  /*
+    Use the rdtscp command instead of RDTSC to provide better ordering guarantees.
+  */

Wrong coding style... [1]

include/grub/i386/tsc.h grub_get_tsc (void) · line 50

Quoted 1 line
include/grub/i386/tsc.h +1 -0
+  asm volatile ("rdtscp\n\t""lfence\n\t":"=a"(lo), "=d"(hi));

This is a mess...

asm volatile ("rdtscp; lfence" : "=a" (lo), "=d" (hi));

include/grub/i386/tsc.h grub_get_tsc (void) · line 52

Quoted 2 lines
include/grub/i386/tsc.h +0 -0
   return (((grub_uint64_t) hi) << 32) | lo;
 }

Daniel

[1] https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Comments

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#2 lore raw

PATCH 1/1 Bugfix: grub menu gets stuck

Adding "if (grub_tsc_rate == 0)" judgement just in case other unknown instruction exception, so that "grub_tsc_calibrate_from_pit||grub_tsc_calibrate_from_efi" getting "grub_tsc_rate" methods have a opportunity to be performed but causing grub menu stucking.

---
 grub-core/kern/i386/tsc_pmtimer.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
grub-core/kern/i386/tsc_pmtimer.c +10 -0
index 5c03c510a..90db49514 100644
@@ -143,5 +143,15 @@ grub_tsc_calibrate_from_pmtimer (void)
   if (tsc_diff == 0)
     return 0;
   grub_tsc_rate = grub_divmod64 ((1ULL << 32), tsc_diff, 0);
+  /*
+   * Specifically, when the tsc_diff (end_tsc - start_tsc) is greater than (1ULL << 32),
+   * the result of grub_divmod64() becomes zero, causing grub_tsc_rate to always be zero.
+   * As a result, grub_tsc_get_time_ms() consistently returns zero, and the grub menu
+   * countdown gets stuck. To resolve this, we return 0 to proceed to the next calibration
+   * function when grub_tsc_rate is zero.
+   */
+  if (grub_tsc_rate == 0)
+    return 0;
+
   return 1;
 }
Signature · 7 lines
2.20.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by on v1 1/1
#3 lore raw

Re: [PATCH] Bugfix: grub menu gets stuck

Quoted 9 lines · Duan Yayong via Grub-devel

Adding "if (grub_tsc_rate == 0)" judgement just in case other unknown instruction exception, so that "grub_tsc_calibrate_from_pit||grub_tsc_calibrate_from_efi" getting "grub_tsc_rate" methods have a opportunity to be performed but causing grub menu stucking.

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#4 lore raw
In reply to Daniel Kiper

Hello Daniel,

Thanks very much for your valuable comments.
For all your comments, I will reply case by case as follows:

- Please wrap lines in the commit message. Now it is unreadable.

FYI, lines can be (a bit) longer than 80 chars...

- *Yes, already wrapped commit message with a new patch, which*

*is sent before this mail.*

- Could you point exact paragraph(s) explaining this in Intel SDM?
- *Intel® 64 and IA-32 Architectures*

*Software Developer’s Manual Volume 2B: 4-558 RDTSC—Read Time-Stamp Counter & 4-560 RDTSCP—Read Time-Stamp Counter and Processor ID.*

- *URL for* *Volume 2B:
**https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html
<https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html>*
- *URL for full Manual:
**https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html
<https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html>*

- How this applies to the AMD? Additionally, it seems to me we should check

for rdtscp instruction availability and fallback to rdtsc/lfence when it is not present.

- *A**bout this question, after reading the manual and related grub code

      again,*

*I think the grub_cpuid() function to be executed before "rdtsc" instruction is enough*
*to make sure all previous instructions have been excuted before "rdtsc" instruction.*
*But this grub menu issue is still reproduced. I think it is because we did not ensure*
*serialization between "rdtsc" instruction and its subsequent instructions. So I am*
*researching the two corrections below and doing related stress tests:* *Patch 1:*
grub_cpuid (0,a,b,c,d);

include/grub/i386/tsc.h grub_get_tsc (void)

Quoted 6 lines
include/grub/i386/tsc.h +2 -4
-  /* Read TSC value.  We cannot use "=A", since this would use
-     %rax on x86_64. */
-  asm volatile ("rdtsc":"=a" (lo), "=d" (hi));
-
+  /*
+    Use the rdtscp command instead of RDTSC to provide better ordering

guarantees.

include/grub/i386/tsc.h grub_get_tsc (void) · line 52

Quoted 4 lines
include/grub/i386/tsc.h +2 -0
+  */
+  asm volatile ("rdtsc*;lfence*":"=a"(lo), "=d"(hi));
   return (((grub_uint64_t) hi) << 32) | lo;
 }

*Patch 2:*
grub_cpuid (0,a,b,c,d);

include/grub/i386/tsc.h grub_get_tsc (void)

Quoted 6 lines
include/grub/i386/tsc.h +2 -4
-  /* Read TSC value.  We cannot use "=A", since this would use
-     %rax on x86_64. */
-  asm volatile ("rdtsc":"=a" (lo), "=d" (hi));
-
+  /*
+    Use the rdtscp command instead of RDTSC to provide better ordering

guarantees.

include/grub/i386/tsc.h grub_get_tsc (void) · line 52

Quoted 5 lines
include/grub/i386/tsc.h +3 -0
+  */
+  asm volatile ("rdtsc":"=a"(lo), "=d"(hi));
+  *grub_cpuid (0,a,b,c,d);*
   return (((grub_uint64_t) hi) << 32) | lo;
 }

- I think the grub-core/kern/i386/tsc_pmtimer.c fix should be in separate patch.
- *Yes, a new separate patch has already been sent before this mail.*

- Wrong coding style.
- *The sent patch was fixed before this mail.*
- *The upcoming patch about serialization instruction*

*adjustment will fix it also.*

If there are any other problems, please feel free to let me know. Thanks for your time again.
-------------------------------------------------------------------
Best Regards,
段亚勇 Data-SYS-STE-操作系统
上海市杨浦区抖音新江湾广场 T2B 3F
-------------------------------------------------------------------
From: "Daniel Kiper"<[email protected]>
Date: Tue, Nov 26, 2024, 23:33
Subject: [External] Re: [PATCH] BugFix: grub menu gets stuck due to unreliable rdtsc instruction.
To: "Duan Yayong"<[email protected]>
Cc: <[email protected]>, <[email protected]>, < [email protected]>, <[email protected]>, < [email protected]>, "Li Yongqiang"<[email protected]>, "Sun Ming"<[email protected]>

Quoted 6 lines · Duan Yayong via Grub-devel

This patch is used to fix grub menu gets stuck in server
AC poweron/poweroff stress test of x86_64, which is reproduced with 1/200 ratio. The root cause analysis as below:

Q. What's the code logic?
A. "grub_tsc_init" function will init tsc by setting grub_tsc_rate, which

call stack is:

Quoted 2 lines

grub_tsc_init -> grub_tsc_calibrate_from_pmtimer -> grub_divmod64 Among, "grub_divmod64" function needs "tsc_diff" as the second

parameter. In "grub_pmtimer_wait_count_tsc", we will call "grub_get_tsc" function to get time stamp counter value to assign to "start_tsc" variable, and get into "while(1)" loop space to get "end_tsc" variable value with same function, after 3580 ticks, return "end_tsc - start_tsc". Actually, "rdtsc" instruction will be called in "grub_get_tsc", but "rdtsc" instruction is not reliable(for the reason see the next question), which will cause "tsc_diff" to be a very big number larger than (1UL << 32) or a negative number, so that grub_tsc_rate will be zero. When "run_menu" function is startup, and calls "grub_tsc_get_time_ms" function to get current time to check if timeout time reach, at this time, "grub_tsc_get_time_ms" function will return zero due to zero "grub_tsc_rate" variable, then grub menu gets stuck...

Please wrap lines in the commit message. Now it is unreadable. FYI, lines can be (a bit) longer than 80 chars...

Quoted 4 lines

Q. What's the difference between rdtsc and rdtscp instructions in x86_64 architecture? Here is more explanations from Intel Manual per check with Intel OS expert as below:

Could you point exact paragraph(s) explaining this in Intel SDM? How this applies to the AMD? Additionally, it seems to me we should check for rdtscp instruction availability and fallback to rdtsc/lfence when it is not present.

Quoted 1 line

A. The RDTSC instruction is not a serializing instruction. It does not

necessarily wait until all previous instructions have been executedbefore reading the counter. Similarly, subsequent instructions may begin execution before the read operation is performed. The following items may guide software seeking to order executions of RDTSC:

Quoted 1 line

- If software requires RDTSC to be executed only after all previouss

instructions have executed and all previous loads are globally visible, 1 it can execute LFENCE immediately before RDTSC.

Quoted 1 line

- If software requires RDTSC to be executed only after all previouss

instructions have executed and all previous loads and stores areglobally visible, it can execute the sequence MFENCE;LFENCE immediately before RDTSC.

Quoted 1 line

- If software requires RDTSC to be executed prior to execution of any

sulbsequent instruction (including any memory accesses), it can execute the sequence LFENCE immediately after RDTSC.

Quoted 2 lines

A. The RDTSCP instruction is not a serializing instruction, but it does

wait until all previous instructions have executed and all previous loads are globally visible. 1 But it does not wait for previous stores to be globally visible, and subsequent instructions may begin execution before the read operation is performed. The following items may guide softwareseeking to order executions of RDTSCP:

Quoted 1 line

- If software requires RDTSCP to be executed only after all previous

stores are globally visible, it can execute MFENCE immediatelybefore RDTSCP.

Quoted 1 line

- If software requires RDTSCP to be executed prior to execution of any

subsequent instruction (including any memory accesses), itcan execute LFENCE immediately after RDTSCP.

Quoted 4 lines

Q. Why do we do this fix?

A. Changing "rdtsc" instruction to "rdtscp" to make sure do read counter

operation after previous instruction have executed and all previous loads are globally visiable, that means we keep current and previous instruction serializing; add "lfence" load barrier instruction after "rdrscp" instruction, that means we keep current and subsequent instrution serializing. So we must get correct tsc value in this timeout check scenario.

Quoted 2 lines

A. Adding "if (grub_tsc_rate == 0)" judgement just in case other unknown

instruction exception, so that "grub_tsc_calibrate_from_pit||grub_tsc_calibrate_from_efi" getting "grub_tsc_rate" methods have a opportunity to be performed but causing grub menu stucking.

Quoted 2 lines

A. After this change, we cannot reproduce this issue via 1060 AC

poweron/poweroff stress test cycles.

Review

Quoted 10 lines
---
 grub-core/kern/i386/tsc_pmtimer.c | 11 +++++++++++
 include/grub/i386/tsc.h           |  8 ++++----
 2 files changed, 15 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/grub-core/kern/i386/tsc_pmtimer.c

grub-core/kern/i386/tsc_pmtimer.c grub_tsc_calibrate_from_pmtimer (void) · line 147

Quoted 9 lines

index 5c03c510a..04f7c2006 100644

grub-core/kern/i386/tsc_pmtimer.c +2 -0
@@ -143,5 +143,16 @@ grub_tsc_calibrate_from_pmtimer (void)
   if (tsc_diff == 0)
     return 0;
   grub_tsc_rate = grub_divmod64 ((1ULL << 32), tsc_diff, 0);
+  /*
+    Specifically, when the tsc_diff (end_tsc - start_tsc) is greater

than (1ULL << 32),

grub-core/kern/i386/tsc_pmtimer.c grub_tsc_calibrate_from_pmtimer (void) · line 148

Quoted 1 line
grub-core/kern/i386/tsc_pmtimer.c +1 -0
+    the result of grub_divmod64() becomes zero, causing grub_tsc_rate to

always be zero.

grub-core/kern/i386/tsc_pmtimer.c grub_tsc_calibrate_from_pmtimer (void) · line 149

Quoted 1 line
grub-core/kern/i386/tsc_pmtimer.c +1 -0
+    As a result, grub_tsc_get_time_ms() consistently returns zero, and

the grub menu

grub-core/kern/i386/tsc_pmtimer.c grub_tsc_calibrate_from_pmtimer (void) · line 150

Quoted 1 line
grub-core/kern/i386/tsc_pmtimer.c +1 -0
+    countdown gets stuck. To resolve this, we return 0 to proceed to the

next calibration

grub-core/kern/i386/tsc_pmtimer.c grub_tsc_calibrate_from_pmtimer (void) · line 152

Quoted 2 lines
grub-core/kern/i386/tsc_pmtimer.c +2 -0
+    function when grub_tsc_rate is zero.
+  */

Wrong coding style for comment [1]. Please fix it...

grub-core/kern/i386/tsc_pmtimer.c grub_tsc_calibrate_from_pmtimer (void) · line 156

Quoted 4 lines
grub-core/kern/i386/tsc_pmtimer.c +4 -0
+  if (grub_tsc_rate == 0) {
+    return 0;
+  }
+

I think the grub-core/kern/i386/tsc_pmtimer.c fix should be in separate patch.

Quoted 7 lines
grub-core/kern/i386/tsc_pmtimer.c +0 -0
   return 1;
 }
include/grub/i386/tsc.h +0 -0
index 947e62fa1..543de93e0 100644

include/grub/i386/tsc.h grub_get_tsc (void) · line 48

Quoted 9 lines
include/grub/i386/tsc.h +2 -4
@@ -44,10 +44,10 @@ grub_get_tsc (void)
   /* The CPUID instruction is a 'serializing' instruction, and
      avoids out-of-order execution of the RDTSC instruction. */
   grub_cpuid (0,a,b,c,d);
-  /* Read TSC value.  We cannot use "=A", since this would use
-     %rax on x86_64. */
-  asm volatile ("rdtsc":"=a" (lo), "=d" (hi));
-
+  /*
+    Use the rdtscp command instead of RDTSC to provide better ordering

guarantees.

include/grub/i386/tsc.h grub_get_tsc (void) · line 49

Quoted 1 line
include/grub/i386/tsc.h +1 -0
+  */

Wrong coding style... [1]

include/grub/i386/tsc.h grub_get_tsc (void) · line 50

Quoted 1 line
include/grub/i386/tsc.h +1 -0
+  asm volatile ("rdtscp\n\t""lfence\n\t":"=a"(lo), "=d"(hi));

This is a mess...

asm volatile ("rdtscp; lfence" : "=a" (lo), "=d" (hi));

include/grub/i386/tsc.h grub_get_tsc (void) · line 52

Quoted 2 lines
include/grub/i386/tsc.h +0 -0
   return (((grub_uint64_t) hi) << 32) | lo;
 }

Daniel

[1] https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Comments

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#5 lore raw

PATCH 1/1 BugFix: grub menu gets stuck due to unserialized rdtsc

In reply to Daniel Kiper

This patch is used to fix grub menu gets stuck in server
AC poweron/poweroff stress test of x86_64, which is reproduced with 1/200 ratio. The root cause analysis as below:

Q:
What's the code logic?
A:
"grub_tsc_init" function will init tsc by setting grub_tsc_rate, which call stack is:

grub_tsc_init -> grub_tsc_calibrate_from_pmtimer -> grub_divmod64

Among, "grub_divmod64" function needs "tsc_diff" as the second parameter. In "grub_pmtimer_wait_count_tsc", we will call "grub_get_tsc" function to get time stamp counter value to assign to "start_tsc" variable, and get into "while(1)" loop space to get "end_tsc" variable value with same function, after 3580 ticks, return "end_tsc - start_tsc". Actually, "rdtsc" instruction will be called in "grub_get_tsc", but "rdtsc" instruction is not reliable(for the reason see the next question), which will cause "tsc_diff" to be a very big number larger than (1UL << 32) or a negative number, so that grub_tsc_rate will be zero. When "run_menu" function is startup, and calls "grub_tsc_get_time_ms" function to get current time to check if timeout time reach, at this time, "grub_tsc_get_time_ms" function will return zero due to zero "grub_tsc_rate" variable, then grub menu gets stuck...

Q:
What's the difference between rdtsc and rdtscp instructions in x86_64 architecture? Here is more explanations from Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 2B: https://cdrdv2.intel.com/v1/dl/getContent/671241.

A:
In page 4-558 -> RDTSC—Read Time-Stamp Counter:

The RDTSC instruction is not a serializing instruction.
It does not necessarily wait until all previous instructions
have been executed before reading the counter.
Similarly, subsequent instructions may begin execution before
the read operation is performed. The following items may guide software seeking to order executions of RDTSC:
- If software requires RDTSC to be executed only after all previous instructions have executed and all previous loads are globally visible, it can execute LFENCE immediately before RDTSC.
- If software requires RDTSC to be executed only after all previous instructions have executed and all previous loads and stores are globally visible, it can execute the sequence MFENCE;LFENCE immediately before RDTSC.
- If software requires RDTSC to be executed prior to execution of any subsequent instruction (including any memory accesses), it can execute the sequence LFENCE immediately after RDTSC.

A:
In page 4-560 -> RDTSCP—Read Time-Stamp Counter and Processor ID:

The RDTSCP instruction is not a serializing instruction,
but it does wait until all previous instructions have executed and all previous loads are globally visible. 1 But it does not wait for previous stores to be globally visible, and subsequent instructions may begin execution before the read operation is performed. The following items may guide softwareseeking to order executions of RDTSCP:
- If software requires RDTSCP to be executed only after all previous stores are globally visible, it can execute MFENCE immediately before RDTSCP.
- If software requires RDTSCP to be executed prior to execution of any subsequent instruction (including any memory accesses), itcan execute LFENCE immediately after RDTSCP.

Q:
Why there is a cpuid serilizing instruction before rdtsc instruction, but "grub_get_tsc" still cannot work as expect?

A:
From Intel® 64 and IA-32 Architectures Software Developer's Manual Volume 2A: Instruction Set Reference, A-L:
https://cdrdv2.intel.com/v1/dl/getContent/671199

In page 3-222 -> CPUID—CPU Identification:
"CPUID" can be executed at any privilege level to serialize instruction execution. Serializing instruction execution guarantees that any modifications to flags, registers, and memory for previous instructions are completed before the next instruction is fetched and executed.

So we only kept the instruction "rdtsc" and its previous instruction in order currently. But it is still out-of-order possibility between rdtsc instruction and its subsequent instruction.

Q:
Why do we do this fix?

A:
In the one hand, Add "cpuid" instruction after "rdtsc" instruction to make sure "rdtsc" instruction to be executed prior to execution of any subsequent instruction, about serializing execution that all previous instructions have been executed before "rdtsc", there is a "cpuid" usage in original code.
In the other hand, using "cpuid" instruction ranther than "lfence" can make sure a forward compatibility for previous HW.

Base this fix, we did 1500 cycles power on/off stress test, and did not reproduce this issue again.

---
 include/grub/i386/tsc.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/grub/i386/tsc.h +6 -0
index 947e62fa1..1814704fc 100644
@@ -47,6 +47,12 @@ grub_get_tsc (void)
   /* Read TSC value.  We cannot use "=A", since this would use
      %rax on x86_64. */
   asm volatile ("rdtsc":"=a" (lo), "=d" (hi));
+  /* Add CPUID instruction after rdtsc instruction to ensure
+   * rdtsc instruction's execution is prior to subsequent
+   * instruction to avoid out-of-order bewteen rdtsc and
+   * its subsequent instruction.
+   */
+  grub_cpuid (0,a,b,c,d);
 
   return (((grub_uint64_t) hi) << 32) | lo;
 }
Signature · 7 lines
2.20.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by on v1 1/1
#6 lore raw

Re: [PATCH] BugFix: grub menu gets stuck due to unserialized rdtsc

Quoted 26 lines · Duan Yayong

This patch is used to fix grub menu gets stuck in server
AC poweron/poweroff stress test of x86_64, which is reproduced with 1/200 ratio. The root cause analysis as below:

Q:
What's the code logic?
A:
"grub_tsc_init" function will init tsc by setting grub_tsc_rate, which call stack is:

grub_tsc_init -> grub_tsc_calibrate_from_pmtimer -> grub_divmod64

Among, "grub_divmod64" function needs "tsc_diff" as the second parameter. In "grub_pmtimer_wait_count_tsc", we will call "grub_get_tsc" function to get time stamp counter value to assign to "start_tsc" variable, and get into "while(1)" loop space to get "end_tsc" variable value with same function, after 3580 ticks, return "end_tsc - start_tsc". Actually, "rdtsc" instruction will be called in "grub_get_tsc", but "rdtsc" instruction is not reliable(for the reason see the next question), which will cause "tsc_diff" to be a very big number larger than (1UL << 32) or a negative number, so that grub_tsc_rate will be zero. When "run_menu" function is startup, and calls "grub_tsc_get_time_ms" function to get current time to check if timeout time reach, at this time, "grub_tsc_get_time_ms" function will return zero due to zero "grub_tsc_rate" variable, then grub menu gets stuck...

Quoted 5 lines

Q:
What's the difference between rdtsc and rdtscp instructions in x86_64 architecture? Here is more explanations from Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 2B: https://cdrdv2.intel.com/v1/dl/getContent/671241.

I would suggest to add document version number next time.

Quoted 36 lines

A:
In page 4-558 -> RDTSC—Read Time-Stamp Counter:

The RDTSC instruction is not a serializing instruction.
It does not necessarily wait until all previous instructions
have been executed before reading the counter.
Similarly, subsequent instructions may begin execution before
the read operation is performed. The following items may guide software seeking to order executions of RDTSC:
- If software requires RDTSC to be executed only after all previous instructions have executed and all previous loads are globally visible, it can execute LFENCE immediately before RDTSC.
- If software requires RDTSC to be executed only after all previous instructions have executed and all previous loads and stores are globally visible, it can execute the sequence MFENCE;LFENCE immediately before RDTSC.
- If software requires RDTSC to be executed prior to execution of any subsequent instruction (including any memory accesses), it can execute the sequence LFENCE immediately after RDTSC.

A:
In page 4-560 -> RDTSCP—Read Time-Stamp Counter and Processor ID:

The RDTSCP instruction is not a serializing instruction,
but it does wait until all previous instructions have executed and all previous loads are globally visible. 1 But it does not wait for previous stores to be globally visible, and subsequent instructions may begin execution before the read operation is performed. The following items may guide softwareseeking to order executions of RDTSCP:
- If software requires RDTSCP to be executed only after all previous stores are globally visible, it can execute MFENCE immediately before RDTSCP.
- If software requires RDTSCP to be executed prior to execution of any subsequent instruction (including any memory accesses), itcan execute LFENCE immediately after RDTSCP.

Quoted 8 lines

Q:
Why there is a cpuid serilizing instruction before rdtsc instruction, but "grub_get_tsc" still cannot work as expect?

A:

Quoted 1 line

From Intel® 64 and IA-32 Architectures Software Developer's

Manual Volume 2A: Instruction Set Reference, A-L:
https://cdrdv2.intel.com/v1/dl/getContent/671199

Ditto...

Quoted 22 lines

In page 3-222 -> CPUID—CPU Identification:
"CPUID" can be executed at any privilege level to serialize instruction execution. Serializing instruction execution guarantees that any modifications to flags, registers, and memory for previous instructions are completed before the next instruction is fetched and executed.

So we only kept the instruction "rdtsc" and its previous instruction in order currently. But it is still out-of-order possibility between rdtsc instruction and its subsequent instruction.

Q:
Why do we do this fix?

A:
In the one hand, Add "cpuid" instruction after "rdtsc" instruction to make sure "rdtsc" instruction to be executed prior to execution of any subsequent instruction, about serializing execution that all previous instructions have been executed before "rdtsc", there is a "cpuid" usage in original code.
In the other hand, using "cpuid" instruction ranther than "lfence" can make sure a forward compatibility for previous HW.

Quoted 6 lines

Base this fix, we did 1500 cycles power on/off stress test, and did not reproduce this issue again.

Quoted 9 lines
---
 include/grub/i386/tsc.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/grub/i386/tsc.h +0 -0
index 947e62fa1..1814704fc 100644

include/grub/i386/tsc.h grub_get_tsc (void) · line 55

Quoted 9 lines
include/grub/i386/tsc.h +6 -0
@@ -47,6 +47,12 @@ grub_get_tsc (void)
   /* Read TSC value.  We cannot use "=A", since this would use
      %rax on x86_64. */
   asm volatile ("rdtsc":"=a" (lo), "=d" (hi));
+  /* Add CPUID instruction after rdtsc instruction to ensure
+   * rdtsc instruction's execution is prior to subsequent
+   * instruction to avoid out-of-order bewteen rdtsc and
+   * its subsequent instruction.
+   */
+  grub_cpuid (0,a,b,c,d);

Wrong coding style for comment and code. I will fix both for you this time...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

author Reviewed-by Daniel Kiper
#7 lore raw

Re: [External] Re: [PATCH] BugFix: grub menu gets stuck due to unserialized rdtsc

Got it. Thanks for your help Daniel!

-------------------------------------------------------------------
Best Regards,
段亚勇 Data-SYS-STE-操作系统
上海市杨浦区抖音新江湾广场 T2B 3F
-------------------------------------------------------------------
From: "Daniel Kiper"<[email protected]>
Date: Fri, Dec 20, 2024, 03:49
Subject: [External] Re: [PATCH] BugFix: grub menu gets stuck due to unserialized rdtsc
To: "Duan Yayong"<[email protected]>
Cc: <[email protected]>, <[email protected]>, < [email protected]>, <[email protected]>, < [email protected]>, "Li Yongqiang"<[email protected]>, "Sun Ming"<[email protected]>

Quoted 12 lines · Duan Yayong

This patch is used to fix grub menu gets stuck in server
AC poweron/poweroff stress test of x86_64, which is reproduced with 1/200 ratio. The root cause analysis as below:

Q:
What's the code logic?
A:
"grub_tsc_init" function will init tsc by setting grub_tsc_rate, which call stack is:

grub_tsc_init -> grub_tsc_calibrate_from_pmtimer -> grub_divmod64

Quoted 9 lines

Among, "grub_divmod64" function needs "tsc_diff" as the second parameter. In "grub_pmtimer_wait_count_tsc", we will call "grub_get_tsc" function to get time stamp counter value to assign to "start_tsc" variable, and get into "while(1)" loop space to get "end_tsc" variable value with same function, after 3580 ticks, return "end_tsc - start_tsc". Actually, "rdtsc" instruction will be called in "grub_get_tsc", but "rdtsc" instruction is not reliable(for the reason see the next question), which will cause "tsc_diff" to be a very big number larger than (1UL << 32) or a negative number, so that grub_tsc_rate will be

zero.

Quoted 2 lines

When "run_menu" function is startup, and calls "grub_tsc_get_time_ms" function to get current time to check if timeout time reach, at this

time,

Quoted 8 lines

"grub_tsc_get_time_ms" function will return zero due to zero "grub_tsc_rate" variable, then grub menu gets stuck...

Q:
What's the difference between rdtsc and rdtscp instructions in x86_64 architecture? Here is more explanations from Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 2B: https://cdrdv2.intel.com/v1/dl/getContent/671241.

I would suggest to add document version number next time.

Quoted 11 lines

A:
In page 4-558 -> RDTSC—Read Time-Stamp Counter:

The RDTSC instruction is not a serializing instruction.
It does not necessarily wait until all previous instructions have been executed before reading the counter.
Similarly, subsequent instructions may begin execution before the read operation is performed. The following items may guide software seeking to order executions of RDTSC:
- If software requires RDTSC to be executed only after all previous instructions have executed and all previous loads are globally

visible,

Quoted 3 lines

it can execute LFENCE immediately before RDTSC.
- If software requires RDTSC to be executed only after all previous instructions have executed and all previous loads and stores are

globally

Quoted 4 lines

visible, it can execute the sequence MFENCE;LFENCE immediately before RDTSC.
- If software requires RDTSC to be executed prior to execution of any subsequent instruction (including any memory accesses), it can

execute

Quoted 18 lines

the sequence LFENCE immediately after RDTSC.

A:
In page 4-560 -> RDTSCP—Read Time-Stamp Counter and Processor ID:

The RDTSCP instruction is not a serializing instruction,
but it does wait until all previous instructions have executed and all previous loads are globally visible. 1 But it does not wait for previous stores to be globally visible, and subsequent instructions may begin execution before the read operation is performed. The following items may guide softwareseeking to order executions of RDTSCP:
- If software requires RDTSCP to be executed only after all previous stores are globally visible, it can execute MFENCE immediately before RDTSCP.
- If software requires RDTSCP to be executed prior to execution of any subsequent instruction (including any memory accesses), itcan execute LFENCE immediately after RDTSCP.

Quoted 8 lines

Q:
Why there is a cpuid serilizing instruction before rdtsc instruction, but "grub_get_tsc" still cannot work as expect?

A:

Quoted 1 line

From Intel® 64 and IA-32 Architectures Software Developer's

Manual Volume 2A: Instruction Set Reference, A-L:
https://cdrdv2.intel.com/v1/dl/getContent/671199

Ditto...

Quoted 2 lines

In page 3-222 -> CPUID—CPU Identification:
"CPUID" can be executed at any privilege level to serialize instruction

execution.

Quoted 1 line

Serializing instruction execution guarantees that any modifications to

flags,

Quoted 4 lines

registers, and memory for previous instructions are completed before the next instruction is fetched and executed.

So we only kept the instruction "rdtsc" and its previous instruction in

order

Quoted 15 lines

currently. But it is still out-of-order possibility between rdtsc instruction and its subsequent instruction.

Q:
Why do we do this fix?

A:
In the one hand, Add "cpuid" instruction after "rdtsc" instruction to make sure "rdtsc" instruction to be executed prior to execution of any subsequent instruction, about serializing execution that all previous instructions have been executed before "rdtsc", there is a "cpuid" usage in original code.
In the other hand, using "cpuid" instruction ranther than "lfence" can make sure a forward compatibility for previous HW.

Quoted 6 lines

Base this fix, we did 1500 cycles power on/off stress test, and did not reproduce this issue again.

Quoted 9 lines
---
 include/grub/i386/tsc.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/grub/i386/tsc.h +0 -0
index 947e62fa1..1814704fc 100644

include/grub/i386/tsc.h grub_get_tsc (void) · line 55

Quoted 9 lines
include/grub/i386/tsc.h +6 -0
@@ -47,6 +47,12 @@ grub_get_tsc (void)
   /* Read TSC value.  We cannot use "=A", since this would use
      %rax on x86_64. */
   asm volatile ("rdtsc":"=a" (lo), "=d" (hi));
+  /* Add CPUID instruction after rdtsc instruction to ensure
+   * rdtsc instruction's execution is prior to subsequent
+   * instruction to avoid out-of-order bewteen rdtsc and
+   * its subsequent instruction.
+   */
+  grub_cpuid (0,a,b,c,d);

Wrong coding style for comment and code. I will fix both for you this time...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

author Reviewed-by Daniel Kiper
#8 lore raw

Re: [External] Re: [PATCH] BugFix: grub menu gets stuck due to unserialized rdtsc

Hi Daniel,
Sorry for disturbing you again!
May I know the ETA of my two patches arriving at grub master branch? Because we recognized that debian 12.10 version is going to be planned in debian community.
We sincerely hope we can use this bugfix in debian 12.10.

-------------------------------------------------------------------
Best Regards,
Will Duan Data-SYS-STE-OS
3F, T2B, Xinjiangwan Square, Douyin, Yangpu District, Shanghai
-------------------------------------------------------------------
From: "段亚勇"<[email protected]>
Date: Fri, Dec 20, 2024, 17:02
Subject: Re: [External] Re: [PATCH] BugFix: grub menu gets stuck due to unserialized rdtsc
To: "Daniel Kiper"<[email protected]>
Cc: <[email protected]>, <[email protected]>, < [email protected]>, <[email protected]>, < [email protected]>, "Li Yongqiang"<[email protected]>, "Sun Ming"<[email protected]>
Got it. Thanks for your help Daniel!

-------------------------------------------------------------------
Best Regards,
段亚勇 Data-SYS-STE-操作系统
上海市杨浦区抖音新江湾广场 T2B 3F
-------------------------------------------------------------------
From: "Daniel Kiper"<[email protected]>
Date: Fri, Dec 20, 2024, 03:49
Subject: [External] Re: [PATCH] BugFix: grub menu gets stuck due to unserialized rdtsc
To: "Duan Yayong"<[email protected]>
Cc: <[email protected]>, <[email protected]>, < [email protected]>, <[email protected]>, < [email protected]>, "Li Yongqiang"<[email protected]>, "Sun Ming"<[email protected]>

Quoted 12 lines · Duan Yayong

This patch is used to fix grub menu gets stuck in server
AC poweron/poweroff stress test of x86_64, which is reproduced with 1/200 ratio. The root cause analysis as below:

Q:
What's the code logic?
A:
"grub_tsc_init" function will init tsc by setting grub_tsc_rate, which call stack is:

grub_tsc_init -> grub_tsc_calibrate_from_pmtimer -> grub_divmod64

Quoted 9 lines

Among, "grub_divmod64" function needs "tsc_diff" as the second parameter. In "grub_pmtimer_wait_count_tsc", we will call "grub_get_tsc" function to get time stamp counter value to assign to "start_tsc" variable, and get into "while(1)" loop space to get "end_tsc" variable value with same function, after 3580 ticks, return "end_tsc - start_tsc". Actually, "rdtsc" instruction will be called in "grub_get_tsc", but "rdtsc" instruction is not reliable(for the reason see the next question), which will cause "tsc_diff" to be a very big number larger than (1UL << 32) or a negative number, so that grub_tsc_rate will be

zero.

Quoted 2 lines

When "run_menu" function is startup, and calls "grub_tsc_get_time_ms" function to get current time to check if timeout time reach, at this

time,

Quoted 8 lines

"grub_tsc_get_time_ms" function will return zero due to zero "grub_tsc_rate" variable, then grub menu gets stuck...

Q:
What's the difference between rdtsc and rdtscp instructions in x86_64 architecture? Here is more explanations from Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 2B: https://cdrdv2.intel.com/v1/dl/getContent/671241.

I would suggest to add document version number next time.

Quoted 11 lines

A:
In page 4-558 -> RDTSC—Read Time-Stamp Counter:

The RDTSC instruction is not a serializing instruction.
It does not necessarily wait until all previous instructions have been executed before reading the counter.
Similarly, subsequent instructions may begin execution before the read operation is performed. The following items may guide software seeking to order executions of RDTSC:
- If software requires RDTSC to be executed only after all previous instructions have executed and all previous loads are globally

visible,

Quoted 3 lines

it can execute LFENCE immediately before RDTSC.
- If software requires RDTSC to be executed only after all previous instructions have executed and all previous loads and stores are

globally

Quoted 4 lines

visible, it can execute the sequence MFENCE;LFENCE immediately before RDTSC.
- If software requires RDTSC to be executed prior to execution of any subsequent instruction (including any memory accesses), it can

execute

Quoted 18 lines

the sequence LFENCE immediately after RDTSC.

A:
In page 4-560 -> RDTSCP—Read Time-Stamp Counter and Processor ID:

The RDTSCP instruction is not a serializing instruction,
but it does wait until all previous instructions have executed and all previous loads are globally visible. 1 But it does not wait for previous stores to be globally visible, and subsequent instructions may begin execution before the read operation is performed. The following items may guide softwareseeking to order executions of RDTSCP:
- If software requires RDTSCP to be executed only after all previous stores are globally visible, it can execute MFENCE immediately before RDTSCP.
- If software requires RDTSCP to be executed prior to execution of any subsequent instruction (including any memory accesses), itcan execute LFENCE immediately after RDTSCP.

Quoted 8 lines

Q:
Why there is a cpuid serilizing instruction before rdtsc instruction, but "grub_get_tsc" still cannot work as expect?

A:

Quoted 1 line

From Intel® 64 and IA-32 Architectures Software Developer's

Manual Volume 2A: Instruction Set Reference, A-L:
https://cdrdv2.intel.com/v1/dl/getContent/671199

Ditto...

Quoted 2 lines

In page 3-222 -> CPUID—CPU Identification:
"CPUID" can be executed at any privilege level to serialize instruction

execution.

Quoted 1 line

Serializing instruction execution guarantees that any modifications to

flags,

Quoted 4 lines

registers, and memory for previous instructions are completed before the next instruction is fetched and executed.

So we only kept the instruction "rdtsc" and its previous instruction in

order

Quoted 15 lines

currently. But it is still out-of-order possibility between rdtsc instruction and its subsequent instruction.

Q:
Why do we do this fix?

A:
In the one hand, Add "cpuid" instruction after "rdtsc" instruction to make sure "rdtsc" instruction to be executed prior to execution of any subsequent instruction, about serializing execution that all previous instructions have been executed before "rdtsc", there is a "cpuid" usage in original code.
In the other hand, using "cpuid" instruction ranther than "lfence" can make sure a forward compatibility for previous HW.

Quoted 6 lines

Base this fix, we did 1500 cycles power on/off stress test, and did not reproduce this issue again.

Quoted 9 lines
---
 include/grub/i386/tsc.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/grub/i386/tsc.h +0 -0
index 947e62fa1..1814704fc 100644

include/grub/i386/tsc.h grub_get_tsc (void) · line 55

Quoted 9 lines
include/grub/i386/tsc.h +6 -0
@@ -47,6 +47,12 @@ grub_get_tsc (void)
   /* Read TSC value.  We cannot use "=A", since this would use
      %rax on x86_64. */
   asm volatile ("rdtsc":"=a" (lo), "=d" (hi));
+  /* Add CPUID instruction after rdtsc instruction to ensure
+   * rdtsc instruction's execution is prior to subsequent
+   * instruction to avoid out-of-order bewteen rdtsc and
+   * its subsequent instruction.
+   */
+  grub_cpuid (0,a,b,c,d);

Wrong coding style for comment and code. I will fix both for you this time...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

author Reviewed-by Daniel Kiper
#9 lore raw

Re: [PATCH] BugFix: grub menu gets stuck due to unserialized rdtsc

In reply to 段亚勇 via Grub-devel

Hello Daniel,
May I know the *Merge Plan* of Grub Master branch?
From this time, we almost check grub master changes every day and take enough patience to wait for the bugfix to be merged. But we found the recent update of master branch has no our two bugfix patches. If there are any extra problems, we sincerely hope we can know it, so that we can plan our OS release.

Our two bugfix patches:
https://lists.gnu.org/archive/html/grub-devel/2024-11/msg00222.html
https://lists.gnu.org/archive/html/grub-devel/2025-01/msg00051.html

Looking forward to your reply.
-------------------------------------------------------------------
Best Regards,
Will Duan Data-SYS-STE-OS
3F, T2B, Xinjiangwan Square, Douyin, Yangpu District, ShanghaiF
-------------------------------------------------------------------
From: "段亚勇"<[email protected]>
Date: Fri, Dec 20, 2024, 17:02
Subject: Re: [External] Re: [PATCH] BugFix: grub menu gets stuck due to unserialized rdtsc
To: "Daniel Kiper"<[email protected]>
Cc: <[email protected]>, <[email protected]>, < [email protected]>, <[email protected]>, < [email protected]>, "Li Yongqiang"<[email protected]>, "Sun Ming"<[email protected]>
Got it. Thanks for your help Daniel!

-------------------------------------------------------------------
Best Regards,
段亚勇 Data-SYS-STE-操作系统
上海市杨浦区抖音新江湾广场 T2B 3F
-------------------------------------------------------------------
From: "Daniel Kiper"<[email protected]>
Date: Fri, Dec 20, 2024, 03:49
Subject: [External] Re: [PATCH] BugFix: grub menu gets stuck due to unserialized rdtsc
To: "Duan Yayong"<[email protected]>
Cc: <[email protected]>, <[email protected]>, < [email protected]>, <[email protected]>, < [email protected]>, "Li Yongqiang"<[email protected]>, "Sun Ming"<[email protected]>

Quoted 12 lines · Duan Yayong

This patch is used to fix grub menu gets stuck in server
AC poweron/poweroff stress test of x86_64, which is reproduced with 1/200 ratio. The root cause analysis as below:

Q:
What's the code logic?
A:
"grub_tsc_init" function will init tsc by setting grub_tsc_rate, which call stack is:

grub_tsc_init -> grub_tsc_calibrate_from_pmtimer -> grub_divmod64

Quoted 9 lines

Among, "grub_divmod64" function needs "tsc_diff" as the second parameter. In "grub_pmtimer_wait_count_tsc", we will call "grub_get_tsc" function to get time stamp counter value to assign to "start_tsc" variable, and get into "while(1)" loop space to get "end_tsc" variable value with same function, after 3580 ticks, return "end_tsc - start_tsc". Actually, "rdtsc" instruction will be called in "grub_get_tsc", but "rdtsc" instruction is not reliable(for the reason see the next question), which will cause "tsc_diff" to be a very big number larger than (1UL << 32) or a negative number, so that grub_tsc_rate will be

zero.

Quoted 2 lines

When "run_menu" function is startup, and calls "grub_tsc_get_time_ms" function to get current time to check if timeout time reach, at this

time,

Quoted 8 lines

"grub_tsc_get_time_ms" function will return zero due to zero "grub_tsc_rate" variable, then grub menu gets stuck...

Q:
What's the difference between rdtsc and rdtscp instructions in x86_64 architecture? Here is more explanations from Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 2B: https://cdrdv2.intel.com/v1/dl/getContent/671241.

I would suggest to add document version number next time.

Quoted 11 lines

A:
In page 4-558 -> RDTSC—Read Time-Stamp Counter:

The RDTSC instruction is not a serializing instruction.
It does not necessarily wait until all previous instructions have been executed before reading the counter.
Similarly, subsequent instructions may begin execution before the read operation is performed. The following items may guide software seeking to order executions of RDTSC:
- If software requires RDTSC to be executed only after all previous instructions have executed and all previous loads are globally

visible,

Quoted 3 lines

it can execute LFENCE immediately before RDTSC.
- If software requires RDTSC to be executed only after all previous instructions have executed and all previous loads and stores are

globally

Quoted 4 lines

visible, it can execute the sequence MFENCE;LFENCE immediately before RDTSC.
- If software requires RDTSC to be executed prior to execution of any subsequent instruction (including any memory accesses), it can

execute

Quoted 18 lines

the sequence LFENCE immediately after RDTSC.

A:
In page 4-560 -> RDTSCP—Read Time-Stamp Counter and Processor ID:

The RDTSCP instruction is not a serializing instruction,
but it does wait until all previous instructions have executed and all previous loads are globally visible. 1 But it does not wait for previous stores to be globally visible, and subsequent instructions may begin execution before the read operation is performed. The following items may guide softwareseeking to order executions of RDTSCP:
- If software requires RDTSCP to be executed only after all previous stores are globally visible, it can execute MFENCE immediately before RDTSCP.
- If software requires RDTSCP to be executed prior to execution of any subsequent instruction (including any memory accesses), itcan execute LFENCE immediately after RDTSCP.

Quoted 8 lines

Q:
Why there is a cpuid serilizing instruction before rdtsc instruction, but "grub_get_tsc" still cannot work as expect?

A:

Quoted 1 line

From Intel® 64 and IA-32 Architectures Software Developer's

Manual Volume 2A: Instruction Set Reference, A-L:
https://cdrdv2.intel.com/v1/dl/getContent/671199

Ditto...

Quoted 2 lines

In page 3-222 -> CPUID—CPU Identification:
"CPUID" can be executed at any privilege level to serialize instruction

execution.

Quoted 1 line

Serializing instruction execution guarantees that any modifications to

flags,

Quoted 4 lines

registers, and memory for previous instructions are completed before the next instruction is fetched and executed.

So we only kept the instruction "rdtsc" and its previous instruction in

order

Quoted 15 lines

currently. But it is still out-of-order possibility between rdtsc instruction and its subsequent instruction.

Q:
Why do we do this fix?

A:
In the one hand, Add "cpuid" instruction after "rdtsc" instruction to make sure "rdtsc" instruction to be executed prior to execution of any subsequent instruction, about serializing execution that all previous instructions have been executed before "rdtsc", there is a "cpuid" usage in original code.
In the other hand, using "cpuid" instruction ranther than "lfence" can make sure a forward compatibility for previous HW.

Quoted 6 lines

Base this fix, we did 1500 cycles power on/off stress test, and did not reproduce this issue again.

Quoted 9 lines
---
 include/grub/i386/tsc.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/grub/i386/tsc.h +0 -0
index 947e62fa1..1814704fc 100644

include/grub/i386/tsc.h grub_get_tsc (void) · line 55

Quoted 9 lines
include/grub/i386/tsc.h +6 -0
@@ -47,6 +47,12 @@ grub_get_tsc (void)
   /* Read TSC value.  We cannot use "=A", since this would use
      %rax on x86_64. */
   asm volatile ("rdtsc":"=a" (lo), "=d" (hi));
+  /* Add CPUID instruction after rdtsc instruction to ensure
+   * rdtsc instruction's execution is prior to subsequent
+   * instruction to avoid out-of-order bewteen rdtsc and
+   * its subsequent instruction.
+   */
+  grub_cpuid (0,a,b,c,d);

Wrong coding style for comment and code. I will fix both for you this time...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#10 lore raw

Re: [PATCH] BugFix: grub menu gets stuck due to unserialized rdtsc

Hi,

Quoted 11 lines · 段亚勇

Hello Daniel,
May I know the Merge Plan of Grub Master branch?
From this time, we almost check grub master changes every day and take enough patience to wait for the bugfix to be merged. But we found the recent update of master branch has no our two bugfix patches. If there are any extra problems, we sincerely hope we can know it, so that we can plan our OS release.

Our two bugfix patches:
https://lists.gnu.org/archive/html/grub-devel/2024-11/msg00222.html
https://lists.gnu.org/archive/html/grub-devel/2025-01/msg00051.html

The second one is queued for push. It should happen no later than by the end of the week. Sorry for delay...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#11 lore raw

Re: [External] Re: [PATCH] BugFix: grub menu gets stuck due to unserialized rdtsc

Hi,

That's alright!
May I know if the first patch is allowed to be merged? And how long will it be merged approximately?
So that I can track it after that time! Thanks!
-------------------------------------------------------------------
Best Regards,
Will Duan Data-SYS-STE-OS
3F, T2B, Xinjiangwan Square, Yangpu District, Shanghai
-------------------------------------------------------------------
From: "Daniel Kiper"<[email protected]>
Date: Tue, Feb 25, 2025, 23:35
Subject: [External] Re: [PATCH] BugFix: grub menu gets stuck due to unserialized rdtsc
To: "段亚勇"<[email protected]>
Cc: <[email protected]>, <[email protected]>, < [email protected]>, <[email protected]>, < [email protected]>, "Li Yongqiang"<[email protected]>, "Sun Ming"<[email protected]>
Hi,

Quoted 11 lines · 段亚勇

Hello Daniel,
May I know the Merge Plan of Grub Master branch?
From this time, we almost check grub master changes every day and take enough patience to wait for the bugfix to be merged. But we found the recent update of master branch has no our two bugfix patches. If there are any extra problems, we sincerely hope we can know it, so that we can plan our OS release.

Our two bugfix patches:
https://lists.gnu.org/archive/html/grub-devel/2024-11/msg00222.html
https://lists.gnu.org/archive/html/grub-devel/2025-01/msg00051.html

The second one is queued for push. It should happen no later than by the end of the week. Sorry for delay...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#12 lore raw

Re: [External] Re: [PATCH] BugFix: grub menu gets stuck due to unserialized rdtsc

Quoted 6 lines · 段亚勇 via Grub-devel

Hi,

That's alright!
May I know if the first patch is allowed to be merged? And how long will it be merged approximately?
So that I can track it after that time! Thanks!

Sorry, I thought the first patch is earlier version of the second. Both will be merged by the end of this week.

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#13 lore raw

Re: [External] Re: [PATCH] BugFix: grub menu gets stuck due to unserialized rdtsc

Got it! Thanks so much!

-------------------------------------------------------------------
Best Regards,
Will Duan Data-SYS-STE-OS
3F, T2B, Xinjiangwan Square, Yangpu District, Shanghai
-------------------------------------------------------------------
From: "Daniel Kiper"<[email protected]>
Date: Wed, Feb 26, 2025, 21:39
Subject: Re: [External] Re: [PATCH] BugFix: grub menu gets stuck due to unserialized rdtsc
To: "段亚勇"<[email protected]>
Cc: <[email protected]>, <[email protected]>, < [email protected]>, <[email protected]>, < [email protected]>, "Li Yongqiang"<[email protected]>, "Sun Ming"<[email protected]>

Quoted 4 lines · 段亚勇 via Grub-devel

Hi,

That's alright!
May I know if the first patch is allowed to be merged? And how long will

it be

Quoted 2 lines

merged approximately?
So that I can track it after that time! Thanks!

Sorry, I thought the first patch is earlier version of the second. Both will be merged by the end of this week.

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel