x86/pvclock: improve atomic update of last_value in pvclock_clocksource_read
PATCH 1/1 x86/pvclock: improve atomic update of last_value in pvclock_clocksource_read
Improve atomic update of last_value in pvclock_clocksource_read:
- Atomic update can be skipped if the "last_value" is already equal to "ret".
- The detection of atomic update failure is not correct. The value, returned by atomic64_cmpxchg should be compared to the old value from the location to be updated. If these two are the same, then atomic update succeeded and "last_value" location is updated to "ret" in an atomic way. Otherwise, the atomic update failed and it should be retried with the value from "last_value" - exactly what atomic64_try_cmpxchg does in a correct and more optimal way.
--- arch/x86/kernel/pvclock.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
arch/x86/kernel/pvclock.c +2 -3
@@ -102,10 +102,9 @@ u64 pvclock_clocksource_read(struct pvclock_vcpu_time_info *src)*/ last =atomic64_read (&last_value);do { -if (ret < last) +if (ret <= last)return last; - last =atomic64_cmpxchg (&last_value, last, ret); - }while (unlikely (last != ret)); + }while (!atomic64_try_cmpxchg (&last_value, &last, ret));return ret; }
Signature · 1 line
2.39.0
[tip: sched/core] x86/pvclock: Improve atomic update of last_value in pvclock_clocksource_read()
The following commit has been merged into the sched/core branch of tip:
The following commit has been merged into the sched/core branch of tip:
Commit-ID: 5c9da9fe826d4a0d84bb322cca27cc8ad8d23b24 Gitweb: https://git.kernel.org/tip/5c9da9fe826d4a0d84bb322cca27cc8ad8d23b24 Author: Uros Bizjak <[email protected]> AuthorDate: Thu, 26 Jan 2023 16:08:35 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Tue, 31 Jan 2023 15:01:46 +01:00
x86/pvclock: Improve atomic update of last_value in pvclock_clocksource_read()
Improve atomic update of last_value in pvclock_clocksource_read:
- Atomic update can be skipped if the "last_value" is already equal to "ret".
- The detection of atomic update failure is not correct. The value, returned by atomic64_cmpxchg should be compared to the old value from the location to be updated. If these two are the same, then atomic update succeeded and "last_value" location is updated to "ret" in an atomic way. Otherwise, the atomic update failed and it should be retried with the value from "last_value" - exactly what atomic64_try_cmpxchg does in a correct and more optimal way.
--- arch/x86/kernel/pvclock.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
arch/x86/kernel/pvclock.c +2 -3
@@ -102,10 +102,9 @@ u64 pvclock_clocksource_read(struct pvclock_vcpu_time_info *src)*/ last =atomic64_read (&last_value);do { -if (ret < last) +if (ret <= last)return last; - last =atomic64_cmpxchg (&last_value, last, ret); - }while (unlikely (last != ret)); + }while (!atomic64_try_cmpxchg (&last_value, &last, ret));return ret; }