Skip to content

Commit af3a4a8

Browse files
committed
Fix up pool allocator stuff.
1 parent 5069d91 commit af3a4a8

5 files changed

+131
-26
lines changed

overlays/bootstrap.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ in {
289289
++ final.lib.optional (versionAtLeast "9.6" && versionLessThan "9.9" && final.stdenv.targetPlatform.isMusl) ./patches/ghc/ghc-9.6-0002-Drop-spurious-8-byte-offset-from-elf_plt.patch
290290
++ final.lib.optional (versionAtLeast "9.6" && versionLessThan "9.9" && final.stdenv.targetPlatform.isMusl) ./patches/ghc/ghc-9.6-0003-Better-pool-alignment.-We-still-hardcode-section-ali.patch
291291
++ final.lib.optional (versionAtLeast "9.6" && versionLessThan "9.9" && final.stdenv.targetPlatform.isMusl) ./patches/ghc/ghc-9.6-0007-fixup-Better-pool-alignment.-We-still-hardcode-secti.patch
292+
++ final.lib.optional (versionAtLeast "9.6" && versionLessThan "9.9" && final.stdenv.targetPlatform.isMusl) ./patches/ghc/ghc-9.6-0008-pool-improvements.patch
292293
# these two are abit questionable. They are pretty rough, and assume static binary as well as posix.
293294
# ++ final.lib.optional (versionAtLeast "9.6" && versionLessThan "9.9" && final.stdenv.targetPlatform.isMusl) ./patches/ghc/ghc-9.6-0004-ghcidladdr.patch
294295
# ++ final.lib.optional (versionAtLeast "9.6" && versionLessThan "9.9" && final.stdenv.targetPlatform.isMusl) ./patches/ghc/ghc-9.6-0005-Better-interpreter-debugging.-Needs-ghcidladdr.patch

overlays/patches/ghc/ghc-9.6-0003-Better-pool-alignment.-We-still-hardcode-section-ali.patch

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,28 @@ index fe3406e..9f56812 100644
1515
+++ b/rts/linker/Elf.c
1616
@@ -131,7 +131,7 @@
1717
https://refspecs.linuxfoundation.org/elf/gabi4+/contents.html
18-
18+
1919
*/
2020
-void * memPoolAlloc(SectionKind kind, StgWord size);
2121
+void * memPoolAlloc(SectionKind kind, StgWord align, StgWord size);
22-
22+
2323
void * __pool = NULL;
2424
void * __pool_rw_offset = NULL;
2525
@@ -144,8 +144,8 @@ void memPoolProtect( void ) {
2626
}
2727
}
28-
28+
2929
-StgWord poolAlign(StgWord size) {
30-
- return (size + 0xff) & ~0xff;
30+
- return (size + 0x3f) & ~0x3f;
3131
+StgWord poolAlign(StgWord align, StgWord size) {
3232
+ return (size + align) & ~align;
3333
}
34-
34+
3535
void * printPoolInfo( void ) {
3636
@@ -156,7 +156,8 @@ void * printPoolInfo( void ) {
3737
return NULL;
3838
}
39-
39+
4040
-void * memPoolAlloc(SectionKind kind, StgWord size) {
4141
+void * memPoolAlloc(SectionKind kind, StgWord align, StgWord size) {
4242
+ ASSERT(0x0 == (size & align));
@@ -50,14 +50,14 @@ index fe3406e..9f56812 100644
5050
- StgWord alignedSize = poolAlign(size);
5151
+ StgWord alignedSize = size;
5252
void * ret = NULL;
53-
53+
5454
if(kind == SECTIONKIND_CODE_OR_RODATA) {
5555
@@ -875,7 +876,7 @@ ocGetNames_ELF ( ObjectCode* oc )
5656
* address might be out of range for sections that are mmaped.
5757
*/
5858
alloc = SECTION_POOL;
5959
- start = memPoolAlloc(kind, size);
60-
+ start = memPoolAlloc(kind, 0xff, size);
60+
+ start = memPoolAlloc(kind, 0x3f, size);
6161
// mmapAnonForLinker(size);
6262
if (start == NULL) {
6363
barf("failed to mmap memory for bss. "
@@ -66,7 +66,7 @@ index fe3406e..9f56812 100644
6666
mapped_start = start;
6767
mapped_offset = 0;
6868
- mapped_size = poolAlign(size);
69-
+ mapped_size = poolAlign(0xff, size);
69+
+ mapped_size = poolAlign(0x3f, size);
7070
}
7171
CHECK(start != 0x0);
7272
#else
@@ -87,28 +87,28 @@ index fe3406e..9f56812 100644
8787
* break up the section itself.
8888
*/
8989
+ unsigned stub_align = 0x7;
90-
+ unsigned section_align = 0xff;
91-
90+
+ unsigned section_align = 0x3f;
91+
9292
unsigned nstubs = numberOfStubsForSection(oc, i);
9393
- unsigned stub_space = STUB_SIZE * nstubs;
9494
+ unsigned stub_space = poolAlign(stub_align, STUB_SIZE * nstubs);
95-
95+
9696
- void * mem = memPoolAlloc(kind, stub_space + size);
9797
+ void * mem = memPoolAlloc(kind, section_align, poolAlign(section_align, stub_space + size));
9898
// void * mem = mmapAnonForLinker(size+stub_space);
99-
99+
100100
if( mem == MAP_FAILED ) {
101101
@@ -939,7 +942,7 @@ ocGetNames_ELF ( ObjectCode* oc )
102102
alloc = SECTION_POOL;
103-
103+
104104
mapped_offset = 0;
105105
- mapped_size = poolAlign(size+stub_space);
106106
+ mapped_size = poolAlign(section_align, size + stub_space);
107107
start = mem;
108108
mapped_start = mem;
109109
#else
110110
@@ -978,7 +981,7 @@ ocGetNames_ELF ( ObjectCode* oc )
111-
111+
112112
#if defined(NEED_PLT)
113113
oc->sections[i].info->nstubs = 0;
114114
- oc->sections[i].info->stub_offset = (uint8_t*)mem + size;
@@ -121,7 +121,7 @@ index fe3406e..9f56812 100644
121121
if(common_size > 0) {
122122
common_mem = // mmapAnonForLinker(common_size);
123123
- memPoolAlloc(SECTIONKIND_RWDATA, common_size);
124-
+ memPoolAlloc(SECTIONKIND_RWDATA, 0xff, common_size);
124+
+ memPoolAlloc(SECTIONKIND_RWDATA, 0x3f, common_size);
125125
if (common_mem == NULL) {
126126
barf("ocGetNames_ELF: Failed to allocate memory for SHN_COMMONs");
127127
}
@@ -135,8 +135,8 @@ index fb8fbfb..c6e185d 100644
135135
void * printPoolInfo ( void );
136136
-StgWord poolAlign ( StgWord size );
137137
+StgWord poolAlign ( StgWord align, StgWord size );
138-
138+
139139
#include "EndPrivate.h"
140-
--
140+
--
141141
2.33.0
142142

overlays/patches/ghc/ghc-9.6-0007-fixup-Better-pool-alignment.-We-still-hardcode-secti.patch

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ index 05db9b3..fd33458 100644
1616
* address might be out of range for sections that are mmaped.
1717
*/
1818
alloc = SECTION_POOL;
19-
- start = memPoolAlloc(kind, 0xff, size);
20-
+ start = memPoolAlloc(kind, 0xff, poolAlign(0xff, size));
19+
- start = memPoolAlloc(kind, 0x3f, size);
20+
+ start = memPoolAlloc(kind, 0x3f, poolAlign(0x3f, size));
2121
// mmapAnonForLinker(size);
2222
if (start == NULL) {
2323
barf("failed to mmap memory for bss. "
2424
@@ -1035,7 +1035,7 @@ ocGetNames_ELF ( ObjectCode* oc )
2525
void * common_mem = NULL;
2626
if(common_size > 0) {
2727
common_mem = // mmapAnonForLinker(common_size);
28-
- memPoolAlloc(SECTIONKIND_RWDATA, 0xff, common_size);
29-
+ memPoolAlloc(SECTIONKIND_RWDATA, 0xff, poolAlign(0xff,common_size));
28+
- memPoolAlloc(SECTIONKIND_RWDATA, 0x3f, common_size);
29+
+ memPoolAlloc(SECTIONKIND_RWDATA, 0x3f, poolAlign(0x3f,common_size));
3030
if (common_mem == NULL) {
3131
barf("ocGetNames_ELF: Failed to allocate memory for SHN_COMMONs");
3232
}
33-
--
33+
--
3434
2.33.0
3535

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
diff --git a/rts/linker/Elf.c b/rts/linker/Elf.c
2+
index 96b93ba..fca17ff 100644
3+
--- a/rts/linker/Elf.c
4+
+++ b/rts/linker/Elf.c
5+
@@ -137,10 +137,12 @@ void * __pool = NULL;
6+
void * __pool_rw_offset = NULL;
7+
void * __pool_rx_offset = NULL;
8+
9+
+#define POOL_SIZE 512
10+
+
11+
void memPoolProtect( void ) {
12+
if(__pool != NULL) {
13+
- mprotect(__pool, 256*1024*1024, PROT_READ | PROT_WRITE);
14+
- mprotect((void*)((uintptr_t)__pool + 256*1024*1024), 256*1024*1024, PROT_READ | PROT_WRITE | PROT_EXEC);
15+
+ mprotect(__pool, (POOL_SIZE>>2)*1024*1024, PROT_READ | PROT_WRITE);
16+
+ mprotect((void*)((uintptr_t)__pool + (POOL_SIZE>>2)*1024*1024), (POOL_SIZE-(POOL_SIZE>>2))*1024*1024, PROT_READ | PROT_WRITE | PROT_EXEC);
17+
}
18+
}
19+
20+
@@ -150,8 +152,8 @@ StgWord poolAlign(StgWord align, StgWord size) {
21+
22+
void * printPoolInfo( void ) {
23+
printf("Pool size: %ldM\n", ((uintptr_t)__pool_rx_offset - (uintptr_t)__pool_rw_offset)/(1024*1024));
24+
- printf("RW: %p -- %p\n", __pool_rw_offset, (void*)((uintptr_t)__pool + 256*1024*1024));
25+
- printf("RX: %p -- %p\n", (void*)((uintptr_t)__pool + 256*1024*1024), __pool_rx_offset);
26+
+ printf("RW: %p -- %p; %ldM\n", __pool_rw_offset, (void*)((uintptr_t)__pool + (POOL_SIZE>>2)*1024*1024), (((uintptr_t)__pool + (POOL_SIZE>>2)*1024*1024) - (uintptr_t)__pool_rw_offset)/(1024*1024));
27+
+ printf("RX: %p -- %p; %ldM\n", (void*)((uintptr_t)__pool + (POOL_SIZE>>2)*1024*1024), __pool_rx_offset, ((uintptr_t)__pool_rx_offset - ((uintptr_t)__pool + (POOL_SIZE>>2)*1024*1024))/(1024*1024));
28+
fflush(stdout);
29+
return NULL;
30+
}
31+
@@ -160,13 +162,13 @@ void * memPoolAlloc(SectionKind kind, StgWord align, StgWord size) {
32+
ASSERT(0x0 == (size & align));
33+
if(__pool == NULL) {
34+
// allocate 1 GB of virtual memory.
35+
- __pool = mmapAnonForLinker(512*1024*1024);
36+
+ __pool = mmapAnonForLinker(POOL_SIZE*1024*1024);
37+
if(__pool == NULL) {
38+
printf("Failed to allocate memory pool\n");
39+
fflush(stdout);
40+
return NULL;
41+
}
42+
- __pool_rw_offset = (void*)((uintptr_t)__pool + 256*1024*1024);
43+
+ __pool_rw_offset = (void*)((uintptr_t)__pool + (POOL_SIZE>>2)*1024*1024);
44+
__pool_rx_offset = __pool_rw_offset;
45+
}
46+
// align to 64 bytes
47+
@@ -177,8 +179,9 @@ void * memPoolAlloc(SectionKind kind, StgWord align, StgWord size) {
48+
ret = __pool_rx_offset;
49+
// printf("Allocated RX %p\n", ret);
50+
__pool_rx_offset = (void*)((uintptr_t)__pool_rx_offset + alignedSize);
51+
- if((uintptr_t)__pool_rx_offset > (uintptr_t)__pool + 512*1024*1024) {
52+
- printf("Out of memory pool\n");
53+
+ if((uintptr_t)__pool_rx_offset > (uintptr_t)__pool + POOL_SIZE*1024*1024) {
54+
+ printf("Out of memory pool, for RX, trying to allocate %ld\n", alignedSize);
55+
+ printPoolInfo();
56+
fflush(stdout);
57+
return NULL;
58+
}
59+
@@ -187,7 +190,8 @@ void * memPoolAlloc(SectionKind kind, StgWord align, StgWord size) {
60+
ret = __pool_rw_offset;
61+
// printf("Allocated RW %p\n", ret);
62+
if((uintptr_t)__pool_rw_offset < (uintptr_t)__pool) {
63+
- printf("Out of memory pool\n");
64+
+ printf("Out of memory pool, for RW, trying to allocate %ld\n", alignedSize);
65+
+ printPoolInfo();
66+
fflush(stdout);
67+
return NULL;
68+
}
69+
@@ -876,7 +880,7 @@ ocGetNames_ELF ( ObjectCode* oc )
70+
* address might be out of range for sections that are mmaped.
71+
*/
72+
alloc = SECTION_POOL;
73+
- start = memPoolAlloc(kind, 0x3f, poolAlign(0x3f, size));
74+
+ start = memPoolAlloc(kind, 0xf, poolAlign(0xf, size));
75+
// mmapAnonForLinker(size);
76+
if (start == NULL) {
77+
barf("failed to mmap memory for bss. "
78+
@@ -884,7 +888,7 @@ ocGetNames_ELF ( ObjectCode* oc )
79+
}
80+
mapped_start = start;
81+
mapped_offset = 0;
82+
- mapped_size = poolAlign(0x3f, size);
83+
+ mapped_size = poolAlign(0xf, size);
84+
}
85+
CHECK(start != 0x0);
86+
#else
87+
@@ -921,7 +925,7 @@ ocGetNames_ELF ( ObjectCode* oc )
88+
* break up the section itself.
89+
*/
90+
unsigned stub_align = 0x7;
91+
- unsigned section_align = 0x3f;
92+
+ unsigned section_align = 0xf;
93+
94+
unsigned nstubs = numberOfStubsForSection(oc, i);
95+
unsigned stub_space = poolAlign(stub_align, STUB_SIZE * nstubs);
96+
@@ -1035,7 +1039,7 @@ ocGetNames_ELF ( ObjectCode* oc )
97+
void * common_mem = NULL;
98+
if(common_size > 0) {
99+
common_mem = // mmapAnonForLinker(common_size);
100+
- memPoolAlloc(SECTIONKIND_RWDATA, 0x3f, poolAlign(0x3f,common_size));
101+
+ memPoolAlloc(SECTIONKIND_RWDATA, 0xf, poolAlign(0xf,common_size));
102+
if (common_mem == NULL) {
103+
barf("ocGetNames_ELF: Failed to allocate memory for SHN_COMMONs");
104+
}

overlays/patches/ghc/ghc-9.6-linker-pool-allocator-2.patch

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ index 4b5a506..fe3406e 100644
3434
}
3535

3636
+StgWord poolAlign(StgWord size) {
37-
+ return (size + 0xff) & ~0xff;
37+
+ return (size + 0x3f) & ~0x3f;
3838
+}
3939
+
4040
+void * printPoolInfo( void ) {
4141
+ printf("Pool size: %ldM\n", ((uintptr_t)__pool_rx_offset - (uintptr_t)__pool_rw_offset)/(1024*1024));
42-
+ printf("RW: %p -- %p\n", __pool_rw_offset, (void*)((uintptr_t)__pool + 512*1024*1024));
43-
+ printf("RX: %p -- %p\n", (void*)((uintptr_t)__pool + 512*1024*1024), __pool_rx_offset);
42+
+ printf("RW: %p -- %p\n", __pool_rw_offset, (void*)((uintptr_t)__pool + 256*1024*1024));
43+
+ printf("RX: %p -- %p\n", (void*)((uintptr_t)__pool + 256*1024*1024), __pool_rx_offset);
4444
+ fflush(stdout);
4545
+ return NULL;
4646
+}

0 commit comments

Comments
 (0)