x86/tsc: Add tsc_guess flag disabling CPUID.16h use for tsc calibration
diff mbox series

Message ID 03j72W25Dne_HDSwI8Y7xiXPzvEBX5Ezw_xw8ed8DC83bpdMxoPcjhbinNcDD0yeoX9GGN691f3kqqtGLztTnW8Pay3FrbO5sTlj3vjnh-Y=@protonmail.com
State New
Headers show
Series
  • x86/tsc: Add tsc_guess flag disabling CPUID.16h use for tsc calibration
Related show

Commit Message

Krzysztof Piecuch Jan. 14, 2020, 2:23 p.m. UTC
Changing base clock frequency directly impacts tsc hz but not CPUID.16h
values. An overclocked CPU supporting CPUID.16h and partial CPUID.15h
support will set tsc hz according to "best guess" given by CPUID.16h
relying on tsc_refine_calibration_work to give better numbers later.
tsc_refine_calibration_work will refuse to do its work when the outcome is
off the early tsc hz value by more than 1% which is certain to happen on an
overclocked system.

Signed-off-by: Krzysztof Piecuch <piecuch@protonmail.com>
---
 Documentation/admin-guide/kernel-parameters.txt |  6 ++++++
 arch/x86/kernel/tsc.c                           | 12 ++++++++++--
 2 files changed, 16 insertions(+), 2 deletions(-)

--
2.20.1

Comments

Andy Lutomirski Jan. 15, 2020, 8:15 p.m. UTC | #1
On Tue, Jan 14, 2020 at 6:39 AM Krzysztof Piecuch
<piecuch@protonmail.com> wrote:
>
> Changing base clock frequency directly impacts tsc hz but not CPUID.16h
> values. An overclocked CPU supporting CPUID.16h and partial CPUID.15h
> support will set tsc hz according to "best guess" given by CPUID.16h
> relying on tsc_refine_calibration_work to give better numbers later.
> tsc_refine_calibration_work will refuse to do its work when the outcome is
> off the early tsc hz value by more than 1% which is certain to happen on an
> overclocked system.
>
> Signed-off-by: Krzysztof Piecuch <piecuch@protonmail.com>
> ---
>  Documentation/admin-guide/kernel-parameters.txt |  6 ++++++
>  arch/x86/kernel/tsc.c                           | 12 ++++++++++--
>  2 files changed, 16 insertions(+), 2 deletions(-)
>
> diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
> index ade4e6ec23e0..54ae9e153a19 100644
> --- a/Documentation/admin-guide/kernel-parameters.txt
> +++ b/Documentation/admin-guide/kernel-parameters.txt
> @@ -4905,6 +4905,12 @@
>                         interruptions from clocksource watchdog are not
>                         acceptable).
>
> +       tsc_guess=      [X86,INTEL] Don't use data provided by CPUID.16h during
> +                       early tsc calibration. Disabling this may be useful for
> +                       CPUs with altered base clocks.
> +                       Format: <bool> (1/Y/y=enable, 0/N/n=disable)
> +                       default: enabled

This has more negatives than makes sense.  I also think it's wrong.  How about:

tsc_guess= ... Use data provided by CPUID.16h ...
Thomas Gleixner Jan. 15, 2020, 9:47 p.m. UTC | #2
Krzysztof Piecuch <piecuch@protonmail.com> writes:

> Changing base clock frequency directly impacts tsc hz but not CPUID.16h
> values. An overclocked CPU supporting CPUID.16h and partial CPUID.15h
> support will set tsc hz according to "best guess" given by CPUID.16h
> relying on tsc_refine_calibration_work to give better numbers later.
> tsc_refine_calibration_work will refuse to do its work when the outcome is
> off the early tsc hz value by more than 1% which is certain to happen on an
> overclocked system.

The above sets the context which is great, but it does not explain what the
solution is.

> +	tsc_guess=	[X86,INTEL] Don't use data provided by CPUID.16h during
> +			early tsc calibration. Disabling this may be useful for
> +			CPUs with altered base clocks.
> +			Format: <bool> (1/Y/y=enable, 0/N/n=disable)
> +			default: enabled

