Skip to content

Map M:N thread stack chunks initially as PROT_NONE#16232

Merged
jhawthorn merged 1 commit intoruby:masterfrom
jhawthorn:mn_threads_prot_none
Mar 5, 2026
Merged

Map M:N thread stack chunks initially as PROT_NONE#16232
jhawthorn merged 1 commit intoruby:masterfrom
jhawthorn:mn_threads_prot_none

Conversation

@jhawthorn
Copy link
Copy Markdown
Member

Previously we initially mapped the full 512MB chunk as PROT_READ|PROD_WRITE and then set a guard page to PROT_NONE the first time a new thread stack is needed. Usually that's okay as we don't touch that memory until it is needed and so it doesn't count towards RSS.

However, on Linux even with vm.overcommit_memory=0 (the default) if on a system (like a tiny cloud VM) with <512MB of RAM+swap that would error with.

Thread#initialize': can't create Thread: Cannot allocate memory (ThreadError)

This changes the chunk to be mapped initially with PROT_NONE, then instead of mapping the guard pages we map in the machine and VM stacks using mprotect. This ensures we don't commit stack memory until it is first used, and as a side benefit any stray pointers into unused stack should segfault.

When a stack is freed/reused there is no change from the previous behaviour, we just use madvise and leave the same regions in place.

The memory layouts are mostly the same (afaict VMA sizes are the same) except around the unallocated space, testing with RUBY_MN_THREADS=1 ./miniruby -e 'th = 4.times.map { Thread.new { sleep 10 } }; system("cat /proc/#$$/maps")'

Before

7ffb37c00000-7ffb37d01000 rw-p 00000000 00:00 0                          [anon:Ruby:nt_alloc_thread_stack_chunk] <-- header region + VM stack 0
7ffb37d01000-7ffb37d02000 ---p 00000000 00:00 0                          [anon:Ruby:nt_alloc_thread_stack_chunk] <-- guard page
7ffb37d02000-7ffb37f02000 rw-p 00000000 00:00 0                          [anon:Ruby:nt_alloc_thread_stack_chunk] <-- machine stack 0 + VM stack 1
7ffb37f02000-7ffb37f03000 ---p 00000000 00:00 0                          [anon:Ruby:nt_alloc_thread_stack_chunk] <-- guard page
7ffb37f03000-7ffb38103000 rw-p 00000000 00:00 0                          [anon:Ruby:nt_alloc_thread_stack_chunk] <-- machine stack 1 + VM stack 2
7ffb38103000-7ffb38104000 ---p 00000000 00:00 0                          [anon:Ruby:nt_alloc_thread_stack_chunk] <-- guard page
7ffb38104000-7ffb38304000 rw-p 00000000 00:00 0                          [anon:Ruby:nt_alloc_thread_stack_chunk] <-- machine stack 2 + VM stack 3
7ffb38304000-7ffb38305000 ---p 00000000 00:00 0                          [anon:Ruby:nt_alloc_thread_stack_chunk] <-- guard page
7ffb38305000-7ffb57c00000 rw-p 00000000 00:00 0                          [anon:Ruby:nt_alloc_thread_stack_chunk] <-- ~500MB unallocated space, R/W + machine stack 3
7ff4e3200000-7ff4e3301000 rw-p 00000000 00:00 0                          [anon:Ruby:nt_alloc_thread_stack_chunk] <-- header region + VM stack 0
7ff4e3301000-7ff4e3302000 ---p 00000000 00:00 0                          [anon:Ruby:nt_alloc_thread_stack_chunk] <-- guard page
7ff4e3302000-7ff4e3502000 rw-p 00000000 00:00 0                          [anon:Ruby:nt_alloc_thread_stack_chunk] <-- machine stack 0 + VM stack 1
7ff4e3502000-7ff4e3503000 ---p 00000000 00:00 0                          [anon:Ruby:nt_alloc_thread_stack_chunk] <-- guard page
7ff4e3503000-7ff4e3703000 rw-p 00000000 00:00 0                          [anon:Ruby:nt_alloc_thread_stack_chunk] <-- machine stack 1 + VM stack 2
7ff4e3703000-7ff4e3704000 ---p 00000000 00:00 0                          [anon:Ruby:nt_alloc_thread_stack_chunk] <-- guard page
7ff4e3704000-7ff4e3904000 rw-p 00000000 00:00 0                          [anon:Ruby:nt_alloc_thread_stack_chunk] <-- machine stack 2 + VM stack 3
7ff4e3904000-7ff4e3905000 ---p 00000000 00:00 0                          [anon:Ruby:nt_alloc_thread_stack_chunk] <-- guard page
7ff4e3905000-7ff4e3a05000 rw-p 00000000 00:00 0                          [anon:Ruby:nt_alloc_thread_stack_chunk] <-- machine stack 3
7ff4e3a05000-7ff503200000 ---p 00000000 00:00 0                          [anon:Ruby:nt_alloc_thread_stack_chunk] <-- ~500MB unallocated space, PROT_NONE

On linux adjacent VMAs with the same permissions are merged, on macos they aren't. I don't know about other platforms. I think it's probably okay if they aren't.

@jhawthorn jhawthorn requested a review from ko1 February 24, 2026 04:25
Previously we initially mapped the full 512MB chunk as
PROT_READ|PROD_WRITE and then set a guard page to PROT_NONE the first
time a new thread stack is needed. Usually that's okay as we don't touch
that memory until it is needed and so it doesn't count towards RSS.

However, on Linux even with vm.overcommit_memory=0 (the default) if on a
system (like a tiny cloud VM) with <512MB of RAM+swap that would error
with.

    Thread#initialize': can't create Thread: Cannot allocate memory (ThreadError)

This changes the chunk to be mapped initially with PROT_NONE, then
instead of mapping the guard pages we map in the machine and VM stacks
using mprotect. This ensures we don't commit stack memory until it is
first used, and as a side benefit any stray pointers into unused stack
should segfault.

When a stack is freed/reused there is no change from the previous
behaviour, we just use madvise and leave the same regions in place.

[Bug #21944]
@jhawthorn jhawthorn force-pushed the mn_threads_prot_none branch from c17ce50 to fe32432 Compare March 5, 2026 18:35
@jhawthorn jhawthorn enabled auto-merge (rebase) March 5, 2026 18:35
@jhawthorn jhawthorn merged commit 407dd02 into ruby:master Mar 5, 2026
91 checks passed
@nobu
Copy link
Copy Markdown
Member

nobu commented Mar 6, 2026

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants