Skip to contentSkip to conversation

VM: x86: Return ealier if clocksource has not changed

RFC PATCH In review Chen Yu 2 participants 7 messages Dec 23, 2016 – Dec 29, 2016

Subsystems (4)
lore
Share & tools
lore

Chen Yu

author
#0 lore raw

RFC PATCH 1/1 VM: x86: Return ealier if clocksource has not changed

Currently the notifier of pvclock_gtod_notify() get invoked frequently due to the periodic update_wall_time(). This might slow down the system a little bit as there might be redundant execution code path and unnecessary lock contention
in update_pvclock_gtod(), which was found when I was doing suspend/resume speed testings. As pvclock_gtod_notify() should be invoked only when clocksource has changed, according to Commit 16e8d74d2da9 ("KVM: x86: notifier for clocksource changes") , either we can add a new notifier for clocksource switch, or we can simply bypass the following code in pvclock_gtod_notify() earlier if there is no clocksource switch.

Cc Paolo Bonzini <[email protected]>
Cc "Radim Krcmar" <[email protected]>
Cc Marcelo Tosatti <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Wanpeng Li <[email protected]>
---
 arch/x86/kvm/x86.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +3 -2
index 445c51b..54aa32d 100644
@@ -5961,13 +5961,14 @@ static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
 	struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
 	struct timekeeper *tk = priv;
 
+	if (likely(gtod->clock.vclock_mode == VCLOCK_TSC))
+		return 0;
 	update_pvclock_gtod(tk);
 
 	/* disable master clock if host does not trust, or does not
 	 * use, TSC clocksource
 	 */
-	if (gtod->clock.vclock_mode != VCLOCK_TSC &&
-	    atomic_read(&kvm_guest_has_master_clock) != 0)
+	if (atomic_read(&kvm_guest_has_master_clock) != 0)
 		queue_work(system_long_wq, &pvclock_gtod_work);
 
 	return 0;
Signature · 1 line
2.7.4
#1 lore raw
Quoted 29 lines · Chen Yu

Currently the notifier of pvclock_gtod_notify() get invoked frequently due to the periodic update_wall_time(). This might slow down the system a little bit as there might be redundant execution code path and unnecessary lock contention
in update_pvclock_gtod(), which was found when I was doing suspend/resume speed testings. As pvclock_gtod_notify() should be invoked only when clocksource has changed, according to Commit 16e8d74d2da9 ("KVM: x86: notifier for clocksource changes") , either we can add a new notifier for clocksource switch, or we can simply bypass the following code in pvclock_gtod_notify() earlier if there is no clocksource switch.

Cc Paolo Bonzini <[email protected]>
Cc "Radim Krcmar" <[email protected]>
Cc Marcelo Tosatti <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Wanpeng Li <[email protected]>
---
 arch/x86/kvm/x86.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +0 -0
index 445c51b..54aa32d 100644

arch/x86/kvm/x86.c static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused, · line 5965

Quoted 5 lines
arch/x86/kvm/x86.c +2 -0
@@ -5961,13 +5961,14 @@ static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
 	struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
 	struct timekeeper *tk = priv;
 
+	if (likely(gtod->clock.vclock_mode == VCLOCK_TSC))
+		return 0;

I think this is only safe if any of the values in "struct pvclock_gtod_data" are unchanged. Otherwise the local (KVM) copy is kept incorrect.

Quoted 13 lines
arch/x86/kvm/x86.c +1 -2
 	update_pvclock_gtod(tk);
 
 	/* disable master clock if host does not trust, or does not
 	 * use, TSC clocksource
 	 */
-	if (gtod->clock.vclock_mode != VCLOCK_TSC &&
-	    atomic_read(&kvm_guest_has_master_clock) != 0)
+	if (atomic_read(&kvm_guest_has_master_clock) != 0)
 		queue_work(system_long_wq, &pvclock_gtod_work);
 
 	return 0;
Signature · 1 line
2.7.4
author
#2 lore raw

Hi Marcelo,

Quoted 33 lines · Marcelo Tosatti
Quoted 32 lines · Chen Yu