That's really a misnomer. CPUID.16h is way more than a guess. It's
pretty accurate except for the case you describe. This command line
option should clearly tell what it is about, i.e. overclocking.

Aside of that we have to be careful because on quite some modern systems
CPUID 16h is the only way to calibrate TSC and local APIC because PIT
and HPET are either not exposed or disfunct. So disabling CPUID.16h
should be prominently noted in dmesg.

Thanks,

       tglx
kbuild test robot Jan. 16, 2020, 7:38 a.m. UTC | #3
Hi Krzysztof,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on tip/auto-latest]
[also build test ERROR on linux/master tip/x86/core linus/master v5.5-rc6 next-20200110]
[if your patch is applied to the wrong git tree, please drop us a note to help
improve the system. BTW, we also suggest to use '--base' option to specify the
base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Krzysztof-Piecuch/x86-tsc-Add-tsc_guess-flag-disabling-CPUID-16h-use-for-tsc-calibration/20200115-111400
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 425cc0b850aaaa121b4ddf32c029a72e7a260dfb
config: x86_64-randconfig-c002-20200115 (attached as .config)
compiler: gcc-7 (Debian 7.5.0-3) 7.5.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

If you fix the issue, kindly add following tag
Reported-by: kbuild test robot <lkp@intel.com>

All error/warnings (new ones prefixed by >>):

>> arch/x86//kernel/tsc.c:62:26: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'tsc_guess'
    static bool _read_mostly tsc_guess = 1;
                             ^~~~~~~~~
   arch/x86//kernel/tsc.c: In function 'tsc_guess_setup':
>> arch/x86//kernel/tsc.c:65:25: error: 'tsc_guess' undeclared (first use in this function); did you mean 'tss_desc'?
     return strtobool(buf, &tsc_guess);
                            ^~~~~~~~~
                            tss_desc
   arch/x86//kernel/tsc.c:65:25: note: each undeclared identifier is reported only once for each function it appears in
   arch/x86//kernel/tsc.c: In function 'native_calibrate_tsc':
   arch/x86//kernel/tsc.c:664:26: error: 'tsc_guess' undeclared (first use in this function); did you mean 'tss_desc'?
     if (crystal_khz == 0 && tsc_guess &&
                             ^~~~~~~~~
                             tss_desc
   arch/x86//kernel/tsc.c: In function 'cpu_khz_from_cpuid':
   arch/x86//kernel/tsc.c:703:43: error: 'tsc_guess' undeclared (first use in this function); did you mean 'tss_desc'?
     if (boot_cpu_data.cpuid_level < 0x16 || !tsc_guess)
                                              ^~~~~~~~~
                                              tss_desc
   arch/x86//kernel/tsc.c: In function 'tsc_guess_setup':
>> arch/x86//kernel/tsc.c:66:1: warning: control reaches end of non-void function [-Wreturn-type]
    }
    ^

vim +62 arch/x86//kernel/tsc.c

    61	
  > 62	static bool _read_mostly tsc_guess = 1;
    63	static int __init tsc_guess_setup(char *buf)
    64	{
  > 65		return strtobool(buf, &tsc_guess);
  > 66	}
    67	early_param("tsc_guess", tsc_guess_setup);
    68	

---
0-DAY kernel test infrastructure                 Open Source Technology Center
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org Intel Corporation
kbuild test robot Jan. 16, 2020, 9:41 a.m. UTC | #4
Hi Krzysztof,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on tip/auto-latest]
[also build test ERROR on linux/master tip/x86/core linus/master v5.5-rc6 next-20200110]
[if your patch is applied to the wrong git tree, please drop us a note to help
improve the system. BTW, we also suggest to use '--base' option to specify the
base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Krzysztof-Piecuch/x86-tsc-Add-tsc_guess-flag-disabling-CPUID-16h-use-for-tsc-calibration/20200115-111400
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 425cc0b850aaaa121b4ddf32c029a72e7a260dfb
config: i386-randconfig-a003-20200115 (attached as .config)
compiler: gcc-4.9 (Debian 4.9.2-10+deb8u1) 4.9.2
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

If you fix the issue, kindly add following tag
Reported-by: kbuild test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   arch/x86/kernel/tsc.c:62:26: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'tsc_guess'
    static bool _read_mostly tsc_guess = 1;
                             ^
   arch/x86/kernel/tsc.c: In function 'tsc_guess_setup':
>> arch/x86/kernel/tsc.c:65:25: error: 'tsc_guess' undeclared (first use in this function)
     return strtobool(buf, &tsc_guess);
                            ^
   arch/x86/kernel/tsc.c:65:25: note: each undeclared identifier is reported only once for each function it appears in
   arch/x86/kernel/tsc.c: In function 'native_calibrate_tsc':
   arch/x86/kernel/tsc.c:664:26: error: 'tsc_guess' undeclared (first use in this function)
     if (crystal_khz == 0 && tsc_guess &&
                             ^
   arch/x86/kernel/tsc.c: In function 'cpu_khz_from_cpuid':
   arch/x86/kernel/tsc.c:703:43: error: 'tsc_guess' undeclared (first use in this function)
     if (boot_cpu_data.cpuid_level < 0x16 || !tsc_guess)
                                              ^
   arch/x86/kernel/tsc.c: In function 'tsc_guess_setup':
   arch/x86/kernel/tsc.c:66:1: warning: control reaches end of non-void function [-Wreturn-type]
    }
    ^

vim +/tsc_guess +65 arch/x86/kernel/tsc.c

    61	
    62	static bool _read_mostly tsc_guess = 1;
    63	static int __init tsc_guess_setup(char *buf)
    64	{
  > 65		return strtobool(buf, &tsc_guess);
    66	}
    67	early_param("tsc_guess", tsc_guess_setup);
    68	

---
0-DAY kernel test infrastructure                 Open Source Technology Center
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org Intel Corporation

Patch
diff mbox series

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index ade4e6ec23e0..54ae9e153a19 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -4905,6 +4905,12 @@ 
 			interruptions from clocksource watchdog are not
 			acceptable).

