Skip to content

Commit 2f737e4

Browse files
authored
[mono][interp] Fix concurrency issues with publishing transfomed imethod (#87555)
* [mono][utils] Move all membar code to single file * [mono][utils] Redefine write and read memory barriers Before this change they were doing a full memory barrier, regardless of architecture. We have a beginning of more precise implementation via the *_FENCE defines. Implement mono_memory_write_barrier and mono_memory_read_barrier reusing these defines instead. The only consequence of this change is that, on x86 and amd64, `mono_memory_write_barrier` and `mono_memory_read_barrier` become a compiler barrier instead of a full mfence. * [mono][interp] Fix concurrency issues with publishing transfomed imethod When publishing a transformed InterpMethod*, we first set all relevant fields (like `code`, `alloca_size` etc), we execute a write barrier and finally we set the `transformed` flag. On relaxed memory arches we need to have a read barrier on the consumer, since there is no data dependency between `transformed` and the other fields of `InterpMethod`. On arm this change does a full barrier (we could get away with just a load acquire but we haven't yet added support for emitting this in the runtime). Still, this doesn't seem to introduce a heavy perf penalty (on my arm64 M1) but we can revisit if necessary. On x86/amd64 this is a compiler barrier so it should have no impact. WASM is single threaded for now.
1 parent 4ae1668 commit 2f737e4

27 files changed

+79
-115
lines changed

src/mono/mono/metadata/domain.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <mono/utils/atomic.h>
2323
#include <mono/utils/mono-compiler.h>
2424
#include <mono/utils/mono-logger-internals.h>
25-
#include <mono/utils/mono-membar.h>
2625
#include <mono/utils/mono-counters.h>
2726
#include <mono/utils/hazard-pointer.h>
2827
#include <mono/utils/mono-tls.h>

src/mono/mono/metadata/icall.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
#endif
3939

4040
#include "mono/metadata/icall-internals.h"
41-
#include "mono/utils/mono-membar.h"
4241
#include <mono/metadata/object.h>
4342
#include <mono/metadata/threads.h>
4443
#include <mono/metadata/threads-types.h>

src/mono/mono/metadata/jit-info.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include <mono/utils/atomic.h>
2424
#include <mono/utils/mono-compiler.h>
2525
#include <mono/utils/mono-logger-internals.h>
26-
#include <mono/utils/mono-membar.h>
26+
#include <mono/utils/mono-memory-model.h>
2727
#include <mono/utils/hazard-pointer.h>
2828
#include <mono/utils/mono-tls.h>
2929
#include <mono/utils/mono-mmap.h>

src/mono/mono/metadata/loader.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
#include <mono/metadata/jit-info.h>
4747
#include <mono/utils/mono-logger-internals.h>
4848
#include <mono/utils/mono-dl.h>
49-
#include <mono/utils/mono-membar.h>
49+
#include <mono/utils/mono-memory-model.h>
5050
#include <mono/utils/mono-counters.h>
5151
#include <mono/utils/mono-error-internals.h>
5252
#include <mono/utils/mono-tls.h>

src/mono/mono/metadata/property-bag.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
#include <mono/metadata/property-bag.h>
1111
#include <mono/utils/atomic.h>
12-
#include <mono/utils/mono-membar.h>
12+
#include <mono/utils/mono-memory-model.h>
1313

1414
/*
1515
* mono_property_bag_get:

src/mono/mono/metadata/reflection.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
1414
*/
1515
#include <config.h>
16-
#include "mono/utils/mono-membar.h"
16+
#include "mono/utils/mono-memory-model.h"
1717
#include "mono/metadata/assembly-internals.h"
1818
#include "mono/metadata/reflection-internals.h"
1919
#include "mono/metadata/tabledefs.h"

src/mono/mono/metadata/string-icalls.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <stdlib.h>
1515
#include <stdio.h>
1616
#include <string.h>
17-
#include "mono/utils/mono-membar.h"
17+
#include "mono/utils/mono-memory-model.h"
1818
#include <mono/metadata/string-icalls.h>
1919
#include <mono/metadata/class-internals.h>
2020
#include <mono/metadata/appdomain.h>

src/mono/mono/metadata/threads-types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <mono/metadata/object.h>
2020
#include "mono/metadata/handle.h"
2121
#include "mono/utils/mono-compiler.h"
22-
#include "mono/utils/mono-membar.h"
22+
#include "mono/utils/mono-memory-model.h"
2323
#include "mono/utils/mono-threads.h"
2424
#include "mono/metadata/class-internals.h"
2525
#include <mono/metadata/icalls.h>

src/mono/mono/metadata/threads.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include <mono/utils/monobitset.h>
3737
#include <mono/utils/mono-compiler.h>
3838
#include <mono/utils/mono-mmap.h>
39-
#include <mono/utils/mono-membar.h>
39+
#include <mono/utils/mono-memory-model.h>
4040
#include <mono/utils/mono-time.h>
4141
#include <mono/utils/mono-threads.h>
4242
#include <mono/utils/mono-threads-coop.h>

src/mono/mono/mini/interp/interp.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include <mono/utils/mono-logger-internals.h>
2828
#include <mono/utils/mono-tls-inline.h>
2929
#include <mono/utils/mono-threads.h>
30-
#include <mono/utils/mono-membar.h>
30+
#include <mono/utils/mono-memory-model.h>
3131

3232
#ifdef HAVE_ALLOCA_H
3333
# include <alloca.h>
@@ -3606,6 +3606,8 @@ method_entry (ThreadContext *context, InterpFrame *frame,
36063606
frame->stack = (stackval*)context->stack_pointer;
36073607
return slow;
36083608
}
3609+
} else {
3610+
mono_memory_read_barrier ();
36093611
}
36103612

36113613
return slow;

0 commit comments

Comments
 (0)