Skip to content

Commit f81e3ed

Browse files
Copilotdevreal
andcommitted
Fix additional atomic access issues and add volatile variants
Co-authored-by: devreal <10974502+devreal@users.noreply.github.com>
1 parent 3759195 commit f81e3ed

File tree

8 files changed

+58
-28
lines changed

8 files changed

+58
-28
lines changed

opal/class/opal_lifo.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static inline bool opal_update_counted_pointer(volatile opal_counted_pointer_t *
7070
opal_counted_pointer_t *old, opal_list_item_t *item)
7171
{
7272
opal_counted_pointer_t new_p;
73-
opal_atomic_store_ptr_relaxed(&new_p.data.item, (intptr_t) item);
73+
opal_atomic_store_ptr_volatile_relaxed(&new_p.data.item, (intptr_t) item);
7474
new_p.data.counter = old->data.counter + 1;
7575
return opal_atomic_compare_exchange_strong_128(&addr->atomic_value, &old->value, new_p.value);
7676
}
@@ -122,7 +122,7 @@ OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_lifo_t);
122122
*/
123123
static inline bool opal_lifo_is_empty(opal_lifo_t *lifo)
124124
{
125-
return (opal_list_item_t *) opal_atomic_load_ptr_relaxed(&lifo->opal_lifo_head.data.item) == &lifo->opal_lifo_ghost;
125+
return (opal_list_item_t *) opal_atomic_load_ptr_volatile_relaxed(&lifo->opal_lifo_head.data.item) == &lifo->opal_lifo_ghost;
126126
}
127127