+	tsc_guess=	[X86,INTEL] Don't use data provided by CPUID.16h during
+			early tsc calibration. Disabling this may be useful for
+			CPUs with altered base clocks.
+			Format: <bool> (1/Y/y=enable, 0/N/n=disable)
+			default: enabled
+
 	tsx=		[X86] Control Transactional Synchronization
 			Extensions (TSX) feature in Intel processors that
 			support TSX control.
diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c
index 7e322e2daaf5..a807c33a3d41 100644
--- a/arch/x86/kernel/tsc.c
+++ b/arch/x86/kernel/tsc.c
@@ -59,6 +59,13 @@  struct cyc2ns {

 static DEFINE_PER_CPU_ALIGNED(struct cyc2ns, cyc2ns);

+static bool _read_mostly tsc_guess = 1;
+static int __init tsc_guess_setup(char *buf)
+{
+	return strtobool(buf, &tsc_guess);
+}
+early_param("tsc_guess", tsc_guess_setup);
+
 __always_inline void cyc2ns_read_begin(struct cyc2ns_data *data)
 {
 	int seq, idx;
@@ -654,7 +661,8 @@  unsigned long native_calibrate_tsc(void)
 	 * clock, but we can easily calculate it to a high degree of accuracy
 	 * by considering the crystal ratio and the CPU speed.
 	 */
-	if (crystal_khz == 0 && boot_cpu_data.cpuid_level >= 0x16) {
+	if (crystal_khz == 0 && tsc_guess &&
+		boot_cpu_data.cpuid_level >= 0x16) {
 		unsigned int eax_base_mhz, ebx, ecx, edx;

 		cpuid(0x16, &eax_base_mhz, &ebx, &ecx, &edx);
@@ -692,7 +700,7 @@  static unsigned long cpu_khz_from_cpuid(void)
 	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
 		return 0;

-	if (boot_cpu_data.cpuid_level < 0x16)
+	if (boot_cpu_data.cpuid_level < 0x16 || !tsc_guess)
 		return 0;

 	eax_base_mhz = ebx_max_mhz = ecx_bus_mhz = edx = 0;