[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [RFC] xen/arm: guard pages and overflow detection for vCPU stacks


  • To: Gabriel Quintáns Souto <gabi.qs.mail@xxxxxxxxx>, xen-devel@xxxxxxxxxxxxxxxxxxxx
  • From: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
  • Date: Sat, 28 Nov 2026 19:09:13 +0000
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=citrix.com; dmarc=pass action=none header.from=citrix.com; dkim=pass header.d=citrix.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=YLzionL5hU/zgC5FOYZBf30yxE9qTYEB5mbN1IWXOGw=; b=PYfFm9L0el+vVdTq8zou9N+RpIOitxXPzgeHgPQIDG4qRj3d2z2wE+ZoW79Rldub7Aivr3RYay8riQYWNclG2S57L6C736qDB5tp5XHeoqmFSvsgZ9LygG3NNAeYE89J3P99LRi9UKxOG892VpeR77CzikQQu7pP6sRlJVUL0nMYP+rVaDqrdpPQpAmSZEITP2DsM9/YwyA2GdbP/8qAgOpzu8c4430wOzja3NDn7LMbEikdHYsg+fFcRQZmrDYbDCQHe2FPGROP+640CUBSRJMOoUpkJt+7SsraDEdz61DjDyf5/IikNLs5ZEA3qSCfvm4P4EJ8y66cfR30V7FvIQ==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ONsubJrJsM5Awf0yY9zaz+yTLIuLeThBGM7kIumnE9sfDXtAYvWmJQFNQViZALmrS/Fu/SiDZUibz91JMHKrXgGpW9BYBdKUOIDJFMdSGJKu0hAt3vj10u2SjIOKHaDvOim7oOBLgOKGri+1N6wNUkqHa7Ruxhx958Pnoq367JV0CAL6nzUttkNOJyCjZLIGL33vnKWyb++KYCYgLwBVG8IbZEFGnEtpOsItlr+gRWGfaTjfS4U4sHoTM2Vx8CIPlvZvz5g+UQcwd5f9/wXOH3w1VAhT/HL2VLigZ9D56xQE/G3EwhHF70lzC4etfha8FGfAmwjURzORLyJcY3qmwg==
  • Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=selector1 header.d=citrix.com header.i="@citrix.com" header.h="From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck"
  • Authentication-results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=citrix.com;
  • Autocrypt: addr=andrew.cooper3@xxxxxxxxxx; keydata= xsFNBFLhNn8BEADVhE+Hb8i0GV6mihnnr/uiQQdPF8kUoFzCOPXkf7jQ5sLYeJa0cQi6Penp VtiFYznTairnVsN5J+ujSTIb+OlMSJUWV4opS7WVNnxHbFTPYZVQ3erv7NKc2iVizCRZ2Kxn srM1oPXWRic8BIAdYOKOloF2300SL/bIpeD+x7h3w9B/qez7nOin5NzkxgFoaUeIal12pXSR Q354FKFoy6Vh96gc4VRqte3jw8mPuJQpfws+Pb+swvSf/i1q1+1I4jsRQQh2m6OTADHIqg2E ofTYAEh7R5HfPx0EXoEDMdRjOeKn8+vvkAwhviWXTHlG3R1QkbE5M/oywnZ83udJmi+lxjJ5 YhQ5IzomvJ16H0Bq+TLyVLO/VRksp1VR9HxCzItLNCS8PdpYYz5TC204ViycobYU65WMpzWe LFAGn8jSS25XIpqv0Y9k87dLbctKKA14Ifw2kq5OIVu2FuX+3i446JOa2vpCI9GcjCzi3oHV e00bzYiHMIl0FICrNJU0Kjho8pdo0m2uxkn6SYEpogAy9pnatUlO+erL4LqFUO7GXSdBRbw5 gNt25XTLdSFuZtMxkY3tq8MFss5QnjhehCVPEpE6y9ZjI4XB8ad1G4oBHVGK5LMsvg22PfMJ ISWFSHoF/B5+lHkCKWkFxZ0gZn33ju5n6/FOdEx4B8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xTC/7tvG3euCklmkn9Sl9IAKFu29RSo d5bD8kMSCYsTqtTfT6W4A3qHGvIDta3ptLYpIAOD2sY3GYq2nf3Bbzx81wZK14JdDDHUX2Rs 6+ahAA==
  • Cc: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, sstabellini@xxxxxxxxxx, "Orzel, Michal" <michal.orzel@xxxxxxx>, bertrand.marquis@xxxxxxx, julien@xxxxxxx, Volodymyr_Babchuk@xxxxxxxx, stewart.hildebrand@xxxxxxx
  • Delivery-date: Sat, 28 Nov 2026 19:09:35 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

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



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.