From 6f78083f2aff59007540884865f064cebe0fea2e Mon Sep 17 00:00:00 2001 From: Laurent Morichetti Date: Fri, 29 Jul 2022 17:13:25 -0700 Subject: [PATCH] Fix a race condition when setting the callbacks When acquiring the reader's lock (sem_sync()...sem_release()), it is possible for a writer to squeeze by if the reader goes into sync_wait(). The writer could re-acquire the entry.sync between entry.sem > 0 and entry.sync = 0. void sync_wait(uint32_t id) { sem_decrement(id); while (entry(id).sync.load()) {} // <--- HERE sem_increment(id); } This could result in both the reader and the writer accessing { callback, arg } at the same time, and the reader could read inconsistent data, for example: { new callback, old arg }. The solution is to re-test entry.sync when returning from sync_wait(): void sem_sync(uint32_t id) { sem_increment(id); - if (entry(id).sync.load() == true) sync_wait(id); + while (entry(id).sync.load() == true) sync_wait(id); } Change-Id: I22f74f4cb9a5f027aac8aa4ed3e633acc19df4b8 --- hipamd/src/hip_prof_api.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hipamd/src/hip_prof_api.h b/hipamd/src/hip_prof_api.h index 787a62c5ed..76bfa4e394 100644 --- a/hipamd/src/hip_prof_api.h +++ b/hipamd/src/hip_prof_api.h @@ -134,7 +134,7 @@ class api_callbacks_table_t { inline void sem_sync(const uint32_t& id) { sem_increment(id); - if (entry(id).sync.load() == true) sync_wait(id); + while (entry(id).sync.load() == true) sync_wait(id); } inline void sem_release(const uint32_t& id) {