* [RFC] xen/arm: guard pages and overflow detection for vCPU stacks
@ 2026-11-28 17:58 Gabriel Quintáns Souto
2026-11-28 19:09 ` Andrew Cooper
0 siblings, 1 reply; 3+ messages in thread
From: Gabriel Quintáns Souto @ 2026-11-28 17:58 UTC (permalink / raw)
To: xen-devel
Cc: sstabellini, Orzel, Michal, bertrand.marquis, julien,
Volodymyr_Babchuk, stewart.hildebrand
Hi all,
In the "Stack checking on Arm" thread [1], Julien suggested, and
Stefano agreed, that we eventually want stack guards on Arm. As far
as I can see in current staging, nothing like this has landed yet.
This is complementary to the stack protector work [2].
I'd like to work on this, and agree on the approach before writing
patches.
Proposal:
Part 1: vCPU stacks in the vmap area, with a guard page
arch_vcpu_create() keeps allocating STACK_ORDER physically
contiguous pages, but switches from alloc_xenheap_pages() to
alloc_domheap_pages(). It then maps them with __vmap(), asking for
an alignment of 2 * STACK_SIZE. The vmap allocator always leaves
at least one unmapped page below each allocation, so every stack
gets a guard page right below it.
The physical layout of the stack is the same as today. Only the
virtual address changes. get_cpu_info(), show_trace() and
guest_mode() mask with STACK_SIZE, and they keep working because
the stack is still STACK_SIZE aligned.
cpu0_boot_stack is only used until start_xen() switches to idle
vCPU 0, so it stays as it is.
Part 2: Overflow detection on arm64
On its own, a guard page turns an overflow into a recursive data
abort, because entry.S pushes the frame on the same stack. I would
follow what KVM does for the nVHE hypervisor [3][4]. Because the
stack is aligned to twice its size, bit log2(STACK_SIZE) of SP is
clear while SP is inside the stack and set once it has dropped
below it. In the EL2h vectors, I'd test that bit before saving any
state and without clobbering a GPR. On overflow, the handler
switches to a small per-CPU overflow stack and panics. Traps from
the guest go through the lower-EL vectors and start on an empty
stack, and therefore skip the check and get no extra overhead.
The bit test is the only reason for aligning to 2 * STACK_SIZE
rather than just STACK_SIZE.
Part 1 already covers arm32. For this part, I'd start with arm64
and look at arm32 afterwards.
So far I've looked at what could break with a vmap'ed stack.
virt_to_page() only works on directmap addresses, but the only place
that uses it on a stack address is the free_xenheap_pages() in
arch_vcpu_destroy(), and that goes away anyway.
I didn't find anything that hands the physical address of a stack
buffer to hardware or firmware. Even if there were, the allocation
stays physically contiguous.
On arm32 each CPU has its own root table. prepare_secondary_mm()
copies all of CPU0's entries into it (except the domheap ones). The
vmap area is below 1GB, under a second-level table that all CPUs
share, so a stack mapped after the copy is visible on every CPU.
Two things I'd like your opinion on:
- Aligning each stack to 64K costs up to 32K of extra VA per vCPU.
The 1GB arm64 vmap area then fits about 16k stacks, shared with
ioremap and the other vmap users. Is that acceptable, or would you
prefer a dedicated region?
- Always on for MMU builds, or behind a Kconfig option?
If the direction looks fine, I'll send part 1 first, and part 2 as a
separate series.
Best regards,
Gabriel
[1] https://lists.xenproject.org/archives/html/xen-devel/2024-07/msg01578.html
[2] be2a6a1096 ("xen: common: add ability to enable stack protector")
[3] Linux 1a919b17ef01 ("KVM: arm64: Add guard pages for pKVM
(protected nVHE) hypervisor stack")
[4] Linux 66de19fad9ef ("KVM: arm64: Detect and handle hypervisor
stack overflows")
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [RFC] xen/arm: guard pages and overflow detection for vCPU stacks 2026-11-28 17:58 [RFC] xen/arm: guard pages and overflow detection for vCPU stacks Gabriel Quintáns Souto @ 2026-11-28 19:09 ` Andrew Cooper 2026-11-28 21:49 ` Gabriel Quintáns Souto 0 siblings, 1 reply; 3+ messages in thread From: Andrew Cooper @ 2026-11-28 19:09 UTC (permalink / raw) To: Gabriel Quintáns Souto, xen-devel Cc: Andrew Cooper, sstabellini, Orzel, Michal, bertrand.marquis, julien, Volodymyr_Babchuk, stewart.hildebrand On 28/11/2026 5:58 pm, Gabriel Quintáns Souto wrote: > Hi all, > > In the "Stack checking on Arm" thread [1], Julien suggested, and > Stefano agreed, that we eventually want stack guards on Arm. As far > as I can see in current staging, nothing like this has landed yet. > This is complementary to the stack protector work [2]. > > I'd like to work on this, and agree on the approach before writing > patches. > > Proposal: > > Part 1: vCPU stacks in the vmap area, with a guard page > > arch_vcpu_create() keeps allocating STACK_ORDER physically > contiguous pages, but switches from alloc_xenheap_pages() to > alloc_domheap_pages(). It then maps them with __vmap(), asking for > an alignment of 2 * STACK_SIZE. The vmap allocator always leaves > at least one unmapped page below each allocation, so every stack > gets a guard page right below it. > > The physical layout of the stack is the same as today. Only the > virtual address changes. get_cpu_info(), show_trace() and > guest_mode() mask with STACK_SIZE, and they keep working because > the stack is still STACK_SIZE aligned. > > cpu0_boot_stack is only used until start_xen() switches to idle > vCPU 0, so it stays as it is. > > Part 2: Overflow detection on arm64 > > On its own, a guard page turns an overflow into a recursive data > abort, because entry.S pushes the frame on the same stack. I would > follow what KVM does for the nVHE hypervisor [3][4]. Because the > stack is aligned to twice its size, bit log2(STACK_SIZE) of SP is > clear while SP is inside the stack and set once it has dropped > below it. In the EL2h vectors, I'd test that bit before saving any > state and without clobbering a GPR. On overflow, the handler > switches to a small per-CPU overflow stack and panics. Traps from > the guest go through the lower-EL vectors and start on an empty > stack, and therefore skip the check and get no extra overhead. > > The bit test is the only reason for aligning to 2 * STACK_SIZE > rather than just STACK_SIZE. > > Part 1 already covers arm32. For this part, I'd start with arm64 > and look at arm32 afterwards. > > So far I've looked at what could break with a vmap'ed stack. > > virt_to_page() only works on directmap addresses, but the only place > that uses it on a stack address is the free_xenheap_pages() in > arch_vcpu_destroy(), and that goes away anyway. > > I didn't find anything that hands the physical address of a stack > buffer to hardware or firmware. Even if there were, the allocation > stays physically contiguous. > > On arm32 each CPU has its own root table. prepare_secondary_mm() > copies all of CPU0's entries into it (except the domheap ones). The > vmap area is below 1GB, under a second-level table that all CPUs > share, so a stack mapped after the copy is visible on every CPU. > > Two things I'd like your opinion on: > > - Aligning each stack to 64K costs up to 32K of extra VA per vCPU. > The 1GB arm64 vmap area then fits about 16k stacks, shared with > ioremap and the other vmap users. Is that acceptable, or would you > prefer a dedicated region? > - Always on for MMU builds, or behind a Kconfig option? > > If the direction looks fine, I'll send part 1 first, and part 2 as a > separate series. I'm the last person to work on the x86 stacks, but I'm not terribly familiar with the ARM ones. Apologies if I've overlooked anything. ARM uses a stack order of 3, meaning 32k stacks. It has been this way since the very outset (2b36ebd4a2f), having been copied from x86. On x86 we fit 8 separate stacks (of differing size between 1k and 8k) into that space, but ARM appears to have no such subdivision within it. 32k is almost certainly overly-large for what Xen needs. ARM does have a cpu_info block but this was also copied from x86 in the same commit. For historic reasons, the x86 register normally used as the thread-local pointer (%gs) is owned by PV guest kernels under Xen. This is why the stacks have an alignment requirement on their size, so the ANDs and ORs in get_cpu_info() can work with nothing but the stack pointer somewhere within the designated stack. AIUI, ARM always has a good TPIDR_EL2 so does not actually need to copy the same trick x86 uses. In fact, ARM's get_cpu_info() should probably be simply: return (void *)current->arch.stack + STACK_SIZE - sizeof(struct cpu_info); which removes all alignment requirement I believe. Anyway, you can probably use an order 2 allocation and make one of those pages be the guard page, for a 12k stack. That gets you your main stack and safety stack in the same space as the stacks currently occupy. ~Andrew ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [RFC] xen/arm: guard pages and overflow detection for vCPU stacks 2026-11-28 19:09 ` Andrew Cooper @ 2026-11-28 21:49 ` Gabriel Quintáns Souto 0 siblings, 0 replies; 3+ messages in thread From: Gabriel Quintáns Souto @ 2026-11-28 21:49 UTC (permalink / raw) To: Andrew Cooper, Gabriel Quintáns Souto, xen-devel Cc: sstabellini, Orzel, Michal, bertrand.marquis, julien, Volodymyr_Babchuk, stewart.hildebrand On Sat Nov 28, 2026 at 8:09 PM CET, Andrew Cooper wrote: > On 28/11/2026 5:58 pm, Gabriel Quintáns Souto wrote: >> Hi all, >> >> In the "Stack checking on Arm" thread [1], Julien suggested, and >> Stefano agreed, that we eventually want stack guards on Arm. As far >> as I can see in current staging, nothing like this has landed yet. >> This is complementary to the stack protector work [2]. >> >> I'd like to work on this, and agree on the approach before writing >> patches. >> >> Proposal: >> >> Part 1: vCPU stacks in the vmap area, with a guard page >> >> arch_vcpu_create() keeps allocating STACK_ORDER physically >> contiguous pages, but switches from alloc_xenheap_pages() to >> alloc_domheap_pages(). It then maps them with __vmap(), asking for >> an alignment of 2 * STACK_SIZE. The vmap allocator always leaves >> at least one unmapped page below each allocation, so every stack >> gets a guard page right below it. >> >> The physical layout of the stack is the same as today. Only the >> virtual address changes. get_cpu_info(), show_trace() and >> guest_mode() mask with STACK_SIZE, and they keep working because >> the stack is still STACK_SIZE aligned. >> >> cpu0_boot_stack is only used until start_xen() switches to idle >> vCPU 0, so it stays as it is. >> >> Part 2: Overflow detection on arm64 >> >> On its own, a guard page turns an overflow into a recursive data >> abort, because entry.S pushes the frame on the same stack. I would >> follow what KVM does for the nVHE hypervisor [3][4]. Because the >> stack is aligned to twice its size, bit log2(STACK_SIZE) of SP is >> clear while SP is inside the stack and set once it has dropped >> below it. In the EL2h vectors, I'd test that bit before saving any >> state and without clobbering a GPR. On overflow, the handler >> switches to a small per-CPU overflow stack and panics. Traps from >> the guest go through the lower-EL vectors and start on an empty >> stack, and therefore skip the check and get no extra overhead. >> >> The bit test is the only reason for aligning to 2 * STACK_SIZE >> rather than just STACK_SIZE. >> >> Part 1 already covers arm32. For this part, I'd start with arm64 >> and look at arm32 afterwards. >> >> So far I've looked at what could break with a vmap'ed stack. >> >> virt_to_page() only works on directmap addresses, but the only place >> that uses it on a stack address is the free_xenheap_pages() in >> arch_vcpu_destroy(), and that goes away anyway. >> >> I didn't find anything that hands the physical address of a stack >> buffer to hardware or firmware. Even if there were, the allocation >> stays physically contiguous. >> >> On arm32 each CPU has its own root table. prepare_secondary_mm() >> copies all of CPU0's entries into it (except the domheap ones). The >> vmap area is below 1GB, under a second-level table that all CPUs >> share, so a stack mapped after the copy is visible on every CPU. >> >> Two things I'd like your opinion on: >> >> - Aligning each stack to 64K costs up to 32K of extra VA per vCPU. >> The 1GB arm64 vmap area then fits about 16k stacks, shared with >> ioremap and the other vmap users. Is that acceptable, or would you >> prefer a dedicated region? >> - Always on for MMU builds, or behind a Kconfig option? >> >> If the direction looks fine, I'll send part 1 first, and part 2 as a >> separate series. > > I'm the last person to work on the x86 stacks, but I'm not terribly > familiar with the ARM ones. Apologies if I've overlooked anything. Thanks for the background, that helps. > ARM uses a stack order of 3, meaning 32k stacks. It has been this way > since the very outset (2b36ebd4a2f), having been copied from x86. > > On x86 we fit 8 separate stacks (of differing size between 1k and 8k) > into that space, but ARM appears to have no such subdivision within it. > 32k is almost certainly overly-large for what Xen needs. I agree 32k looks large, especially since Arm has one stack per vCPU rather than per pCPU. The design works with any power-of-two size, so moving to 16k would only mean changing STACK_ORDER. But I'd keep that as a separate question for Julien and Stefano, after some measurement. > ARM does have a cpu_info block but this was also copied from x86 in the > same commit. > > For historic reasons, the x86 register normally used as the thread-local > pointer (%gs) is owned by PV guest kernels under Xen. This is why the > stacks have an alignment requirement on their size, so the ANDs and ORs > in get_cpu_info() can work with nothing but the stack pointer somewhere > within the designated stack. > > AIUI, ARM always has a good TPIDR_EL2 so does not actually need to copy > the same trick x86 uses. In fact, ARM's get_cpu_info() should probably > be simply: > > return (void *)current->arch.stack + STACK_SIZE - sizeof(struct cpu_info); > > > which removes all alignment requirement I believe. On Arm, get_cpu_info() is also used to get the cpu_info of the stack we are actually running on, which isn't always current's. start_secondary() does memset(get_cpu_info(), ...) before set_processor_id() and set_current(). And at the end of start_xen(), the memcpy() into idle_vcpu[0]->arch.cpu_info relies on get_cpu_info() returning cpu0_boot_stack's cpu_info, while current is already idle vCPU 0. More importantly for this series, arm64 has nothing like IST, so nothing switches stacks for us when we hit the guard page. That's what part 2 does in software, following KVM, and the check in the vectors only works because the stack VA is aligned to a power of two. So I'd rather keep the alignment. > Anyway, you can probably use an order 2 allocation and make one of those > pages be the guard page, for a 12k stack. That gets you your main stack > and safety stack in the same space as the stacks currently occupy. On Arm the directmap uses block mappings, and xen_pt_update() can't shatter them, so the guard can't be carved out of the allocation. It has to be an unmapped page in the vmap area, and then it doesn't need to be backed by RAM. Best regards, Gabriel ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-28 21:50 UTC | newest] Thread overview: 3+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-28 17:58 [RFC] xen/arm: guard pages and overflow detection for vCPU stacks Gabriel Quintáns Souto 2026-11-28 19:09 ` Andrew Cooper 2026-11-28 21:49 ` Gabriel Quintáns Souto
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.