timing: fix timing_stop() ref counting

When there are more timing_stop() calls then timing_start(),
the reference counter will go negative, resulting in the next
timing_start() call not starting the timer. Without timer
running, getting cycles elasped would not work. So fix
the ref counting so it won't dip below zero.

Fixes #30397

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
This commit is contained in:
Daniel Leung 2020-12-11 12:35:16 -08:00 committed by Ioannis Glaropoulos
commit 6ab4886506

View file

@ -46,7 +46,22 @@ void timing_start(void)
void timing_stop(void)
{
if (atomic_dec(&started_ref) > 1) {
atomic_t old_value, new_value;
/* Make sure this does decrement past zero. */
do {
old_value = atomic_get(&started_ref);
if (old_value <= 0) {
break;
}
new_value = old_value - 1;
} while (atomic_cas(&started_ref, old_value, new_value) == 0);
/*
* new_value may be uninitialized, so use old_value here.
*/
if (old_value > 1) {
return;
}