Currently the notifier of pvclock_gtod_notify() get invoked frequently due to the periodic update_wall_time(). This might slow down the system a little bit as there might be redundant execution code path and unnecessary lock contention
in update_pvclock_gtod(), which was found when I was doing suspend/resume speed testings. As pvclock_gtod_notify() should be invoked only when clocksource has changed, according to Commit 16e8d74d2da9 ("KVM: x86: notifier for clocksource changes") , either we can add a new notifier for clocksource switch, or we can simply bypass the following code in pvclock_gtod_notify() earlier if there is no clocksource switch.

Cc Paolo Bonzini <[email protected]>
Cc "Radim Krcmar" <[email protected]>
Cc Marcelo Tosatti <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Wanpeng Li <[email protected]>
---
 arch/x86/kvm/x86.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +0 -0
index 445c51b..54aa32d 100644
@@ -5961,13 +5961,14 @@ static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
 	struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
 	struct timekeeper *tk = priv;
 
Quoted 6 lines

arch/x86/kvm/x86.c static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused, · line 5965

Quoted 2 lines
arch/x86/kvm/x86.c +2 -0
+	if (likely(gtod->clock.vclock_mode == VCLOCK_TSC))
+		return 0;

I think this is only safe if any of the values in "struct pvclock_gtod_data" are unchanged. Otherwise the local (KVM) copy is kept incorrect.

I missread the code previously and I thought only under the condition the clocksource has been switched to another one will the KVM copy be touched. Apparently it is not the case because the copy should be updated on-time during normal tick, right?
thanks for your reply,
Yu

Quoted 14 lines
Quoted 14 lines
arch/x86/kvm/x86.c +1 -2
 	update_pvclock_gtod(tk);
 
 	/* disable master clock if host does not trust, or does not
 	 * use, TSC clocksource
 	 */
-	if (gtod->clock.vclock_mode != VCLOCK_TSC &&
-	    atomic_read(&kvm_guest_has_master_clock) != 0)
+	if (atomic_read(&kvm_guest_has_master_clock) != 0)
 		queue_work(system_long_wq, &pvclock_gtod_work);
 
 	return 0;
Signature · 1 line
2.7.4
#3 lore raw
Quoted 41 lines · Chen Yu

Hi Marcelo,

Quoted 33 lines · Marcelo Tosatti
Quoted 32 lines · Chen Yu

Currently the notifier of pvclock_gtod_notify() get invoked frequently due to the periodic update_wall_time(). This might slow down the system a little bit as there might be redundant execution code path and unnecessary lock contention
in update_pvclock_gtod(), which was found when I was doing suspend/resume speed testings. As pvclock_gtod_notify() should be invoked only when clocksource has changed, according to Commit 16e8d74d2da9 ("KVM: x86: notifier for clocksource changes") , either we can add a new notifier for clocksource switch, or we can simply bypass the following code in pvclock_gtod_notify() earlier if there is no clocksource switch.

Cc Paolo Bonzini <[email protected]>
Cc "Radim Krcmar" <[email protected]>
Cc Marcelo Tosatti <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Wanpeng Li <[email protected]>
---
 arch/x86/kvm/x86.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +0 -0
index 445c51b..54aa32d 100644
@@ -5961,13 +5961,14 @@ static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
 	struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
 	struct timekeeper *tk = priv;
 
Quoted 6 lines

arch/x86/kvm/x86.c static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused, · line 5965

Quoted 2 lines
arch/x86/kvm/x86.c +2 -0
+	if (likely(gtod->clock.vclock_mode == VCLOCK_TSC))
+		return 0;

I think this is only safe if any of the values in "struct pvclock_gtod_data" are unchanged. Otherwise the local (KVM) copy is kept incorrect.

Quoted 5 lines

I missread the code previously and I thought only under the condition the clocksource has been switched to another one will the KVM copy be touched. Apparently it is not the case because the copy should be updated on-time during normal tick, right?
thanks for your reply,

Yes, it is updated during the normal tick, and mult/freq values change.

However, if none of them change, its not necessary to call the callback. Perhaps you can check if any of the values changed and only invoke the callback in that case?

author
#4 lore raw
Quoted 48 lines · Marcelo Tosatti
Quoted 41 lines · Chen Yu

Hi Marcelo,

Quoted 33 lines · Marcelo Tosatti
Quoted 32 lines · Chen Yu

