From: "Gabriel Quintáns Souto" <gabi.qs.mail@gmail.com>
To: "Andrew Cooper" <andrew.cooper3@citrix.com>,
"Gabriel Quintáns Souto" <gabi.qs.mail@gmail.com>,
xen-devel@lists.xenproject.org
Cc: <sstabellini@kernel.org>, "Orzel, Michal" <michal.orzel@amd.com>,
<bertrand.marquis@arm.com>, <julien@xen.org>,
<Volodymyr_Babchuk@epam.com>, <stewart.hildebrand@amd.com>
Subject: Re: [RFC] xen/arm: guard pages and overflow detection for vCPU stacks
Date: Sat, 28 Nov 2026 22:49:32 +0100 [thread overview]
Message-ID: <DN76BWDYDWD6.1D3A7ZFTY2836@gmail.com> (raw)
In-Reply-To: <ba16c9af-f04c-403a-8409-beefbdc3de56@citrix.com>
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
prev parent reply other threads:[~2026-11-28 21:50 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
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 message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DN76BWDYDWD6.1D3A7ZFTY2836@gmail.com \
--to=gabi.qs.mail@gmail.com \
--cc=Volodymyr_Babchuk@epam.com \
--cc=andrew.cooper3@citrix.com \
--cc=bertrand.marquis@arm.com \
--cc=julien@xen.org \
--cc=michal.orzel@amd.com \
--cc=sstabellini@kernel.org \
--cc=stewart.hildebrand@amd.com \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.