From nobody Sun Feb 8 14:48:26 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 header.i=dpsmith@apertussolutions.com; 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; arc=pass (i=1 dmarc=pass fromdomain=apertussolutions.com) ARC-Seal: i=2; a=rsa-sha256; t=1745100653; cv=pass; d=zohomail.com; s=zohoarc; b=iMuWf2+q6OGgIl83rFxasDCn5oV70F6lfJ0Oa5buloJ9oBJ3Ciyky2jyR/0sN3viWehqm5c+HDF6FUlhb4XS0Dcpnl5nMNjQkQlGvZPJvwsiq+kfT9YKobiMiXE/L7LD9zz21SfZKy3PJRiWLS5sFzs/qXzxRybK5lq/X1oZlxI= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1745100653; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=PuW9czx+Smh71kxlwwA/QW0luHAuyQMJevifyYw5t/k=; b=Qfqw9ewbMGL+WWqhIo0EwdLeIwLvm4tA22GuCPR1tXpvUBuuBIVmv0dmukwNSPDV7Ib5bxjrPf7HbbUoFvYSRDbStP3BWr7FLKTYDXdFyYUL/kZKETErJK1L5+08M1xF2mRdtk+cT+nxDZUzDcABr5rCzuAHNSdXA4WtESC1cI8= ARC-Authentication-Results: i=2; mx.zohomail.com; dkim=pass header.i=dpsmith@apertussolutions.com; 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; arc=pass (i=1 dmarc=pass fromdomain=apertussolutions.com) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1745100653556707.9848228288085; Sat, 19 Apr 2025 15:10:53 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.960052.1352113 (Exim 4.92) (envelope-from ) id 1u6GOT-0007rC-C4; Sat, 19 Apr 2025 22:10:37 +0000 Received: by outflank-mailman (output) from mailman id 960052.1352113; Sat, 19 Apr 2025 22:10:37 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1u6GOT-0007r5-95; Sat, 19 Apr 2025 22:10:37 +0000 Received: by outflank-mailman (input) for mailman id 960052; Sat, 19 Apr 2025 22:10:35 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1u6GOR-0007X1-CG for xen-devel@lists.xenproject.org; Sat, 19 Apr 2025 22:10:35 +0000 Received: from sender4-of-o50.zoho.com (sender4-of-o50.zoho.com [136.143.188.50]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 1c455903-1d6b-11f0-9ffb-bf95429c2676; Sun, 20 Apr 2025 00:10:33 +0200 (CEST) Received: by mx.zohomail.com with SMTPS id 1745100527528975.9353101015664; Sat, 19 Apr 2025 15:08:47 -0700 (PDT) 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" X-Inumbo-ID: 1c455903-1d6b-11f0-9ffb-bf95429c2676 ARC-Seal: i=1; a=rsa-sha256; t=1745100530; cv=none; d=zohomail.com; s=zohoarc; b=mQFgns4wlgiFGGcV0I8st8M/lQ28EOzTgXfoj9QLMsh/zr/1N5Y++d0iENcPHzWujNCIyyw4k550kmo5V7GTSWypYxH3pvmZiHmYYhpfWSmQz8dHMv3u6OOOuARIinEPl7ulkkigaEB/UMFrfClNUd4TwL/H38H/jMrrZ8MlUfA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1745100530; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=PuW9czx+Smh71kxlwwA/QW0luHAuyQMJevifyYw5t/k=; b=ODQ+J7wMqkVXkGo1ANIcUZ7Rk2YnLRVwnQrJGzIoPUsGONt553R6Fu079lNzWlaoc6z7VlcHZp9JgLIWmahvMwRittuVDs9vqfpsMhd3TDXDPybGv6gKie2bbiXEAIXbg7cm8JSpq1x5DgqcKR+JzFUcAi1tGh46QCYLgqlPrjk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=apertussolutions.com; spf=pass smtp.mailfrom=dpsmith@apertussolutions.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1745100530; s=zoho; d=apertussolutions.com; i=dpsmith@apertussolutions.com; h=From:From:To:To:Cc:Cc:Subject:Subject:Date:Date:Message-Id:Message-Id:In-Reply-To:References:MIME-Version:Content-Transfer-Encoding:Reply-To; bh=PuW9czx+Smh71kxlwwA/QW0luHAuyQMJevifyYw5t/k=; b=Sh2tw14027QbFU8ANCcbVzYiHbUlncxACHeVTZmbh/VThXSU3fEEKbwpBMyHfYyp JXlRG2o6JbcgpjpAQgTHjg3o7gLKa3ReRIZUEJjeViuy22wm7Mj5q0ssBaivT71PNkZ Ya8uJoCJkXmC9aTJ7BDtPmxvlbNaNrAKDtEEVMRg= From: "Daniel P. Smith" To: xen-devel@lists.xenproject.org Cc: "Daniel P. Smith" , jason.andryuk@amd.com, stefano.stabellini@amd.com, agarciav@amd.com, Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Subject: [RFC 10/38] x86/boot: refactor dom0 page calculation Date: Sat, 19 Apr 2025 18:07:52 -0400 Message-Id: <20250419220820.4234-11-dpsmith@apertussolutions.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20250419220820.4234-1-dpsmith@apertussolutions.com> References: <20250419220820.4234-1-dpsmith@apertussolutions.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMailClient: External X-ZohoMail-DKIM: pass (identity dpsmith@apertussolutions.com) X-ZM-MESSAGEID: 1745100655725019100 Content-Type: text/plain; charset="utf-8" Caution is needed when dom0 is being constructed as PV using an older kernel that does not have the elf note XEN_ELFNOTE_INIT_P2M. The logic for handling this situation is embedded directly and takes into account whether dom0 mem= ory parameters were specified using the negative allocation syntax. To prepare = for generalizing domain page allocation, isolate this logic to a separate handl= ing function. Signed-off-by: Daniel P. Smith --- xen/arch/x86/dom0_build.c | 76 +++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 35 deletions(-) diff --git a/xen/arch/x86/dom0_build.c b/xen/arch/x86/dom0_build.c index 658d81ab598c..a007e424bbe3 100644 --- a/xen/arch/x86/dom0_build.c +++ b/xen/arch/x86/dom0_build.c @@ -353,12 +353,50 @@ static void __init calculate_dom0_pages( bd->mem_pages =3D nr_pages; } =20 +static void __init dom0_pv_restrict_pages( + struct boot_domain *bd, struct elf_dom_parms *parms) +{ + if ( (parms->p2m_base =3D=3D UNSET_ADDR) && !memsize_gt_zero(&dom0_siz= e) && + (!memsize_gt_zero(&dom0_min_size) || (bd->mem_pages > bd->min_pag= es)) ) + { + /* + * Legacy Linux kernels (i.e. such without a XEN_ELFNOTE_INIT_P2M + * note) require that there is enough virtual space beyond the ini= tial + * allocation to set up their initial page tables. This space is + * roughly the same size as the p2m table, so make sure the initial + * allocation doesn't consume more than about half the space that's + * available between params.virt_base and the address space end. + */ + unsigned long vstart, vend, end; + unsigned long initrd_len =3D bd->ramdisk ? bd->ramdisk->size : 0; + size_t sizeof_long =3D is_pv_32bit_domain(bd->d) ? sizeof(int) : s= izeof(long); + + vstart =3D parms->virt_base; + vend =3D round_pgup(parms->virt_kend); + if ( !parms->unmapped_initrd ) + vend +=3D round_pgup(initrd_len); + end =3D vend + bd->mem_pages * sizeof_long; + + if ( end > vstart ) + end +=3D end - vstart; + if ( end <=3D vstart || + (sizeof_long < sizeof(end) && end > (1UL << (8 * sizeof_long)= )) ) + { + end =3D sizeof_long >=3D sizeof(end) ? 0 : 1UL << (8 * sizeof_= long); + bd->mem_pages =3D (end - vend) / (2 * sizeof_long); + if ( memsize_gt_zero(&dom0_min_size) && + bd->mem_pages < bd->min_pages ) + bd->mem_pages =3D bd->min_pages; + printk("Dom0 memory clipped to %lu pages\n", bd->mem_pages); + } + } +} + unsigned long __init dom0_compute_nr_pages( struct boot_domain *bd, struct elf_dom_parms *parms) { nodeid_t node; struct domain *d =3D bd->d; - unsigned long initrd_len =3D bd->ramdisk ? bd->ramdisk->size : 0; unsigned long avail =3D 0, iommu_pages =3D 0; =20 for_each_node_mask ( node, dom0_nodes ) @@ -404,40 +442,8 @@ unsigned long __init dom0_compute_nr_pages( /* Clamp according to min/max limits and available memory (final). */ calculate_dom0_pages(bd, avail); =20 - if ( is_pv_domain(d) && - (parms->p2m_base =3D=3D UNSET_ADDR) && !memsize_gt_zero(&dom0_siz= e) && - (!memsize_gt_zero(&dom0_min_size) || (bd->mem_pages > bd->min_pag= es)) ) - { - /* - * Legacy Linux kernels (i.e. such without a XEN_ELFNOTE_INIT_P2M - * note) require that there is enough virtual space beyond the ini= tial - * allocation to set up their initial page tables. This space is - * roughly the same size as the p2m table, so make sure the initial - * allocation doesn't consume more than about half the space that's - * available between params.virt_base and the address space end. - */ - unsigned long vstart, vend, end; - size_t sizeof_long =3D is_pv_32bit_domain(d) ? sizeof(int) : sizeo= f(long); - - vstart =3D parms->virt_base; - vend =3D round_pgup(parms->virt_kend); - if ( !parms->unmapped_initrd ) - vend +=3D round_pgup(initrd_len); - end =3D vend + bd->mem_pages * sizeof_long; - - if ( end > vstart ) - end +=3D end - vstart; - if ( end <=3D vstart || - (sizeof_long < sizeof(end) && end > (1UL << (8 * sizeof_long)= )) ) - { - end =3D sizeof_long >=3D sizeof(end) ? 0 : 1UL << (8 * sizeof_= long); - bd->mem_pages =3D (end - vend) / (2 * sizeof_long); - if ( memsize_gt_zero(&dom0_min_size) && - bd->mem_pages < bd->min_pages ) - bd->mem_pages =3D bd->min_pages; - printk("Dom0 memory clipped to %lu pages\n", bd->mem_pages); - } - } + if ( is_pv_domain(d) ) + dom0_pv_restrict_pages(bd, parms); =20 d->max_pages =3D min_t(unsigned long, bd->max_pages, UINT_MAX); =20 --=20 2.30.2