[Linux/ARM32] Instruction cache flush on arm32 linux
A crash occurred when using AOT images. It was generated with /FragileNonVersionable option, but it looks not the point here. When analyzing the problem, the crash was occurred by SIGSEG while accessing the memory address because the value of *.ni.dll partly differs from the value loaded in memory. The code at a different point was associated with an target address of movt, movw instruction.
On a kernel dump, SIGSEGV occurs when trying to access a strange memory address (0x100174d4).
[2-117.0372] 5860: fc52f7fe 63b8f243 3377f6ca 6819681b
9a019803 f7fe6803 bf00fc4d 93042301
[2-117.0372] 5880: e7ffbf00 b0069804 8c10e8bd 4c1ce92d
0b10f10d f2439001 f6ca5338 681b[3377] <-- crashed here, 0x3377
[2-117.0372] 58a0: 2b00681b f7fed001 9801fcb3 fc38f7fe
bf00bf00 8c1ce8bd 4c10e92d f10db086
[2-117.0372] 58c0: 23000b20 93019302 91049005 f2439203
f6ca5338 681b3377 2b00681b f7fed001
Contents of the XXX.ni.dll file is
0003850: c1f2 0102 0198 0021 43f2 bd63 c1f2 0103 .......!C..c....
0003860: fef7 52fc 41f2 b863 c1f2 0103 1b68 1968 ..R.A..c.....h.h
0003870: 0398 019a 0368 fef7 4dfc 00bf 0123 0493 .....h..M....#..
0003880: 00bf ffe7 0498 06b0 bde8 108c 2de9 1c4c ............-..L
0003890: 0df1 100b 0190 41f2 3853 c1f2 [0103] 1b68 ......A.8S.....h <-- The file contains a value of 0x0301 instead of 0x3377
00038a0: 1b68 002b 01d0 fef7 b3fc 0198 fef7 38fc .h.+..........8.
00038b0: 00bf 00bf bde8 1c8c 2de9 104c 86b0 0df1 ........-..L....
00038c0: 200b 0023 0293 0193 0590 0491 0392 41f2 ..#..........A.
Based on the values in the memory, the area is composed of the following code, and the problematic part is the ldr r3, [r3, # 0] part.
(gdb) p/x &code
$2 = 0x1102c
(gdb) p/x 0xad06-0xace0
$3 = 0x26
(gdb) p/x 0x1102c+0x26
$4 = 0x11052
(gdb) x/17i 0x1102c+1
0x1102d <code+1>: ldr r0, [sp, dotnet/runtime#3870] ; 0x2c
0x1102f <code+3>: ldr r1, [sp, dotnet/coreclr#20]
0x11031 <code+5>: ldr r2, [sp, dotnet/coreclr#24]
0x11033 <code+7>: ldr r3, [r0, #0]
0x11035 <code+9>: bl 0xf470
0x11039 <code+13>: nop
0x1103b <code+15>: movw r3, #30480 ; 0x7710
0x1103f <code+19>: movt r3, #43969 ; 0xabc1
0x11043 <code+23>: ldr r0, [r3, #0]
0x11045 <code+25>: bl 0xf6f4
0x11049 <code+29>: str r0, [sp, dotnet/coreclr#16]
0x1104b <code+31>: movw r3, #29908 ; 0x74d4
0x1104f <code+35>: movt r3, #43969 ; 0xabc1
0x11053 <code+39>: ldr r0, [r3, #0] <-- SIGSEGV HERE
0x11055 <code+41>: movw r3, #30448 ; 0x76f0
0x11059 <code+45>: movt r3, #43969 ; 0xabc1
0x1105d <code+49>: ldr r1, [r3, #0]
The strange thing is that the memory address that should be accessed in this area is 0xabc174d4 instead of 0x100174d4.
It seemed to be related to relocation.
We looked at the code and we could find the relevant code in PEImageLayout::ApplyBaseRelocations().
In response to the story that there was an issue of the instruction cache flush associated with the other JIT VM, we tried to force the cache flush as follows, and the symptom of the problem was eliminated.
diff --git a/src/vm/peimagelayout.cpp b/src/vm/peimagelayout.cpp
index 93ab77c..ca4b487 100644
--- a/src/vm/peimagelayout.cpp
+++ b/src/vm/peimagelayout.cpp
@@ -227,6 +227,8 @@ void PEImageLayout::ApplyBaseRelocations()
#ifdef _TARGET_ARM_
case IMAGE_REL_BASED_THUMB_MOV32:
PutThumb2Mov32((UINT16 *)address, GetThumb2Mov32((UINT16 *)address) + delta);
+
+ ClrFlushInstructionCache(address, 8);
break;
#endif
Basically, I think that cache flush through mprotect() is already well done in CoreCLR.
However, when I searched the Internet, I can found the following.
You would expect that the mmap / mprotect syscalls would establish mappings that are immediately updated, I see that the kernel does indeed flush caches on mprotect. In that case, no cache flush would be required. However, I also see that some versions of libc do call cacheflush after mprotect, which would imply that some environments would need the caches flushed (or have previously). I'd take a guess that this is a workaround to a bug.
Https://stackoverflow.com/questions/2777725/does-mprotect-flush-the-instruction-cache-on-arm-linux
I looked at the glibc source code and found the following information:
- Here defines CLEAR_CACHE.
- This is the only place where CLEAR_CACHE is used while relocating object.
- Here warnings that there the CLEAR_CACHE should be present to support text relocation.
It seems that mprotect() alone is not enough to support dynamic relocation of code Linux/ARM.
Perhaps, the comment /* This definition is Linux-specific. */ in dl-machine.h explains why there was no problem in other platform before.
I have some questions at this point.
- Is this the right solution? Is it a best way to solve this problem?
- Doesn't it has any impact on performance?
- If this is a really problem, why has not this been fixed in the linux kernel before?
- Is anyone encountered this problem on
Windows/ARM?
I want anyone to comment.
/cc @parjong @hqueue @seanshpark @chunseoklee @Dmitri-Botcharnikov PTAL and please correct if I missed.
FlushInstructionCache tends to be relatively expensive. It would be better to call it once per region. The solution sounds fine to me otherwise.
@lemmaa are you planning to make a PR for the fix you've suggested (augmented as per @jkotas's comment)?
@dotnet/arm32-contrib Is anyone actively working on this now, or does anyone have plans to work on it?
@BruceForstall we will prepare commit soon cc @kbaladurin
@alpencolt, @kbaladurin do you think you'd be able to get it done and send out a PR by tomorrow? If not, can you please give us some ETA? Thank you.
@janvorli I've created PR based on @jkotas suggestion: https://github.com/dotnet/coreclr/pull/17342
Hopefully fixed by dotnet/coreclr#17342.
@janvorli @jkotas
The change from dotnet/coreclr#17342 increases Shared_Clean on Linux ARM. Do you know, what could be the reason of such behavior?
On EmailUI Tizen Xamarin GUI application (when HelloWorld Xamarin GUI app is in background):
- Before the change:
Rss: 39996.4 kb Private_Dirty: 13760.4 kb Private_Clean: 1384 kb Shared_Dirty: 4 kb Shared_Clean: 24848 kb
- After the change:
Rss: 47722.8 kb Private_Dirty: 13770.8 kb Private_Clean: 1228 kb (-156 kb) Shared_Dirty: 4 kb Shared_Clean: 32720 kb (+7872 kb)
Startup time is nearly the same.
- If flushing is implemented in the next manner (for each instruction), there is no increase in Shared_Clean. However, there is increase in startup time (about 0.3s for Tizen Xamarin GUI apps).
diff --git a/src/vm/peimagelayout.cpp b/src/vm/peimagelayout.cpp
index 0a84892..6c76eda 100644
--- a/src/vm/peimagelayout.cpp
+++ b/src/vm/peimagelayout.cpp
@@ -221,11 +221,15 @@ void PEImageLayout::ApplyBaseRelocations()
{
case IMAGE_REL_BASED_PTR:
*(TADDR *)address += delta;
+
+ ClrFlushInstructionCache(address, 8);
break;
#ifdef _TARGET_ARM_
case IMAGE_REL_BASED_THUMB_MOV32:
PutThumb2Mov32((UINT16 *)address, GetThumb2Mov32((UINT16 *)address) + delta);
+
+ ClrFlushInstructionCache(address, 8);
break;
#endif
cc @alpencolt @kbaladurin
Do you know, what could be the reason of such behavior?
I guess that the Linux kernel will page in everything in the flushed range. It may be just an accounting problem.
However, there is increase in startup time
Yes, flushing for every reloc is going to be very expensive. If you wanted to fix the increase in "shared clean" you have observed, I think you would want to keep track of the range of pages that had relocs applied. When you see a new reloc, check if it is a contiguous extension of the range of pages. If it is, add it to the range. If it is not, start with a new range and flush the previous one. You are welcomed to submit PR.