From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 13756606A31C for ; Sat, 28 Nov 2026 21:50:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=a0U24MM1 Received: from list by lists.xenproject.org with outflank-mailman.1506770.1700233 (Exim 4.92) (envelope-from ) id 1xXQIc-0004kM-Jj; Sat, 28 Nov 2026 21:49:38 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 1506770.1700233; Sat, 28 Nov 2026 21:49:38 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xXQIc-0004kE-Ep; Sat, 28 Nov 2026 21:49:38 +0000 Received: by outflank-mailman (input) for mailman id 1506770; Sat, 28 Nov 2026 21:49:36 +0000 Received: from mx.expurgate.net ([195.190.135.20]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xXQIa-0004k8-M6 for xen-devel@lists.xenproject.org; Sat, 28 Nov 2026 21:49:36 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xXQIZ-004i7F-Rp for xen-devel@lists.xenproject.org; Sat, 28 Nov 2026 22:49:35 +0100 Received: from [10.42.69.6] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6b0b4c18-8faa-0a2a0a5109dd-0a2a4506aa1e-36 for ; Sat, 28 Nov 2026 22:49:35 +0100 Received: from [209.85.128.43] (helo=mail-wm1-f43.google.com) by tlsNG-16d1c6.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6b0b4c6f-195a-0a2a45060019-d155802bdd9f-3 for ; Sat, 28 Nov 2026 22:49:35 +0100 Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-4bb61185057so58638235e9.1 for ; Sat, 28 Nov 2026 13:49:35 -0800 (PST) Received: from localhost (cm-93-156-219-191.telecable.es. [93.156.219.191]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb853ca452sm771814495e9.15.2026.11.28.13.49.33 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 28 Nov 2026 13:49:34 -0800 (PST) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Post: List-Help: List-Subscribe: , Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=20260707 header.d=gmail.com header.i="@gmail.com" header.h="In-Reply-To:References:To:From:Subject:Cc:Message-Id:Date:Content-Type:Content-Transfer-Encoding:Mime-Version" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795902575; x=1796507375; darn=lists.xenproject.org; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-type:content-transfer-encoding:mime-version:from:to:cc :subject:date:message-id:reply-to:content-type; bh=8pkr4T/bD/ZGH+rCm6w6e+gtgEeblYVTI/eoI84Rluo=; b=a0U24MM1sXUqP8AkDRuRswMekjRFAKTUuUkLUzTocZ5Y34M/7cBaAnK4PXDloBhu0x sz+65fHWIajTfcSHVo4x0LUYVTCtQxRz9D7eLaSu2xRNx0D3kaYwWe7FS8Ld0b9UoiD3 czs8xdk1OlPpzItl7KhfOOFowzYZ4wEqIDo0IchOqsNlAAKcJTjKc9uIzJKTFJehaUSX Uu8VnEXbZsfxMF1NQupAsyen2+qC0m6QgaabW5yslSES4YjEvqaBYE6Ua7Da91q+He9h AmuqR8IBnZb/h1hWI/9S2LooaQQh21DXAYigpzSuefqe+/n1ihnGovHaLSe4kZYTnSDg UHuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795902575; x=1796507375; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-type:content-transfer-encoding:mime-version:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=8pkr4T/bD/ZGH+rCm6w6e+gtgEeblYVTI/eoI84Rluo=; b=xdmeS+z0xEmr9A4KMvlHcr+MgjutP284kYmPHQVJ1T9RWEhx1VhB31JqCD2mU5NWzp NVBthk16MXlTTZ+0y3AwfL49wBlNJbIpMcfaA3VD/YLQZXTjsw2cd35z0bpefOhNoVaz qWXdwoUXROrZnVL5onWh1ltB1VPMscTWeORp8X9lt3Dda8td7sPCdmoKDG+LkUI12l9m M40sMCM9Q1I4yZhe7sF46w32xamJdmxfzlVPua7rL4VDDDrmARQ+33yTMHQCY+0VXEeQ q40YdMzx3UX+9JBHSymVTdqExy6OR3tYkoSks2f9FtOLG5LQ1mAdo+hL2fbGKsu4rV5D DHSQ== X-Forwarded-Encrypted: i=1; AL+hErENPHkqCMS0FcAj8sOSr5eW5uAutrT5HtllmCqpn7TwhNkVqg83c/jVxsgb0MwJNGD6qvob3adxJHQ=@lists.xenproject.org X-Gm-Message-State: APuU4Z+np1Ak80YcgTjUnRpHA1tVYJ7fCpgoiuxxRvNbUo8A4B70Dz7Y cZJ6hgVUejd/Tj/iSwPi7fzkjL5nAC2h6Oc/oiLHpkkExnKuf+PzjKrC X-Gm-Gg: AZFdTZfe8zoTpPi/nwSpLOL2mEAXMjo8P72nu7YR6kFw+0nWm8iSRHg372WfXsysmnh 1P8A6fmlAf1cVK/e1DxY4t24jkJYvrTW+Fuz34UtRhEPGvevc+JAd8+2XjmlE8ktulqM1zQz7zp WayG4oSZVMB4BjQ7LbTykVdVS2IPdE6GAnYNQ5/Wp2oKuqYNa13+QBKNbmwL6J5QI6qXfj5SEGI dEVK+Jn6S8nJjGgxkl/JJi0AUQ7s8BSBshpoqu0fT+ZkljmZPHSCwh1aiC9lun/2b6x1wwtXloS Lv9h0gmo+sgj4ocB8nvzPTPgezRle/EUL6gn2jt0/SBkwRMLpdJkJ+gEXJn/0dekAvkNcARhTZS HIRmBauV03Jkb9t8oVACEQklHC5lABipIM4k06LvSEdNG6prrsaXXzsH3KeaD7drxB7zBTNbgOx CKwBSuKEWixab/543We4o9nlFuX9TIOx1zfhzxmLPuoY4ZqFpXi1H4M4nwMeaRAuTYDMnAdLcl1 VgZaLfxCPagnq0IfLDcth/SZNJWuwUG7IjO1jZ79CaWV7CPhB+t6LPAWQlsRKmgQzVEnLrLc+1g x3ibcNDs54wShN1N5NEi9rB7xpUkqOasCoU0RszIFbH73pZ+XgaLakPi656iag== X-Received: by 2002:a05:600c:8210:b0:4bb:6a2d:b438 with SMTP id 5b1f17b1804b1-4bb88786c81mr574444815e9.0.1795902575062; Sat, 28 Nov 2026 13:49:35 -0800 (PST) X-Archived-By: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Sat, 28 Nov 2026 22:49:32 +0100 Message-Id: Cc: , "Orzel, Michal" , , , , Subject: Re: [RFC] xen/arm: guard pages and overflow detection for vCPU stacks From: =?utf-8?q?Gabriel_Quint=C3=A1ns_Souto?= To: "Andrew Cooper" , =?utf-8?q?Gabriel_Quint=C3=A1ns_Souto?= , X-Mailer: aerc 0.21.0 References: In-Reply-To: X-purgate-ID: tlsNG-16d1c6/1795902575-1FCCD77B-AEE61970/0/0 X-purgate-type: clean X-purgate-size: 6291 X-Spam-Status: No, score=-2.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,UNPARSEABLE_RELAY autolearn=no autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On Sat Nov 28, 2026 at 8:09 PM CET, Andrew Cooper wrote: > On 28/11/2026 5:58 pm, Gabriel Quint=C3=A1ns 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_i= nfo); > > > 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