Currently the notifier of pvclock_gtod_notify() get invoked frequently due to the periodic update_wall_time(). This might slow down the system a little bit as there might be redundant execution code path and unnecessary lock contention
in update_pvclock_gtod(), which was found when I was doing suspend/resume speed testings. As pvclock_gtod_notify() should be invoked only when clocksource has changed, according to Commit 16e8d74d2da9 ("KVM: x86: notifier for clocksource changes") , either we can add a new notifier for clocksource switch, or we can simply bypass the following code in pvclock_gtod_notify() earlier if there is no clocksource switch.

Cc Paolo Bonzini <[email protected]>
Cc "Radim Krcmar" <[email protected]>
Cc Marcelo Tosatti <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Wanpeng Li <[email protected]>
---
 arch/x86/kvm/x86.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +0 -0
index 445c51b..54aa32d 100644
@@ -5961,13 +5961,14 @@ static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
 	struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
 	struct timekeeper *tk = priv;
 
Quoted 6 lines

arch/x86/kvm/x86.c static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused, · line 5965

Quoted 2 lines
arch/x86/kvm/x86.c +2 -0
+	if (likely(gtod->clock.vclock_mode == VCLOCK_TSC))
+		return 0;

I think this is only safe if any of the values in "struct pvclock_gtod_data" are unchanged. Otherwise the local (KVM) copy is kept incorrect.

Quoted 5 lines

I missread the code previously and I thought only under the condition the clocksource has been switched to another one will the KVM copy be touched. Apparently it is not the case because the copy should be updated on-time during normal tick, right?
thanks for your reply,

Quoted 6 lines

Yes, it is updated during the normal tick, and mult/freq values change.

However, if none of them change, its not necessary to call the callback. Perhaps you can check if any of the values changed and only invoke the callback in that case?

Yes, this should be an optimization, but most of the callers(workload) come from update_wall_time(), and in this code path the clock source's cycle should already be updated in most cases, so this optimization should not take much effect to reduce the burden I guess?

Thanks,
Yu

#5 lore raw
Quoted 55 lines · Chen Yu
Quoted 48 lines · Marcelo Tosatti
Quoted 41 lines · Chen Yu

Hi Marcelo,

Quoted 33 lines · Marcelo Tosatti
Quoted 32 lines · Chen Yu

Currently the notifier of pvclock_gtod_notify() get invoked frequently due to the periodic update_wall_time(). This might slow down the system a little bit as there might be redundant execution code path and unnecessary lock contention
in update_pvclock_gtod(), which was found when I was doing suspend/resume speed testings. As pvclock_gtod_notify() should be invoked only when clocksource has changed, according to Commit 16e8d74d2da9 ("KVM: x86: notifier for clocksource changes") , either we can add a new notifier for clocksource switch, or we can simply bypass the following code in pvclock_gtod_notify() earlier if there is no clocksource switch.

Cc Paolo Bonzini <[email protected]>
Cc "Radim Krcmar" <[email protected]>
Cc Marcelo Tosatti <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Wanpeng Li <[email protected]>
---
 arch/x86/kvm/x86.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +0 -0
index 445c51b..54aa32d 100644
@@ -5961,13 +5961,14 @@ static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
 	struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
 	struct timekeeper *tk = priv;
 
Quoted 6 lines

arch/x86/kvm/x86.c static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused, · line 5965

Quoted 2 lines
arch/x86/kvm/x86.c +2 -0
+	if (likely(gtod->clock.vclock_mode == VCLOCK_TSC))
+		return 0;

I think this is only safe if any of the values in "struct pvclock_gtod_data" are unchanged. Otherwise the local (KVM) copy is kept incorrect.

Quoted 5 lines

I missread the code previously and I thought only under the condition the clocksource has been switched to another one will the KVM copy be touched. Apparently it is not the case because the copy should be updated on-time during normal tick, right?
thanks for your reply,

Quoted 6 lines

Yes, it is updated during the normal tick, and mult/freq values change.

However, if none of them change, its not necessary to call the callback. Perhaps you can check if any of the values changed and only invoke the callback in that case?

Quoted 7 lines

Yes, this should be an optimization, but most of the callers(workload) come from update_wall_time(), and in this code path the clock source's cycle should already be updated in most cases, so this optimization should not take much effect to reduce the burden I guess?

