From patchwork Tue Apr 9 04:53:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Henry Wang X-Patchwork-Id: 13621799 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id EE198CD129A for ; Tue, 9 Apr 2024 04:54:33 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.702119.1096937 (Exim 4.92) (envelope-from ) id 1ru3Ut-0003gB-RO; Tue, 09 Apr 2024 04:54:15 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 702119.1096937; Tue, 09 Apr 2024 04:54:15 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ru3Ut-0003fS-K1; Tue, 09 Apr 2024 04:54:15 +0000 Received: by outflank-mailman (input) for mailman id 702119; Tue, 09 Apr 2024 04:54:13 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ru3Ur-0003cd-GD for xen-devel@lists.xenproject.org; Tue, 09 Apr 2024 04:54:13 +0000 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on20601.outbound.protection.outlook.com [2a01:111:f403:2412::601]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 3404329f-f62d-11ee-9150-f14010f3f246; Tue, 09 Apr 2024 06:54:11 +0200 (CEST) Received: from CH0PR03CA0207.namprd03.prod.outlook.com (2603:10b6:610:e4::32) by DM4PR12MB6110.namprd12.prod.outlook.com (2603:10b6:8:ad::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.47; Tue, 9 Apr 2024 04:54:08 +0000 Received: from CH1PEPF0000AD78.namprd04.prod.outlook.com (2603:10b6:610:e4:cafe::e2) by CH0PR03CA0207.outlook.office365.com (2603:10b6:610:e4::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7362.34 via Frontend Transport; Tue, 9 Apr 2024 04:54:08 +0000 Received: from SATLEXMB03.amd.com (165.204.84.17) by CH1PEPF0000AD78.mail.protection.outlook.com (10.167.244.56) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7452.22 via Frontend Transport; Tue, 9 Apr 2024 04:54:07 +0000 Received: from SATLEXMB08.amd.com (10.181.40.132) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Mon, 8 Apr 2024 23:54:07 -0500 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB08.amd.com (10.181.40.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Mon, 8 Apr 2024 21:54:06 -0700 Received: from henry-MS-7D54.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2507.35 via Frontend Transport; Mon, 8 Apr 2024 23:54:03 -0500 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: 3404329f-f62d-11ee-9150-f14010f3f246 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UY5kQ36YGvd7l5lO4f8KeLtmNKZbBnqo96YrGAlBVuwHNdMsA60n6xNOYS4kSR+vlm+taUwnM/xxeW2FxtJiS5h92CQwuZOWXz3coM+YEBeF76e+1OF52kW5MSBQjChyb90k/v+1b8F6tJn+jIIFpRaeM8Tx3e/tpo8dEEw7D/gQRbZM28blfOAdKndHW+kRS8WN8UgdJGEbNeKPxdXYPtTkOOpZlMTAjMXIgiNekLQVCaedmAkC93dPaYNsoIgravfUGDoMdZKsaeeYtbyg8zN8H5h88S0vajC8fGTBB/gvW2YraH2VLavqPUg2eDDf8Th3/tMAsbVnU6hmy7E3+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=6nCAvmO7u10UOc0MrZvr0OOFyROWsdMJT9pKEuIzHtU=; b=BpYI+0WbTU0MVhIvFwrBZxBkD955m0WDKNYMjT7JOa61h6y8+3AanvDWwRR80e3qc+j4MY5R1gTsSOv8hEyJgiBZ+DGXuFeKibDacqG3+n+95hqOFJvh2VdSGFYNaVCmcY463xi8f/7edAKVHo/RqhCMCZTAAVAsANvop6NE8j/d/rlR7cwBCT28Xxw/GVm16B/0kNOuY1kwyIPuk+76dbHDdxNYgs5dDXKblw311G2+ETbmRwzuCV9WLIIp3Y9PGfo34eqqSuOyMfM75PuKWUhx3ATHkXNo9gW15HHfuSvZD8u4zypDK0Wvjl/s+HZlTiWLJkdvhdeMGn0805ekjg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=6nCAvmO7u10UOc0MrZvr0OOFyROWsdMJT9pKEuIzHtU=; b=2HvTDgZs7+utvOgmWQt9L4QtAgXnvSpqiYji3mdmvw/XN5GsEDZa2KefWN+GtbbhwnuX1CrVGN43c+chrF9yPeiafKweqvwQ8ACtmN6qfVOhhRYHiaThnoQmJGu1Vd8oT0q7oZPbP3eQMPdELJpWm7DCrpP2OvUBp2KuNJ3NXLI= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; pr=C From: Henry Wang To: CC: Henry Wang , Anthony PERARD , Juergen Gross , Andrew Cooper , George Dunlap , "Jan Beulich" , Julien Grall , "Stefano Stabellini" , Bertrand Marquis , Michal Orzel , "Volodymyr Babchuk" , Alec Kwapis Subject: [PATCH v4 1/5] xen/domctl, tools: Introduce a new domctl to get guest memory map Date: Tue, 9 Apr 2024 12:53:53 +0800 Message-ID: <20240409045357.236802-2-xin.wang2@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240409045357.236802-1-xin.wang2@amd.com> References: <20240409045357.236802-1-xin.wang2@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH1PEPF0000AD78:EE_|DM4PR12MB6110:EE_ X-MS-Office365-Filtering-Correlation-Id: c5877d6d-2134-4aa9-70b5-08dc585116a8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Kgylor3TECOxnq4ReeyDa0oSMwDficSRzB81HTfJZP+35k1bfXoqwFKsxjEDWapsaGuhmr0jp6JoOw/W829V+4LV59VBbFZyPFqw77+omy7LDCaLNMVY+8iywpx+rJZgDMKeigjrIw+m6geyEqSVRkRGibgxqhjI86wlWE/+wMHCYf4CC/aVrkUGgmgYEZozvJrfOXaot9ZssLxvnyW6hxc6b92sJkOtprq41yxGQ43eDT/2Khw8TX7TVcC4MIXB+zxi1z94kRKxwxBR7MLiSNm/9TpHMr06eZw/yntADWB5KLUalFHu0ekWz78x5H8KYzXifusEchuxVNhnAns64Lwq71lJvyBh6ytVrWgfqCRn6cHk7wFCAeleWaH8w/Y9hEPKehwKtlAGP7HGopMswV1+5CDLRjgYCEk0YFZi5PjGI/mvWpDMPyxWwV32/fNivyx4fsmDfETaOhjv0XMsRC3/vUzQQRbOzNLwE1MOvTk43lUhRVUiiZhwTOiT+fMWTW6IYbZBmrj/CFUYFkGN18uuh/3cD/VUmKmRZF7zbBtE6ZiNIYnjDgoInxuMrwPKeDFKkEMogpkSR2cj9Uyi4GsGIHtp2XFOCAD2/lRiWVJZuBkvXbjJZvEXaOTkd/nX7bROhV3ECXTP8YxhYMcqI9cCKMYJku92URIQp5/sPti9BcGuTo4klwBhSgopiciG5YdWt+Xa0IB00Rlu+MvPMWNzLStsABaacx1qLV/+J6UvdfZRnLi2bD8t+dSPSast X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(7416005)(376005)(36860700004)(82310400014)(1800799015);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Apr 2024 04:54:07.4875 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c5877d6d-2134-4aa9-70b5-08dc585116a8 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CH1PEPF0000AD78.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB6110 There are some use cases where the toolstack needs to know the guest memory map. For example, the toolstack helper application "init-dom0less" needs to know the guest magic page regions for 1:1 direct-mapped dom0less DomUs to allocate magic pages. To address such needs, add XEN_DOMCTL_get_mem_map hypercall and related data structures to query the hypervisor for the guest memory map. The guest memory map is recorded in the domain structure and currently only guest magic page region is recorded in the guest memory map. The guest magic page region is initialized at the domain creation time as the layout in the public header, and it is updated for 1:1 dom0less DomUs (see the following commit) to avoid conflict with RAM. Take the opportunity to drop an unnecessary empty line to keep the coding style consistent in the file. Reported-by: Alec Kwapis Signed-off-by: Henry Wang --- RFC: I think the newly introduced "struct xen_domctl_mem_map" is quite duplicated with "struct xen_memory_map", any comment on reuse the "struct xen_memory_map" for simplicity? v4: - Drop the unnecessary initialization and printk. - Use XEN_* prefix instead of GUEST_* for domctl. - Move the mem region type to mem region structure. - Drop the check of Xen internal state in the domctl. - Handle the nr_regions properly (Fill only as much of the array as there is space for, but return the full count to the caller) to make sure the caller can know if it specifies a too small array. v3: - Init the return rc for XEN_DOMCTL_get_mem_map. - Copy the nr_mem_regions back as it should be both IN & OUT. - Check if mem_map->nr_mem_regions exceeds the XEN_MAX_MEM_REGIONS when adding a new entry. - Allow XEN_MAX_MEM_REGIONS to be different between different archs. - Add explicit padding and check to the domctl structures. v2: - New patch --- tools/include/xenctrl.h | 4 ++++ tools/libs/ctrl/xc_domain.c | 37 +++++++++++++++++++++++++++++++ xen/arch/arm/domain.c | 14 ++++++++++++ xen/arch/arm/domctl.c | 28 ++++++++++++++++++++++- xen/arch/arm/include/asm/domain.h | 8 +++++++ xen/include/public/arch-arm.h | 7 ++++++ xen/include/public/domctl.h | 29 ++++++++++++++++++++++++ 7 files changed, 126 insertions(+), 1 deletion(-) diff --git a/tools/include/xenctrl.h b/tools/include/xenctrl.h index 2ef8b4e054..b25e9772a2 100644 --- a/tools/include/xenctrl.h +++ b/tools/include/xenctrl.h @@ -1195,6 +1195,10 @@ int xc_domain_setmaxmem(xc_interface *xch, uint32_t domid, uint64_t max_memkb); +int xc_get_domain_mem_map(xc_interface *xch, uint32_t domid, + struct xen_mem_region mem_regions[], + uint32_t *nr_regions); + int xc_domain_set_memmap_limit(xc_interface *xch, uint32_t domid, unsigned long map_limitkb); diff --git a/tools/libs/ctrl/xc_domain.c b/tools/libs/ctrl/xc_domain.c index f2d9d14b4d..4dba55d01d 100644 --- a/tools/libs/ctrl/xc_domain.c +++ b/tools/libs/ctrl/xc_domain.c @@ -697,6 +697,43 @@ int xc_domain_setmaxmem(xc_interface *xch, return do_domctl(xch, &domctl); } +int xc_get_domain_mem_map(xc_interface *xch, uint32_t domid, + struct xen_mem_region mem_regions[], + uint32_t *nr_regions) +{ + int rc; + uint32_t nr = *nr_regions; + struct xen_domctl domctl = { + .cmd = XEN_DOMCTL_get_mem_map, + .domain = domid, + .u.mem_map = { + .nr_mem_regions = nr, + }, + }; + + DECLARE_HYPERCALL_BOUNCE(mem_regions, sizeof(xen_mem_region_t) * nr, + XC_HYPERCALL_BUFFER_BOUNCE_OUT); + + if ( !mem_regions || xc_hypercall_bounce_pre(xch, mem_regions) || nr < 1 ) + return -1; + + set_xen_guest_handle(domctl.u.mem_map.buffer, mem_regions); + + rc = do_domctl(xch, &domctl); + + xc_hypercall_bounce_post(xch, mem_regions); + + if ( nr < domctl.u.mem_map.nr_mem_regions ) + { + PERROR("Too small nr_regions %u", nr); + return -1; + } + + *nr_regions = domctl.u.mem_map.nr_mem_regions; + + return rc; +} + #if defined(__i386__) || defined(__x86_64__) int xc_domain_set_memory_map(xc_interface *xch, uint32_t domid, diff --git a/xen/arch/arm/domain.c b/xen/arch/arm/domain.c index 34cbfe699a..0c9761b65b 100644 --- a/xen/arch/arm/domain.c +++ b/xen/arch/arm/domain.c @@ -697,6 +697,7 @@ int arch_domain_create(struct domain *d, { unsigned int count = 0; int rc; + struct mem_map_domain *mem_map = &d->arch.mem_map; BUILD_BUG_ON(GUEST_MAX_VCPUS < MAX_VIRT_CPUS); @@ -786,6 +787,19 @@ int arch_domain_create(struct domain *d, d->arch.sve_vl = config->arch.sve_vl; #endif + if ( mem_map->nr_mem_regions < XEN_MAX_MEM_REGIONS ) + { + mem_map->regions[mem_map->nr_mem_regions].start = GUEST_MAGIC_BASE; + mem_map->regions[mem_map->nr_mem_regions].size = GUEST_MAGIC_SIZE; + mem_map->regions[mem_map->nr_mem_regions].type = XEN_MEM_REGION_MAGIC; + mem_map->nr_mem_regions++; + } + else + { + rc = -ENOSPC; + goto fail; + } + return 0; fail: diff --git a/xen/arch/arm/domctl.c b/xen/arch/arm/domctl.c index ad56efb0f5..8f62719cfa 100644 --- a/xen/arch/arm/domctl.c +++ b/xen/arch/arm/domctl.c @@ -148,7 +148,6 @@ long arch_do_domctl(struct xen_domctl *domctl, struct domain *d, return 0; } - case XEN_DOMCTL_vuart_op: { int rc; @@ -176,6 +175,33 @@ long arch_do_domctl(struct xen_domctl *domctl, struct domain *d, return rc; } + case XEN_DOMCTL_get_mem_map: + { + int rc = 0; + uint32_t nr_regions; + + if ( domctl->u.mem_map.pad ) + return -EINVAL; + + /* + * Fill the buffer only as much of the array as there is space for, + * but always return the full count in the hypervisor to the caller. + * This way we can avoid overflowing the buffer and also make sure + * the caller can know if it specifies too small an array. + */ + nr_regions = min(d->arch.mem_map.nr_mem_regions, + domctl->u.mem_map.nr_mem_regions); + + domctl->u.mem_map.nr_mem_regions = d->arch.mem_map.nr_mem_regions; + + if ( copy_to_guest(domctl->u.mem_map.buffer, + d->arch.mem_map.regions, nr_regions) || + __copy_field_to_guest(u_domctl, domctl, + u.mem_map.nr_mem_regions) ) + rc = -EFAULT; + + return rc; + } default: return subarch_do_domctl(domctl, d, u_domctl); } diff --git a/xen/arch/arm/include/asm/domain.h b/xen/arch/arm/include/asm/domain.h index f1d72c6e48..a559a9e499 100644 --- a/xen/arch/arm/include/asm/domain.h +++ b/xen/arch/arm/include/asm/domain.h @@ -10,6 +10,7 @@ #include #include #include +#include #include struct hvm_domain @@ -59,6 +60,11 @@ struct paging_domain { unsigned long p2m_total_pages; }; +struct mem_map_domain { + unsigned int nr_mem_regions; + struct xen_mem_region regions[XEN_MAX_MEM_REGIONS]; +}; + struct arch_domain { #ifdef CONFIG_ARM_64 @@ -77,6 +83,8 @@ struct arch_domain struct paging_domain paging; + struct mem_map_domain mem_map; + struct vmmio vmmio; /* Continuable domain_relinquish_resources(). */ diff --git a/xen/include/public/arch-arm.h b/xen/include/public/arch-arm.h index e167e14f8d..eba61e1ac6 100644 --- a/xen/include/public/arch-arm.h +++ b/xen/include/public/arch-arm.h @@ -223,6 +223,13 @@ typedef uint64_t xen_pfn_t; */ #define XEN_LEGACY_MAX_VCPUS 1 +/* + * Maximum number of memory map regions for guest memory layout. + * Used by XEN_DOMCTL_get_mem_map, currently there is only one region + * for the guest magic pages. + */ +#define XEN_MAX_MEM_REGIONS 1 + typedef uint64_t xen_ulong_t; #define PRI_xen_ulong PRIx64 diff --git a/xen/include/public/domctl.h b/xen/include/public/domctl.h index a33f9ec32b..974c07ee61 100644 --- a/xen/include/public/domctl.h +++ b/xen/include/public/domctl.h @@ -946,6 +946,33 @@ struct xen_domctl_paging_mempool { uint64_aligned_t size; /* Size in bytes. */ }; +#ifndef XEN_MAX_MEM_REGIONS +#define XEN_MAX_MEM_REGIONS 1 +#endif + +struct xen_mem_region { + uint64_aligned_t start; + uint64_aligned_t size; +#define XEN_MEM_REGION_DEFAULT 0 +#define XEN_MEM_REGION_MAGIC 1 + uint32_t type; + /* Must be zero */ + uint32_t pad; +}; +typedef struct xen_mem_region xen_mem_region_t; +DEFINE_XEN_GUEST_HANDLE(xen_mem_region_t); + +struct xen_domctl_mem_map { + /* IN & OUT */ + uint32_t nr_mem_regions; + /* Must be zero */ + uint32_t pad; + /* OUT */ + XEN_GUEST_HANDLE_64(xen_mem_region_t) buffer; +}; +typedef struct xen_domctl_mem_map xen_domctl_mem_map_t; +DEFINE_XEN_GUEST_HANDLE(xen_domctl_mem_map_t); + #if defined(__i386__) || defined(__x86_64__) struct xen_domctl_vcpu_msr { uint32_t index; @@ -1277,6 +1304,7 @@ struct xen_domctl { #define XEN_DOMCTL_vmtrace_op 84 #define XEN_DOMCTL_get_paging_mempool_size 85 #define XEN_DOMCTL_set_paging_mempool_size 86 +#define XEN_DOMCTL_get_mem_map 87 #define XEN_DOMCTL_gdbsx_guestmemio 1000 #define XEN_DOMCTL_gdbsx_pausevcpu 1001 #define XEN_DOMCTL_gdbsx_unpausevcpu 1002 @@ -1339,6 +1367,7 @@ struct xen_domctl { struct xen_domctl_vuart_op vuart_op; struct xen_domctl_vmtrace_op vmtrace_op; struct xen_domctl_paging_mempool paging_mempool; + struct xen_domctl_mem_map mem_map; uint8_t pad[128]; } u; }; From patchwork Tue Apr 9 04:53:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Henry Wang X-Patchwork-Id: 13621796 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2B29ACD1284 for ; Tue, 9 Apr 2024 04:54:33 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.702120.1096951 (Exim 4.92) (envelope-from ) id 1ru3Uy-00046r-5i; Tue, 09 Apr 2024 04:54:20 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 702120.1096951; Tue, 09 Apr 2024 04:54:20 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ru3Uy-00046d-0r; Tue, 09 Apr 2024 04:54:20 +0000 Received: by outflank-mailman (input) for mailman id 702120; Tue, 09 Apr 2024 04:54:18 +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 1ru3Uw-0003cc-AT for xen-devel@lists.xenproject.org; Tue, 09 Apr 2024 04:54:18 +0000 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on20600.outbound.protection.outlook.com [2a01:111:f403:2418::600]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 36e15aed-f62d-11ee-843a-3f19d20e7db6; Tue, 09 Apr 2024 06:54:16 +0200 (CEST) Received: from CY8PR12CA0006.namprd12.prod.outlook.com (2603:10b6:930:4e::11) by PH8PR12MB6988.namprd12.prod.outlook.com (2603:10b6:510:1bf::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.32; Tue, 9 Apr 2024 04:54:11 +0000 Received: from CY4PEPF0000EDD1.namprd03.prod.outlook.com (2603:10b6:930:4e:cafe::1) by CY8PR12CA0006.outlook.office365.com (2603:10b6:930:4e::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7452.36 via Frontend Transport; Tue, 9 Apr 2024 04:54:10 +0000 Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000EDD1.mail.protection.outlook.com (10.167.241.205) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7452.22 via Frontend Transport; Tue, 9 Apr 2024 04:54:10 +0000 Received: from SATLEXMB08.amd.com (10.181.40.132) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Mon, 8 Apr 2024 23:54:09 -0500 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB08.amd.com (10.181.40.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Mon, 8 Apr 2024 21:54:09 -0700 Received: from henry-MS-7D54.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2507.35 via Frontend Transport; Mon, 8 Apr 2024 23:54:07 -0500 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: 36e15aed-f62d-11ee-843a-3f19d20e7db6 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=T+80n9x28wYISmojxRJb4Yr0Yj0UjpVpwF8RtNV+nKNtUzLJH5bxf8NugQkXSPCdPRYdSCxNhH39XTH7LLmiAwBxoFXHuB9pRND4/inL2Ni4ofzeOZHt3+g2kdagdZjUTVnlfRISonQSeA6FW6vTzB8Oaae9ijpKWD4y7V7FK3Fo5bncvYWnjwExzGj3JW/JbxdA+0X3G8mM/Wy3k5LrsMjCfLB1u9LFuSlTl0ImWkRh3Uwu3wMkEQ2yOnbE6VLEXx5RTnuseIRGfgWAMX+/5KC30IJ5qYJq3k+vdqeaGZoqcvUK7CtXNgoufaFHmVyjy1dcfn6C+K23+qfQ6Vj3GQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=UH53OrvZPnAWC4mq34mYlREnVpEthVdOjMxYJ/+8w2M=; b=V80Z4bmtQUzz0MLxNq2hYlZps+SYo7meZ7TqkT1npu66B2l3Nnm2XgurWPBdbSN7u4CtVZBTkTwaNKv5UXmHJ441dnQByhV56mybXAjSMEp8xyAxOAxUPy2f5Yt4nE3DyP8fWNBYIfX7HT/LqerE71JpYeFKedD0K3jkpDjeIJ1Q9IWXYDM4vGRIpOs3JyTaFTn8MDSZzZgFMLDQ7GoVhFT/atic1uKfEd2+jXOG+5RypCi99Ik7wFWDxiqUHqZ+qgN7R58qm9O060W2KEy4PBN4NENci8pNb80pGvFayLS54U+h2ifYXTM23ZU3vsaifJVW3ovRlOCrHPu0ZbUAFQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=UH53OrvZPnAWC4mq34mYlREnVpEthVdOjMxYJ/+8w2M=; b=E2ib80FImKJ7V6d07VN4D4zexXPe9E+qyB5Nnln66mKeDo4K2DvTO99LJrnlJ0LXFDxiBs7LHdDbJvYaDnFdNIQCDHhgIL/huWWckZCtSPNlBvuQcBq9ItxXUJgXxME60Qs19bLglmzc79lHAex3jyrq2uq5w8J8eIGVNExLoRA= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C From: Henry Wang To: CC: Henry Wang , Stefano Stabellini , Julien Grall , Bertrand Marquis , Michal Orzel , "Volodymyr Babchuk" Subject: [PATCH v4 2/5] xen/arm: Generalize the extended region finding logic Date: Tue, 9 Apr 2024 12:53:54 +0800 Message-ID: <20240409045357.236802-3-xin.wang2@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240409045357.236802-1-xin.wang2@amd.com> References: <20240409045357.236802-1-xin.wang2@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000EDD1:EE_|PH8PR12MB6988:EE_ X-MS-Office365-Filtering-Correlation-Id: b7126ebd-67f7-41f3-2cbc-08dc58511859 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: FSIToWUOc1V5CZcu3wjvTesBBRf0USv/aTwpUs1YD4hUZHtqixm6qLCXJlGis7GQFeywu8QUwzTWAlLG5fN/XCf4aYfsN6wDzKuwpzkKBTGQp2GamIz3mdyW+IfX0O6exKyd27+69dMGwFSSE6ELcRuoHimzzswBHhQulAoTd0IjVqjjZjv4PgO/xn8kcltfrmeI8r30OqKOLuPFy5PByCJva4SUwSfW3k8MBOBsAZm/mHKXgRdr5a+20qy3nZV9dYtVjbQHZMd5jKEXKuapp0dI8cGNYmW3YXapxQZVldUXXkqNtTjhjhNqXOoHQEuvmkiEfvFDs1dPyC/5vz6+9MG0dZ3yVGKtQ+vI2EkPUoekpTcPjqznga9JGrTCLitNBw+X6wcRA63514mPpP5vHAywiU4ZUwlosyUMSa+P+KCVic674Xd90drF8Jw9p2GKzpDYu7UmUqLFD/BM5azBoCYyVZj2olF0yIWk8hOND9zERSdVxH5rNhmJVQJUlN/vEUNqwIkW7WvR+RIph01F4DAghRU1ki84NAgLR/8/Jj0RYiXYm8l7iQckE6jj2dF7JqWrpY/DOlxbquUH1wxKKiexNvIYIfxkcUw77htrHUNcDl2RyboDWxeTaw18AheSMjx5A4J6rzdcUjyIlcS9ab+6bmnjETAO6Ml+pEvJCmG/MaFZEtHw36vFX0SDDeHSOW+a9ab5NcFPfhuhXyg2h/QMCkMJBQjGZ0LQBWCiCKSQBDRIUUehpHWHb8mexV3W X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(82310400014)(1800799015)(376005)(36860700004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Apr 2024 04:54:10.2766 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b7126ebd-67f7-41f3-2cbc-08dc58511859 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000EDD1.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB6988 Recently there are needs to find unallocated/used memory regions for different use cases at boot time, and the logic of finding extended regions can be reused for some of the new use cases. This commit therefore generalize the extended region finding logic. Firstly, extract the extended region finding logic to a dedicated helper find_unused_regions(). Then add and pass down a `min_region_size` parameter so that the helpers can take a flexible region size instead of the hardcoded 64MB for extended region. Finally, adjust the variable and function names to make them general instead of for extended region only. Take the opportunity to update the stale in-code comment of find_unallocated_memory() and find_memory_holes(). Signed-off-by: Henry Wang --- v4: - No change v3: - New patch --- xen/arch/arm/domain_build.c | 107 ++++++++++++++---------- xen/arch/arm/include/asm/domain_build.h | 2 + xen/arch/arm/include/asm/setup.h | 5 ++ 3 files changed, 70 insertions(+), 44 deletions(-) diff --git a/xen/arch/arm/domain_build.c b/xen/arch/arm/domain_build.c index 085d88671e..d2a9c047ea 100644 --- a/xen/arch/arm/domain_build.c +++ b/xen/arch/arm/domain_build.c @@ -814,19 +814,21 @@ int __init make_memory_node(const struct domain *d, return res; } -static int __init add_ext_regions(unsigned long s_gfn, unsigned long e_gfn, - void *data) +static int __init add_regions(unsigned long s_gfn, unsigned long e_gfn, + void *data) { - struct meminfo *ext_regions = data; + struct mem_unused_info *unused = data; + struct meminfo *regions = unused->regions; + paddr_t min_region_size = unused->min_region_size; paddr_t start, size; paddr_t s = pfn_to_paddr(s_gfn); paddr_t e = pfn_to_paddr(e_gfn); - if ( ext_regions->nr_banks >= ARRAY_SIZE(ext_regions->bank) ) + if ( regions->nr_banks >= ARRAY_SIZE(regions->bank) ) return 0; /* - * Both start and size of the extended region should be 2MB aligned to + * Both start and size of the region should be 2MB aligned to * potentially allow superpage mapping. */ start = (s + SZ_2M - 1) & ~(SZ_2M - 1); @@ -845,26 +847,27 @@ static int __init add_ext_regions(unsigned long s_gfn, unsigned long e_gfn, * not quite useful but increase bookkeeping and not too large * to skip a large proportion of unused address space. */ - if ( size < MB(64) ) + if ( size < min_region_size ) return 0; - ext_regions->bank[ext_regions->nr_banks].start = start; - ext_regions->bank[ext_regions->nr_banks].size = size; - ext_regions->nr_banks++; + regions->bank[regions->nr_banks].start = start; + regions->bank[regions->nr_banks].size = size; + regions->nr_banks++; return 0; } /* - * Find unused regions of Host address space which can be exposed to Dom0 - * as extended regions for the special memory mappings. In order to calculate - * regions we exclude every region assigned to Dom0 from the Host RAM: + * Find unused regions of Host address space which can be exposed to + * direct-mapped domains as regions for the special memory mappings. + * In order to calculate regions we exclude every region assigned to + * direct-mapped domains from the Host RAM: * - domain RAM * - reserved-memory * - grant table space */ static int __init find_unallocated_memory(const struct kernel_info *kinfo, - struct meminfo *ext_regions) + struct mem_unused_info *unused) { const struct meminfo *assign_mem = &kinfo->mem; struct rangeset *unalloc_mem; @@ -893,7 +896,7 @@ static int __init find_unallocated_memory(const struct kernel_info *kinfo, } } - /* Remove RAM assigned to Dom0 */ + /* Remove RAM assigned to domain */ for ( i = 0; i < assign_mem->nr_banks; i++ ) { start = assign_mem->bank[i].start; @@ -942,10 +945,10 @@ static int __init find_unallocated_memory(const struct kernel_info *kinfo, start = 0; end = (1ULL << p2m_ipa_bits) - 1; res = rangeset_report_ranges(unalloc_mem, PFN_DOWN(start), PFN_DOWN(end), - add_ext_regions, ext_regions); + add_regions, unused); if ( res ) - ext_regions->nr_banks = 0; - else if ( !ext_regions->nr_banks ) + unused->regions->nr_banks = 0; + else if ( !unused->regions->nr_banks ) res = -ENOENT; out: @@ -982,16 +985,16 @@ static int __init handle_pci_range(const struct dt_device_node *dev, } /* - * Find the holes in the Host DT which can be exposed to Dom0 as extended - * regions for the special memory mappings. In order to calculate regions - * we exclude every addressable memory region described by "reg" and "ranges" + * Find the holes in the Host DT which can be exposed to direct-mapped domains + * as regions for the special memory mappings. In order to calculate regions we + * exclude every addressable memory region described by "reg" and "ranges" * properties from the maximum possible addressable physical memory range: * - MMIO * - Host RAM * - PCI aperture */ static int __init find_memory_holes(const struct kernel_info *kinfo, - struct meminfo *ext_regions) + struct mem_unused_info *unused) { struct dt_device_node *np; struct rangeset *mem_holes; @@ -1068,10 +1071,10 @@ static int __init find_memory_holes(const struct kernel_info *kinfo, start = 0; end = (1ULL << p2m_ipa_bits) - 1; res = rangeset_report_ranges(mem_holes, PFN_DOWN(start), PFN_DOWN(end), - add_ext_regions, ext_regions); + add_regions, unused); if ( res ) - ext_regions->nr_banks = 0; - else if ( !ext_regions->nr_banks ) + unused->regions->nr_banks = 0; + else if ( !unused->regions->nr_banks ) res = -ENOENT; out: @@ -1081,35 +1084,62 @@ out: } static int __init find_domU_holes(const struct kernel_info *kinfo, - struct meminfo *ext_regions) + struct mem_unused_info *unused) { unsigned int i; uint64_t bankend; const uint64_t bankbase[] = GUEST_RAM_BANK_BASES; const uint64_t banksize[] = GUEST_RAM_BANK_SIZES; + struct meminfo *regions = unused->regions; + paddr_t min_region_size = unused->min_region_size; int res = -ENOENT; for ( i = 0; i < GUEST_RAM_BANKS; i++ ) { - struct membank *ext_bank = &(ext_regions->bank[ext_regions->nr_banks]); + struct membank *bank = &(regions->bank[regions->nr_banks]); - ext_bank->start = ROUNDUP(bankbase[i] + kinfo->mem.bank[i].size, SZ_2M); + bank->start = ROUNDUP(bankbase[i] + kinfo->mem.bank[i].size, SZ_2M); bankend = ~0ULL >> (64 - p2m_ipa_bits); bankend = min(bankend, bankbase[i] + banksize[i] - 1); - if ( bankend > ext_bank->start ) - ext_bank->size = bankend - ext_bank->start + 1; + if ( bankend > bank->start ) + bank->size = bankend - bank->start + 1; - /* 64MB is the minimum size of an extended region */ - if ( ext_bank->size < MB(64) ) + if ( bank->size < min_region_size ) continue; - ext_regions->nr_banks++; + regions->nr_banks++; res = 0; } return res; } +int __init find_unused_regions(struct domain *d, + const struct kernel_info *kinfo, + struct meminfo *regions, + paddr_t min_region_size) +{ + int res; + struct mem_unused_info unused; + + unused.regions = regions; + unused.min_region_size = min_region_size; + + if ( is_domain_direct_mapped(d) ) + { + if ( !is_iommu_enabled(d) ) + res = find_unallocated_memory(kinfo, &unused); + else + res = find_memory_holes(kinfo, &unused); + } + else + { + res = find_domU_holes(kinfo, &unused); + } + + return res; +} + int __init make_hypervisor_node(struct domain *d, const struct kernel_info *kinfo, int addrcells, int sizecells) @@ -1161,18 +1191,7 @@ int __init make_hypervisor_node(struct domain *d, if ( !ext_regions ) return -ENOMEM; - if ( is_domain_direct_mapped(d) ) - { - if ( !is_iommu_enabled(d) ) - res = find_unallocated_memory(kinfo, ext_regions); - else - res = find_memory_holes(kinfo, ext_regions); - } - else - { - res = find_domU_holes(kinfo, ext_regions); - } - + res = find_unused_regions(d, kinfo, ext_regions, MB(64)); if ( res ) printk(XENLOG_WARNING "%pd: failed to allocate extended regions\n", d); diff --git a/xen/arch/arm/include/asm/domain_build.h b/xen/arch/arm/include/asm/domain_build.h index da9e6025f3..74432123fe 100644 --- a/xen/arch/arm/include/asm/domain_build.h +++ b/xen/arch/arm/include/asm/domain_build.h @@ -10,6 +10,8 @@ bool allocate_bank_memory(struct domain *d, struct kernel_info *kinfo, gfn_t sgfn, paddr_t tot_size); int construct_domain(struct domain *d, struct kernel_info *kinfo); int domain_fdt_begin_node(void *fdt, const char *name, uint64_t unit); +int find_unused_regions(struct domain *d, const struct kernel_info *kinfo, + struct meminfo *regions, paddr_t min_region_size); int make_chosen_node(const struct kernel_info *kinfo); int make_cpus_node(const struct domain *d, void *fdt); int make_hypervisor_node(struct domain *d, const struct kernel_info *kinfo, diff --git a/xen/arch/arm/include/asm/setup.h b/xen/arch/arm/include/asm/setup.h index d15a88d2e0..d24c6d31c8 100644 --- a/xen/arch/arm/include/asm/setup.h +++ b/xen/arch/arm/include/asm/setup.h @@ -61,6 +61,11 @@ struct meminfo { struct membank bank[NR_MEM_BANKS]; }; +struct mem_unused_info { + struct meminfo *regions; + paddr_t min_region_size; +}; + /* * The domU flag is set for kernels and ramdisks of "xen,domain" nodes. * The purpose of the domU flag is to avoid getting confused in From patchwork Tue Apr 9 04:53:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Henry Wang X-Patchwork-Id: 13621797 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 45053CD129D for ; Tue, 9 Apr 2024 04:54:34 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.702121.1096960 (Exim 4.92) (envelope-from ) id 1ru3Uz-0004M6-CA; Tue, 09 Apr 2024 04:54:21 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 702121.1096960; Tue, 09 Apr 2024 04:54:21 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ru3Uz-0004Lx-9U; Tue, 09 Apr 2024 04:54:21 +0000 Received: by outflank-mailman (input) for mailman id 702121; Tue, 09 Apr 2024 04:54:20 +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 1ru3Uy-0003cc-10 for xen-devel@lists.xenproject.org; Tue, 09 Apr 2024 04:54:20 +0000 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on20601.outbound.protection.outlook.com [2a01:111:f403:2009::601]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 38c724e3-f62d-11ee-843a-3f19d20e7db6; Tue, 09 Apr 2024 06:54:18 +0200 (CEST) Received: from CH2PR10CA0011.namprd10.prod.outlook.com (2603:10b6:610:4c::21) by SN7PR12MB7419.namprd12.prod.outlook.com (2603:10b6:806:2a6::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.55; Tue, 9 Apr 2024 04:54:14 +0000 Received: from CH1PEPF0000AD77.namprd04.prod.outlook.com (2603:10b6:610:4c:cafe::ea) by CH2PR10CA0011.outlook.office365.com (2603:10b6:610:4c::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7386.28 via Frontend Transport; Tue, 9 Apr 2024 04:54:13 +0000 Received: from SATLEXMB03.amd.com (165.204.84.17) by CH1PEPF0000AD77.mail.protection.outlook.com (10.167.244.55) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7452.22 via Frontend Transport; Tue, 9 Apr 2024 04:54:13 +0000 Received: from SATLEXMB08.amd.com (10.181.40.132) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Mon, 8 Apr 2024 23:54:12 -0500 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB08.amd.com (10.181.40.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Mon, 8 Apr 2024 21:54:12 -0700 Received: from henry-MS-7D54.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2507.35 via Frontend Transport; Mon, 8 Apr 2024 23:54:10 -0500 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: 38c724e3-f62d-11ee-843a-3f19d20e7db6 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JOqqY1cUfIvEIhzWQoDt+SPx+e8h0Sa4Z4ASpJLvPpEX4pRVZ1m+RY8FGBe9UiU3I+ZUY2OrGUi/8Ee1be7IxbRq92E2RcN3xdQiD2Tm2nx7x9/sg0+kVlLJUqa147dKT2LJLVs5S7JvebU0jKSeiAqsj9Wfp/42lyAT0f+j9C+rFtKpemkaK9F8P44k6nDLHiiRt26M2VBB6thtLa9Idx39eyelgtIoQYilzbQvIdWpF8c0zgNUlvX/6Rc41t/riaVXI9gU4MC71iwjDDCXeNfhK7PtIEqiDkzbjHw64J4uyilTrcgNdEVkvNvtoZFDlq61n9yMDYx+ZgHqMMCfkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=lC+nKYjI93vCI4VeuKL1ZiSEnl9+mCP0TiBahXdLMho=; b=NNPFNzB38MUxjDG1C23cbjLcNWzle/X4/OSeX8kuBMQvVwY9dmaQXlRLzy0WCKh1xULaqA9t3AGzSKE7gDoBMURD43kbbdbzC8imKvOgJFTC2dh7hctQdtpJgpIK9XoY/y2SuBg77aOBskp1Ifx0OhHW03icjiLInGp2BEy/5sGpdOmJZSF0+WNR0M8pm71rPv7Wjq6f6oHZuhV/7rA0aXaDAdmTHO/q5y3o8x9KvLK9xLJBTyh5A/M4yVVzNKmjpA5FlF4K5CLkaAQNaB84cpLmBgySbfKCUiRgAADSVIRQauDmkfniUmBHl7pQav5NfzsDqAOEhUhO7F74UPIqag== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=lC+nKYjI93vCI4VeuKL1ZiSEnl9+mCP0TiBahXdLMho=; b=J/pG5di6qConia4X5mxp53pdESxAk3sCct26UFq0K1lCDMnOIUV9qMU6KrR4US0uIDUXXx8qkhPUbYvjnnwQpOc+wGzx1dYglCfAJXk9kxUgNdTQ2CD+A36ZbQp4nH6iq9VN222RhqIpxeHKchaXl8Uf0fJwsAyWCFgNatUB2vA= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; pr=C From: Henry Wang To: CC: Henry Wang , Stefano Stabellini , Julien Grall , Bertrand Marquis , Michal Orzel , "Volodymyr Babchuk" , Alec Kwapis Subject: [PATCH v4 3/5] xen/arm: Find unallocated spaces for magic pages of direct-mapped domU Date: Tue, 9 Apr 2024 12:53:55 +0800 Message-ID: <20240409045357.236802-4-xin.wang2@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240409045357.236802-1-xin.wang2@amd.com> References: <20240409045357.236802-1-xin.wang2@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH1PEPF0000AD77:EE_|SN7PR12MB7419:EE_ X-MS-Office365-Filtering-Correlation-Id: 024ce0f0-6595-40c4-7447-08dc58511a27 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: jDXw1jgQYHaBBqpUB+R4LAyqIGYizOtsLML9Hzjs2M3XGFk0p+WsH784CdvIY53Mc2vDl9qv2cfcc9BaeJb4uTDzS4KyefuhQEnr90/8o08kI3yLdwy6WK/6V6mHphzlSQxKzzNnMS8dUCla2v31P2ej/Df1s+gwBkppgVoVmDjsDRlQkTzAcOFL3LyF1eK0rykNOABLfJErpBWBCzKMg2cvPsZRjmZopXmQFgGkm42HDNet5/a8ZfyX+XaGngg+LwqklU+8VQi72rogazTkUhCTj0UbeAzkz0zCyN2U333v22t2qFZ3AIchrZK0ew2t21dyDDALC32S+S+dI+h0+Q/hf5S+siSYasAZY+zS0lkVwsi29mRx018Nim4dUaAs4Ye5b9y/sP8oxrj7NY0L3/BhrE5i33rECQIlxkgr5Bkje8HLtvyWCT6AHeoBvbOoprHFpYqDwyzoTdgPUufE5JGAqL+anfNm5F/y9DYrQLLiCUORpoqyJ+LWahskEakhsiomcfmqLgL36VMSXyzjb6mkYmMPoIaU/pQHVilO3GloBfALEK9UvpDpTEMI8G5EIj/pA+GtXJLAIQvgTTl2G540dImztZCq+zwdbj7va7LmjNJjeNiRopji/Iu1L0JIQezV5sPD+d21bwX0TS0gLhR10+JTLa3qBsACzkbEaQr3hLWXclV/TQubN/oLQzPngPPACKQBLmmHGFKW1Z6D4RgjMu7AButQHavDVPvm90WaunsGgFQ8QK3PH2kLs2yb X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(36860700004)(376005)(82310400014)(1800799015);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Apr 2024 04:54:13.3531 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 024ce0f0-6595-40c4-7447-08dc58511a27 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CH1PEPF0000AD77.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7419 For 1:1 direct-mapped dom0less DomUs, the magic pages should not clash with any RAM region. To find a proper region for guest magic pages, we can reuse the logic of finding domain extended regions. If the extended region is enabled, since the extended region banks are at least 64MB, carve out the first 16MB from the first extended region bank for magic pages of direct-mapped domU. If the extended region is disabled, call the newly introduced helper find_11_domU_magic_region() to find a GUEST_MAGIC_SIZE sized unused region. Reported-by: Alec Kwapis Signed-off-by: Henry Wang --- v4: - No change v3: - Extract the logic of finding unallocated spaces for magic pages of direct-mapped domU to a dedicated function in static-memory.c - Properly handle error and free memory in find_11_domU_magic_region() v2: - New patch --- xen/arch/arm/dom0less-build.c | 11 +++++++ xen/arch/arm/domain_build.c | 24 ++++++++++++++- xen/arch/arm/include/asm/domain_build.h | 2 ++ xen/arch/arm/include/asm/static-memory.h | 7 +++++ xen/arch/arm/static-memory.c | 39 ++++++++++++++++++++++++ 5 files changed, 82 insertions(+), 1 deletion(-) diff --git a/xen/arch/arm/dom0less-build.c b/xen/arch/arm/dom0less-build.c index fb63ec6fd1..1963f029fe 100644 --- a/xen/arch/arm/dom0less-build.c +++ b/xen/arch/arm/dom0less-build.c @@ -682,6 +682,17 @@ static int __init prepare_dtb_domU(struct domain *d, struct kernel_info *kinfo) if ( kinfo->dom0less_feature & DOM0LESS_ENHANCED_NO_XS ) { + /* + * Find the guest magic region for 1:1 dom0less domU when the extended + * region is not enabled. + */ + if ( !opt_ext_regions || is_32bit_domain(d) ) + { + ret = find_11_domU_magic_region(d, kinfo); + if ( ret ) + goto err; + } + ret = make_hypervisor_node(d, kinfo, addrcells, sizecells); if ( ret ) goto err; diff --git a/xen/arch/arm/domain_build.c b/xen/arch/arm/domain_build.c index d2a9c047ea..d5a9baf8b0 100644 --- a/xen/arch/arm/domain_build.c +++ b/xen/arch/arm/domain_build.c @@ -46,7 +46,7 @@ integer_param("dom0_max_vcpus", opt_dom0_max_vcpus); * If true, the extended regions support is enabled for dom0 and * dom0less domUs. */ -static bool __initdata opt_ext_regions = true; +bool __initdata opt_ext_regions = true; boolean_param("ext_regions", opt_ext_regions); static u64 __initdata dom0_mem; @@ -1196,6 +1196,28 @@ int __init make_hypervisor_node(struct domain *d, printk(XENLOG_WARNING "%pd: failed to allocate extended regions\n", d); nr_ext_regions = ext_regions->nr_banks; + + /* + * If extended region is enabled, carve out the 16MB guest magic page + * regions from the first bank of extended region (at least 64MB) for + * the 1:1 dom0less DomUs + */ + if ( is_domain_direct_mapped(d) && !is_hardware_domain(d) ) + { + struct mem_map_domain *mem_map = &d->arch.mem_map; + + for ( i = 0; i < mem_map->nr_mem_regions; i++ ) + { + if ( mem_map->regions[i].type == XEN_MEM_REGION_MAGIC ) + { + mem_map->regions[i].start = ext_regions->bank[0].start; + mem_map->regions[i].size = GUEST_MAGIC_SIZE; + + ext_regions->bank[0].start += GUEST_MAGIC_SIZE; + ext_regions->bank[0].size -= GUEST_MAGIC_SIZE; + } + } + } } reg = xzalloc_array(__be32, (nr_ext_regions + 1) * (addrcells + sizecells)); diff --git a/xen/arch/arm/include/asm/domain_build.h b/xen/arch/arm/include/asm/domain_build.h index 74432123fe..063ff727bb 100644 --- a/xen/arch/arm/include/asm/domain_build.h +++ b/xen/arch/arm/include/asm/domain_build.h @@ -4,6 +4,8 @@ #include #include +extern bool opt_ext_regions; + typedef __be32 gic_interrupt_t[3]; bool allocate_bank_memory(struct domain *d, struct kernel_info *kinfo, diff --git a/xen/arch/arm/include/asm/static-memory.h b/xen/arch/arm/include/asm/static-memory.h index 3e3efd70c3..01e51217ca 100644 --- a/xen/arch/arm/include/asm/static-memory.h +++ b/xen/arch/arm/include/asm/static-memory.h @@ -12,6 +12,7 @@ void allocate_static_memory(struct domain *d, struct kernel_info *kinfo, void assign_static_memory_11(struct domain *d, struct kernel_info *kinfo, const struct dt_device_node *node); void init_staticmem_pages(void); +int find_11_domU_magic_region(struct domain *d, struct kernel_info *kinfo); #else /* !CONFIG_STATIC_MEMORY */ @@ -31,6 +32,12 @@ static inline void assign_static_memory_11(struct domain *d, static inline void init_staticmem_pages(void) {}; +static inline int find_11_domU_magic_region(struct domain *d, + struct kernel_info *kinfo) +{ + return 0; +} + #endif /* CONFIG_STATIC_MEMORY */ #endif /* __ASM_STATIC_MEMORY_H_ */ diff --git a/xen/arch/arm/static-memory.c b/xen/arch/arm/static-memory.c index cffbab7241..ab1ec5e73a 100644 --- a/xen/arch/arm/static-memory.c +++ b/xen/arch/arm/static-memory.c @@ -2,6 +2,7 @@ #include +#include #include static bool __init append_static_memory_to_bank(struct domain *d, @@ -276,6 +277,44 @@ void __init init_staticmem_pages(void) } } +int __init find_11_domU_magic_region(struct domain *d, + struct kernel_info *kinfo) +{ + if ( is_domain_direct_mapped(d) ) + { + struct meminfo *magic_region = xzalloc(struct meminfo); + struct mem_map_domain *mem_map = &d->arch.mem_map; + unsigned int i; + int ret = 0; + + if ( !magic_region ) + return -ENOMEM; + + ret = find_unused_regions(d, kinfo, magic_region, GUEST_MAGIC_SIZE); + if ( ret ) + { + printk(XENLOG_WARNING + "%pd: failed to find a region for domain magic pages\n", d); + xfree(magic_region); + return -ENOENT; + } + + /* Update the domain memory map. */ + for ( i = 0; i < mem_map->nr_mem_regions; i++ ) + { + if ( mem_map->regions[i].type == XEN_MEM_REGION_MAGIC ) + { + mem_map->regions[i].start = magic_region->bank[0].start; + mem_map->regions[i].size = GUEST_MAGIC_SIZE; + } + } + + xfree(magic_region); + } + + return 0; +} + /* * Local variables: * mode: C From patchwork Tue Apr 9 04:53:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Henry Wang X-Patchwork-Id: 13621798 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 50CF2CD129C for ; Tue, 9 Apr 2024 04:54:33 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.702122.1096971 (Exim 4.92) (envelope-from ) id 1ru3V1-0004dj-Nv; Tue, 09 Apr 2024 04:54:23 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 702122.1096971; Tue, 09 Apr 2024 04:54:23 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ru3V1-0004dT-Jw; Tue, 09 Apr 2024 04:54:23 +0000 Received: by outflank-mailman (input) for mailman id 702122; Tue, 09 Apr 2024 04:54:21 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ru3Uy-0003cd-Rh for xen-devel@lists.xenproject.org; Tue, 09 Apr 2024 04:54:20 +0000 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on20600.outbound.protection.outlook.com [2a01:111:f403:2417::600]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 39bd7cc6-f62d-11ee-9150-f14010f3f246; Tue, 09 Apr 2024 06:54:19 +0200 (CEST) Received: from CY8PR12CA0009.namprd12.prod.outlook.com (2603:10b6:930:4e::29) by DM4PR12MB6302.namprd12.prod.outlook.com (2603:10b6:8:a4::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.46; Tue, 9 Apr 2024 04:54:16 +0000 Received: from CY4PEPF0000EDD1.namprd03.prod.outlook.com (2603:10b6:930:4e:cafe::77) by CY8PR12CA0009.outlook.office365.com (2603:10b6:930:4e::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7452.37 via Frontend Transport; Tue, 9 Apr 2024 04:54:16 +0000 Received: from SATLEXMB04.amd.com (165.204.84.17) by CY4PEPF0000EDD1.mail.protection.outlook.com (10.167.241.205) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7452.22 via Frontend Transport; Tue, 9 Apr 2024 04:54:16 +0000 Received: from SATLEXMB08.amd.com (10.181.40.132) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Mon, 8 Apr 2024 23:54:16 -0500 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB08.amd.com (10.181.40.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Mon, 8 Apr 2024 21:54:15 -0700 Received: from henry-MS-7D54.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2507.35 via Frontend Transport; Mon, 8 Apr 2024 23:54:13 -0500 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: 39bd7cc6-f62d-11ee-9150-f14010f3f246 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QAH/BlyQwyb87yrM1+QxR6jc1DNMYeHDgBiQTU96fLRRkfwksdJHORgw47ca/ILch/356WcFcF6bEVd2lrSzbue9sLujelXK5qIioyAA0I2qGaW/EkzAyVISsWoqqs4DCUMukA6z5qM3xbuHLeRzaF8dpmyms4bv46M38IlVuOSTb+hvKd26bXIyevIw3QombKUvJH5FkfibMIlKM/d2Z7vlt5jNpWGJI6/abf2EgzrVVx4Fz2MuNznKt29YyR4DT8+IZ6X4HPgZjkbsY+WKirFGqi0dy2xXZUS4ksWyL5fnC+M/+Xa9LO0pcER8PFS6G0bfnSaK560yeS48lkhzSg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=AqNwO/2J9JqnXyE4dOIjQwE9fUs3oiN1zYGcYi0aIXk=; b=IdrYGP8E3+vvXIsofL1ebmz0J3Vz+lo0U4JIq0ZxBjqUJnBaWaELqLH8g87nwmq2xAa+U5OPs5kjuFyo0SMqY55dAUxmYz94zx/TLbAXi2F48cCFBX9SBlvQXOLdeJ9KcHIK3CJdbR/tH/TlgHIUZ6c+T4qFXJ1GXgjedS/mGCz3NGppHsVY2B97amD411UhEuWD54UW7ALL+v0IXtiq+fNUIUV13YMUtGe6AishONKOYCprG20tGbkcyR+Ugem0yKKwDKVngIOO7zGgQXkBoixh+za0PifwTv9jHsBMtGvpvCx7fp2Td9A9MGx67yA7mkA93+kKsEQrrTj3GuONoA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=AqNwO/2J9JqnXyE4dOIjQwE9fUs3oiN1zYGcYi0aIXk=; b=26a8KSJYHFhKTbY9J/wmjuKm6l6L5HNxlCXlLYLJ4fvimG70m3/4RwlUSAcm9xRVcuTZ9DnTKzGS1uXBbSA/6qrkbZt51TpxHAMs0PK0KCuVg/F92zFpsUj42XX8VyAKNDp7ml3UWn1RHwGuega+9EIm+jGIHs+3HmyxvceoJEs= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C From: Henry Wang To: CC: Henry Wang , Anthony PERARD , Andrew Cooper , George Dunlap , Jan Beulich , Julien Grall , Stefano Stabellini , Juergen Gross , Alec Kwapis Subject: [PATCH v4 4/5] xen/memory, tools: Avoid hardcoding GUEST_MAGIC_BASE in init-dom0less Date: Tue, 9 Apr 2024 12:53:56 +0800 Message-ID: <20240409045357.236802-5-xin.wang2@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240409045357.236802-1-xin.wang2@amd.com> References: <20240409045357.236802-1-xin.wang2@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000EDD1:EE_|DM4PR12MB6302:EE_ X-MS-Office365-Filtering-Correlation-Id: ff42c0e3-ea5e-4321-3914-08dc58511c34 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: +RVvzMd7Q96qXuF2nq68C7bPo/+FpPwkszuJg/hM5vmYMwrZn6LQ9zRIBjHXTJPC4YoSbVQr/wT7iF7JQdmMJH/hLFGc23GuG5q7RwrA7BB8T3xvOxMS3wZaw2Ct8rEXeqOvp+TwSeu2Cud4kqBkeZmGsjaqSWilQI1aUQScRqo63V2r5u8Zyz4anQMONTB7V4Dum0OFpkPGITR3wNQvOB/ll+K1I2QIsghEsVgU6A9wSHATiNajd/D/P2D5ztnfWueZVgYm56Pxhsy60mugYGMf3CYPzwVrFzhFqdkGRGnAmCjzSgTldbTl0F8OfzuEBZajveOKVv1kRQq6+OmI/AmN5SdRAmBLrPRYv9BOVuuFblXYEaSpPl2d88W5EzBQPs3MpMfpWZYTNKimNdLklnECpJxSWHOv8g4A8vHki+XJi3G3bH9P+vAN8nuTIGst84iArkUR1LZrmTAqID6lQ9Sw7LYE7sv1+BiyLDohU/8T0FlwwOalxfM+QyJcGYRbmMouYhlV5kaVPsv/sz36gjgvOvSgzWldJ4++TyC2AKidKXBM/69+u5Ce94ep951EyNzxkS0Ud+U6UYoe32MHl+VXGUdafdS0ZyYHnYMYVpRn27e2CaU2r9jSShMcJYE61OfwkBh+3GFnRRAbupPaaK8wqabT/mMxKLA60hJVOG7+TDWow+Xhl5+7oAQaH5Eo5DNSWRzUWa9kgHW+5+p/qTV5gnL+wEs4YcEbkgOzsdF1OmzRbCxTq0MrjnZGlA56 X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(1800799015)(36860700004)(82310400014)(376005);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Apr 2024 04:54:16.7453 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ff42c0e3-ea5e-4321-3914-08dc58511c34 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000EDD1.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB6302 Currently the GUEST_MAGIC_BASE in the init-dom0less application is hardcoded, which will lead to failures for 1:1 direct-mapped Dom0less DomUs. Instead of hardcoding the guest magic pages region, use the XEN_DOMCTL_get_mem_map domctl to get the start address of the guest magic pages region. Add a new sub-op XENMEM_populate_physmap_heap_alloc and the MEMF_force_heap_alloc flag to force populate_physmap() to allocate page from domheap instead of using 1:1 or static allocated pages to map the magic pages. Reported-by: Alec Kwapis Signed-off-by: Henry Wang --- v4: - Use the new subop. - Add assert to check the flag isn't already set when coming back from construct_memop_from_reservation(). - Use &= to clear the flag instead of clear_bit(). - Move the alias to xen/common/memory.c v3: - Don't ignore the error from xc_get_domain_mem_map(). - Re-purposing the _MEMF_no_refcount as _MEMF_force_heap_alloc to avoid introduction of a new, single-use flag. - Reject other reservation sub-ops to use the newly added flag. - Replace all the GUEST_MAGIC_BASE usages. v2: - New patch --- tools/helpers/init-dom0less.c | 35 +++++++++++----- tools/include/xenctrl.h | 7 ++++ tools/libs/ctrl/xc_domain.c | 79 ++++++++++++++++++++++++++--------- xen/common/memory.c | 30 +++++++++++-- xen/include/public/memory.h | 3 +- 5 files changed, 120 insertions(+), 34 deletions(-) diff --git a/tools/helpers/init-dom0less.c b/tools/helpers/init-dom0less.c index fee93459c4..dccab7b29b 100644 --- a/tools/helpers/init-dom0less.c +++ b/tools/helpers/init-dom0less.c @@ -19,24 +19,42 @@ #define XENSTORE_PFN_OFFSET 1 #define STR_MAX_LENGTH 128 +static xen_pfn_t xs_page_base; +static xen_pfn_t xs_page_p2m; + static int alloc_xs_page(struct xc_interface_core *xch, libxl_dominfo *info, uint64_t *xenstore_pfn) { - int rc; - const xen_pfn_t base = GUEST_MAGIC_BASE >> XC_PAGE_SHIFT; - xen_pfn_t p2m = (GUEST_MAGIC_BASE >> XC_PAGE_SHIFT) + XENSTORE_PFN_OFFSET; + int rc, i; + uint32_t nr_regions = XEN_MAX_MEM_REGIONS; + struct xen_mem_region mem_regions[XEN_MAX_MEM_REGIONS] = {0}; + + rc = xc_get_domain_mem_map(xch, info->domid, mem_regions, &nr_regions); + if (rc < 0) + return rc; + + for ( i = 0; i < nr_regions; i++ ) + { + if ( mem_regions[i].type == XEN_MEM_REGION_MAGIC ) + { + xs_page_base = mem_regions[i].start >> XC_PAGE_SHIFT; + xs_page_p2m = (mem_regions[i].start >> XC_PAGE_SHIFT) + + XENSTORE_PFN_OFFSET; + } + } rc = xc_domain_setmaxmem(xch, info->domid, info->max_memkb + (XC_PAGE_SIZE/1024)); if (rc < 0) return rc; - rc = xc_domain_populate_physmap_exact(xch, info->domid, 1, 0, 0, &p2m); + rc = xc_domain_populate_physmap_heap_exact(xch, info->domid, 1, 0, 0, + &xs_page_p2m); if (rc < 0) return rc; - *xenstore_pfn = base + XENSTORE_PFN_OFFSET; + *xenstore_pfn = xs_page_base + XENSTORE_PFN_OFFSET; rc = xc_clear_domain_page(xch, info->domid, *xenstore_pfn); if (rc < 0) return rc; @@ -145,8 +163,7 @@ static int create_xenstore(struct xs_handle *xsh, rc = snprintf(target_memkb_str, STR_MAX_LENGTH, "%"PRIu64, info->current_memkb); if (rc < 0 || rc >= STR_MAX_LENGTH) return rc; - rc = snprintf(ring_ref_str, STR_MAX_LENGTH, "%lld", - (GUEST_MAGIC_BASE >> XC_PAGE_SHIFT) + XENSTORE_PFN_OFFSET); + rc = snprintf(ring_ref_str, STR_MAX_LENGTH, "%"PRIu_xen_pfn, xs_page_p2m); if (rc < 0 || rc >= STR_MAX_LENGTH) return rc; rc = snprintf(xenstore_port_str, STR_MAX_LENGTH, "%u", xenstore_port); @@ -282,9 +299,7 @@ static int init_domain(struct xs_handle *xsh, if (rc) err(1, "writing to xenstore"); - rc = xs_introduce_domain(xsh, info->domid, - (GUEST_MAGIC_BASE >> XC_PAGE_SHIFT) + XENSTORE_PFN_OFFSET, - xenstore_evtchn); + rc = xs_introduce_domain(xsh, info->domid, xs_page_p2m, xenstore_evtchn); if (!rc) err(1, "xs_introduce_domain"); return 0; diff --git a/tools/include/xenctrl.h b/tools/include/xenctrl.h index b25e9772a2..c1a601813a 100644 --- a/tools/include/xenctrl.h +++ b/tools/include/xenctrl.h @@ -1342,6 +1342,13 @@ int xc_domain_populate_physmap(xc_interface *xch, unsigned int mem_flags, xen_pfn_t *extent_start); +int xc_domain_populate_physmap_heap_exact(xc_interface *xch, + uint32_t domid, + unsigned long nr_extents, + unsigned int extent_order, + unsigned int mem_flags, + xen_pfn_t *extent_start); + int xc_domain_populate_physmap_exact(xc_interface *xch, uint32_t domid, unsigned long nr_extents, diff --git a/tools/libs/ctrl/xc_domain.c b/tools/libs/ctrl/xc_domain.c index 4dba55d01d..82c1554613 100644 --- a/tools/libs/ctrl/xc_domain.c +++ b/tools/libs/ctrl/xc_domain.c @@ -916,6 +916,36 @@ int xc_domain_nr_gpfns(xc_interface *xch, uint32_t domid, xen_pfn_t *gpfns) return rc; } +static int xc_populate_physmap_cmd(xc_interface *xch, + unsigned int cmd, + uint32_t domid, + unsigned long nr_extents, + unsigned int extent_order, + unsigned int mem_flags, + xen_pfn_t *extent_start) +{ + int err; + DECLARE_HYPERCALL_BOUNCE(extent_start, nr_extents * sizeof(*extent_start), XC_HYPERCALL_BUFFER_BOUNCE_BOTH); + struct xen_memory_reservation reservation = { + .nr_extents = nr_extents, + .extent_order = extent_order, + .mem_flags = mem_flags, + .domid = domid + }; + + if ( xc_hypercall_bounce_pre(xch, extent_start) ) + { + PERROR("Could not bounce memory for XENMEM_populate_physmap hypercall"); + return -1; + } + set_xen_guest_handle(reservation.extent_start, extent_start); + + err = xc_memory_op(xch, cmd, &reservation, sizeof(reservation)); + + xc_hypercall_bounce_post(xch, extent_start); + return err; +} + int xc_domain_increase_reservation(xc_interface *xch, uint32_t domid, unsigned long nr_extents, @@ -1135,26 +1165,9 @@ int xc_domain_populate_physmap(xc_interface *xch, unsigned int mem_flags, xen_pfn_t *extent_start) { - int err; - DECLARE_HYPERCALL_BOUNCE(extent_start, nr_extents * sizeof(*extent_start), XC_HYPERCALL_BUFFER_BOUNCE_BOTH); - struct xen_memory_reservation reservation = { - .nr_extents = nr_extents, - .extent_order = extent_order, - .mem_flags = mem_flags, - .domid = domid - }; - - if ( xc_hypercall_bounce_pre(xch, extent_start) ) - { - PERROR("Could not bounce memory for XENMEM_populate_physmap hypercall"); - return -1; - } - set_xen_guest_handle(reservation.extent_start, extent_start); - - err = xc_memory_op(xch, XENMEM_populate_physmap, &reservation, sizeof(reservation)); - - xc_hypercall_bounce_post(xch, extent_start); - return err; + return xc_populate_physmap_cmd(xch, XENMEM_populate_physmap, domid, + nr_extents, extent_order, mem_flags, + extent_start); } int xc_domain_populate_physmap_exact(xc_interface *xch, @@ -1182,6 +1195,32 @@ int xc_domain_populate_physmap_exact(xc_interface *xch, return err; } +int xc_domain_populate_physmap_heap_exact(xc_interface *xch, + uint32_t domid, + unsigned long nr_extents, + unsigned int extent_order, + unsigned int mem_flags, + xen_pfn_t *extent_start) +{ + int err; + + err = xc_populate_physmap_cmd(xch, XENMEM_populate_physmap_heap_alloc, + domid, nr_extents, extent_order, mem_flags, + extent_start); + if ( err == nr_extents ) + return 0; + + if ( err >= 0 ) + { + DPRINTF("Failed allocation for dom %d: %ld extents of order %d\n", + domid, nr_extents, extent_order); + errno = EBUSY; + err = -1; + } + + return err; +} + int xc_domain_memory_exchange_pages(xc_interface *xch, uint32_t domid, unsigned long nr_in_extents, diff --git a/xen/common/memory.c b/xen/common/memory.c index b4593f5f45..a4733869c2 100644 --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -155,6 +155,14 @@ static void increase_reservation(struct memop_args *a) a->nr_done = i; } +/* + * Alias of _MEMF_no_refcount to avoid introduction of a new, single-use flag. + * This flag should be used for populate_physmap() only as a re-purposing of + * _MEMF_no_refcount to force a non-1:1 allocation from domheap. + */ +#define _MEMF_force_heap_alloc _MEMF_no_refcount +#define MEMF_force_heap_alloc (1U<<_MEMF_force_heap_alloc) + static void populate_physmap(struct memop_args *a) { struct page_info *page; @@ -219,7 +227,8 @@ static void populate_physmap(struct memop_args *a) } else { - if ( is_domain_direct_mapped(d) ) + if ( is_domain_direct_mapped(d) && + !(a->memflags & MEMF_force_heap_alloc) ) { mfn = _mfn(gpfn); @@ -246,7 +255,8 @@ static void populate_physmap(struct memop_args *a) mfn = _mfn(gpfn); } - else if ( is_domain_using_staticmem(d) ) + else if ( is_domain_using_staticmem(d) && + !(a->memflags & MEMF_force_heap_alloc) ) { /* * No easy way to guarantee the retrieved pages are contiguous, @@ -271,6 +281,14 @@ static void populate_physmap(struct memop_args *a) } else { + /* + * Avoid passing MEMF_force_heap_alloc down to + * alloc_domheap_pages() where the meaning would be the + * original MEMF_no_refcount. + */ + if ( unlikely(a->memflags & MEMF_force_heap_alloc) ) + a->memflags &= ~MEMF_force_heap_alloc; + page = alloc_domheap_pages(d, a->extent_order, a->memflags); if ( unlikely(!page) ) @@ -1404,6 +1422,7 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) { case XENMEM_increase_reservation: case XENMEM_decrease_reservation: + case XENMEM_populate_physmap_heap_alloc: case XENMEM_populate_physmap: if ( copy_from_guest(&reservation, arg, 1) ) return start_extent; @@ -1433,6 +1452,11 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) && (reservation.mem_flags & XENMEMF_populate_on_demand) ) args.memflags |= MEMF_populate_on_demand; + /* Assert flag is not set from construct_memop_from_reservation(). */ + ASSERT(!(args.memflags & MEMF_force_heap_alloc)); + if ( op == XENMEM_populate_physmap_heap_alloc ) + args.memflags |= MEMF_force_heap_alloc; + if ( xsm_memory_adjust_reservation(XSM_TARGET, curr_d, d) ) { rcu_unlock_domain(d); @@ -1453,7 +1477,7 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) case XENMEM_decrease_reservation: decrease_reservation(&args); break; - default: /* XENMEM_populate_physmap */ + default: /* XENMEM_populate_{physmap, physmap_heap_alloc} */ populate_physmap(&args); break; } diff --git a/xen/include/public/memory.h b/xen/include/public/memory.h index 5e545ae9a4..5e79992671 100644 --- a/xen/include/public/memory.h +++ b/xen/include/public/memory.h @@ -21,6 +21,7 @@ #define XENMEM_increase_reservation 0 #define XENMEM_decrease_reservation 1 #define XENMEM_populate_physmap 6 +#define XENMEM_populate_physmap_heap_alloc 29 #if __XEN_INTERFACE_VERSION__ >= 0x00030209 /* @@ -731,7 +732,7 @@ struct xen_vnuma_topology_info { typedef struct xen_vnuma_topology_info xen_vnuma_topology_info_t; DEFINE_XEN_GUEST_HANDLE(xen_vnuma_topology_info_t); -/* Next available subop number is 29 */ +/* Next available subop number is 30 */ #endif /* __XEN_PUBLIC_MEMORY_H__ */ From patchwork Tue Apr 9 04:53:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Henry Wang X-Patchwork-Id: 13621800 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9BD48CD1284 for ; Tue, 9 Apr 2024 04:54:39 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.702123.1096981 (Exim 4.92) (envelope-from ) id 1ru3V6-00050H-V6; Tue, 09 Apr 2024 04:54:28 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 702123.1096981; Tue, 09 Apr 2024 04:54:28 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ru3V6-000502-S9; Tue, 09 Apr 2024 04:54:28 +0000 Received: by outflank-mailman (input) for mailman id 702123; Tue, 09 Apr 2024 04:54:26 +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 1ru3V4-0003cc-Qd for xen-devel@lists.xenproject.org; Tue, 09 Apr 2024 04:54:26 +0000 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on20601.outbound.protection.outlook.com [2a01:111:f403:2414::601]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 3cb58b57-f62d-11ee-843a-3f19d20e7db6; Tue, 09 Apr 2024 06:54:25 +0200 (CEST) Received: from CH2PR10CA0027.namprd10.prod.outlook.com (2603:10b6:610:4c::37) by SJ2PR12MB9209.namprd12.prod.outlook.com (2603:10b6:a03:558::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.46; Tue, 9 Apr 2024 04:54:19 +0000 Received: from CH1PEPF0000AD77.namprd04.prod.outlook.com (2603:10b6:610:4c:cafe::90) by CH2PR10CA0027.outlook.office365.com (2603:10b6:610:4c::37) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7386.28 via Frontend Transport; Tue, 9 Apr 2024 04:54:18 +0000 Received: from SATLEXMB03.amd.com (165.204.84.17) by CH1PEPF0000AD77.mail.protection.outlook.com (10.167.244.55) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7452.22 via Frontend Transport; Tue, 9 Apr 2024 04:54:18 +0000 Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Mon, 8 Apr 2024 23:54:18 -0500 Received: from henry-MS-7D54.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2507.35 via Frontend Transport; Mon, 8 Apr 2024 23:54:16 -0500 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: 3cb58b57-f62d-11ee-843a-3f19d20e7db6 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=b/2AthjBxPKlrB+mUWTrxXkzZIcuyxt7a6wUCo1JnuIBaICh27paQZZ7vSuU3iICU4buFyMJT17rGRa8wACpFWFPaMBsjMWbonsfr1yWkvao299Uj9PJ5N58ng/C6FO1Ya/4WRMxeo2ImJibFHStq0GWI5uE1ThpbMjn3pUlQPxeQK2d+F1okqDE3anWPpaGMkTa48bb7eMSR64GWunQXpiqk2otRaAbjAtKXgXVIUAppIfEoyUyY37iujEITv1WRRpmrxLJlcTalsV/c8OwUAryxB1BwYkDE38ywejGFsgW97G4lOBaf9cWDTLofz2x39nELkqox+iw0DLcIh2oqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=Li9+zc6kRUe5iMzs7R2wr0uz8YAnXguSopUS5SYA47g=; b=OqWfeC/9Lfqj6GaqQ1MEbS8PUuklDMKThYUhLNOUeJOtegcdi33H9WYEx6jyzns+DgI0ItFk2zKXmxDFcizKB0EHDAl1k5TAbcVzhDZiVeM1lnlwOScWDf7ju7tHp9yUk8Cykam0eWXlSmPa7TEw0r9IoNqkD1Ab3SCYkgp4+vDfTi61aK5W6o6gSSRqptEO5gaEfHzoywokIcTFTq70BtM+mLNZNlJU8AV2x1IKjIG0GxHrY39hckJeGnOD51FTNH8CE9cSeF/fcgLFAT7OaobbbokmVlkseNP16OVsaWykQRodszSNFyvMLtQpn7iXVbyOZkX25yYOI1jYrO/NFA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Li9+zc6kRUe5iMzs7R2wr0uz8YAnXguSopUS5SYA47g=; b=w6gdL+0sGsPtVfZBVjgJElGDFfj24dsXE67NCBVamrnpIk6gt7xCa7ncoah6PrkXBrQEIMSTJjgfuXtI3W1n1VDeuV/Yqz3fY3CKmGfij9FuhS7zyy6Ogz8jCzPAw2nwiIFjzw6eh9XPaQvNhmieTsXNXexsLITWswplLzCQ/KE= X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; pr=C From: Henry Wang To: CC: Henry Wang , Anthony PERARD , Juergen Gross , Jan Beulich Subject: [PATCH v4 5/5] tools/libs/ctrl: Simplify xc helpers related to populate_physmap() Date: Tue, 9 Apr 2024 12:53:57 +0800 Message-ID: <20240409045357.236802-6-xin.wang2@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240409045357.236802-1-xin.wang2@amd.com> References: <20240409045357.236802-1-xin.wang2@amd.com> MIME-Version: 1.0 Received-SPF: None (SATLEXMB03.amd.com: xin.wang2@amd.com does not designate permitted sender hosts) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH1PEPF0000AD77:EE_|SJ2PR12MB9209:EE_ X-MS-Office365-Filtering-Correlation-Id: a57aa47a-1741-4c2e-d7e6-08dc58511d75 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ju7yXtu7rExQtyNrbLFzy1nLd45LH7F5tAHTYN1CEXy52IeDfhavaPKeaRL0h6WNjoshZju7efnBC1EEEj/Z+QKk1AD66FLuI5uqa905dDAhwO4U0ENC60ts7HIcg8H4FhnVRD4kGHQhfIimXoO1Gs7M1PNHgcZWWCR80kma74yMp2YBdgCOYNc7W1GFXHO+Twkj+J3cDgzs7bRlH3cZ62WGn0biRgZESLn0suDLNawx19O9gznU1xEAC2j0ZhRBgWuHHPmnbINctPGkuz/TZMYf3sHqrsarywQnjnb/VT3/B/RJWBXo6SbuhJjm9d4viui+16RMiFBydu9a6B1rFyxw47ggt/PizjLRPDjyOZPk0p6NIXeSjv5OGzWojsd49M+5d12T4oWO4WLLNvzdmCDEsRWsSt2YsXAyTmD524BUzlqz7WP+zB1/sZ/lGPjXaluJ6qG8QQ8B9JlzakSnZB80GYpXk8WoYJNnlk4xtnfcpOUc3P5BemWZpAFfsNgVLDJPWgeGjoB3LdeeXnru1T1NKfqizDiWkoNI6rSGhbY30Ihvaz7r/Xm0/Gu6xV+E9IUOWiMgOsAST+iS9hg5lthWTMqxLQtEC2/VKMpRrTK38Ubv6DG3Rcn/s1mFz0G/W3XI4xLsOFf/GGRqqESOgDOjQzOri9skOeoIJeowXAS4L9xQ4O6GRPXp6AFgGR6kWaClYHFV2TAHEKSn7/6Kqm+HuZmsxUk/GOv02BKcF/EPQDAvRWxfu0DWBAeKsHtU X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB03.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(1800799015)(82310400014)(376005)(36860700004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Apr 2024 04:54:18.9000 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a57aa47a-1741-4c2e-d7e6-08dc58511d75 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CH1PEPF0000AD77.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9209 There are currently a lot of xc helpers to call populate_physmap() in the hypervisor with different parameters, such as: - xc_domain_{increase, decrease}_{reservation, reservation_exact} - xc_domain_populate_physmap - xc_domain_populate_{physmap_exact, physmap_heap_exact} Most of them share the same and duplicated logic. Extract the duplicated code of these xc helpers to local helper functions to simplify the code. No functional change intended. Suggested-by: Jan Beulich Signed-off-by: Henry Wang --- v4: - New patch. --- tools/libs/ctrl/xc_domain.c | 178 +++++++++++++----------------------- 1 file changed, 62 insertions(+), 116 deletions(-) diff --git a/tools/libs/ctrl/xc_domain.c b/tools/libs/ctrl/xc_domain.c index 82c1554613..d023596bed 100644 --- a/tools/libs/ctrl/xc_domain.c +++ b/tools/libs/ctrl/xc_domain.c @@ -935,7 +935,7 @@ static int xc_populate_physmap_cmd(xc_interface *xch, if ( xc_hypercall_bounce_pre(xch, extent_start) ) { - PERROR("Could not bounce memory for XENMEM_populate_physmap hypercall"); + PERROR("Could not bounce memory for hypercall %u", cmd); return -1; } set_xen_guest_handle(reservation.extent_start, extent_start); @@ -946,39 +946,8 @@ static int xc_populate_physmap_cmd(xc_interface *xch, return err; } -int xc_domain_increase_reservation(xc_interface *xch, - uint32_t domid, - unsigned long nr_extents, - unsigned int extent_order, - unsigned int mem_flags, - xen_pfn_t *extent_start) -{ - int err; - DECLARE_HYPERCALL_BOUNCE(extent_start, nr_extents * sizeof(*extent_start), XC_HYPERCALL_BUFFER_BOUNCE_BOTH); - struct xen_memory_reservation reservation = { - .nr_extents = nr_extents, - .extent_order = extent_order, - .mem_flags = mem_flags, - .domid = domid - }; - - /* may be NULL */ - if ( xc_hypercall_bounce_pre(xch, extent_start) ) - { - PERROR("Could not bounce memory for XENMEM_increase_reservation hypercall"); - return -1; - } - - set_xen_guest_handle(reservation.extent_start, extent_start); - - err = xc_memory_op(xch, XENMEM_increase_reservation, &reservation, sizeof(reservation)); - - xc_hypercall_bounce_post(xch, extent_start); - - return err; -} - -int xc_domain_increase_reservation_exact(xc_interface *xch, +static int xc_populate_physmap_cmd_exact(xc_interface *xch, + unsigned int cmd, uint32_t domid, unsigned long nr_extents, unsigned int extent_order, @@ -987,58 +956,75 @@ int xc_domain_increase_reservation_exact(xc_interface *xch, { int err; - err = xc_domain_increase_reservation(xch, domid, nr_extents, - extent_order, mem_flags, extent_start); - + err = xc_populate_physmap_cmd(xch, cmd, domid, nr_extents, + extent_order, mem_flags, extent_start); if ( err == nr_extents ) return 0; if ( err >= 0 ) { - DPRINTF("Failed allocation for dom %d: " + switch ( cmd ) + { + case XENMEM_increase_reservation: + DPRINTF("Failed allocation for dom %d: " "%ld extents of order %d, mem_flags %x\n", domid, nr_extents, extent_order, mem_flags); - errno = ENOMEM; + errno = ENOMEM; + break; + case XENMEM_decrease_reservation: + DPRINTF("Failed deallocation for dom %d: %ld extents of order %d\n", + domid, nr_extents, extent_order); + errno = EINVAL; + break; + case XENMEM_populate_physmap_heap_alloc: + case XENMEM_populate_physmap: + DPRINTF("Failed allocation for dom %d: %ld extents of order %d\n", + domid, nr_extents, extent_order); + errno = EBUSY; + break; + default: + DPRINTF("Invalid cmd %u\n", cmd); + errno = EINVAL; + break; + } err = -1; } return err; } -int xc_domain_decrease_reservation(xc_interface *xch, +int xc_domain_increase_reservation(xc_interface *xch, uint32_t domid, unsigned long nr_extents, unsigned int extent_order, + unsigned int mem_flags, xen_pfn_t *extent_start) { - int err; - DECLARE_HYPERCALL_BOUNCE(extent_start, nr_extents * sizeof(*extent_start), XC_HYPERCALL_BUFFER_BOUNCE_BOTH); - struct xen_memory_reservation reservation = { - .nr_extents = nr_extents, - .extent_order = extent_order, - .mem_flags = 0, - .domid = domid - }; - - if ( extent_start == NULL ) - { - DPRINTF("decrease_reservation extent_start is NULL!\n"); - errno = EINVAL; - return -1; - } - - if ( xc_hypercall_bounce_pre(xch, extent_start) ) - { - PERROR("Could not bounce memory for XENMEM_decrease_reservation hypercall"); - return -1; - } - set_xen_guest_handle(reservation.extent_start, extent_start); - - err = xc_memory_op(xch, XENMEM_decrease_reservation, &reservation, sizeof(reservation)); + return xc_populate_physmap_cmd(xch, XENMEM_increase_reservation, domid, + nr_extents, extent_order, mem_flags, + extent_start); +} - xc_hypercall_bounce_post(xch, extent_start); +int xc_domain_increase_reservation_exact(xc_interface *xch, + uint32_t domid, + unsigned long nr_extents, + unsigned int extent_order, + unsigned int mem_flags, + xen_pfn_t *extent_start) +{ + return xc_populate_physmap_cmd_exact(xch, XENMEM_increase_reservation, + domid, nr_extents, extent_order, + mem_flags, extent_start); +} - return err; +int xc_domain_decrease_reservation(xc_interface *xch, + uint32_t domid, + unsigned long nr_extents, + unsigned int extent_order, + xen_pfn_t *extent_start) +{ + return xc_populate_physmap_cmd(xch, XENMEM_decrease_reservation, domid, + nr_extents, extent_order, 0, extent_start); } int xc_domain_decrease_reservation_exact(xc_interface *xch, @@ -1047,23 +1033,9 @@ int xc_domain_decrease_reservation_exact(xc_interface *xch, unsigned int extent_order, xen_pfn_t *extent_start) { - int err; - - err = xc_domain_decrease_reservation(xch, domid, nr_extents, - extent_order, extent_start); - - if ( err == nr_extents ) - return 0; - - if ( err >= 0 ) - { - DPRINTF("Failed deallocation for dom %d: %ld extents of order %d\n", - domid, nr_extents, extent_order); - errno = EINVAL; - err = -1; - } - - return err; + return xc_populate_physmap_cmd_exact(xch, XENMEM_decrease_reservation, + domid, nr_extents, extent_order, + 0, extent_start); } int xc_domain_add_to_physmap(xc_interface *xch, @@ -1177,22 +1149,9 @@ int xc_domain_populate_physmap_exact(xc_interface *xch, unsigned int mem_flags, xen_pfn_t *extent_start) { - int err; - - err = xc_domain_populate_physmap(xch, domid, nr_extents, - extent_order, mem_flags, extent_start); - if ( err == nr_extents ) - return 0; - - if ( err >= 0 ) - { - DPRINTF("Failed allocation for dom %d: %ld extents of order %d\n", - domid, nr_extents, extent_order); - errno = EBUSY; - err = -1; - } - - return err; + return xc_populate_physmap_cmd_exact(xch, XENMEM_populate_physmap, + domid, nr_extents, extent_order, + mem_flags, extent_start); } int xc_domain_populate_physmap_heap_exact(xc_interface *xch, @@ -1202,23 +1161,10 @@ int xc_domain_populate_physmap_heap_exact(xc_interface *xch, unsigned int mem_flags, xen_pfn_t *extent_start) { - int err; - - err = xc_populate_physmap_cmd(xch, XENMEM_populate_physmap_heap_alloc, - domid, nr_extents, extent_order, mem_flags, - extent_start); - if ( err == nr_extents ) - return 0; - - if ( err >= 0 ) - { - DPRINTF("Failed allocation for dom %d: %ld extents of order %d\n", - domid, nr_extents, extent_order); - errno = EBUSY; - err = -1; - } - - return err; + return xc_populate_physmap_cmd_exact(xch, + XENMEM_populate_physmap_heap_alloc, + domid, nr_extents, extent_order, + mem_flags, extent_start); } int xc_domain_memory_exchange_pages(xc_interface *xch,