2021-09-29 11:46:02

by Li RongQing

[permalink] [raw]
Subject: [PATCH] KVM: Micro-optimize pvclock_clocksource_read

Compare the return of atomic64_cmpxchg with previous save for last,
to reduce the number of while-loop iterations with atomic operations
from two to one in the most common situation

Original patch at:
https://kvm.vger.kernel.narkive.com/WraXedaQ/patch-kvm-pvclock-clocksource-read-while-loop-optimization

Signed-off-by: Li RongQing <[email protected]>
---
arch/x86/kernel/pvclock.c | 5 +++--
1 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kernel/pvclock.c b/arch/x86/kernel/pvclock.c
index eda37df..98d106f 100644
--- a/arch/x86/kernel/pvclock.c
+++ b/arch/x86/kernel/pvclock.c
@@ -67,8 +67,8 @@ u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src)
u64 pvclock_clocksource_read(struct pvclock_vcpu_time_info *src)
{
unsigned version;
+ u64 last, save;
u64 ret;
- u64 last;
u8 flags;

do {
@@ -104,8 +104,9 @@ u64 pvclock_clocksource_read(struct pvclock_vcpu_time_info *src)
do {
if (ret < last)
return last;
+ save = last;
last = atomic64_cmpxchg(&last_value, last, ret);
- } while (unlikely(last != ret));
+ } while (unlikely(last != save));

return ret;
}
--
1.7.1