Thanks,
Yu

I don't understand your reasoning.

"If the clock source parameters are already updated then optimization does not make much effect".

If the clock source parameters are updated (that is there has been no change in any of the values in pvclock_gtod_data), then you can skip the callback. This case reduces the burden.

Right?

author
#6 lore raw
Quoted 66 lines · Marcelo Tosatti
Quoted 55 lines · Chen Yu
Quoted 48 lines · Marcelo Tosatti
Quoted 41 lines · Chen Yu

Hi Marcelo,

Quoted 33 lines · Marcelo Tosatti
Quoted 32 lines · Chen Yu

Currently the notifier of pvclock_gtod_notify() get invoked frequently due to the periodic update_wall_time(). This might slow down the system a little bit as there might be redundant execution code path and unnecessary lock contention
in update_pvclock_gtod(), which was found when I was doing suspend/resume speed testings. As pvclock_gtod_notify() should be invoked only when clocksource has changed, according to Commit 16e8d74d2da9 ("KVM: x86: notifier for clocksource changes") , either we can add a new notifier for clocksource switch, or we can simply bypass the following code in pvclock_gtod_notify() earlier if there is no clocksource switch.

Cc Paolo Bonzini <[email protected]>
Cc "Radim Krcmar" <[email protected]>
Cc Marcelo Tosatti <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Wanpeng Li <[email protected]>
---
 arch/x86/kvm/x86.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +0 -0
index 445c51b..54aa32d 100644
@@ -5961,13 +5961,14 @@ static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
 	struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
 	struct timekeeper *tk = priv;
 
Quoted 6 lines

arch/x86/kvm/x86.c static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused, · line 5965

Quoted 2 lines
arch/x86/kvm/x86.c +2 -0
+	if (likely(gtod->clock.vclock_mode == VCLOCK_TSC))
+		return 0;

I think this is only safe if any of the values in "struct pvclock_gtod_data" are unchanged. Otherwise the local (KVM) copy is kept incorrect.

Quoted 5 lines

I missread the code previously and I thought only under the condition the clocksource has been switched to another one will the KVM copy be touched. Apparently it is not the case because the copy should be updated on-time during normal tick, right?
thanks for your reply,

Quoted 6 lines

Yes, it is updated during the normal tick, and mult/freq values change.

However, if none of them change, its not necessary to call the callback. Perhaps you can check if any of the values changed and only invoke the callback in that case?

Quoted 7 lines

Yes, this should be an optimization, but most of the callers(workload) come from update_wall_time(), and in this code path the clock source's cycle should already be updated in most cases, so this optimization should not take much effect to reduce the burden I guess?

Thanks,
Yu

I don't understand your reasoning.

Quoted 10 lines

"If the clock source parameters are already updated then optimization does not make much effect".

If the clock source parameters are updated (that is there has been no change in any of the values in pvclock_gtod_data), then you can skip the callback. This case reduces the burden.

Right?

Yes, in general case we can improve the code logic. Previously I was thinking of the case I encountered:

1. There are quite some invokes of pvclock_gtod_notify() caught by ftrace, and most of them should be triggered by update_wall_time()
2. If we optimize the code not to invoke pvclock_gtod_notify() if there is no modification of pvclock_gtod_data, it will reduce the burden for general use cases, but not for update_wall_time(), as it has already been modified in update_wall_time.

But yes, you are right, above is just my scenario, the optimization you mentioned is a generic solution for most cases. Do you mean the following solution?

kernel/time/timekeeping.c +7 -1
Index: linux/kernel/time/timekeeping.c
===================================================================
@@ -527,7 +527,13 @@ static RAW_NOTIFIER_HEAD(pvclock_gtod_ch
 
 static void update_pvclock_gtod(struct timekeeper *tk, bool was_set)
 {
-	raw_notifier_call_chain(&pvclock_gtod_chain, was_set, tk);
+	static struct timekeeper prev_timekeeper;
+
+	/* Only notify if the clocksource has changed.*/
+	if (memcmp(tk, &prev_timekeeper, sizeof(struct timekeeper))) {
+		raw_notifier_call_chain(&pvclock_gtod_chain, was_set, tk);
+		memcpy(&prev_timekeeper, tk, sizeof(struct timekeeper));
+	}
 }
 
 /**