From nobody Fri Nov 29 19:48:35 2024 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1632437382; cv=none; d=zohomail.com; s=zohoarc; b=bLLBuMP/VA7xH6JZBv6dAJnaBx7KxPnPSzxlUjD8xSaWVjdsbgDq75fKMscDMGM1Hvtpsvxa3gYFzrcLP0ZdeSf9sNECrVCMpHGT6WeVOt9TjQUJyvp8jyPdRfm06uwyqxwsoZ4f0q2SZVIjqGDf5283UyDoq9xGGjM2HHzlyS4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1632437382; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=rDIaq2zIrPogpfXrurzqgzFbIZpi6DRuEYogRKmWMm0=; b=Pbte56/au0IpBKOqKveun4Qdiqk8m3HwkQj/pSE7her4gjvJgyJWr6k1Oea1aFkDxmZFBcX2nTJPsVNLMqJJGSEdalr3HjNiMQgXQM5GZ3Y06y5suVsYtpXWga4givTKBob1Cu+GgBXVDulGHmDUcbDsERfFsuFZoios9qzZUYw= 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 1632437382974748.8284979651776; Thu, 23 Sep 2021 15:49:42 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.194612.346720 (Exim 4.92) (envelope-from ) id 1mTXWu-0004Ml-Hz; Thu, 23 Sep 2021 22:49:24 +0000 Received: by outflank-mailman (output) from mailman id 194612.346720; Thu, 23 Sep 2021 22:49:24 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mTXWu-0004Mc-Da; Thu, 23 Sep 2021 22:49:24 +0000 Received: by outflank-mailman (input) for mailman id 194612; Thu, 23 Sep 2021 22:49:22 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mTXWs-0003lw-A6 for xen-devel@lists.xenproject.org; Thu, 23 Sep 2021 22:49:22 +0000 Received: from mail-lf1-x130.google.com (unknown [2a00:1450:4864:20::130]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 58cc834b-4318-4c72-8ca9-65c42e9cea80; Thu, 23 Sep 2021 22:49:12 +0000 (UTC) Received: by mail-lf1-x130.google.com with SMTP id u18so31252076lfd.12 for ; Thu, 23 Sep 2021 15:49:12 -0700 (PDT) Received: from otyshchenko.router ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id q5sm230091lfd.17.2021.09.23.15.49.10 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 23 Sep 2021 15:49:11 -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: 58cc834b-4318-4c72-8ca9-65c42e9cea80 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=rDIaq2zIrPogpfXrurzqgzFbIZpi6DRuEYogRKmWMm0=; b=dLv7npLjP8uJTP2IyctwBKqjaIitWlDkbYgkYLkb1/Sc/+vF8HpovJ7nCgGZgK9hMC 8E80aNUeUFnRcrwGoVcId2iZFCOPdcp68BYdkrYfnZ1XtRzCusS04518eaGb+VHErbfR CfHW9MyjFOh/NEaWEYHxMlc0KBcLu9VdXQBedMww97DPwKuQKvvAouG9yvL2492H9787 yrcW6Ea7WDtCwafEk4f3Rrc8ALeMAbTeIrGrEOmZPmD16WXv/6qH415Kh6a110e6CnVT I0RmnjoTg4btPRNTtZs84Wh+noIFor2/vRXI31q4xpyHPU9Inz4hga2/UYVgdCXRCL0p 6pcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=rDIaq2zIrPogpfXrurzqgzFbIZpi6DRuEYogRKmWMm0=; b=cPWwW3GXnnbLevJ37zEKRxPpi4bijEjMUTaw/UdkIA0UeIKKPDI6vssb31Wdoq2PQS t9/1vFYPFV7owHJhms+byGgI5r3PKmWv/yuxFKavk/dgld15ilS5OW9BeBuw9DwuaOGa fY98MoOiSYXQ/oK89rJnNoUvKFpnnrzJuOpH/Nvd+9UTQQFwFsaRPMB+CGGZhArbWmnd jB5P2Y0FBWNaQ3WfRvXKNjhziRI4HogotRV1Cu8965WdmTNzmi+zbXOudVjAyA1A72Vx JDRroHrlTE37nnrInDq0AAy/Nzn8nJdsY7YQEcQ1FiPQNrb4jhmhhcR4tE9T2uglMFYC Vsvw== X-Gm-Message-State: AOAM532Birc9ZcM5qdtonzptNBJ7bNxg6dHwxAyO6R6oN9UKUFa9zBBe qyiQEXoHxtnYNhQsF+BB0oFMyuUeuQU= X-Google-Smtp-Source: ABdhPJxy5ThjF9sKx20tt0R6AYiEQmDq94Nfet1V5taXeyG5QHJoFEGJbDk0IEgOkGY5indJ35tg4A== X-Received: by 2002:a2e:a261:: with SMTP id k1mr7628694ljm.509.1632437351535; Thu, 23 Sep 2021 15:49:11 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Cc: Oleksandr Tyshchenko , Andrew Cooper , George Dunlap , Ian Jackson , Jan Beulich , Julien Grall , Stefano Stabellini , Wei Liu , Volodymyr Babchuk Subject: [PATCH V3 2/3] xen/arm: Add handling of extended regions for Dom0 Date: Fri, 24 Sep 2021 01:48:53 +0300 Message-Id: <1632437334-12015-3-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1632437334-12015-1-git-send-email-olekstysh@gmail.com> References: <1632437334-12015-1-git-send-email-olekstysh@gmail.com> X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1632437384032100001 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Oleksandr Tyshchenko The extended region (safe range) is a region of guest physical address space which is unused and could be safely used to create grant/foreign mappings instead of wasting real RAM pages from the domain memory for establishing these mappings. The extended regions are chosen at the domain creation time and advertised to it via "reg" property under hypervisor node in the guest device-tree. As region 0 is reserved for grant table space (always present), the indexes for extended regions are 1...N. If extended regions could not be allocated for some reason, Xen doesn't fail and behaves as usual, so only inserts region 0. Please note the following limitations: - The extended region feature is only supported for 64-bit domain currently. - The ACPI case is not covered. *** As Dom0 is direct mapped domain on Arm (e.g. MFN =3D=3D GFN) the algorithm to choose extended regions for it is different in comparison with the algorithm for non-direct mapped DomU. What is more, that extended regions should be chosen differently whether IOMMU is enabled or not. Provide RAM not assigned to Dom0 if IOMMU is disabled or memory holes found in host device-tree if otherwise. Make sure that extended regions are 2MB-aligned and located within maximum possible addressable physical memory range. The minimum size of extended region is 64MB. The maximum number of extended regions is 128, which is an artificial limit to minimize code changes (we reuse struct meminfo to describe extended regions, so there are an array field for 128 elements). It worth mentioning that unallocated memory solution (when the IOMMU is disabled) will work safely until Dom0 is able to allocate memory outside of the original range. Also introduce command line option to be able to globally enable or disable support for extended regions for Dom0 (enabled by default). Suggested-by: Julien Grall Signed-off-by: Oleksandr Tyshchenko --- Please note, we need to decide which approach to use in find_unallocated_me= mory(), you can find details at: https://lore.kernel.org/xen-devel/28503e09-44c3-f623-bb8d-8778bb94225f@gmai= l.com/ Changes RFC -> V2: - update patch description - drop uneeded "extended-region" DT property Changes V2 -> V3: - update patch description - add comment for "size" calculation in add_ext_regions() - clarify "end" calculation in find_unallocated_memory() and find_memory_holes() - only pick up regions with size >=3D 64MB - allocate reg dynamically instead of keeping on the stack in make_hypervisor_node() - do not show warning for 32-bit domain - drop Linux specific limits EXT_REGION_* - also cover "ranges" property in find_memory_holes() - add command line arg to enable/disable extended region support --- docs/misc/xen-command-line.pandoc | 7 + xen/arch/arm/domain_build.c | 280 ++++++++++++++++++++++++++++++++++= +++- 2 files changed, 284 insertions(+), 3 deletions(-) diff --git a/docs/misc/xen-command-line.pandoc b/docs/misc/xen-command-line= .pandoc index 177e656..3bb8eb7 100644 --- a/docs/misc/xen-command-line.pandoc +++ b/docs/misc/xen-command-line.pandoc @@ -1081,6 +1081,13 @@ hardware domain is architecture dependent. Note that specifying zero as domU value means zero, while for dom0 it means to use the default. =20 +### ext_regions (Arm) +> `=3D ` + +> Default : `true` + +Flag to globally enable or disable support for extended regions for dom0. + ### flask > `=3D permissive | enforcing | late | disabled` =20 diff --git a/xen/arch/arm/domain_build.c b/xen/arch/arm/domain_build.c index d233d63..81997d5 100644 --- a/xen/arch/arm/domain_build.c +++ b/xen/arch/arm/domain_build.c @@ -34,6 +34,10 @@ static unsigned int __initdata opt_dom0_max_vcpus; integer_param("dom0_max_vcpus", opt_dom0_max_vcpus); =20 +/* If true, the extended regions support is enabled for dom0 */ +static bool __initdata opt_ext_regions =3D true; +boolean_param("ext_regions", opt_ext_regions); + static u64 __initdata dom0_mem; static bool __initdata dom0_mem_set; =20 @@ -886,6 +890,233 @@ static int __init make_memory_node(const struct domai= n *d, return res; } =20 +static int __init add_ext_regions(unsigned long s, unsigned long e, void *= data) +{ + struct meminfo *ext_regions =3D data; + paddr_t start, size; + + if ( ext_regions->nr_banks >=3D ARRAY_SIZE(ext_regions->bank) ) + return 0; + + /* Both start and size of the extended region should be 2MB aligned */ + start =3D (s + SZ_2M - 1) & ~(SZ_2M - 1); + if ( start > e ) + return 0; + + /* + * e is actually "end-1" because it is called by rangeset functions + * which are inclusive of the last address. + */ + e +=3D 1; + size =3D (e - start) & ~(SZ_2M - 1); + if ( size < MB(64) ) + return 0; + + ext_regions->bank[ext_regions->nr_banks].start =3D start; + ext_regions->bank[ext_regions->nr_banks].size =3D size; + ext_regions->nr_banks++; + + return 0; +} + +static int __init find_unallocated_memory(const struct kernel_info *kinfo, + struct meminfo *ext_regions) +{ + const struct meminfo *assign_mem =3D &kinfo->mem; + struct rangeset *unalloc_mem; + paddr_t start, end; + unsigned int i; + int res; + + dt_dprintk("Find unallocated memory for extended regions\n"); + + unalloc_mem =3D rangeset_new(NULL, NULL, 0); + if ( !unalloc_mem ) + return -ENOMEM; + + /* Start with all available RAM */ + for ( i =3D 0; i < bootinfo.mem.nr_banks; i++ ) + { + start =3D bootinfo.mem.bank[i].start; + end =3D bootinfo.mem.bank[i].start + bootinfo.mem.bank[i].size; + res =3D rangeset_add_range(unalloc_mem, start, end - 1); + if ( res ) + { + printk(XENLOG_ERR "Failed to add: %#"PRIx64"->%#"PRIx64"\n", + start, end); + goto out; + } + } + + /* Remove RAM assigned to Dom0 */ + for ( i =3D 0; i < assign_mem->nr_banks; i++ ) + { + start =3D assign_mem->bank[i].start; + end =3D assign_mem->bank[i].start + assign_mem->bank[i].size; + res =3D rangeset_remove_range(unalloc_mem, start, end - 1); + if ( res ) + { + printk(XENLOG_ERR "Failed to remove: %#"PRIx64"->%#"PRIx64"\n", + start, end); + goto out; + } + } + + /* Remove reserved-memory regions */ + for ( i =3D 0; i < bootinfo.reserved_mem.nr_banks; i++ ) + { + start =3D bootinfo.reserved_mem.bank[i].start; + end =3D bootinfo.reserved_mem.bank[i].start + + bootinfo.reserved_mem.bank[i].size; + res =3D rangeset_remove_range(unalloc_mem, start, end - 1); + if ( res ) + { + printk(XENLOG_ERR "Failed to remove: %#"PRIx64"->%#"PRIx64"\n", + start, end); + goto out; + } + } + + /* Remove grant table region */ + start =3D kinfo->gnttab_start; + end =3D kinfo->gnttab_start + kinfo->gnttab_size; + res =3D rangeset_remove_range(unalloc_mem, start, end - 1); + if ( res ) + { + printk(XENLOG_ERR "Failed to remove: %#"PRIx64"->%#"PRIx64"\n", + start, end); + goto out; + } + + start =3D 0; + end =3D (1ULL << p2m_ipa_bits) - 1; + res =3D rangeset_report_ranges(unalloc_mem, start, end, + add_ext_regions, ext_regions); + if ( res ) + ext_regions->nr_banks =3D 0; + else if ( !ext_regions->nr_banks ) + res =3D -ENOENT; + +out: + rangeset_destroy(unalloc_mem); + + return res; +} + +static int __init find_memory_holes(const struct kernel_info *kinfo, + struct meminfo *ext_regions) +{ + struct dt_device_node *np; + struct rangeset *mem_holes; + paddr_t start, end; + unsigned int i; + int res; + + dt_dprintk("Find memory holes for extended regions\n"); + + mem_holes =3D rangeset_new(NULL, NULL, 0); + if ( !mem_holes ) + return -ENOMEM; + + /* Start with maximum possible addressable physical memory range */ + start =3D 0; + end =3D (1ULL << p2m_ipa_bits) - 1; + res =3D rangeset_add_range(mem_holes, start, end); + if ( res ) + { + printk(XENLOG_ERR "Failed to add: %#"PRIx64"->%#"PRIx64"\n", + start, end); + goto out; + } + + /* + * Remove regions described by "reg" and "ranges" properties where + * the memory is addressable (MMIO, RAM, PCI BAR, etc). + */ + dt_for_each_device_node( dt_host, np ) + { + unsigned int naddr; + u64 addr, size; + + naddr =3D dt_number_of_address(np); + + for ( i =3D 0; i < naddr; i++ ) + { + res =3D dt_device_get_address(np, i, &addr, &size); + if ( res ) + { + printk(XENLOG_ERR "Unable to retrieve address %u for %s\n", + i, dt_node_full_name(np)); + goto out; + } + + start =3D addr & PAGE_MASK; + end =3D PAGE_ALIGN(addr + size); + res =3D rangeset_remove_range(mem_holes, start, end - 1); + if ( res ) + { + printk(XENLOG_ERR "Failed to remove: %#"PRIx64"->%#"PRIx64= "\n", + start, end); + goto out; + } + } + + if ( dt_device_type_is_equal(np, "pci" ) ) + { + unsigned int range_size, nr_ranges; + int na, ns, pna; + const __be32 *ranges; + u32 len; + + /* + * Looking for non-empty ranges property which in the context + * of the PCI host bridge device node describes the BARs for + * the PCI devices. + */ + ranges =3D dt_get_property(np, "ranges", &len); + if ( !ranges || !len ) + continue; + + pna =3D dt_n_addr_cells(np); + na =3D dt_child_n_addr_cells(np); + ns =3D dt_child_n_size_cells(np); + range_size =3D pna + na + ns; + nr_ranges =3D len / sizeof(__be32) / range_size; + + for ( i =3D 0; i < nr_ranges; i++, ranges +=3D range_size ) + { + /* Skip the child address and get the parent (CPU) address= */ + addr =3D dt_read_number(ranges + na, pna); + size =3D dt_read_number(ranges + na + pna, ns); + + start =3D addr & PAGE_MASK; + end =3D PAGE_ALIGN(addr + size); + res =3D rangeset_remove_range(mem_holes, start, end - 1); + if ( res ) + { + printk(XENLOG_ERR "Failed to remove: %#"PRIx64"->%#"PR= Ix64"\n", + start, end); + goto out; + } + } + } + } + + start =3D 0; + end =3D (1ULL << p2m_ipa_bits) - 1; + res =3D rangeset_report_ranges(mem_holes, start, end, + add_ext_regions, ext_regions); + if ( res ) + ext_regions->nr_banks =3D 0; + else if ( !ext_regions->nr_banks ) + res =3D -ENOENT; + +out: + rangeset_destroy(mem_holes); + + return res; +} + static int __init make_hypervisor_node(struct domain *d, const struct kernel_info *kinfo, int addrcells, int sizecells) @@ -893,11 +1124,12 @@ static int __init make_hypervisor_node(struct domain= *d, const char compat[] =3D "xen,xen-"__stringify(XEN_VERSION)"."__stringify(XEN_SUBVERSION)"\= 0" "xen,xen"; - __be32 reg[4]; + __be32 *reg, *cells; gic_interrupt_t intr; - __be32 *cells; int res; void *fdt =3D kinfo->fdt; + struct meminfo *ext_regions; + unsigned int i; =20 dt_dprintk("Create hypervisor node\n"); =20 @@ -919,12 +1151,54 @@ static int __init make_hypervisor_node(struct domain= *d, if ( res ) return res; =20 + reg =3D xzalloc_array(__be32, (NR_MEM_BANKS + 1) * 4); + if ( !reg ) + return -ENOMEM; + + ext_regions =3D xzalloc(struct meminfo); + if ( !ext_regions ) + { + xfree(reg); + return -ENOMEM; + } + + if ( !opt_ext_regions ) + printk(XENLOG_DEBUG "The extended regions support is disabled\n"); + else if ( is_32bit_domain(d) ) + printk(XENLOG_DEBUG "The extended regions are only supported for 6= 4-bit guest currently\n"); + else + { + if ( !is_iommu_enabled(d) ) + res =3D find_unallocated_memory(kinfo, ext_regions); + else + res =3D find_memory_holes(kinfo, ext_regions); + + if ( res ) + printk(XENLOG_WARNING "Failed to allocate extended regions\n"); + } + /* reg 0 is grant table space */ cells =3D ®[0]; dt_child_set_range(&cells, addrcells, sizecells, kinfo->gnttab_start, kinfo->gnttab_size); + /* reg 1...N are extended regions */ + for ( i =3D 0; i < ext_regions->nr_banks; i++ ) + { + u64 start =3D ext_regions->bank[i].start; + u64 size =3D ext_regions->bank[i].size; + + dt_dprintk("Extended region %d: %#"PRIx64"->%#"PRIx64"\n", + i, start, start + size); + + dt_child_set_range(&cells, addrcells, sizecells, start, size); + } + res =3D fdt_property(fdt, "reg", reg, - dt_cells_to_size(addrcells + sizecells)); + dt_cells_to_size(addrcells + sizecells) * + (ext_regions->nr_banks + 1)); + xfree(ext_regions); + xfree(reg); + if ( res ) return res; =20 --=20 2.7.4