128128
#if OPAL_HAVE_ATOMIC_COMPARE_EXCHANGE_128 && !OPAL_HAVE_ATOMIC_LLSC_PTR
@@ -133,7 +133,7 @@ static inline bool opal_lifo_is_empty(opal_lifo_t *lifo)
133133
*/
134134
static inline opal_list_item_t *opal_lifo_push_atomic(opal_lifo_t *lifo, opal_list_item_t *item)
135135
{
136-
opal_list_item_t *next = (opal_list_item_t *) opal_atomic_load_ptr_relaxed(&lifo->opal_lifo_head.data.item);
136+
opal_list_item_t *next = (opal_list_item_t *) opal_atomic_load_ptr_volatile_relaxed(&lifo->opal_lifo_head.data.item);
137137

138138
do {
139139
item->opal_list_next = next;
@@ -181,7 +181,7 @@ static inline opal_list_item_t *opal_lifo_pop_atomic(opal_lifo_t *lifo)
181181
*/
182182
static inline opal_list_item_t *opal_lifo_push_atomic(opal_lifo_t *lifo, opal_list_item_t *item)
183183
{
184-
opal_list_item_t *next = (opal_list_item_t *) opal_atomic_load_ptr_relaxed(&lifo->opal_lifo_head.data.item);
184+
opal_list_item_t *next = (opal_list_item_t *) opal_atomic_load_ptr_volatile_relaxed(&lifo->opal_lifo_head.data.item);
185185

186186
/* item free acts as a mini lock to avoid ABA problems */
187187
item->item_free = 1;
@@ -242,7 +242,7 @@ static inline opal_list_item_t *opal_lifo_pop_atomic(opal_lifo_t *lifo)
242242
{
243243
opal_list_item_t *item, *head, *ghost = &lifo->opal_lifo_ghost;
244244

245-
while ((item = (opal_list_item_t *) opal_atomic_load_ptr_relaxed(&lifo->opal_lifo_head.data.item)) != ghost) {
245+
while ((item = (opal_list_item_t *) opal_atomic_load_ptr_volatile_relaxed(&lifo->opal_lifo_head.data.item)) != ghost) {
246246
/* ensure it is safe to pop the head */
247247
if (opal_atomic_swap_32((opal_atomic_int32_t *) &item->item_free, 1)) {
248248
continue;
@@ -282,17 +282,17 @@ static inline opal_list_item_t *opal_lifo_pop_atomic(opal_lifo_t *lifo)
282282
/* single-threaded versions of the lifo functions */
283283
static inline opal_list_item_t *opal_lifo_push_st(opal_lifo_t *lifo, opal_list_item_t *item)
284284
{
285-
item->opal_list_next = (opal_list_item_t *) opal_atomic_load_ptr_relaxed(&lifo->opal_lifo_head.data.item);
285+
item->opal_list_next = (opal_list_item_t *) opal_atomic_load_ptr_volatile_relaxed(&lifo->opal_lifo_head.data.item);
286286
item->item_free = 0;
287-
opal_atomic_store_ptr_relaxed(&lifo->opal_lifo_head.data.item, (intptr_t) item);
287+
opal_atomic_store_ptr_volatile_relaxed(&lifo->opal_lifo_head.data.item, (intptr_t) item);
288288
return (opal_list_item_t *) item->opal_list_next;
289289
}
290290

291291
static inline opal_list_item_t *opal_lifo_pop_st(opal_lifo_t *lifo)
292292
{
293293
opal_list_item_t *item;
294-
item = (opal_list_item_t *) opal_atomic_load_ptr_relaxed(&lifo->opal_lifo_head.data.item);
295-
opal_atomic_store_ptr_relaxed(&lifo->opal_lifo_head.data.item, (intptr_t) item->opal_list_next);
294+
item = (opal_list_item_t *) opal_atomic_load_ptr_volatile_relaxed(&lifo->opal_lifo_head.data.item);
295+
opal_atomic_store_ptr_volatile_relaxed(&lifo->opal_lifo_head.data.item, (intptr_t) item->opal_list_next);
296296
if (item == &lifo->opal_lifo_ghost) {
297297
return NULL;
298298
}

opal/include/opal_stdatomic.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ static inline intptr_t opal_atomic_load_ptr(const opal_atomic_intptr_t *addr, me
113113
return atomic_load_explicit(&addr->value, order);
114114
}
115115

116+
static inline intptr_t opal_atomic_load_ptr_volatile(const volatile opal_atomic_intptr_t *addr, memory_order order)
117+
{
118+
return atomic_load_explicit(&addr->value, order);
119+
}
120+
116121
static inline uintptr_t opal_atomic_load_uptr(const opal_atomic_uintptr_t *addr, memory_order order)
117122
{
118123
return atomic_load_explicit(&addr->value, order);
@@ -164,6 +169,11 @@ static inline void opal_atomic_store_ptr(opal_atomic_intptr_t *addr, intptr_t va
164169
atomic_store_explicit(&addr->value, value, order);
165170
}
166171

172+
static inline void opal_atomic_store_ptr_volatile(volatile opal_atomic_intptr_t *addr, intptr_t value, memory_order order)
173+
{
174+
atomic_store_explicit(&addr->value, value, order);
175+
}
176+
167177
static inline void opal_atomic_store_uptr(opal_atomic_uintptr_t *addr, uintptr_t value, memory_order order)
168178
{
169179
atomic_store_explicit(&addr->value, value, order);
@@ -185,6 +195,11 @@ static inline intptr_t opal_atomic_load_ptr_relaxed(const opal_atomic_intptr_t *
185195
return opal_atomic_load_ptr(addr, memory_order_relaxed);
186196
}
187197

198+
static inline intptr_t opal_atomic_load_ptr_volatile_relaxed(const volatile opal_atomic_intptr_t *addr)
199+
{
200+
return opal_atomic_load_ptr_volatile(addr, memory_order_relaxed);
201+
}
202+
188203
static inline void opal_atomic_store_32_relaxed(opal_atomic_int32_t *addr, int32_t value)
189204
{
190205
opal_atomic_store_32(addr, value, memory_order_relaxed);
@@ -200,6 +215,11 @@ static inline void opal_atomic_store_ptr_relaxed(opal_atomic_intptr_t *addr, int
200215
opal_atomic_store_ptr(addr, value, memory_order_relaxed);
201216
}
202217

218+
static inline void opal_atomic_store_ptr_volatile_relaxed(volatile opal_atomic_intptr_t *addr, intptr_t value)
219+
{
220+
opal_atomic_store_ptr_volatile(addr, value, memory_order_relaxed);
221+
}
222+
203223
static inline size_t opal_atomic_load_size_t_relaxed(const opal_atomic_size_t *addr)
204224
{
205225
return opal_atomic_load_size_t(addr, memory_order_relaxed);
@@ -210,6 +230,16 @@ static inline void opal_atomic_store_size_t_relaxed(opal_atomic_size_t *addr, si
210230
opal_atomic_store_size_t(addr, value, memory_order_relaxed);
211231
}
212232

233+
static inline uint32_t opal_atomic_load_uint32_relaxed(const opal_atomic_uint32_t *addr)
234+
{
235+
return opal_atomic_load_uint32(addr, memory_order_relaxed);
236+
}
237+
238+
static inline void opal_atomic_store_uint32_relaxed(opal_atomic_uint32_t *addr, uint32_t value)
239+
{
240+
opal_atomic_store_uint32(addr, value, memory_order_relaxed);
241+
}
242+
213243
# endif /* OPAL_USE_C11_ATOMICS == 0 */
214244

215245
# if HAVE_OPAL_INT128_T

opal/mca/btl/sm/btl_sm_fifo.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,18 @@ static inline mca_btl_sm_hdr_t *sm_fifo_read(sm_fifo_t *fifo, struct mca_btl_bas
7373
mca_btl_sm_hdr_t *hdr;
7474
fifo_value_t value;
7575

76-
if (SM_FIFO_FREE == fifo->fifo_head) {
76+
if (SM_FIFO_FREE == opal_atomic_load_ptr_relaxed(&fifo->fifo_head)) {
7777
return NULL;
7878
}
7979

8080
opal_atomic_rmb();
8181

82-
value = fifo->fifo_head;
82+
value = opal_atomic_load_ptr_relaxed(&fifo->fifo_head);
8383

8484
*ep = &mca_btl_sm_component.endpoints[value >> MCA_BTL_SM_OFFSET_BITS];
8585
hdr = (mca_btl_sm_hdr_t *) relative2virtual(value);
8686

87-
fifo->fifo_head = SM_FIFO_FREE;
87+
opal_atomic_store_ptr_relaxed(&fifo->fifo_head, SM_FIFO_FREE);
8888

8989
assert(hdr->next != value);
9090

@@ -96,10 +96,10 @@ static inline mca_btl_sm_hdr_t *sm_fifo_read(sm_fifo_t *fifo, struct mca_btl_bas
9696
opal_atomic_rmb();
9797
}
9898

99-
fifo->fifo_head = hdr->next;
99+
opal_atomic_store_ptr_relaxed(&fifo->fifo_head, hdr->next);
100100
}
101101
} else {
102-
fifo->fifo_head = hdr->next;
102+
opal_atomic_store_ptr_relaxed(&fifo->fifo_head, hdr->next);
103103
}
104104

105105
opal_atomic_wmb();
@@ -111,7 +111,7 @@ static inline void sm_fifo_init(sm_fifo_t *fifo)
111111
/* due to a compiler bug in Oracle C 5.15 the following line was broken into two. Not
112112
* ideal but oh well. See #5814 */
113113
/* fifo->fifo_head = fifo->fifo_tail = SM_FIFO_FREE; */
114-
fifo->fifo_head = SM_FIFO_FREE;
114+
opal_atomic_store_ptr_relaxed(&fifo->fifo_head, SM_FIFO_FREE);
115115
fifo->fifo_tail = SM_FIFO_FREE;
116116
fifo->fbox_available = mca_btl_sm_component.fbox_max;
117117
mca_btl_sm_component.my_fifo = fifo;
@@ -131,7 +131,7 @@ static inline void sm_fifo_write(sm_fifo_t *fifo, fifo_value_t value)
131131
mca_btl_sm_hdr_t *hdr = (mca_btl_sm_hdr_t *) relative2virtual(prev);
132132
hdr->next = value;
133133
} else {
134-
fifo->fifo_head = value;
134+
opal_atomic_store_ptr_relaxed(&fifo->fifo_head, value);
135135
}
136136

137137
opal_atomic_wmb();

opal/mca/common/sm/common_sm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ static mca_common_sm_module_t *attach_and_init(opal_shmem_ds_t *shmem_bufp, size
110110
/* initialize some segment information */
111111
size_t mem_offset = map->module_data_addr - (unsigned char *) map->module_seg;
112112
opal_atomic_lock_init(&map->module_seg->seg_lock, OPAL_ATOMIC_LOCK_UNLOCKED);
113-
map->module_seg->seg_inited = 0;
114-
map->module_seg->seg_num_procs_inited = 0;
113+
opal_atomic_store_32_relaxed(&map->module_seg->seg_inited, 0);
114+
opal_atomic_store_size_t_relaxed(&map->module_seg->seg_num_procs_inited, 0);
115115
map->module_seg->seg_offset = mem_offset;
116116
map->module_seg->seg_size = size - mem_offset;
117117
opal_atomic_wmb();

opal/mca/rcache/base/rcache_base_frame.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ static void mca_rcache_base_registration_constructor(mca_rcache_base_registratio
5757
reg->rcache = NULL;
5858
reg->base = NULL;
5959
reg->bound = NULL;
60-
reg->ref_count = 0;
61-
reg->flags = 0;
60+
opal_atomic_store_32_relaxed(&reg->ref_count, 0);
61+
opal_atomic_store_uint32_relaxed(&reg->flags, 0);
6262
}
6363

6464
OBJ_CLASS_INSTANCE(mca_rcache_base_registration_t, opal_free_list_item_t,

opal/mca/rcache/base/rcache_base_vma_tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ static int mca_rcache_base_tree_dump_range_helper(uint64_t low, uint64_t high, v
144144
mca_rcache_base_registration_t *reg = (mca_rcache_base_registration_t *) data;
145145

146146
opal_output(0, " reg: base=%p, bound=%p, ref_count=%d, flags=0x%x", (void *) reg->base,
147-
(void *) reg->bound, reg->ref_count, reg->flags);
147+
(void *) reg->bound, opal_atomic_load_32_relaxed(&reg->ref_count), opal_atomic_load_uint32_relaxed(&reg->flags));
148148

149149
return OPAL_SUCCESS;
150150
}

opal/mca/threads/base/wait_sync.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void opal_threads_base_wait_sync_global_wakeup_mt(int status)
4747
opal_mutex_unlock(&wait_sync_lock);
4848
}
4949

50-
static opal_atomic_int32_t num_thread_in_progress = 0;
50+
static opal_atomic_int32_t num_thread_in_progress = { .value = 0 };
5151

5252
#define WAIT_SYNC_PASS_OWNERSHIP(who) \
5353
do { \
@@ -62,7 +62,7 @@ int ompi_sync_wait_mt(ompi_wait_sync_t *sync)
6262
* race condition around the release of the synchronization using the
6363
* signaling field.
6464
*/
65-
if (sync->count <= 0) {
65+
if (opal_atomic_load_32_relaxed(&sync->count) <= 0) {
6666
return (0 == sync->status) ? OPAL_SUCCESS : OPAL_ERROR;
6767
}
6868

@@ -72,7 +72,7 @@ int ompi_sync_wait_mt(ompi_wait_sync_t *sync)
7272
/* Now that we hold the lock make sure another thread has not already
7373
* call cond_signal.
7474
*/
75-
if (sync->count <= 0) {
75+
if (opal_atomic_load_32_relaxed(&sync->count) <= 0) {
7676
opal_thread_internal_mutex_unlock(&sync->lock);
7777
return (0 == sync->status) ? OPAL_SUCCESS : OPAL_ERROR;
7878
}
@@ -97,7 +97,7 @@ int ompi_sync_wait_mt(ompi_wait_sync_t *sync)
9797
* - our sync has been triggered.
9898
*/
9999
check_status:
100-
if (sync != opal_threads_base_wait_sync_list && num_thread_in_progress >= opal_max_thread_in_progress) {
100+
if (sync != opal_threads_base_wait_sync_list && opal_atomic_load_32_relaxed(&num_thread_in_progress) >= opal_max_thread_in_progress) {
101101
opal_thread_internal_cond_wait(&sync->condition, &sync->lock);
102102

103103
/**
@@ -106,7 +106,7 @@ int ompi_sync_wait_mt(ompi_wait_sync_t *sync)
106106
* promoted as the progress manager.
107107
*/
108108

109-
if (sync->count <= 0) { /* Completed? */
109+
if (opal_atomic_load_32_relaxed(&sync->count) <= 0) { /* Completed? */
110110
opal_thread_internal_mutex_unlock(&sync->lock);
111111
goto i_am_done;
112112
}
@@ -116,7 +116,7 @@ int ompi_sync_wait_mt(ompi_wait_sync_t *sync)
116116
opal_thread_internal_mutex_unlock(&sync->lock);
117117

118118
OPAL_THREAD_ADD_FETCH32(&num_thread_in_progress, 1);
119-
while (sync->count > 0) { /* progress till completion */
119+
while (opal_atomic_load_32_relaxed(&sync->count) > 0) { /* progress till completion */
120120
/* don't progress with the sync lock locked or you'll deadlock */
121121
opal_progress();
122122
}

opal/mca/threads/wait_sync.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static inline int sync_wait_st(ompi_wait_sync_t *sync)
100100
assert(NULL == sync->next);
101101
opal_threads_base_wait_sync_list = sync;
102102

103-
while (sync->count > 0) {
103+
while (opal_atomic_load_32_relaxed(&sync->count) > 0) {
104104
opal_progress();
105105
}
106106
opal_threads_base_wait_sync_list = NULL;

0 commit comments

Comments
 (0)