diff mbox series

[v13,11/13] tsc: Switch to native sched clock

Message ID 20241021055156.2342564-12-nikunj@amd.com (mailing list archive)
State New
Headers show
Series Add Secure TSC support for SNP guests | expand

Commit Message

Nikunj A. Dadhania Oct. 21, 2024, 5:51 a.m. UTC
Although the kernel switches over to stable TSC clocksource instead of PV
clocksource, the scheduler still keeps on using PV clocks as the sched
clock source. This is because the following KVM, Xen and VMWare, switches
the paravirt sched clock handler in their init routines. The HyperV is the
only PV clock source that checks if the platform provides invariant TSC and
does not switch to PV sched clock.

When switching back to stable TSC, restore the scheduler clock to
native_sched_clock().

As the clock selection happens in the stop machine context, schedule
delayed work to update the static_call()

Cc: Alexey Makhalov <alexey.makhalov@broadcom.com>
Cc: Juergen Gross <jgross@suse.com>
Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Signed-off-by: Nikunj A Dadhania <nikunj@amd.com>
---
 arch/x86/kernel/tsc.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

Comments

Tom Lendacky Oct. 21, 2024, 2:37 p.m. UTC | #1
On 10/21/24 00:51, Nikunj A Dadhania wrote:
> Although the kernel switches over to stable TSC clocksource instead of PV
> clocksource, the scheduler still keeps on using PV clocks as the sched
> clock source. This is because the following KVM, Xen and VMWare, switches

s/the following//
s/switches/switch/

> the paravirt sched clock handler in their init routines. The HyperV is the

s/The HyperV/HyperV/

> only PV clock source that checks if the platform provides invariant TSC and

s/provides invariant/provides an invariant/

Thanks,
Tom

> does not switch to PV sched clock.
> 
> When switching back to stable TSC, restore the scheduler clock to
> native_sched_clock().
> 
> As the clock selection happens in the stop machine context, schedule
> delayed work to update the static_call()
> 
> Cc: Alexey Makhalov <alexey.makhalov@broadcom.com>
> Cc: Juergen Gross <jgross@suse.com>
> Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
> Signed-off-by: Nikunj A Dadhania <nikunj@amd.com>
> ---
>  arch/x86/kernel/tsc.c | 19 +++++++++++++++++++
>  1 file changed, 19 insertions(+)
> 
> diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c
> index 27faf121fb78..38e35cac6c42 100644
> --- a/arch/x86/kernel/tsc.c
> +++ b/arch/x86/kernel/tsc.c
> @@ -272,10 +272,25 @@ bool using_native_sched_clock(void)
>  {
>  	return static_call_query(pv_sched_clock) == native_sched_clock;
>  }
> +
> +static void enable_native_sc_work(struct work_struct *work)
> +{
> +	pr_info("using native sched clock\n");
> +	paravirt_set_sched_clock(native_sched_clock);
> +}
> +static DECLARE_DELAYED_WORK(enable_native_sc, enable_native_sc_work);
> +
> +static void enable_native_sched_clock(void)
> +{
> +	if (!using_native_sched_clock())
> +		schedule_delayed_work(&enable_native_sc, 0);
> +}
>  #else
>  u64 sched_clock_noinstr(void) __attribute__((alias("native_sched_clock")));
>  
>  bool using_native_sched_clock(void) { return true; }
> +
> +void enable_native_sched_clock(void) { }
>  #endif
>  
>  notrace u64 sched_clock(void)
> @@ -1157,6 +1172,10 @@ static void tsc_cs_tick_stable(struct clocksource *cs)
>  static int tsc_cs_enable(struct clocksource *cs)
>  {
>  	vclocks_set_used(VDSO_CLOCKMODE_TSC);
> +
> +	/* Restore native_sched_clock() when switching to TSC */
> +	enable_native_sched_clock();
> +
>  	return 0;
>  }
>
kernel test robot Oct. 21, 2024, 9:30 p.m. UTC | #2
Hi Nikunj,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 0a895c0d9b73d934de95aa0dd4e631c394bdd25d]

