From nobody Tue Jul 30 07:57:11 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4WY6yJ0mHJz5RSxM; Tue, 30 Jul 2024 07:57:12 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4WY6yH72Y7z44Y3; Tue, 30 Jul 2024 07:57:11 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1722326232; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=DPjt9zPRPBTH2NldU3av/dV99VeSyB1sLWG/i6NKjXA=; b=Z1k0WOuE4JOLGh3Ru44Gs0MDhF+/FERg6Bu9JJpX/tLMVqGQs9Oa5+GDkE2pCXv/oX6VS3 Xya5Mzdo4V9YbH1ppymIPZmQRY+n5VQK8i4+hLrti4rg5KEGoKDs3GPe5HQHmIjnbkivA7 NxfPlAxCHXuiFGlF5O+5QRfH7ulfz/wRpek9E4vta7diM16KBetHDcfIF06JEd9H7tuScy t/2BG9191LzLGMmr5Kz+zs7SWYDPKHXfvdrJY6lBQXrh1aQuH2Vu/5Ad5Sek8jiWB4z0Mj nlDDW1xN+S4836OizB0jQGJs1rO/rueya4W98CNf6TMNh97J6m4GL6uLA524XA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1722326232; a=rsa-sha256; cv=none; b=wMOZJ9qkMegYcWrngXEgrj6UDwnFvktXIRvF3dZG4Z48FEE225j+DLMCR6Yvi46HQ8xK5z /oHX+SQ8EVhhzBwPh93bvG/qPFGPJVVqEO2o/HCNRwU0RI7qUg+9pNGVMcfh9C0nsrB52H BJx9pjFOMEG9HLhWDO97hPZQhZxhnhNmZZsxck+sgN61q0/CzbumE2nFy6Yk/NykxTlAjm qEKPYvGLgM4KraBlo9z6dydWek3Y97OJvCiBNMUR0JYpiQksICIGuRcMwfPQkLCQt/ALGT Dlaj+gFcUNZiGbhqD8Z6y1CR6/KdONhcv0kxmb6uKVI30KQNPlwFRiWS0fKwOg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1722326232; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=DPjt9zPRPBTH2NldU3av/dV99VeSyB1sLWG/i6NKjXA=; b=PjSA3e4UHIubxFCykq4Soe/lI6HLc1+OBoaKGNMOYHQuKMEtsNvGKnay/coZOXJ8SqKoJU AjkMorh6ANerGiahK48wWCPVV9e9bWAZv0K53DaSksJuLolndCz3VuDRY0ggwGfsCiXarq X5bt5nW/P+Bk7PZOt9k1GP0dFhMqAKLmLdj4MA8vN4gu45m41qX5FnPvdk/OSdBgHpu4RG Ss5HI5JgRpv8qKEz7wekqEN3BJC11FCYeP2JCSYG11GdHUM5s2KaYCaSvb6z6MVhospzDd ylNh1LktV4dps0LK4ye1xScOXxQDJ6aMZYUV8w0gtm6yUEu1y5TsDS9cRLqpQg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4WY6yH6dfVz17sm; Tue, 30 Jul 2024 07:57:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 46U7vB0T066414; Tue, 30 Jul 2024 07:57:11 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 46U7vBVT066411; Tue, 30 Jul 2024 07:57:11 GMT (envelope-from git) Date: Tue, 30 Jul 2024 07:57:11 GMT Message-Id: <202407300757.46U7vBVT066411@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Roger Pau =?utf-8?Q?Monn=C3=A9?= Subject: git: baa006f34227 - main - xen: introduce a per-arch scratch mapping ranges List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: royger X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: baa006f3422719d22da2ea920a5a46b608f47ce2 Auto-Submitted: auto-generated The branch main has been updated by royger: URL: https://cgit.FreeBSD.org/src/commit/?id=baa006f3422719d22da2ea920a5a46b608f47ce2 commit baa006f3422719d22da2ea920a5a46b608f47ce2 Author: Roger Pau Monné AuthorDate: 2024-07-25 09:00:08 +0000 Commit: Roger Pau Monné CommitDate: 2024-07-30 07:39:38 +0000 xen: introduce a per-arch scratch mapping ranges The current approach is to create foreign mappings in any unpopulated address on the memory map. This however can cause issues, as late-loaded drivers could then found their MMIO region has been stolen to be used as foreign mapping scratch space (due to the Xen drivers having started first). Add a local resource manager to the xenpv bus driver, architectures can add suitable resources to that resource manager at boot in order to be used in preference of the generic MMIO resource allocator. No functional change, as the introduced scratch mapping range is not yet populated. Sponsored by: Cloud Software Group MFC after: 1 week Reviewed by: Elliott Mitchell Differential revision: https://reviews.freebsd.org/D46122 --- sys/dev/xen/bus/xenpv.c | 77 +++++++++++++++++++++++++++++++++++++++++++++---- sys/xen/xen-os.h | 5 ++++ 2 files changed, 76 insertions(+), 6 deletions(-) diff --git a/sys/dev/xen/bus/xenpv.c b/sys/dev/xen/bus/xenpv.c index 169e52f3a879..5073b93b24f1 100644 --- a/sys/dev/xen/bus/xenpv.c +++ b/sys/dev/xen/bus/xenpv.c @@ -65,6 +65,16 @@ #define LOW_MEM_LIMIT 0 #endif +/* + * Memory ranges available for creating external mappings (foreign or grant + * pages for example). + */ +static struct rman unpopulated_mem = { + .rm_end = ~0, + .rm_type = RMAN_ARRAY, + .rm_descr = "Xen scratch memory", +}; + static void xenpv_identify(driver_t *driver, device_t parent) { @@ -91,10 +101,25 @@ xenpv_probe(device_t dev) return (BUS_PROBE_NOWILDCARD); } +/* Dummy init for arches that don't have a specific implementation. */ +int __weak_symbol +xen_arch_init_physmem(device_t dev, struct rman *mem) +{ + + return (0); +} + static int xenpv_attach(device_t dev) { - int error; + int error = rman_init(&unpopulated_mem); + + if (error != 0) + return (error); + + error = xen_arch_init_physmem(dev, &unpopulated_mem); + if (error != 0) + return (error); /* * Let our child drivers identify any child devices that they @@ -110,6 +135,14 @@ xenpv_attach(device_t dev) return (error); } +static int +release_unpopulated_mem(device_t dev, struct resource *res) +{ + + return (rman_is_region_manager(res, &unpopulated_mem) ? + rman_release_resource(res) : bus_release_resource(dev, res)); +} + static struct resource * xenpv_alloc_physmem(device_t dev, device_t child, int *res_id, size_t size) { @@ -117,17 +150,48 @@ xenpv_alloc_physmem(device_t dev, device_t child, int *res_id, size_t size) vm_paddr_t phys_addr; void *virt_addr; int error; + const unsigned int flags = RF_ACTIVE | RF_UNMAPPED | + RF_ALIGNMENT_LOG2(PAGE_SHIFT); + + KASSERT((size & PAGE_MASK) == 0, ("unaligned size requested")); + size = round_page(size); + + /* Attempt to allocate from arch resource manager. */ + res = rman_reserve_resource(&unpopulated_mem, 0, ~0, size, flags, + child); + if (res != NULL) { + rman_set_rid(res, *res_id); + rman_set_type(res, SYS_RES_MEMORY); + } else { + static bool warned = false; - res = bus_alloc_resource(child, SYS_RES_MEMORY, res_id, LOW_MEM_LIMIT, - ~0, size, RF_ACTIVE | RF_UNMAPPED); - if (res == NULL) + /* Fallback to generic MMIO allocator. */ + if (__predict_false(!warned)) { + warned = true; + device_printf(dev, + "unable to allocate from arch specific routine, " + "fall back to unused memory areas\n"); + } + res = bus_alloc_resource(child, SYS_RES_MEMORY, res_id, + LOW_MEM_LIMIT, ~0, size, flags); + } + + if (res == NULL) { + device_printf(dev, + "failed to allocate Xen unpopulated memory\n"); return (NULL); + } phys_addr = rman_get_start(res); error = vm_phys_fictitious_reg_range(phys_addr, phys_addr + size, VM_MEMATTR_XEN); if (error) { - bus_release_resource(child, SYS_RES_MEMORY, *res_id, res); + int error = release_unpopulated_mem(child, res); + + if (error != 0) + device_printf(dev, "failed to release resource: %d\n", + error); + return (NULL); } virt_addr = pmap_mapdev_attr(phys_addr, size, VM_MEMATTR_XEN); @@ -150,7 +214,8 @@ xenpv_free_physmem(device_t dev, device_t child, int res_id, struct resource *re pmap_unmapdev(virt_addr, size); vm_phys_fictitious_unreg_range(phys_addr, phys_addr + size); - return (bus_release_resource(child, SYS_RES_MEMORY, res_id, res)); + + return (release_unpopulated_mem(child, res)); } static device_method_t xenpv_methods[] = { diff --git a/sys/xen/xen-os.h b/sys/xen/xen-os.h index 98be41124846..bae4f9ef1d13 100644 --- a/sys/xen/xen-os.h +++ b/sys/xen/xen-os.h @@ -39,6 +39,8 @@ #include #ifndef __ASSEMBLY__ +#include + #include #include @@ -158,6 +160,9 @@ void xc_printf(const char *, ...) __printflike(1, 2); */ void xen_emergency_print(const char *str, size_t size); +/* Arch-specific helper to init scratch mapping space. */ +int xen_arch_init_physmem(device_t dev, struct rman *mem); + #ifndef xen_mb #define xen_mb() mb() #endif