From nobody Mon Nov 23 22:28:41 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1794308722; cv=none; d=zohomail.com; s=zohoarc; b=Zq8QGxF4AB0n12q3MeKL+gJyjjKLL4Z75FpGl6jLmzHmzEqe0CiloUIqS1LkvvhD43TtfXZRNyKLtkRBO+MJRbFLZNjWtqagSXf7wxRjZ91vB1fbFnN1kkQEcPSVSYrAKOqFfD4JYGfxA3wV9VJH/0rPVvMX+nssHxI/3pJxSPQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1794308722; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=ZEw6NHp/22gMco07BUEhZfZRjVEg7f7ZcgTkDRMQDZQ=; b=VeoZnwl4RQ21CrFl3Y6E53T7o3MrOSDHLc537uQ7O+ZMIhs6EI3NgbnMZHY/7za90+77bgDGXCnU87cGvKQ4Hegtv90sIS6NcOvkvD5Ex9L1dhiowvgUY+eMHCXOQttoMWlRPgKHgPEI+sCP7Quph2w2a8HLCW8O7ZCusb8u9WQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1794308722722197.9841205536071; Tue, 10 Nov 2026 03:05:22 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.1481534.1685431 (Exim 4.92) (envelope-from ) id 1xQjf3-000120-7h; Tue, 10 Nov 2026 11:05:09 +0000 Received: by outflank-mailman (output) from mailman id 1481534.1685431; Tue, 10 Nov 2026 11:05:09 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xQjf3-00011t-4J; Tue, 10 Nov 2026 11:05:09 +0000 Received: by outflank-mailman (input) for mailman id 1481534; Tue, 10 Nov 2026 11:05:07 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xQjf1-00011n-HS for xen-devel@lists.xenproject.org; Tue, 10 Nov 2026 11:05:07 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xQjf1-000000067uE-0MFC; Tue, 10 Nov 2026 11:05:07 +0000 Received: from [217.155.165.12] (helo=4b664947-eb84-4a1c-9b91-21fc87073f93.fritz.box.net) by xenbits.xenproject.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (Exim 4.98.2) (envelope-from ) id 1xQjf1-0000000HPwA-1JL3; Tue, 10 Nov 2026 11:05:07 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version: Message-ID:Date:Subject:Cc:To:From; bh=ZEw6NHp/22gMco07BUEhZfZRjVEg7f7ZcgTkDRMQDZQ=; b=2lN3dCoUOETQeEQKN5UmLOTkyz a6Dwf6A3tbAzE23FqriFPKfJelm03q9IZzDCY/y0YsYWj8eSqQ0KHfM1ns8+xoIbklZkLTbN/Yg5P sPo6DKd7P8cxcDhA+X3ld6+DhyKeKHKCfJBYk8oEUF5TavPyDrhHYxprzZTliaY9Jww8=; From: George Dunlap To: xen-devel@lists.xenproject.org Cc: Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie Subject: [PATCH] x86/pv: free the shared_info page switch_compat() replaces Date: Tue, 10 Nov 2026 11:05:00 +0000 Message-ID: <20261110110500.27221-1-gwd@xenproject.org> X-Mailer: git-send-email 2.56.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1794308725322158500 Content-Type: text/plain; charset="utf-8" For a 32-bit PV guest, switch_compat() replaces a shared_info page above 4GiB with one below, and drops the old page with a bare put_page(). Unfortunately, this violates the expected xenheap-page-shared-with-guest freeing discipline. Xenheap pages don't start with a reference count, and aren't put back into the allocation pool after their reference count drops to zero; they're removed from the domain's xenheap list, but someone needs to explicitly call free_xenheap_page() on them for them to return to their pool. d->shared_info here only has a non-zero reference count because it was shared with a guest; and the thing that holds that reference is the PGC_allocated bit. Dropping the reference without having been the one to clear the PGC_allocated bit puts the page in an invalid state. If there are no outstanding references to d->shared_info when switch_compat() happens, the result for pages moved this way is merely a memory leak: the page is removed from the xenheap list, and nothing ever calls free_xenheap_page() on it. So whenever this path is triggered, a page is leaked. Theoretically, if there *is* an outstanding reference to d->shared_info, *and* that reference is still held when the domain is destroyed, then the result is a reference count underflow: Because the reference count never drops to zero, the page stays on the xenheap list; then during domain destruction, relinquish_memory() walks the list, sees the PGC_allocated bit, and calls put_page() again -- potentially dropping the page count to zero. When the other reference holder drops their reference, the count underflows. (put_page() only ASSERT()s that the count is non-zero, so this wouldn't cause a crash in a release build; and the page is never freed, so other potential hazards of general ref count underflow are avoided.) Fortunately this is impossible with the in-tree toolstack, which only switches a domain while building or restoring it, and doesn't have a mapping of shared_info that it holds across that call. Release the page the way domain destruction releases d->shared_info: drop the allocation reference with put_page_alloc_ref(), as relinquish_memory() does, which clears PGC_allocated and takes the page off the xenheap list once nothing else refers to it, and free it with free_xenheap_page() once an RCU grace period has passed. The free waits for the grace period because do_settime() writes every domain's wallclock holding only wc_lock and the domain list's RCU read lock, neither of which excludes switch_compat(), through a pointer it may have read before the switch. The RCU callback uses the domain's own RCU head, d->rcu, and holds a reference to the domain. That reference keeps the domain from being destroyed, and its struct freed, before the callback has run. It also keeps d->rcu's only other user, domain_destroy(), from queueing the head in the meantime; if the callback drops the last reference, domain_destroy() queues d->rcu again, which RCU has finished with by then. No other reference to the page is expected: with no memory, the domain has no page tables that could map it, and the in-tree toolstack holds no mapping of it across the switch. Also allocate the new page into a local variable rather than straight into d->shared_info: if the allocation failed, a concurrent do_settime() could dereference the NULL pointer before the old value was restored. Fixes: 8a9255405ca6 ("x86/domain: adjust limitation on shared_info allocati= on below 4G") Suggested-by: Jan Beulich Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: George Dunlap --- xen/arch/x86/include/asm/domain.h | 3 +++ xen/arch/x86/pv/domain.c | 33 +++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index 634d0710bf..906078641c 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -302,6 +302,9 @@ struct pv_domain =20 /* Maximum physical-address bitwidth supported by this guest. */ unsigned int physaddr_bitsize; + + /* shared_info page switch_compat() replaced, until it is freed. */ + void *old_shared_info; #endif =20 /* map_domain_page() mapping cache. */ diff --git a/xen/arch/x86/pv/domain.c b/xen/arch/x86/pv/domain.c index 0c42ae58aa..dc481bcc07 100644 --- a/xen/arch/x86/pv/domain.c +++ b/xen/arch/x86/pv/domain.c @@ -231,6 +231,14 @@ unsigned long pv_make_cr4(const struct vcpu *v) } =20 #ifdef CONFIG_PV32 +static void cf_check _free_old_shared_info(struct rcu_head *head) +{ + struct domain *d =3D container_of(head, struct domain, rcu); + + FREE_XENHEAP_PAGE(d->arch.pv.old_shared_info); + put_domain(d); +} + int switch_compat(struct domain *d) { struct vcpu *v; @@ -256,16 +264,16 @@ int switch_compat(struct domain *d) if ( virt_to_maddr(d->shared_info) >> 32 ) { shared_info_t *prev =3D d->shared_info; + shared_info_t *next =3D alloc_xenheap_pages(0, MEMF_bits(32)); =20 - d->shared_info =3D alloc_xenheap_pages(0, MEMF_bits(32)); - if ( !d->shared_info ) + if ( !next ) { - d->shared_info =3D prev; rc =3D -ENOMEM; goto undo_and_fail; } - clear_page(d->shared_info); - share_xen_page_with_guest(virt_to_page(d->shared_info), d, SHARE_r= w); + clear_page(next); + share_xen_page_with_guest(virt_to_page(next), d, SHARE_rw); + d->shared_info =3D next; /* * Ensure all pointers to the old shared_info page are replaced. = vCPUs * below XEN_LEGACY_MAX_VCPUS may have stashed a pointer to @@ -273,7 +281,20 @@ int switch_compat(struct domain *d) */ for_each_vcpu ( d, v ) vcpu_info_reset(v); - put_page(virt_to_page(prev)); + + /* + * Drop the allocation reference, which takes the page off the + * domain's xenheap list, and free the page once an RCU grace peri= od + * has passed: do_settime() may still be writing through a pointer= to + * it. The reference taken on the domain keeps it alive until the= n, + * and keeps domain_destroy(), the only other user of d->rcu, from + * queueing it while the callback is pending. + */ + put_page_alloc_ref(virt_to_page(prev)); + ASSERT(!d->arch.pv.old_shared_info); + d->arch.pv.old_shared_info =3D prev; + get_knownalive_domain(d); + call_rcu(&d->rcu, _free_old_shared_info); } =20 for_each_vcpu( d, v ) --=20 2.56.0