url:    https://github.com/intel-lab-lkp/linux/commits/Nikunj-A-Dadhania/x86-sev-Carve-out-and-export-SNP-guest-messaging-init-routines/20241021-140125
base:   0a895c0d9b73d934de95aa0dd4e631c394bdd25d
patch link:    https://lore.kernel.org/r/20241021055156.2342564-12-nikunj%40amd.com
patch subject: [PATCH v13 11/13] tsc: Switch to native sched clock
config: x86_64-allnoconfig (https://download.01.org/0day-ci/archive/20241022/202410220542.6y7YYM8W-lkp@intel.com/config)
compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241022/202410220542.6y7YYM8W-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202410220542.6y7YYM8W-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> arch/x86/kernel/tsc.c:293:6: warning: no previous prototype for function 'enable_native_sched_clock' [-Wmissing-prototypes]
     293 | void enable_native_sched_clock(void) { }
         |      ^
   arch/x86/kernel/tsc.c:293:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
     293 | void enable_native_sched_clock(void) { }
         | ^
         | static 
   1 warning generated.


vim +/enable_native_sched_clock +293 arch/x86/kernel/tsc.c

   292	
 > 293	void enable_native_sched_clock(void) { }
   294	#endif
   295
kernel test robot Oct. 21, 2024, 11:03 p.m. UTC | #3
Hi Nikunj,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 0a895c0d9b73d934de95aa0dd4e631c394bdd25d]

url:    https://github.com/intel-lab-lkp/linux/commits/Nikunj-A-Dadhania/x86-sev-Carve-out-and-export-SNP-guest-messaging-init-routines/20241021-140125
base:   0a895c0d9b73d934de95aa0dd4e631c394bdd25d
patch link:    https://lore.kernel.org/r/20241021055156.2342564-12-nikunj%40amd.com
patch subject: [PATCH v13 11/13] tsc: Switch to native sched clock
config: i386-buildonly-randconfig-002-20241022 (https://download.01.org/0day-ci/archive/20241022/202410220640.W8Ynjt5W-lkp@intel.com/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241022/202410220640.W8Ynjt5W-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202410220640.W8Ynjt5W-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> arch/x86/kernel/tsc.c:293:6: warning: no previous prototype for 'enable_native_sched_clock' [-Wmissing-prototypes]
     293 | void enable_native_sched_clock(void) { }
         |      ^~~~~~~~~~~~~~~~~~~~~~~~~


vim +/enable_native_sched_clock +293 arch/x86/kernel/tsc.c

   292	
 > 293	void enable_native_sched_clock(void) { }
   294	#endif
   295
Nikunj A. Dadhania Oct. 22, 2024, 4:26 a.m. UTC | #4
On 10/21/2024 8:07 PM, Tom Lendacky wrote:
> On 10/21/24 00:51, Nikunj A Dadhania wrote:
>> Although the kernel switches over to stable TSC clocksource instead of PV
>> clocksource, the scheduler still keeps on using PV clocks as the sched
>> clock source. This is because the following KVM, Xen and VMWare, switches
> 
> s/the following//
> s/switches/switch/
> 
>> the paravirt sched clock handler in their init routines. The HyperV is the
> 
> s/The HyperV/HyperV/
> 
>> only PV clock source that checks if the platform provides invariant TSC and
> 
> s/provides invariant/provides an invariant/

Sure, I will update the message.

Regards
Nikunj
diff mbox series

Patch

diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c
index 27faf121fb78..38e35cac6c42 100644
--- a/arch/x86/kernel/tsc.c
+++ b/arch/x86/kernel/tsc.c
@@ -272,10 +272,25 @@  bool using_native_sched_clock(void)
 {
 	return static_call_query(pv_sched_clock) == native_sched_clock;
 }
+
+static void enable_native_sc_work(struct work_struct *work)
+{
+	pr_info("using native sched clock\n");
+	paravirt_set_sched_clock(native_sched_clock);
+}
+static DECLARE_DELAYED_WORK(enable_native_sc, enable_native_sc_work);
+
+static void enable_native_sched_clock(void)
+{
+	if (!using_native_sched_clock())
+		schedule_delayed_work(&enable_native_sc, 0);
+}
 #else
 u64 sched_clock_noinstr(void) __attribute__((alias("native_sched_clock")));
 
 bool using_native_sched_clock(void) { return true; }
+
+void enable_native_sched_clock(void) { }
 #endif
 
 notrace u64 sched_clock(void)
@@ -1157,6 +1172,10 @@  static void tsc_cs_tick_stable(struct clocksource *cs)
 static int tsc_cs_enable(struct clocksource *cs)
 {
 	vclocks_set_used(VDSO_CLOCKMODE_TSC);
+
+	/* Restore native_sched_clock() when switching to TSC */
+	enable_native_sched_clock();
+
 	return 0;
 }