From patchwork Tue Dec 17 17:06:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlo Nonato X-Patchwork-Id: 13912222 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 41AE0E7718A for ; Tue, 17 Dec 2024 17:07:00 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.859573.1271698 (Exim 4.92) (envelope-from ) id 1tNb1x-0006PO-JT; Tue, 17 Dec 2024 17:06:45 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 859573.1271698; Tue, 17 Dec 2024 17:06:45 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tNb1x-0006PH-GC; Tue, 17 Dec 2024 17:06:45 +0000 Received: by outflank-mailman (input) for mailman id 859573; Tue, 17 Dec 2024 17:06:43 +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 1tNb1v-0006B0-Q1 for xen-devel@lists.xenproject.org; Tue, 17 Dec 2024 17:06:43 +0000 Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [2a00:1450:4864:20::62c]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 49ce3bb5-bc99-11ef-a0d6-8be0dac302b0; Tue, 17 Dec 2024 18:06:42 +0100 (CET) Received: by mail-ej1-x62c.google.com with SMTP id a640c23a62f3a-aa69077b93fso826752366b.0 for ; Tue, 17 Dec 2024 09:06:42 -0800 (PST) Received: from carlo-ubuntu.home.arpa (host-95-230-250-178.business.telecomitalia.it. [95.230.250.178]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aab963910f7sm461362666b.166.2024.12.17.09.06.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Dec 2024 09:06:41 -0800 (PST) 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: 49ce3bb5-bc99-11ef-a0d6-8be0dac302b0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=minervasys-tech.20230601.gappssmtp.com; s=20230601; t=1734455202; x=1735060002; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=xHNrR+HjNxd8NEksH9lJNBl3N9ejsE80krMW4m0ypTg=; b=vZklG4MJM4w3WxvIQfu1K9qP1GNU2pR8c4zMTJ/CPp7LdPyTfb7tFJrbToU3M+Y036 W4S1qEJXlmwSisQEBUn7Gtiycne8kKBOlIOTBhU/CXAet4dg90hMNOSFdcD1Sv0LDK8h kMWKuzjc1LsBW70xDZZJwBIpnB2LYqdbyfJfbbNSD4qiZjKW8LSKgjMrvRA7iMZKVys+ waFvqg33LNjHgjXv6MkVefKeUmHMOvuS+aVnYv98lZQ8SNUSdZupC7kWxJGKEnW2oT+W l1uUHzpVDIZTgkjD3B+gH19gnI8Lo1cBNz89q3+3blrN7EQjUbEqBBQ4uYZngqLqQnS7 8guQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734455202; x=1735060002; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xHNrR+HjNxd8NEksH9lJNBl3N9ejsE80krMW4m0ypTg=; b=G+mvtAqImP06z6hkJ37cCkXLtV7xro95DY0LvhjltnVIJKzRukmzBDtwq7Pb6jYKvJ OT9IWwrvJCrGAcPbU+hqGjw8QoQwRHPA7eXIKfowHCKHeaW6RmYbX7asPJQ65MQ+Ytpn RAqvt0X27IR5Ds69IYMZvi1lB2Sm36655M2AyTzhn56OpecHgOKeeFnycB2cCGrZt6xM f155qIOg0jmrCLBKRMq+D3fRE/lJAMu6HLDaY8XD+qbVOV0Zc7hyqT+5/jkHcS8/EnZU dtmOKg9yCBvwWukhdXVP0HBfkTm188CNqeWGPKSk+2VYtM9TvQ/tKEtuVzxlkceinBps Bgxg== X-Gm-Message-State: AOJu0YzzBVzkOG3+CN5Qjj4Z1MGOyyWPlT8m4+2Jx1lZ1K6RvN/O2qbk ksG2Dzhm84CHlHrTGAKZHHcLGm0SAtgejEaGp+/qYL/I5boeuqp4w9q0EVAI6nhVkr/7Ejv+8v5 D//k= X-Gm-Gg: ASbGnctIqmXi93vzKZoV5pOYi0rZu5JJR1fufo7T3pzh2uiWSO3Ixjq40e2ELqIwRKn VWft61KoJfM7yyZOztnkTCLZ/LEVF6KqctJ21WBP7NLuxP6b9Js2EoB4os4mAmUJZCZ78vZZZnb ea4wOo6lhxVuYhNiqyLpyXce00hiWn8nxsWo8cXGZUP4r1/rx/TFoBNObFM4GO3EXt1YwNO2R9M CMlnKBT1LBd+pxOYytxhy4zXARtGc9DAyPcdgTI+OX1Fz0o4M6tThZtpHUyi9ay+BDpqC0y4fKS 0o0MiLXEZv7TwZ85bbLJHthizwrTghkLO4QmmeEWBUhcScRWVQnv0GWs5ng= X-Google-Smtp-Source: AGHT+IGUs53RL/lkjj0WLexo2rboX2ZGZReEVmzmFyMksq9J60QIEuGrvJrV3cgmjA4pdLAJZWaX5g== X-Received: by 2002:a17:906:709:b0:aab:dee6:a3a2 with SMTP id a640c23a62f3a-aabdee6a3femr317696166b.47.1734455201568; Tue, 17 Dec 2024 09:06:41 -0800 (PST) From: Carlo Nonato To: xen-devel@lists.xenproject.org Cc: andrea.bastoni@minervasys.tech, marco.solieri@minervasys.tech, Carlo Nonato , Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Stefano Stabellini Subject: [PATCH v13 01/12] xen/common: add cache coloring common code Date: Tue, 17 Dec 2024 18:06:26 +0100 Message-ID: <20241217170637.233097-2-carlo.nonato@minervasys.tech> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241217170637.233097-1-carlo.nonato@minervasys.tech> References: <20241217170637.233097-1-carlo.nonato@minervasys.tech> MIME-Version: 1.0 Last Level Cache (LLC) coloring allows to partition the cache in smaller chunks called cache colors. Since not all architectures can actually implement it, add a HAS_LLC_COLORING Kconfig option. LLC_COLORS_ORDER Kconfig option has a range maximum of 10 (2^10 = 1024) because that's the number of colors that fit in a 4 KiB page when integers are 4 bytes long. LLC colors are a property of the domain, so struct domain has to be extended. Based on original work from: Luca Miccio Signed-off-by: Carlo Nonato Signed-off-by: Marco Solieri Acked-by: Michal Orzel --- v13: - added note to cmdline docs for llc-nr-ways and llc-size shorthand precedence v12: - fixed build errors - added opt_llc_coloring to improve readibility - reverted llc_coloring_enabled type to bool v11: - __COLORING_H__ -> __XEN_LLC_COLORING_H__ in llc-coloring.h - added SPDX tag to cache-coloring.rst - llc-coloring=off now takes precedence over other cmdline options - removed useless #includes v10: - fixed commit message to use LLC_COLORS_ORDER - added documentation to index.rst - moved check on CONFIG_NUMA in arch/arm/Kconfig (next patch) - fixed copyright line - fixed array type for colors parameter in print_colors() - added check on (way_size & ~PAGE_MASK) v9: - dropped _MAX_ from CONFIG_MAX_LLC_COLORS_ORDER v8: - minor documentation fixes - "llc-coloring=on" is inferred from "llc-nr-ways" and "llc-size" usage - turned CONFIG_NR_LLC_COLORS to CONFIG_MAX_LLC_COLORS_ORDER, base-2 exponent - moved Kconfig options to common/Kconfig - don't crash if computed max_nr_colors is too large v7: - SUPPORT.md changes added to this patch - extended documentation to better address applicability of cache coloring - "llc-nr-ways" and "llc-size" params introduced in favor of "llc-way-size" - moved dump_llc_coloring_info() call in 'm' keyhandler (pagealloc_info()) v6: - moved almost all code in common - moved documentation in this patch - reintroduced range for CONFIG_NR_LLC_COLORS - reintroduced some stub functions to reduce the number of checks on llc_coloring_enabled - moved domain_llc_coloring_free() in same patch where allocation happens - turned "d->llc_colors" to pointer-to-const - llc_coloring_init() now returns void and panics if errors are found v5: - used - instead of _ for filenames - removed domain_create_llc_colored() - removed stub functions - coloring domain fields are now #ifdef protected v4: - Kconfig options moved to xen/arch - removed range for CONFIG_NR_LLC_COLORS - added "llc_coloring_enabled" global to later implement the boot-time switch - added domain_create_llc_colored() to be able to pass colors - added is_domain_llc_colored() macro --- SUPPORT.md | 7 ++ docs/index.rst | 1 + docs/misc/cache-coloring.rst | 118 ++++++++++++++++++++++++++++ docs/misc/xen-command-line.pandoc | 39 ++++++++++ xen/common/Kconfig | 21 +++++ xen/common/Makefile | 1 + xen/common/keyhandler.c | 3 + xen/common/llc-coloring.c | 124 ++++++++++++++++++++++++++++++ xen/common/page_alloc.c | 3 + xen/include/xen/llc-coloring.h | 36 +++++++++ xen/include/xen/sched.h | 5 ++ 11 files changed, 358 insertions(+) create mode 100644 docs/misc/cache-coloring.rst create mode 100644 xen/common/llc-coloring.c create mode 100644 xen/include/xen/llc-coloring.h diff --git a/SUPPORT.md b/SUPPORT.md index 82239d0294..998faf5635 100644 --- a/SUPPORT.md +++ b/SUPPORT.md @@ -401,6 +401,13 @@ by maintaining multiple physical to machine (p2m) memory mappings. Status, x86 HVM: Tech Preview Status, ARM: Tech Preview +### Cache coloring + +Allows to reserve Last Level Cache (LLC) partitions for Dom0, DomUs and Xen +itself. + + Status, Arm64: Experimental + ## Resource Management ### CPU Pools diff --git a/docs/index.rst b/docs/index.rst index 1d44796d72..1bb8d02ea3 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -66,6 +66,7 @@ Documents in need of some rearranging. misc/xen-makefiles/makefiles misra/index fusa/index + misc/cache-coloring Miscellanea diff --git a/docs/misc/cache-coloring.rst b/docs/misc/cache-coloring.rst new file mode 100644 index 0000000000..371f21a0e7 --- /dev/null +++ b/docs/misc/cache-coloring.rst @@ -0,0 +1,118 @@ +.. SPDX-License-Identifier: CC-BY-4.0 + +Xen cache coloring user guide +============================= + +The cache coloring support in Xen allows to reserve Last Level Cache (LLC) +partitions for Dom0, DomUs and Xen itself. Currently only ARM64 is supported. +Cache coloring realizes per-set cache partitioning in software and is applicable +to shared LLCs as implemented in Cortex-A53, Cortex-A72 and similar CPUs. + +To compile LLC coloring support set ``CONFIG_LLC_COLORING=y``. + +If needed, change the maximum number of colors with +``CONFIG_LLC_COLORS_ORDER=``. + +Runtime configuration is done via `Command line parameters`_. + +Background +********** + +Cache hierarchy of a modern multi-core CPU typically has first levels dedicated +to each core (hence using multiple cache units), while the last level is shared +among all of them. Such configuration implies that memory operations on one +core (e.g. running a DomU) are able to generate interference on another core +(e.g. hosting another DomU). Cache coloring realizes per-set cache-partitioning +in software and mitigates this, guaranteeing more predictable performances for +memory accesses. +Software-based cache coloring is particularly useful in those situations where +no hardware mechanisms (e.g., DSU-based way partitioning) are available to +partition caches. This is the case for e.g., Cortex-A53, A57 and A72 CPUs that +feature a L2 LLC cache shared among all cores. + +The key concept underlying cache coloring is a fragmentation of the memory +space into a set of sub-spaces called colors that are mapped to disjoint cache +partitions. Technically, the whole memory space is first divided into a number +of subsequent regions. Then each region is in turn divided into a number of +subsequent sub-colors. The generic i-th color is then obtained by all the +i-th sub-colors in each region. + +:: + + Region j Region j+1 + ..................... ............ + . . . + . . + _ _ _______________ _ _____________________ _ _ + | | | | | | | + | c_0 | c_1 | | c_n | c_0 | c_1 | + _ _ _|_____|_____|_ _ _|_____|_____|_____|_ _ _ + : : + : :... ... . + : color 0 + :........................... ... . + : + . . ..................................: + +How colors are actually defined depends on the function that maps memory to +cache lines. In case of physically-indexed, physically-tagged caches with linear +mapping, the set index is found by extracting some contiguous bits from the +physical address. This allows colors to be defined as shown in figure: they +appear in memory as subsequent blocks of equal size and repeats themselves after +``n`` different colors, where ``n`` is the total number of colors. + +If some kind of bit shuffling appears in the mapping function, then colors +assume a different layout in memory. Those kind of caches aren't supported by +the current implementation. + +**Note**: Finding the exact cache mapping function can be a really difficult +task since it's not always documented in the CPU manual. As said Cortex-A53, A57 +and A72 are known to work with the current implementation. + +How to compute the number of colors +################################### + +Given the linear mapping from physical memory to cache lines for granted, the +number of available colors for a specific platform is computed using three +parameters: + +- the size of the LLC. +- the number of the LLC ways. +- the page size used by Xen. + +The first two parameters can be found in the processor manual, while the third +one is the minimum mapping granularity. Dividing the cache size by the number of +its ways we obtain the size of a way. Dividing this number by the page size, +the number of total cache colors is found. So for example an Arm Cortex-A53 +with a 16-ways associative 1 MiB LLC can isolate up to 16 colors when pages are +4 KiB in size. + +Effective colors assignment +########################### + +When assigning colors, if one wants to avoid cache interference between two +domains, different colors needs to be used for their memory. + +Command line parameters +*********************** + +Specific documentation is available at `docs/misc/xen-command-line.pandoc`. + ++----------------------+-------------------------------+ +| **Parameter** | **Description** | ++----------------------+-------------------------------+ +| ``llc-coloring`` | Enable coloring at runtime | ++----------------------+-------------------------------+ +| ``llc-size`` | Set the LLC size | ++----------------------+-------------------------------+ +| ``llc-nr-ways`` | Set the LLC number of ways | ++----------------------+-------------------------------+ + +Auto-probing of LLC specs +######################### + +LLC size and number of ways are probed automatically by default. + +LLC specs can be manually set via the above command line parameters. This +bypasses any auto-probing and it's used to overcome failing situations, such as +flawed probing logic, or for debugging/testing purposes. diff --git a/docs/misc/xen-command-line.pandoc b/docs/misc/xen-command-line.pandoc index 293dbc1a95..ca105ec559 100644 --- a/docs/misc/xen-command-line.pandoc +++ b/docs/misc/xen-command-line.pandoc @@ -1708,6 +1708,45 @@ This option is intended for debugging purposes only. Enable MSR_DEBUGCTL.LBR in hypervisor context to be able to dump the Last Interrupt/Exception To/From record with other registers. +### llc-coloring (arm64) +> `= ` + +> Default: `false` + +Flag to enable or disable LLC coloring support at runtime. This option is +available only when `CONFIG_LLC_COLORING` is enabled. See the general +cache coloring documentation for more info. + +### llc-nr-ways (arm64) +> `= ` + +> Default: `Obtained from hardware` + +Specify the number of ways of the Last Level Cache. This option is available +only when `CONFIG_LLC_COLORING` is enabled. LLC size and number of ways are used +to find the number of supported cache colors. By default the value is +automatically computed by probing the hardware, but in case of specific needs, +it can be manually set. Those include failing probing and debugging/testing +purposes so that it's possible to emulate platforms with different number of +supported colors. If set, also "llc-size" must be set, otherwise the default +will be used. Note that using both options implies "llc-coloring=on" unless an +earlier "llc-coloring=off" is there. + +### llc-size (arm64) +> `= ` + +> Default: `Obtained from hardware` + +Specify the size of the Last Level Cache. This option is available only when +`CONFIG_LLC_COLORING` is enabled. LLC size and number of ways are used to find +the number of supported cache colors. By default the value is automatically +computed by probing the hardware, but in case of specific needs, it can be +manually set. Those include failing probing and debugging/testing purposes so +that it's possible to emulate platforms with different number of supported +colors. If set, also "llc-nr-ways" must be set, otherwise the default will be +used. Note that using both options implies "llc-coloring=on" unless an +earlier "llc-coloring=off" is there. + ### lock-depth-size > `= ` diff --git a/xen/common/Kconfig b/xen/common/Kconfig index 90268d9249..b4ec6893be 100644 --- a/xen/common/Kconfig +++ b/xen/common/Kconfig @@ -71,6 +71,9 @@ config HAS_IOPORTS config HAS_KEXEC bool +config HAS_LLC_COLORING + bool + config HAS_PIRQ bool @@ -516,4 +519,22 @@ config TRACEBUFFER to be collected at run time for debugging or performance analysis. Memory and execution overhead when not active is minimal. +config LLC_COLORING + bool "Last Level Cache (LLC) coloring" if EXPERT + depends on HAS_LLC_COLORING + +config LLC_COLORS_ORDER + int "Maximum number of LLC colors (base-2 exponent)" + range 1 10 + default 7 + depends on LLC_COLORING + help + Controls the build-time size of various arrays associated with LLC + coloring. The value is a base-2 exponent. Refer to cache coloring + documentation for how to compute the number of colors supported by the + platform. This is only an upper bound. The runtime value is autocomputed + or manually set via cmdline parameters. + The default value corresponds to an 8 MiB 16-ways LLC, which should be + more than what's needed in the general case. + endmenu diff --git a/xen/common/Makefile b/xen/common/Makefile index b279b09bfb..cba3b32733 100644 --- a/xen/common/Makefile +++ b/xen/common/Makefile @@ -25,6 +25,7 @@ obj-y += keyhandler.o obj-$(CONFIG_KEXEC) += kexec.o obj-$(CONFIG_KEXEC) += kimage.o obj-$(CONFIG_LIVEPATCH) += livepatch.o livepatch_elf.o +obj-$(CONFIG_LLC_COLORING) += llc-coloring.o obj-$(CONFIG_MEM_ACCESS) += mem_access.o obj-y += memory.o obj-y += multicall.o diff --git a/xen/common/keyhandler.c b/xen/common/keyhandler.c index 6da291b34e..6ea54838d4 100644 --- a/xen/common/keyhandler.c +++ b/xen/common/keyhandler.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -304,6 +305,8 @@ static void cf_check dump_domains(unsigned char key) arch_dump_domain_info(d); + domain_dump_llc_colors(d); + rangeset_domain_printk(d); dump_pageframe_info(d); diff --git a/xen/common/llc-coloring.c b/xen/common/llc-coloring.c new file mode 100644 index 0000000000..335a907296 --- /dev/null +++ b/xen/common/llc-coloring.c @@ -0,0 +1,124 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Last Level Cache (LLC) coloring common code + * + * Copyright (C) 2024, Advanced Micro Devices, Inc. + * Copyright (C) 2024, Minerva Systems SRL + */ +#include +#include +#include + +#define NR_LLC_COLORS (1U << CONFIG_LLC_COLORS_ORDER) + +/* + * -1: not specified (disabled unless llc-size and llc-nr-ways present) + * 0: explicitly disabled through cmdline + * 1: explicitly enabled through cmdline + */ +static int8_t __initdata opt_llc_coloring = -1; +boolean_param("llc-coloring", opt_llc_coloring); + +static bool __ro_after_init llc_coloring_enabled; + +static unsigned int __initdata llc_size; +size_param("llc-size", llc_size); +static unsigned int __initdata llc_nr_ways; +integer_param("llc-nr-ways", llc_nr_ways); +/* Number of colors available in the LLC */ +static unsigned int __ro_after_init max_nr_colors; + +static void print_colors(const unsigned int colors[], unsigned int num_colors) +{ + unsigned int i; + + printk("{ "); + for ( i = 0; i < num_colors; i++ ) + { + unsigned int start = colors[i], end = start; + + printk("%u", start); + + for ( ; i < num_colors - 1 && end + 1 == colors[i + 1]; i++, end++ ) + ; + + if ( start != end ) + printk("-%u", end); + + if ( i < num_colors - 1 ) + printk(", "); + } + printk(" }\n"); +} + +void __init llc_coloring_init(void) +{ + unsigned int way_size; + + llc_coloring_enabled = (opt_llc_coloring >= 1); + if ( (opt_llc_coloring != 0) && llc_size && llc_nr_ways ) + { + llc_coloring_enabled = true; + way_size = llc_size / llc_nr_ways; + } + else if ( !llc_coloring_enabled ) + return; + else + { + way_size = get_llc_way_size(); + if ( !way_size ) + panic("LLC probing failed and 'llc-size' or 'llc-nr-ways' missing\n"); + } + + if ( way_size & ~PAGE_MASK ) + panic("LLC way size must be a multiple of PAGE_SIZE\n"); + + /* + * The maximum number of colors must be a power of 2 in order to correctly + * map them to bits of an address. + */ + max_nr_colors = way_size >> PAGE_SHIFT; + + if ( max_nr_colors & (max_nr_colors - 1) ) + panic("Number of LLC colors (%u) isn't a power of 2\n", max_nr_colors); + + if ( max_nr_colors > NR_LLC_COLORS ) + { + printk(XENLOG_WARNING + "Number of LLC colors (%u) too big. Using configured max %u\n", + max_nr_colors, NR_LLC_COLORS); + max_nr_colors = NR_LLC_COLORS; + } + else if ( max_nr_colors < 2 ) + panic("Number of LLC colors %u < 2\n", max_nr_colors); + + arch_llc_coloring_init(); +} + +void dump_llc_coloring_info(void) +{ + if ( !llc_coloring_enabled ) + return; + + printk("LLC coloring info:\n"); + printk(" Number of LLC colors supported: %u\n", max_nr_colors); +} + +void domain_dump_llc_colors(const struct domain *d) +{ + if ( !llc_coloring_enabled ) + return; + + printk("%u LLC colors: ", d->num_llc_colors); + print_colors(d->llc_colors, d->num_llc_colors); +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c index 1f424333db..da505a79f6 100644 --- a/xen/common/page_alloc.c +++ b/xen/common/page_alloc.c @@ -126,6 +126,7 @@ #include #include #include +#include #include #include #include @@ -2647,6 +2648,8 @@ static void cf_check pagealloc_info(unsigned char key) } printk(" Dom heap: %lukB free\n", total << (PAGE_SHIFT-10)); + + dump_llc_coloring_info(); } static __init int cf_check pagealloc_keyhandler_init(void) diff --git a/xen/include/xen/llc-coloring.h b/xen/include/xen/llc-coloring.h new file mode 100644 index 0000000000..0acd8d0ad6 --- /dev/null +++ b/xen/include/xen/llc-coloring.h @@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Last Level Cache (LLC) coloring common header + * + * Copyright (C) 2024, Advanced Micro Devices, Inc. + * Copyright (C) 2024, Minerva Systems SRL + */ +#ifndef __XEN_LLC_COLORING_H__ +#define __XEN_LLC_COLORING_H__ + +struct domain; + +#ifdef CONFIG_LLC_COLORING +void llc_coloring_init(void); +void dump_llc_coloring_info(void); +void domain_dump_llc_colors(const struct domain *d); +#else +static inline void llc_coloring_init(void) {} +static inline void dump_llc_coloring_info(void) {} +static inline void domain_dump_llc_colors(const struct domain *d) {} +#endif + +unsigned int get_llc_way_size(void); +void arch_llc_coloring_init(void); + +#endif /* __XEN_LLC_COLORING_H__ */ + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h index 711668e028..037c83fda2 100644 --- a/xen/include/xen/sched.h +++ b/xen/include/xen/sched.h @@ -640,6 +640,11 @@ struct domain /* Holding CDF_* constant. Internal flags for domain creation. */ unsigned int cdf; + +#ifdef CONFIG_LLC_COLORING + unsigned int num_llc_colors; + const unsigned int *llc_colors; +#endif }; static inline struct page_list_head *page_to_list( From patchwork Tue Dec 17 17:06:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlo Nonato X-Patchwork-Id: 13912220 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 EDDD1E7718E for ; Tue, 17 Dec 2024 17:07:00 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.859574.1271704 (Exim 4.92) (envelope-from ) id 1tNb1x-0006Vj-Vi; Tue, 17 Dec 2024 17:06:45 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 859574.1271704; Tue, 17 Dec 2024 17:06:45 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tNb1x-0006V4-S5; Tue, 17 Dec 2024 17:06:45 +0000 Received: by outflank-mailman (input) for mailman id 859574; Tue, 17 Dec 2024 17:06:44 +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 1tNb1w-0006B0-K8 for xen-devel@lists.xenproject.org; Tue, 17 Dec 2024 17:06:44 +0000 Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [2a00:1450:4864:20::536]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 4a6bfb08-bc99-11ef-a0d6-8be0dac302b0; Tue, 17 Dec 2024 18:06:43 +0100 (CET) Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-5d3bdccba49so9612786a12.1 for ; Tue, 17 Dec 2024 09:06:43 -0800 (PST) Received: from carlo-ubuntu.home.arpa (host-95-230-250-178.business.telecomitalia.it. [95.230.250.178]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aab963910f7sm461362666b.166.2024.12.17.09.06.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Dec 2024 09:06:42 -0800 (PST) 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: 4a6bfb08-bc99-11ef-a0d6-8be0dac302b0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=minervasys-tech.20230601.gappssmtp.com; s=20230601; t=1734455203; x=1735060003; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HRiXjScTKjjwC6ubbpUmf/5GyGMeNLHHwHKs6KCqd38=; b=T6BomwBXqEi05TR8U4IufFgmeqcFjTaAmxznVFo6TrfqdNTSbgzWPOSTA2bdL0vdht O1UhGcSbrQDqH5QYwDdtoOzDmEDkd9ubZoHNeBOYh06mE0N4EC9aCFW5QwZ6iZ81LSOK qDwNll9SnPV/EzBw/BAdlmhj4IfBR/JoVQ2g0UEtPRyP2SC/sPaHLWPkQY56zbfqMpFS W5mP/LbVdB1i0znVMiH/gYLUeNKTSntNZsI/XpjcUHGz3c7yx/u0J0QTYneb1JBLOsMc WsCu3pivm1i+8yAbfPrtNK+DDglpqKiPgkXKCxKTdcfmon63RDpEWNPCQ2URTpviXt6E B85A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734455203; x=1735060003; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HRiXjScTKjjwC6ubbpUmf/5GyGMeNLHHwHKs6KCqd38=; b=Aqu26L1WFobo5SQwbT0E3gJp7jWGQ4txXE2NKjH8ILS7x3vBX0eLevf/5W7FeivwhR 6FA7XVaWy2Q15T0kpLFoqsKv+cON+0QQp1xHInZR3mFwZCRU6HE5ecAj/lFUn5n8syOI 97y0kpz2PkrIsMH3xrcuye00ikQwhWjQClIp+XZJRPW/juwKRyJW5kaYSZxdDfFO0Pjf x042tUP8Qg7qAF/9cTdB8KHFYaxKUHVpvaaST4vM1HQweIIy35hqeHUbVCJ92KnYEAns DKNMK22omE6h74V9lWX8ShqTrVK10s4OFtOpyyH4d1sQcozO7CyHZZDKhFfqMZntRxKC QF7g== X-Gm-Message-State: AOJu0YwEWQfeJEBDNnX/J5DCr9wZTZoRlqMQiiocgSkriZFdAF9McFMS nWBB2osV5dIY9GfYqcyZMF3k0aUeGNnwGMF0cMfFnsYUCiOuZS5hE3BNx15YHYqOK63K085sRRQ oew4= X-Gm-Gg: ASbGncvgMfuteaIfmTdHSIkmZw2jv9zelYjlfAQaNnHHCryH8B0u7ThBns/H9CwqtmF TL3UD8Zd7Nat48Rg1MDQll3pWt6gquxcF53//962EnA7vLhhpIdUo8i9fQiZM2dQZQCPdDbv69Z hUwDCih3xxZamvT3IpLoaT0jYnwVnjZe78F80mxBYcgYcbNRNN7Qy9MlP5Jxo8O6zxv91S3SPMW brXpQEY2GX1vY7NBA7e6YsP1BUfCqqlikv3zRpvY6KR+IcMbh+R78LI56aLNF3fn3DnegBOxjEB n9w9/k0omS5OHgWa47qIpazev4Jr3iD5jj38j4HvEE4sYyzSQB8Lix2drkw= X-Google-Smtp-Source: AGHT+IG8/f8xMijmze2k8W3XnEhE7dCYgbw/N1p0uKnCR58cGxdUxzFjMwod1mbqor/6uSqlFXmRqg== X-Received: by 2002:a17:907:2d8e:b0:aa6:a05c:b076 with SMTP id a640c23a62f3a-aabdb3be7d5mr449039966b.26.1734455202735; Tue, 17 Dec 2024 09:06:42 -0800 (PST) From: Carlo Nonato To: xen-devel@lists.xenproject.org Cc: andrea.bastoni@minervasys.tech, marco.solieri@minervasys.tech, Carlo Nonato , Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Stefano Stabellini , Bertrand Marquis , Volodymyr Babchuk Subject: [PATCH v13 02/12] xen/arm: add initial support for LLC coloring on arm64 Date: Tue, 17 Dec 2024 18:06:27 +0100 Message-ID: <20241217170637.233097-3-carlo.nonato@minervasys.tech> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241217170637.233097-1-carlo.nonato@minervasys.tech> References: <20241217170637.233097-1-carlo.nonato@minervasys.tech> MIME-Version: 1.0 LLC coloring needs to know the last level cache layout in order to make the best use of it. This can be probed by inspecting the CLIDR_EL1 register, so the Last Level is defined as the last level visible by this register. Note that this excludes system caches in some platforms. Static memory allocation and cache coloring are incompatible because static memory can't be guaranteed to use only colors assigned to the domain. Panic during DomUs creation when both are enabled. Based on original work from: Luca Miccio Signed-off-by: Carlo Nonato Signed-off-by: Marco Solieri Reviewed-by: Michal Orzel Acked-by: Jan Beulich --- v13: - no changes v12: - fixed build errors v11: - removed useless #define from processor.h v10: - moved CONFIG_NUMA check in arch/arm/Kconfig v9: - no changes v8: - no changes v7: - only minor changes v6: - get_llc_way_size() now checks for at least separate I/D caches v5: - used - instead of _ for filenames - moved static-mem check in this patch - moved dom0 colors parsing in next patch - moved color allocation and configuration in next patch - moved check_colors() in next patch - colors are now printed in short form v4: - added "llc-coloring" cmdline option for the boot-time switch - dom0 colors are now checked during domain init as for any other domain - fixed processor.h masks bit width - check for overflow in parse_color_config() - check_colors() now checks also that colors are sorted and unique --- docs/misc/cache-coloring.rst | 14 +++++ xen/arch/arm/Kconfig | 1 + xen/arch/arm/Makefile | 1 + xen/arch/arm/dom0less-build.c | 6 +++ xen/arch/arm/include/asm/processor.h | 15 ++++++ xen/arch/arm/llc-coloring.c | 79 ++++++++++++++++++++++++++++ xen/arch/arm/setup.c | 3 ++ xen/common/llc-coloring.c | 4 +- xen/include/xen/llc-coloring.h | 6 +++ 9 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 xen/arch/arm/llc-coloring.c diff --git a/docs/misc/cache-coloring.rst b/docs/misc/cache-coloring.rst index 371f21a0e7..12972dbb2c 100644 --- a/docs/misc/cache-coloring.rst +++ b/docs/misc/cache-coloring.rst @@ -113,6 +113,20 @@ Auto-probing of LLC specs LLC size and number of ways are probed automatically by default. +In the Arm implementation, this is done by inspecting the CLIDR_EL1 register. +This means that other system caches that aren't visible there are ignored. + LLC specs can be manually set via the above command line parameters. This bypasses any auto-probing and it's used to overcome failing situations, such as flawed probing logic, or for debugging/testing purposes. + +Known issues and limitations +**************************** + +"xen,static-mem" isn't supported when coloring is enabled +######################################################### + +In the domain configuration, "xen,static-mem" allows memory to be statically +allocated to the domain. This isn't possible when LLC coloring is enabled, +because that memory can't be guaranteed to use only colors assigned to the +domain. diff --git a/xen/arch/arm/Kconfig b/xen/arch/arm/Kconfig index 604aba4996..c5e7b74733 100644 --- a/xen/arch/arm/Kconfig +++ b/xen/arch/arm/Kconfig @@ -8,6 +8,7 @@ config ARM_64 depends on !ARM_32 select 64BIT select HAS_FAST_MULTIPLY + select HAS_LLC_COLORING if !NUMA config ARM def_bool y diff --git a/xen/arch/arm/Makefile b/xen/arch/arm/Makefile index e4ad1ce851..ccbfc61f88 100644 --- a/xen/arch/arm/Makefile +++ b/xen/arch/arm/Makefile @@ -35,6 +35,7 @@ obj-$(CONFIG_IOREQ_SERVER) += ioreq.o obj-y += irq.o obj-y += kernel.init.o obj-$(CONFIG_LIVEPATCH) += livepatch.o +obj-$(CONFIG_LLC_COLORING) += llc-coloring.o obj-$(CONFIG_MEM_ACCESS) += mem_access.o obj-y += mm.o obj-y += monitor.o diff --git a/xen/arch/arm/dom0less-build.c b/xen/arch/arm/dom0less-build.c index f328a044e9..d93a85434e 100644 --- a/xen/arch/arm/dom0less-build.c +++ b/xen/arch/arm/dom0less-build.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -890,7 +891,12 @@ void __init create_domUs(void) panic("No more domain IDs available\n"); if ( dt_find_property(node, "xen,static-mem", NULL) ) + { + if ( llc_coloring_enabled ) + panic("LLC coloring and static memory are incompatible\n"); + flags |= CDF_staticmem; + } if ( dt_property_read_bool(node, "direct-map") ) { diff --git a/xen/arch/arm/include/asm/processor.h b/xen/arch/arm/include/asm/processor.h index 8e02410465..60b587db69 100644 --- a/xen/arch/arm/include/asm/processor.h +++ b/xen/arch/arm/include/asm/processor.h @@ -18,6 +18,21 @@ #define CTR_IDC_SHIFT 28 #define CTR_DIC_SHIFT 29 +/* CCSIDR Current Cache Size ID Register */ +#define CCSIDR_LINESIZE_MASK _AC(0x7, UL) +#define CCSIDR_NUMSETS_SHIFT 13 +#define CCSIDR_NUMSETS_MASK _AC(0x3fff, UL) +#define CCSIDR_NUMSETS_SHIFT_FEAT_CCIDX 32 +#define CCSIDR_NUMSETS_MASK_FEAT_CCIDX _AC(0xffffff, UL) + +/* CSSELR Cache Size Selection Register */ +#define CSSELR_LEVEL_SHIFT 1 + +/* CLIDR Cache Level ID Register */ +#define CLIDR_CTYPEn_SHIFT(n) (3 * ((n) - 1)) +#define CLIDR_CTYPEn_MASK _AC(0x7, UL) +#define CLIDR_CTYPEn_LEVELS 7 + #define ICACHE_POLICY_VPIPT 0 #define ICACHE_POLICY_AIVIVT 1 #define ICACHE_POLICY_VIPT 2 diff --git a/xen/arch/arm/llc-coloring.c b/xen/arch/arm/llc-coloring.c new file mode 100644 index 0000000000..1c7b92bc45 --- /dev/null +++ b/xen/arch/arm/llc-coloring.c @@ -0,0 +1,79 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Last Level Cache (LLC) coloring support for ARM + * + * Copyright (C) 2024, Advanced Micro Devices, Inc. + * Copyright (C) 2024, Minerva Systems SRL + */ +#include +#include + +#include +#include +#include + +/* Return the LLC way size by probing the hardware */ +unsigned int __init get_llc_way_size(void) +{ + register_t ccsidr_el1; + register_t clidr_el1 = READ_SYSREG(CLIDR_EL1); + register_t csselr_el1 = READ_SYSREG(CSSELR_EL1); + register_t id_aa64mmfr2_el1 = READ_SYSREG(ID_AA64MMFR2_EL1); + uint32_t ccsidr_numsets_shift = CCSIDR_NUMSETS_SHIFT; + uint32_t ccsidr_numsets_mask = CCSIDR_NUMSETS_MASK; + unsigned int n, line_size, num_sets; + + for ( n = CLIDR_CTYPEn_LEVELS; n != 0; n-- ) + { + uint8_t ctype_n = (clidr_el1 >> CLIDR_CTYPEn_SHIFT(n)) & + CLIDR_CTYPEn_MASK; + + /* Unified cache (see Arm ARM DDI 0487J.a D19.2.27) */ + if ( ctype_n == 0b100 ) + break; + } + + if ( n == 0 ) + return 0; + + WRITE_SYSREG((n - 1) << CSSELR_LEVEL_SHIFT, CSSELR_EL1); + isb(); + + ccsidr_el1 = READ_SYSREG(CCSIDR_EL1); + + /* Arm ARM: (Log2(Number of bytes in cache line)) - 4 */ + line_size = 1U << ((ccsidr_el1 & CCSIDR_LINESIZE_MASK) + 4); + + /* If FEAT_CCIDX is enabled, CCSIDR_EL1 has a different bit layout */ + if ( (id_aa64mmfr2_el1 >> ID_AA64MMFR2_CCIDX_SHIFT) & 0x7 ) + { + ccsidr_numsets_shift = CCSIDR_NUMSETS_SHIFT_FEAT_CCIDX; + ccsidr_numsets_mask = CCSIDR_NUMSETS_MASK_FEAT_CCIDX; + } + + /* Arm ARM: (Number of sets in cache) - 1 */ + num_sets = ((ccsidr_el1 >> ccsidr_numsets_shift) & ccsidr_numsets_mask) + 1; + + printk(XENLOG_INFO "LLC found: L%u (line size: %u bytes, sets num: %u)\n", + n, line_size, num_sets); + + /* Restore value in CSSELR_EL1 */ + WRITE_SYSREG(csselr_el1, CSSELR_EL1); + isb(); + + return line_size * num_sets; +} + +void __init arch_llc_coloring_init(void) +{ +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/arch/arm/setup.c b/xen/arch/arm/setup.c index 545702d8a3..5f1993ffed 100644 --- a/xen/arch/arm/setup.c +++ b/xen/arch/arm/setup.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -334,6 +335,8 @@ void asmlinkage __init start_xen(unsigned long fdt_paddr) printk("Command line: %s\n", cmdline); cmdline_parse(cmdline); + llc_coloring_init(); + setup_mm(); vm_init(); diff --git a/xen/common/llc-coloring.c b/xen/common/llc-coloring.c index 335a907296..b034c0169c 100644 --- a/xen/common/llc-coloring.c +++ b/xen/common/llc-coloring.c @@ -8,6 +8,8 @@ #include #include #include +#include +#include #define NR_LLC_COLORS (1U << CONFIG_LLC_COLORS_ORDER) @@ -19,7 +21,7 @@ static int8_t __initdata opt_llc_coloring = -1; boolean_param("llc-coloring", opt_llc_coloring); -static bool __ro_after_init llc_coloring_enabled; +bool __ro_after_init llc_coloring_enabled; static unsigned int __initdata llc_size; size_param("llc-size", llc_size); diff --git a/xen/include/xen/llc-coloring.h b/xen/include/xen/llc-coloring.h index 0acd8d0ad6..a3ebb17186 100644 --- a/xen/include/xen/llc-coloring.h +++ b/xen/include/xen/llc-coloring.h @@ -8,13 +8,19 @@ #ifndef __XEN_LLC_COLORING_H__ #define __XEN_LLC_COLORING_H__ +#include + struct domain; #ifdef CONFIG_LLC_COLORING +extern bool llc_coloring_enabled; + void llc_coloring_init(void); void dump_llc_coloring_info(void); void domain_dump_llc_colors(const struct domain *d); #else +#define llc_coloring_enabled false + static inline void llc_coloring_init(void) {} static inline void dump_llc_coloring_info(void) {} static inline void domain_dump_llc_colors(const struct domain *d) {} From patchwork Tue Dec 17 17:06:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlo Nonato X-Patchwork-Id: 13912219 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 D9395E77184 for ; Tue, 17 Dec 2024 17:06:59 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.859575.1271723 (Exim 4.92) (envelope-from ) id 1tNb20-0006wO-Ea; Tue, 17 Dec 2024 17:06:48 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 859575.1271723; Tue, 17 Dec 2024 17:06:48 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tNb20-0006uz-8u; Tue, 17 Dec 2024 17:06:48 +0000 Received: by outflank-mailman (input) for mailman id 859575; Tue, 17 Dec 2024 17:06:46 +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 1tNb1y-0006gw-Km for xen-devel@lists.xenproject.org; Tue, 17 Dec 2024 17:06:46 +0000 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [2a00:1450:4864:20::631]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 4ae9e4bd-bc99-11ef-99a3-01e77a169b0f; Tue, 17 Dec 2024 18:06:44 +0100 (CET) Received: by mail-ej1-x631.google.com with SMTP id a640c23a62f3a-aa6c0d1833eso984010766b.1 for ; Tue, 17 Dec 2024 09:06:44 -0800 (PST) Received: from carlo-ubuntu.home.arpa (host-95-230-250-178.business.telecomitalia.it. [95.230.250.178]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aab963910f7sm461362666b.166.2024.12.17.09.06.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Dec 2024 09:06:43 -0800 (PST) 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: 4ae9e4bd-bc99-11ef-99a3-01e77a169b0f DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=minervasys-tech.20230601.gappssmtp.com; s=20230601; t=1734455204; x=1735060004; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=c1jVlKhA9VJnBdspkJfeB+eigkbC7mCy9c7Bxblk1nU=; b=jrWjG9Y6LGd8qEhCiJ+R+uFbnguX/MhHs01eFS4hhCrYrBtHM/tFj+WqXa80EGIpPL uK6Tvu+q29WRPQ/qYzvalw/nAUF2Px2OZq9Qyo+3MBG6ZaxIieiphzPbfypNgS46G5yH 1nv4xqOaSQ623+QarUHoLxNtzuHkpIIbVYFQs6BvTWP2nEecx+p4q/YyRY4TU9k4uODa OuTdrNBb72YsVZWMsfHByD5MULcLcwgMKAxtC5qpHBtEB517rRHEoIC5M0Zx5eqoI3gw aEg4UbyOaL+QKI8Lgth9lPU9+IReXTZYa2bGfOwbqWIY3p4xob+n6E7SxHHWmL0iOW6B MCMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734455204; x=1735060004; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=c1jVlKhA9VJnBdspkJfeB+eigkbC7mCy9c7Bxblk1nU=; b=p9VpX0FN9ChWUsPPMvFtlZ9sTjlwrMWp6S1oMGqd1oACT/20W+SDOjzF0rSYCzrZa6 ZcFnhmcC0k2eMDmdJ92ZG0MoV5mDu8HElCj3/LvJH0PCeZRO3A9baLn3aA+HL7jqE1MB UkexKsD0mfkuPA8IRwaslaacpVsirWUWY3HUP5Qe4Zt9vtqhZlUgqgMCJHSTX4J7A2oA lX8PhbC48jgMNg/I4nCKca2ir/HYLdRWQ7WDjvXYjpedMfsQBCqmSm5b5LjvkzDjud3L ZzHF0W1cGKQzcWt9Z7exvK7kCrKwosll/7WuPsmHHZihIlsrQfq9XhxHMUKWHIRaH/Qg siUA== X-Gm-Message-State: AOJu0Ywb3SDCicjxQr5v6/y4/NMuzD2nHzj2E80jXjoBYz1XXGtQT28+ JY6lZiFIidkdGwOAwew+YSjGH3+CwQTffIZ7W10ryCCIZfoWoyUBjedcxgjBg9V6oQ31WBpH5bK BFG4= X-Gm-Gg: ASbGncu7rmHAyzoLXVDllR0WY932yKnmZ90G8yhehy2NUYsR5Z33uKWwEjLbIw2cBqn 78eC+tXBwvDUXPp2/jTc0DESLOop+2rJBLi1uPDaeAhEu8jYNR+bQGvdTKufNH+g4KOG/TkDUjL yyc/vRUcLLgHwMVQKROHkIBdSNQewhIqOvk8crdDzdxaBZ+HwElSrTGd4AsN+gRoATQMdk4Bree w25JsumxrEb8RUat36TIyN/FigpW/YiKRXi0ATHmn8qvKp15LcadN4OxqqkcqTxKCgbiI0xNA1n kqnaqjfbZKP2UyNc6xKqwdBUvijR8XB0sJsuACyeNu/aYtx/WalGQLB31vc= X-Google-Smtp-Source: AGHT+IHv8NfUVs+Q5cfXVQYQIcawUaVEn8Uu8aJJVoV3/NupeHNzvX8VUhUn2BY81HDLnv9Q63id4g== X-Received: by 2002:a17:906:1313:b0:aab:dc3e:1c84 with SMTP id a640c23a62f3a-aabdc3e1f33mr396402066b.17.1734455203787; Tue, 17 Dec 2024 09:06:43 -0800 (PST) From: Carlo Nonato To: xen-devel@lists.xenproject.org Cc: andrea.bastoni@minervasys.tech, marco.solieri@minervasys.tech, Carlo Nonato , Stefano Stabellini , Julien Grall , Bertrand Marquis , Michal Orzel , Volodymyr Babchuk Subject: [PATCH v13 03/12] xen/arm: permit non direct-mapped Dom0 construction Date: Tue, 17 Dec 2024 18:06:28 +0100 Message-ID: <20241217170637.233097-4-carlo.nonato@minervasys.tech> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241217170637.233097-1-carlo.nonato@minervasys.tech> References: <20241217170637.233097-1-carlo.nonato@minervasys.tech> MIME-Version: 1.0 Cache coloring requires Dom0 not to be direct-mapped because of its non contiguous mapping nature, so allocate_memory() is needed in this case. 8d2c3ab18cc1 ("arm/dom0less: put dom0less feature code in a separate module") moved allocate_memory() in dom0less_build.c. In order to use it in Dom0 construction bring it back to domain_build.c and declare it in domain_build.h. Adapt the implementation of allocate_memory() so that it uses the host layout when called on the hwdom, via find_unallocated_memory(). Since gnttab information are needed in the process, move find_gnttab_region() before allocate_memory() in construct_dom0(). Introduce add_hwdom_free_regions() callback to add hwdom banks in descending order. Signed-off-by: Carlo Nonato Reviewed-by: Michal Orzel --- v13: - removed ASSERT(llc_coloring_enabled) from allocate_memory() - mentioned the reordering of find_gnttab_region() in the commit msg - fixed gnttab bank size assignment in allocate_memory() v12: - used the new generic find_unallocated_memory() - added add_hwdom_free_regions() callback v11: - GUEST_RAM_BANKS instead of hardcoding the number of banks in allocate_memory() - hwdom_ext_regions -> hwdom_free_mem in allocate_memory() - added a comment in allocate_memory() when skipping small banks v10: - fixed a compilation bug that happened when dom0less support was disabled v9: - no changes v8: - patch adapted to new changes to allocate_memory() v7: - allocate_memory() now uses the host layout when called on the hwdom v6: - new patch --- xen/arch/arm/dom0less-build.c | 44 ------- xen/arch/arm/domain_build.c | 156 +++++++++++++++++++++++- xen/arch/arm/include/asm/domain_build.h | 1 + 3 files changed, 153 insertions(+), 48 deletions(-) diff --git a/xen/arch/arm/dom0less-build.c b/xen/arch/arm/dom0less-build.c index d93a85434e..67b1503647 100644 --- a/xen/arch/arm/dom0less-build.c +++ b/xen/arch/arm/dom0less-build.c @@ -49,50 +49,6 @@ bool __init is_dom0less_mode(void) return ( !dom0found && domUfound ); } -static void __init allocate_memory(struct domain *d, struct kernel_info *kinfo) -{ - struct membanks *mem = kernel_info_get_mem(kinfo); - unsigned int i; - paddr_t bank_size; - - printk(XENLOG_INFO "Allocating mappings totalling %ldMB for %pd:\n", - /* Don't want format this as PRIpaddr (16 digit hex) */ - (unsigned long)(kinfo->unassigned_mem >> 20), d); - - mem->nr_banks = 0; - bank_size = MIN(GUEST_RAM0_SIZE, kinfo->unassigned_mem); - if ( !allocate_bank_memory(kinfo, gaddr_to_gfn(GUEST_RAM0_BASE), - bank_size) ) - goto fail; - - bank_size = MIN(GUEST_RAM1_SIZE, kinfo->unassigned_mem); - if ( !allocate_bank_memory(kinfo, gaddr_to_gfn(GUEST_RAM1_BASE), - bank_size) ) - goto fail; - - if ( kinfo->unassigned_mem ) - goto fail; - - for( i = 0; i < mem->nr_banks; i++ ) - { - printk(XENLOG_INFO "%pd BANK[%d] %#"PRIpaddr"-%#"PRIpaddr" (%ldMB)\n", - d, - i, - mem->bank[i].start, - mem->bank[i].start + mem->bank[i].size, - /* Don't want format this as PRIpaddr (16 digit hex) */ - (unsigned long)(mem->bank[i].size >> 20)); - } - - return; - -fail: - panic("Failed to allocate requested domain memory." - /* Don't want format this as PRIpaddr (16 digit hex) */ - " %ldKB unallocated. Fix the VMs configurations.\n", - (unsigned long)kinfo->unassigned_mem >> 10); -} - #ifdef CONFIG_VGICV2 static int __init make_gicv2_domU_node(struct kernel_info *kinfo) { diff --git a/xen/arch/arm/domain_build.c b/xen/arch/arm/domain_build.c index adf26f2778..17cc76b6f2 100644 --- a/xen/arch/arm/domain_build.c +++ b/xen/arch/arm/domain_build.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -416,7 +417,6 @@ static void __init allocate_memory_11(struct domain *d, } } -#ifdef CONFIG_DOM0LESS_BOOT bool __init allocate_domheap_memory(struct domain *d, paddr_t tot_size, alloc_domheap_mem_cb cb, void *extra) { @@ -508,7 +508,6 @@ bool __init allocate_bank_memory(struct kernel_info *kinfo, gfn_t sgfn, return true; } -#endif /* * When PCI passthrough is available we want to keep the @@ -900,6 +899,52 @@ int __init add_ext_regions(unsigned long s_gfn, unsigned long e_gfn, return 0; } +static int __init add_hwdom_free_regions(unsigned long s_gfn, + unsigned long e_gfn, void *data) +{ + struct membanks *free_regions = data; + paddr_t start, size; + paddr_t s = pfn_to_paddr(s_gfn); + paddr_t e = pfn_to_paddr(e_gfn); + unsigned int i, j; + + if ( free_regions->nr_banks >= free_regions->max_banks ) + return 0; + + /* + * Both start and size of the free region should be 2MB aligned to + * potentially allow superpage mapping. + */ + start = (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 += 1; + size = (e - start) & ~(SZ_2M - 1); + + /* Find the insert position (descending order). */ + for ( i = 0; i < free_regions->nr_banks ; i++ ) + if ( size > free_regions->bank[i].size ) + break; + + /* Move the other banks to make space. */ + for ( j = free_regions->nr_banks; j > i ; j-- ) + { + free_regions->bank[j].start = free_regions->bank[j - 1].start; + free_regions->bank[j].size = free_regions->bank[j - 1].size; + } + + free_regions->bank[i].start = start; + free_regions->bank[i].size = size; + free_regions->nr_banks++; + + return 0; +} + /* * Find unused regions of Host address space which can be exposed to domain * using the host memory layout. In order to calculate regions we exclude every @@ -977,6 +1022,106 @@ out: return res; } +void __init allocate_memory(struct domain *d, struct kernel_info *kinfo) +{ + struct membanks *mem = kernel_info_get_mem(kinfo); + unsigned int i, nr_banks = GUEST_RAM_BANKS; + struct membanks *hwdom_free_mem = NULL; + + printk(XENLOG_INFO "Allocating mappings totalling %ldMB for %pd:\n", + /* Don't want format this as PRIpaddr (16 digit hex) */ + (unsigned long)(kinfo->unassigned_mem >> 20), d); + + mem->nr_banks = 0; + /* + * Use host memory layout for hwdom. Only case for this is when LLC coloring + * is enabled. + */ + if ( is_hardware_domain(d) ) + { + struct membanks *gnttab = xzalloc_flex_struct(struct membanks, bank, 1); + /* + * Exclude the following regions: + * 1) Remove reserved memory + * 2) Grant table assigned to hwdom + */ + const struct membanks *mem_banks[] = { + bootinfo_get_reserved_mem(), + gnttab, + }; + + if ( !gnttab ) + goto fail; + + gnttab->nr_banks = 1; + gnttab->bank[0].start = kinfo->gnttab_start; + gnttab->bank[0].size = kinfo->gnttab_size; + + hwdom_free_mem = xzalloc_flex_struct(struct membanks, bank, + NR_MEM_BANKS); + if ( !hwdom_free_mem ) + goto fail; + + hwdom_free_mem->max_banks = NR_MEM_BANKS; + + if ( find_unallocated_memory(kinfo, mem_banks, ARRAY_SIZE(mem_banks), + hwdom_free_mem, add_hwdom_free_regions) ) + goto fail; + + nr_banks = hwdom_free_mem->nr_banks; + xfree(gnttab); + } + + for ( i = 0; kinfo->unassigned_mem > 0 && nr_banks > 0; i++, nr_banks-- ) + { + paddr_t bank_start, bank_size; + + if ( is_hardware_domain(d) ) + { + bank_start = hwdom_free_mem->bank[i].start; + bank_size = hwdom_free_mem->bank[i].size; + } + else + { + const uint64_t bankbase[] = GUEST_RAM_BANK_BASES; + const uint64_t banksize[] = GUEST_RAM_BANK_SIZES; + + if ( i >= GUEST_RAM_BANKS ) + goto fail; + + bank_start = bankbase[i]; + bank_size = banksize[i]; + } + + bank_size = MIN(bank_size, kinfo->unassigned_mem); + if ( !allocate_bank_memory(kinfo, gaddr_to_gfn(bank_start), bank_size) ) + goto fail; + } + + if ( kinfo->unassigned_mem ) + goto fail; + + for( i = 0; i < mem->nr_banks; i++ ) + { + printk(XENLOG_INFO "%pd BANK[%d] %#"PRIpaddr"-%#"PRIpaddr" (%ldMB)\n", + d, + i, + mem->bank[i].start, + mem->bank[i].start + mem->bank[i].size, + /* Don't want format this as PRIpaddr (16 digit hex) */ + (unsigned long)(mem->bank[i].size >> 20)); + } + + xfree(hwdom_free_mem); + return; + + fail: + panic("Failed to allocate requested domain memory." + /* Don't want format this as PRIpaddr (16 digit hex) */ + " %ldKB unallocated. Fix the VMs configurations.\n", + (unsigned long)kinfo->unassigned_mem >> 10); +} + static int __init handle_pci_range(const struct dt_device_node *dev, uint64_t addr, uint64_t len, void *data) { @@ -1235,7 +1380,7 @@ int __init make_hypervisor_node(struct domain *d, ext_regions->max_banks = NR_MEM_BANKS; - if ( is_domain_direct_mapped(d) ) + if ( domain_use_host_layout(d) ) { if ( !is_iommu_enabled(d) ) res = find_host_extended_regions(kinfo, ext_regions); @@ -2164,8 +2309,11 @@ static int __init construct_dom0(struct domain *d) /* type must be set before allocate_memory */ d->arch.type = kinfo.type; #endif - allocate_memory_11(d, &kinfo); find_gnttab_region(d, &kinfo); + if ( is_domain_direct_mapped(d) ) + allocate_memory_11(d, &kinfo); + else + allocate_memory(d, &kinfo); rc = process_shm_chosen(d, &kinfo); if ( rc < 0 ) diff --git a/xen/arch/arm/include/asm/domain_build.h b/xen/arch/arm/include/asm/domain_build.h index e712afbc7f..5d77af2e8b 100644 --- a/xen/arch/arm/include/asm/domain_build.h +++ b/xen/arch/arm/include/asm/domain_build.h @@ -11,6 +11,7 @@ bool allocate_domheap_memory(struct domain *d, paddr_t tot_size, alloc_domheap_mem_cb cb, void *extra); bool allocate_bank_memory(struct kernel_info *kinfo, gfn_t sgfn, paddr_t tot_size); +void allocate_memory(struct domain *d, struct kernel_info *kinfo); int construct_domain(struct domain *d, struct kernel_info *kinfo); int domain_fdt_begin_node(void *fdt, const char *name, uint64_t unit); int make_chosen_node(const struct kernel_info *kinfo); From patchwork Tue Dec 17 17:06:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlo Nonato X-Patchwork-Id: 13912223 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 AE7CAE7718D for ; Tue, 17 Dec 2024 17:07:00 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.859576.1271718 (Exim 4.92) (envelope-from ) id 1tNb20-0006tX-5w; Tue, 17 Dec 2024 17:06:48 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 859576.1271718; Tue, 17 Dec 2024 17:06:48 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tNb20-0006tK-2f; Tue, 17 Dec 2024 17:06:48 +0000 Received: by outflank-mailman (input) for mailman id 859576; Tue, 17 Dec 2024 17:06:46 +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 1tNb1y-0006B0-LY for xen-devel@lists.xenproject.org; Tue, 17 Dec 2024 17:06:46 +0000 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [2a00:1450:4864:20::532]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 4bbe35a4-bc99-11ef-a0d6-8be0dac302b0; Tue, 17 Dec 2024 18:06:45 +0100 (CET) Received: by mail-ed1-x532.google.com with SMTP id 4fb4d7f45d1cf-5d3e829ff44so11936217a12.0 for ; Tue, 17 Dec 2024 09:06:45 -0800 (PST) Received: from carlo-ubuntu.home.arpa (host-95-230-250-178.business.telecomitalia.it. [95.230.250.178]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aab963910f7sm461362666b.166.2024.12.17.09.06.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Dec 2024 09:06:44 -0800 (PST) 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: 4bbe35a4-bc99-11ef-a0d6-8be0dac302b0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=minervasys-tech.20230601.gappssmtp.com; s=20230601; t=1734455205; x=1735060005; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=CtIKFLoM6ff7CtjdbQpOl+Sf+qsR3Tbe9ZtV5iZhVh4=; b=QYBhKQslzQbT3f48tSet/eHvKCD6uB/zXVywOC0YX6MxA32OditFSdLMQlFPM5lnkR Yyf58fYotW9wJP3boy7eaGgWpXzFIkJYNhxUDzH1Ef4wrlbR0Ehj2lfKS3Gir5OqFWH4 Oj45I3KQNOtScup7teN9b3CEQURsOHk1HJtQ34SA263+jAtl9q1hcKOypHPSfVSfEKx/ Q7Ug9XYrhGyzx9rSMUV/Uavgq4XjJSul1oKx3/iCERzTWV1GIsRstQpkiB2/f7UYQesQ JrcnmQ/QB3ybxqvUbCVPwjX7CiJhcHQctY6SFzyhRH2DfCQvcIHvMJgFqOp7SAFW1VT4 NFcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734455205; x=1735060005; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=CtIKFLoM6ff7CtjdbQpOl+Sf+qsR3Tbe9ZtV5iZhVh4=; b=w9lWvvJCEBM1Q/+udjUmWpDElg7qpZ3q3K3U+7/vLaCfWmPRcmHDdclNCQoimbnQDM CEHVfHlb2JkmNA5cH3g5pq5XdR3Y/I+HwvklL4c4ZEEco3iANGhnsJ+FrXy7vGuT8DAj u3/10ZDkfLqZn28LBHe1+Qrq34zivl5HYHZF21lLmHlMhLWVT7WnvSBOBA+ky+V166nA surgoxGQnUcXEgvRdyBNhbuJNwSbToCwTsDY6zo8OpvuJsmSS29B+FVUBu0lH/L3Gre1 ycsTPHgoJXnos5+LyeZc05z5x/AALbWSJXVVY2up/hCCfzIXJEpBH9bRy0U1trdxG4X2 NksA== X-Gm-Message-State: AOJu0YyGrhR25XzqMoOLR+PTbe3Z4Wjd2A64EkuGnC18v746e0EHPP4z L3HWm/DyiWL1Bn4R/b4Z6BaVQHt6SLhQlrlLa7qQEGqTO1IofQBHqxuFVWA1FY8mgq39zEDlAvv gYOc= X-Gm-Gg: ASbGnctJdcTKD2Ta8r1T3ncwlRsqOpbhFA1gjXkBkBHxJvNkby7zbIIrZJB4xqcPBQ4 kyhI2CUCCz+Hk0t75LAhY2bvuPYaLwbOC95NYIlVxYZholgLnkPfPocRxkrqSR/R8N7bV77rkL3 lpfvBV8M+ZpJPzQcubkjewpUGSZ3pbSPGV0aBz3mr3n7QXaQh8NzWA1Sa5K4346N7pegg5oNS21 GsnoeFTduBIKKpyufa2vRT388pK+9OkrlrdPZZREy64fxNYm0cu8USuyU5OCCk2rdVemdcx5OlT vaN3iUD5/ODZxwvcb1V6/Pxik3BJ1Df44KXiKin9lUP4lxanyEWFnq0yXqg= X-Google-Smtp-Source: AGHT+IEsFLfarCLyU2p9IiihuOgyXSgr1SXUa4ThtBEQ+us37TZQxDMvYFBHTgf3inP8xmb9YfN9hA== X-Received: by 2002:a17:907:a089:b0:aa6:8b4a:4695 with SMTP id a640c23a62f3a-aabdcb8265fmr398063866b.31.1734455204824; Tue, 17 Dec 2024 09:06:44 -0800 (PST) From: Carlo Nonato To: xen-devel@lists.xenproject.org Cc: andrea.bastoni@minervasys.tech, marco.solieri@minervasys.tech, Carlo Nonato , Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Stefano Stabellini , Bertrand Marquis , Volodymyr Babchuk Subject: [PATCH v13 04/12] xen/arm: add Dom0 cache coloring support Date: Tue, 17 Dec 2024 18:06:29 +0100 Message-ID: <20241217170637.233097-5-carlo.nonato@minervasys.tech> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241217170637.233097-1-carlo.nonato@minervasys.tech> References: <20241217170637.233097-1-carlo.nonato@minervasys.tech> MIME-Version: 1.0 Add a command line parameter to allow the user to set the coloring configuration for Dom0. A common configuration syntax for cache colors is introduced and documented. Take the opportunity to also add: - default configuration notion. - function to check well-formed configurations. Direct mapping Dom0 isn't possible when coloring is enabled, so CDF_directmap flag is removed when creating it. Based on original work from: Luca Miccio Signed-off-by: Carlo Nonato Signed-off-by: Marco Solieri Reviewed-by: Jan Beulich Reviewed-by: Michal Orzel --- v13: - no changes v12: - no changes v11: - minor changes v10: - fixed array type for colors parameter in check_colors() v9: - moved domain_llc_coloring_free() in next patch cause it's never used for dom0 v8: - added bound check on dom0_num_colors - default colors array set just once v7: - parse_color_config() doesn't accept leading/trailing commas anymore - removed alloc_colors() helper v6: - moved domain_llc_coloring_free() in this patch - removed domain_alloc_colors() in favor of a more explicit allocation - parse_color_config() now accepts the size of the array to be filled - allocate_memory() moved in another patch v5: - Carlo Nonato as the new author - moved dom0 colors parsing (parse_colors()) in this patch - added dom0_set_llc_colors() to set dom0 colors after creation - moved color allocation and checking in this patch - error handling when allocating color arrays - FIXME: copy pasted allocate_memory() cause it got moved v4: - dom0 colors are dynamically allocated as for any other domain (colors are duplicated in dom0_colors and in the new array, but logic is simpler) --- docs/misc/cache-coloring.rst | 29 ++++++++ docs/misc/xen-command-line.pandoc | 9 +++ xen/arch/arm/domain_build.c | 9 ++- xen/common/llc-coloring.c | 120 +++++++++++++++++++++++++++++- xen/include/xen/llc-coloring.h | 1 + 5 files changed, 166 insertions(+), 2 deletions(-) diff --git a/docs/misc/cache-coloring.rst b/docs/misc/cache-coloring.rst index 12972dbb2c..7b47d0ed92 100644 --- a/docs/misc/cache-coloring.rst +++ b/docs/misc/cache-coloring.rst @@ -107,6 +107,35 @@ Specific documentation is available at `docs/misc/xen-command-line.pandoc`. +----------------------+-------------------------------+ | ``llc-nr-ways`` | Set the LLC number of ways | +----------------------+-------------------------------+ +| ``dom0-llc-colors`` | Dom0 color configuration | ++----------------------+-------------------------------+ + +Colors selection format +*********************** + +Regardless of the memory pool that has to be colored (Xen, Dom0/DomUs), +the color selection can be expressed using the same syntax. In particular a +comma-separated list of colors or ranges of colors is used. +Ranges are hyphen-separated intervals (such as `0-4`) and are inclusive on both +sides. + +Note that: + +- no spaces are allowed between values. +- no overlapping ranges or duplicated colors are allowed. +- values must be written in ascending order. + +Examples: + ++-------------------+-----------------------------+ +| **Configuration** | **Actual selection** | ++-------------------+-----------------------------+ +| 1-2,5-8 | [1, 2, 5, 6, 7, 8] | ++-------------------+-----------------------------+ +| 4-8,10,11,12 | [4, 5, 6, 7, 8, 10, 11, 12] | ++-------------------+-----------------------------+ +| 0 | [0] | ++-------------------+-----------------------------+ Auto-probing of LLC specs ######################### diff --git a/docs/misc/xen-command-line.pandoc b/docs/misc/xen-command-line.pandoc index ca105ec559..be70d6af61 100644 --- a/docs/misc/xen-command-line.pandoc +++ b/docs/misc/xen-command-line.pandoc @@ -963,6 +963,15 @@ Controls for the dom0 IOMMU setup. Specify a list of IO ports to be excluded from dom0 access. +### dom0-llc-colors (arm64) +> `= List of [ | - ]` + +> Default: `All available LLC colors` + +Specify dom0 LLC color configuration. This option is available only when +`CONFIG_LLC_COLORING` is enabled. If the parameter is not set, all available +colors are used. + ### dom0_max_vcpus Either: diff --git a/xen/arch/arm/domain_build.c b/xen/arch/arm/domain_build.c index 17cc76b6f2..b072a16249 100644 --- a/xen/arch/arm/domain_build.c +++ b/xen/arch/arm/domain_build.c @@ -2356,6 +2356,7 @@ void __init create_dom0(void) .max_maptrack_frames = -1, .grant_opts = XEN_DOMCTL_GRANT_version(opt_gnttab_max_version), }; + unsigned int flags = CDF_privileged; int rc; /* The vGIC for DOM0 is exactly emulating the hardware GIC */ @@ -2383,10 +2384,16 @@ void __init create_dom0(void) panic("SVE vector length error\n"); } - dom0 = domain_create(0, &dom0_cfg, CDF_privileged | CDF_directmap); + if ( !llc_coloring_enabled ) + flags |= CDF_directmap; + + dom0 = domain_create(0, &dom0_cfg, flags); if ( IS_ERR(dom0) ) panic("Error creating domain 0 (rc = %ld)\n", PTR_ERR(dom0)); + if ( llc_coloring_enabled && (rc = dom0_set_llc_colors(dom0)) ) + panic("Error initializing LLC coloring for domain 0 (rc = %d)\n", rc); + if ( alloc_dom0_vcpu0(dom0) == NULL ) panic("Error creating domain 0 vcpu0\n"); diff --git a/xen/common/llc-coloring.c b/xen/common/llc-coloring.c index b034c0169c..5984ae82e7 100644 --- a/xen/common/llc-coloring.c +++ b/xen/common/llc-coloring.c @@ -29,6 +29,66 @@ static unsigned int __initdata llc_nr_ways; integer_param("llc-nr-ways", llc_nr_ways); /* Number of colors available in the LLC */ static unsigned int __ro_after_init max_nr_colors; +/* Default coloring configuration */ +static unsigned int __ro_after_init default_colors[NR_LLC_COLORS]; + +static unsigned int __initdata dom0_colors[NR_LLC_COLORS]; +static unsigned int __initdata dom0_num_colors; + +/* + * Parse the coloring configuration given in the buf string, following the + * syntax below. + * + * COLOR_CONFIGURATION ::= COLOR | RANGE,...,COLOR | RANGE + * RANGE ::= COLOR-COLOR + * + * Example: "0,2-6,15-16" represents the set of colors: 0,2,3,4,5,6,15,16. + */ +static int __init parse_color_config(const char *buf, unsigned int colors[], + unsigned int max_num_colors, + unsigned int *num_colors) +{ + const char *s = buf; + + *num_colors = 0; + + while ( *s != '\0' ) + { + unsigned int color, start, end; + + start = simple_strtoul(s, &s, 0); + + if ( *s == '-' ) /* Range */ + { + s++; + end = simple_strtoul(s, &s, 0); + } + else /* Single value */ + end = start; + + if ( start > end || (end - start) > (UINT_MAX - *num_colors) || + (*num_colors + (end - start)) >= max_num_colors ) + return -EINVAL; + + /* Colors are range checked in check_colors() */ + for ( color = start; color <= end; color++ ) + colors[(*num_colors)++] = color; + + if ( *s == ',' ) + s++; + else if ( *s != '\0' ) + break; + } + + return *s ? -EINVAL : 0; +} + +static int __init parse_dom0_colors(const char *s) +{ + return parse_color_config(s, dom0_colors, ARRAY_SIZE(dom0_colors), + &dom0_num_colors); +} +custom_param("dom0-llc-colors", parse_dom0_colors); static void print_colors(const unsigned int colors[], unsigned int num_colors) { @@ -53,9 +113,27 @@ static void print_colors(const unsigned int colors[], unsigned int num_colors) printk(" }\n"); } +static bool __init check_colors(const unsigned int colors[], + unsigned int num_colors) +{ + unsigned int i; + + for ( i = 0; i < num_colors; i++ ) + { + if ( colors[i] >= max_nr_colors ) + { + printk(XENLOG_ERR "LLC color %u >= %u (max allowed)\n", colors[i], + max_nr_colors); + return false; + } + } + + return true; +} + void __init llc_coloring_init(void) { - unsigned int way_size; + unsigned int way_size, i; llc_coloring_enabled = (opt_llc_coloring >= 1); if ( (opt_llc_coloring != 0) && llc_size && llc_nr_ways ) @@ -94,6 +172,9 @@ void __init llc_coloring_init(void) else if ( max_nr_colors < 2 ) panic("Number of LLC colors %u < 2\n", max_nr_colors); + for ( i = 0; i < max_nr_colors; i++ ) + default_colors[i] = i; + arch_llc_coloring_init(); } @@ -115,6 +196,43 @@ void domain_dump_llc_colors(const struct domain *d) print_colors(d->llc_colors, d->num_llc_colors); } +static void __init domain_set_default_colors(struct domain *d) +{ + printk(XENLOG_WARNING + "LLC color config not found for %pd, using all colors\n", d); + + d->llc_colors = default_colors; + d->num_llc_colors = max_nr_colors; +} + +int __init dom0_set_llc_colors(struct domain *d) +{ + typeof(*dom0_colors) *colors; + + if ( !dom0_num_colors ) + { + domain_set_default_colors(d); + return 0; + } + + if ( (dom0_num_colors > max_nr_colors) || + !check_colors(dom0_colors, dom0_num_colors) ) + { + printk(XENLOG_ERR "%pd: bad LLC color config\n", d); + return -EINVAL; + } + + colors = xmalloc_array(typeof(*dom0_colors), dom0_num_colors); + if ( !colors ) + return -ENOMEM; + + memcpy(colors, dom0_colors, sizeof(*colors) * dom0_num_colors); + d->llc_colors = colors; + d->num_llc_colors = dom0_num_colors; + + return 0; +} + /* * Local variables: * mode: C diff --git a/xen/include/xen/llc-coloring.h b/xen/include/xen/llc-coloring.h index a3ebb17186..d29ebeb4c5 100644 --- a/xen/include/xen/llc-coloring.h +++ b/xen/include/xen/llc-coloring.h @@ -28,6 +28,7 @@ static inline void domain_dump_llc_colors(const struct domain *d) {} unsigned int get_llc_way_size(void); void arch_llc_coloring_init(void); +int dom0_set_llc_colors(struct domain *d); #endif /* __XEN_LLC_COLORING_H__ */ From patchwork Tue Dec 17 17:06:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlo Nonato X-Patchwork-Id: 13912218 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 120D9E77188 for ; Tue, 17 Dec 2024 17:07:00 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.859577.1271728 (Exim 4.92) (envelope-from ) id 1tNb20-00071M-Vw; Tue, 17 Dec 2024 17:06:48 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 859577.1271728; Tue, 17 Dec 2024 17:06:48 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tNb20-0006zN-Ir; Tue, 17 Dec 2024 17:06:48 +0000 Received: by outflank-mailman (input) for mailman id 859577; Tue, 17 Dec 2024 17:06:47 +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 1tNb1z-0006B0-Fj for xen-devel@lists.xenproject.org; Tue, 17 Dec 2024 17:06:47 +0000 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [2a00:1450:4864:20::62f]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 4c32b1cb-bc99-11ef-a0d6-8be0dac302b0; Tue, 17 Dec 2024 18:06:46 +0100 (CET) Received: by mail-ej1-x62f.google.com with SMTP id a640c23a62f3a-aa68b513abcso922175566b.0 for ; Tue, 17 Dec 2024 09:06:46 -0800 (PST) Received: from carlo-ubuntu.home.arpa (host-95-230-250-178.business.telecomitalia.it. [95.230.250.178]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aab963910f7sm461362666b.166.2024.12.17.09.06.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Dec 2024 09:06:45 -0800 (PST) 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: 4c32b1cb-bc99-11ef-a0d6-8be0dac302b0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=minervasys-tech.20230601.gappssmtp.com; s=20230601; t=1734455206; x=1735060006; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2S4/V5E4tJ5kPAHOqK5areTT6H+4uOWSSoM5Y54F91A=; b=kLKS3ZqO0+C1WxMX+JokFh0/ol3RC9DazhZAkAGkWH/AjZvjNZ8DQR3uB8dWDFB74q +6zAO2wjdZVJ4ntiko4f3jMUAkuaTvfnynCVMJnribqzfTcquNVibpm/bQtSU8+9osQX IATked7CPNzg5I9DrD7VYSiFWvCd1l5P1xYK7KVnNorNKQswR4aYYoOx+dvHw69VXwa1 9H5Hqt900HtpjMW5yeOxkqRX5w8mBe0H3vfBezR2QLt3YUBwrm/67XKqw8gHYZt+ZTsR ebfwC9QlTJZm2f0AqVgJIeCKJm3DoNAmOINx+1vq6b5GY++2lcMAEU6pxaPKdDIhpWo6 CVcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734455206; x=1735060006; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2S4/V5E4tJ5kPAHOqK5areTT6H+4uOWSSoM5Y54F91A=; b=kO2uRMVnWEsDqBvolkhxDQCcunRtWWeIpxwefPqT4tE+YsK/VfMlV4hspEhIUvCWfl nFYr+l539i9P70d5yFx/Q+VofNKZH/4VrKNy/X1za8XLHavYqic/Ve1TSYChPFVgzjnp LnhoR4IvMxv6dimcoL74UJIzjvT6Y6RSlMrrEn3wn12rs0NDE4or6BB+fhP92CdGAhFX sirRJoMWW15VEr/mSHJE+J/MQlBeuX60HFWXeAecZO58P6BZvoAn6oSAcRRztluf2lTh EqzY6p9VjMFU+fmud3sAyNA4xPlSSnkYH6Au1pSvhEAy5w3aul2Dg7jDhzy9l1CWHt40 jQcA== X-Gm-Message-State: AOJu0YwdI3KJLcvCRcrmvzC+pBJBNUAHx/i//LtMnLnb7J4ty9gXtkDJ H09UxB8OySgjVrZEpsSIvaF+RoD1joyGFE9khaI9zMX9mu4T6x565rDLgNz7/LmafbR1cyu0B29 5IWA= X-Gm-Gg: ASbGncszokdC59ezyoXC24mr1IWK29wa2//izRkqn4m0XNyykpuNe7+egB4tzz5lepH /OcmWhC/oxM6O1/nR6GFwaPbtA1tgFOqS1N6JDbzm8Q+e3/XWBfMdSONn84e2xrbEV3bdgRNFuT aj9sMzUNFSrQe6pHn3f+xjD7Ti5ieNGerdIduAXaQhe+lMn44FggjJVRbHKvcI84Yb1NwWPS/ry ntqeiTsJDNRDdl+vidVGcfAKwZICGLYxT4kWgA57b7BbolEVD86xVzOqUFEr1SQA1TyhXDre3XM 7Rtz8/Pn4YanqVNQzx6hjdCD0t9xztsVYp+w0P0DxlVJgPY1P9uBpWkom1g= X-Google-Smtp-Source: AGHT+IExagnqRoh8QBrUeufotoNpy1yEQfm4ChjJPBuVBy8EYoooKITYLw1dG1jy7T5JMA1rOaQFgw== X-Received: by 2002:a17:907:d15:b0:aa6:8f39:5c with SMTP id a640c23a62f3a-aab7792cff5mr2000325166b.17.1734455205778; Tue, 17 Dec 2024 09:06:45 -0800 (PST) From: Carlo Nonato To: xen-devel@lists.xenproject.org Cc: andrea.bastoni@minervasys.tech, marco.solieri@minervasys.tech, Carlo Nonato , Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Stefano Stabellini Subject: [PATCH v13 05/12] xen: extend domctl interface for cache coloring Date: Tue, 17 Dec 2024 18:06:30 +0100 Message-ID: <20241217170637.233097-6-carlo.nonato@minervasys.tech> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241217170637.233097-1-carlo.nonato@minervasys.tech> References: <20241217170637.233097-1-carlo.nonato@minervasys.tech> MIME-Version: 1.0 Add a new domctl hypercall to allow the user to set LLC coloring configurations. Colors can be set only once, just after domain creation, since recoloring isn't supported. Based on original work from: Luca Miccio Signed-off-by: Carlo Nonato Signed-off-by: Marco Solieri Reviewed-by: Jan Beulich --- v13: - no changes v12: - no changes v11: - minor changes v10: - fixed array type for colors parameter in check_colors() v9: - moved domain_llc_coloring_free() in next patch cause it's never used for dom0 v8: - added bound check on dom0_num_colors - default colors array set just once v7: - parse_color_config() doesn't accept leading/trailing commas anymore - removed alloc_colors() helper v6: - moved domain_llc_coloring_free() in this patch - removed domain_alloc_colors() in favor of a more explicit allocation - parse_color_config() now accepts the size of the array to be filled - allocate_memory() moved in another patch v5: - Carlo Nonato as the new author - moved dom0 colors parsing (parse_colors()) in this patch - added dom0_set_llc_colors() to set dom0 colors after creation - moved color allocation and checking in this patch - error handling when allocating color arrays - FIXME: copy pasted allocate_memory() cause it got moved v4: - dom0 colors are dynamically allocated as for any other domain (colors are duplicated in dom0_colors and in the new array, but logic is simpler) --- xen/common/domain.c | 3 ++ xen/common/domctl.c | 10 +++++++ xen/common/llc-coloring.c | 55 ++++++++++++++++++++++++++++++++-- xen/include/public/domctl.h | 9 ++++++ xen/include/xen/llc-coloring.h | 5 ++++ 5 files changed, 79 insertions(+), 3 deletions(-) diff --git a/xen/common/domain.c b/xen/common/domain.c index e33a0a5a21..0c4cc77111 100644 --- a/xen/common/domain.c +++ b/xen/common/domain.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -1303,6 +1304,8 @@ void domain_destroy(struct domain *d) { BUG_ON(!d->is_dying); + domain_llc_coloring_free(d); + /* May be already destroyed, or get_domain() can race us. */ if ( atomic_cmpxchg(&d->refcnt, 0, DOMAIN_DESTROYED) != 0 ) return; diff --git a/xen/common/domctl.c b/xen/common/domctl.c index 444e072fdc..05abb581a0 100644 --- a/xen/common/domctl.c +++ b/xen/common/domctl.c @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -866,6 +867,15 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl) __HYPERVISOR_domctl, "h", u_domctl); break; + case XEN_DOMCTL_set_llc_colors: + if ( op->u.set_llc_colors.pad ) + ret = -EINVAL; + else if ( llc_coloring_enabled ) + ret = domain_set_llc_colors(d, &op->u.set_llc_colors); + else + ret = -EOPNOTSUPP; + break; + default: ret = arch_do_domctl(op, d, u_domctl); break; diff --git a/xen/common/llc-coloring.c b/xen/common/llc-coloring.c index 5984ae82e7..3dd1aaa4df 100644 --- a/xen/common/llc-coloring.c +++ b/xen/common/llc-coloring.c @@ -5,6 +5,7 @@ * Copyright (C) 2024, Advanced Micro Devices, Inc. * Copyright (C) 2024, Minerva Systems SRL */ +#include #include #include #include @@ -113,8 +114,7 @@ static void print_colors(const unsigned int colors[], unsigned int num_colors) printk(" }\n"); } -static bool __init check_colors(const unsigned int colors[], - unsigned int num_colors) +static bool check_colors(const unsigned int colors[], unsigned int num_colors) { unsigned int i; @@ -196,7 +196,7 @@ void domain_dump_llc_colors(const struct domain *d) print_colors(d->llc_colors, d->num_llc_colors); } -static void __init domain_set_default_colors(struct domain *d) +static void domain_set_default_colors(struct domain *d) { printk(XENLOG_WARNING "LLC color config not found for %pd, using all colors\n", d); @@ -233,6 +233,55 @@ int __init dom0_set_llc_colors(struct domain *d) return 0; } +int domain_set_llc_colors(struct domain *d, + const struct xen_domctl_set_llc_colors *config) +{ + unsigned int *colors; + + if ( d->num_llc_colors ) + return -EEXIST; + + if ( !config->num_llc_colors ) + { + domain_set_default_colors(d); + return 0; + } + + if ( config->num_llc_colors > max_nr_colors ) + return -EINVAL; + + colors = xmalloc_array(unsigned int, config->num_llc_colors); + if ( !colors ) + return -ENOMEM; + + if ( copy_from_guest(colors, config->llc_colors, config->num_llc_colors) ) + { + xfree(colors); + return -EFAULT; + } + + if ( !check_colors(colors, config->num_llc_colors) ) + { + printk(XENLOG_ERR "%pd: bad LLC color config\n", d); + xfree(colors); + return -EINVAL; + } + + d->llc_colors = colors; + d->num_llc_colors = config->num_llc_colors; + + return 0; +} + +void domain_llc_coloring_free(struct domain *d) +{ + if ( !llc_coloring_enabled || d->llc_colors == default_colors ) + return; + + /* free pointer-to-const using __va(__pa()) */ + xfree(__va(__pa(d->llc_colors))); +} + /* * Local variables: * mode: C diff --git a/xen/include/public/domctl.h b/xen/include/public/domctl.h index 353f831e40..e2d392d1e5 100644 --- a/xen/include/public/domctl.h +++ b/xen/include/public/domctl.h @@ -1236,6 +1236,13 @@ struct xen_domctl_dt_overlay { }; #endif +struct xen_domctl_set_llc_colors { + /* IN LLC coloring parameters */ + uint32_t num_llc_colors; + uint32_t pad; + XEN_GUEST_HANDLE_64(uint32) llc_colors; +}; + struct xen_domctl { uint32_t cmd; #define XEN_DOMCTL_createdomain 1 @@ -1325,6 +1332,7 @@ struct xen_domctl { #define XEN_DOMCTL_set_paging_mempool_size 86 #define XEN_DOMCTL_dt_overlay 87 #define XEN_DOMCTL_gsi_permission 88 +#define XEN_DOMCTL_set_llc_colors 89 #define XEN_DOMCTL_gdbsx_guestmemio 1000 #define XEN_DOMCTL_gdbsx_pausevcpu 1001 #define XEN_DOMCTL_gdbsx_unpausevcpu 1002 @@ -1391,6 +1399,7 @@ struct xen_domctl { #if defined(__arm__) || defined(__aarch64__) struct xen_domctl_dt_overlay dt_overlay; #endif + struct xen_domctl_set_llc_colors set_llc_colors; uint8_t pad[128]; } u; }; diff --git a/xen/include/xen/llc-coloring.h b/xen/include/xen/llc-coloring.h index d29ebeb4c5..26353c808a 100644 --- a/xen/include/xen/llc-coloring.h +++ b/xen/include/xen/llc-coloring.h @@ -11,6 +11,7 @@ #include struct domain; +struct xen_domctl_set_llc_colors; #ifdef CONFIG_LLC_COLORING extern bool llc_coloring_enabled; @@ -18,17 +19,21 @@ extern bool llc_coloring_enabled; void llc_coloring_init(void); void dump_llc_coloring_info(void); void domain_dump_llc_colors(const struct domain *d); +void domain_llc_coloring_free(struct domain *d); #else #define llc_coloring_enabled false static inline void llc_coloring_init(void) {} static inline void dump_llc_coloring_info(void) {} static inline void domain_dump_llc_colors(const struct domain *d) {} +static inline void domain_llc_coloring_free(struct domain *d) {} #endif unsigned int get_llc_way_size(void); void arch_llc_coloring_init(void); int dom0_set_llc_colors(struct domain *d); +int domain_set_llc_colors(struct domain *d, + const struct xen_domctl_set_llc_colors *config); #endif /* __XEN_LLC_COLORING_H__ */ From patchwork Tue Dec 17 17:06:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlo Nonato X-Patchwork-Id: 13912221 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 AE639E7718B for ; Tue, 17 Dec 2024 17:07:00 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.859578.1271736 (Exim 4.92) (envelope-from ) id 1tNb21-0007Do-FF; Tue, 17 Dec 2024 17:06:49 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 859578.1271736; Tue, 17 Dec 2024 17:06:49 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tNb21-0007Ao-7a; Tue, 17 Dec 2024 17:06:49 +0000 Received: by outflank-mailman (input) for mailman id 859578; Tue, 17 Dec 2024 17:06:48 +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 1tNb20-0006B0-99 for xen-devel@lists.xenproject.org; Tue, 17 Dec 2024 17:06:48 +0000 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [2a00:1450:4864:20::62f]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 4cb1fec7-bc99-11ef-a0d6-8be0dac302b0; Tue, 17 Dec 2024 18:06:47 +0100 (CET) Received: by mail-ej1-x62f.google.com with SMTP id a640c23a62f3a-aa6aad76beeso774557266b.2 for ; Tue, 17 Dec 2024 09:06:47 -0800 (PST) Received: from carlo-ubuntu.home.arpa (host-95-230-250-178.business.telecomitalia.it. [95.230.250.178]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aab963910f7sm461362666b.166.2024.12.17.09.06.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Dec 2024 09:06:46 -0800 (PST) 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: 4cb1fec7-bc99-11ef-a0d6-8be0dac302b0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=minervasys-tech.20230601.gappssmtp.com; s=20230601; t=1734455207; x=1735060007; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=PKbMPc6hxIrj5w4kArM2ZAdbV73PCxtXLWMN/R1bwd0=; b=j8HzRT3z281SXi1UQbLirdumsLBSd7RWl/fNxUn4JCMxpzfh4Zn1bSvFHAstDGkIpC lhvz4/qWBXH/9WGIo2eVrGcpqrErFKSpP+fCEWMqeYAaKclfLmRik9+NazGlOkhpvEDa UaKn23dykypgqrDkjtLKZtuAKSHjKKaQbP84TnMiwD4rsSqQUsWMlBH1EgB82RrM2paE EX5opxpzdnAnUtQaUMY1ubAXY5EkCqGG13uRxebV+BjMI1dclQ5x6O7GDssyJ9DL/TW2 0wWOUgJ30MNq+gulbPXDoRQsQio6uiuT4fZKm8P9h39m6DR3qp2BL0d0olmYYguCpss1 xL9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734455207; x=1735060007; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PKbMPc6hxIrj5w4kArM2ZAdbV73PCxtXLWMN/R1bwd0=; b=qlxFFSv7SbKWGP32HYKSb8mXzETxPmfOquiqksM/0lrikMu5MQZs5NB7/gV6P9Bvm0 fNF72z2Fn2Dz0W8P0j4wOxJjrG6EGsGY9GPBbxl4JCqYdKrlaqCIulaRghf9rML0a4lV /3iAffmDvFLPktplEtfb8Ro8aGMeKnbQsSoI2i78uWzzQSGS2wMD/OS4AfcR/vi4dG9K dzy4rIXhx+DBQKHe6AYeQIyZvoPSXXtlo2EApu4f6cCbRXMq/DIu1KxxaUK0QqE5Y16K qsqknpChJEdQislccHyRMvF2GkyagpnxfkbGTdBB+gPyV/KupyldComfCe4YUGrOI3oq Da9w== X-Gm-Message-State: AOJu0YyeLK7rGlWSXSOYRAA3hBy94u4jCRn4M/7b4kzER0jEhRAy2qxM Q2pHpxcCf9mTGYsuZAXRVAffi5h7ZSguHg71Fz91XgYDalCvfNWf8FWZ/53X+JMzk99sXBnW2jt un5g= X-Gm-Gg: ASbGncsNT45nhPZDBvQaWa3O2JzI/bDQ0ERQNJ7LRN35j1tnD9QhCYT0k9ZDd6o7uGy lfm5AW+vHtYJHuUVF2I5sDrH5mc/G52KQMacHFqjyt7UlEO8qtTR3/L1Hn+RirDPWV4MDTDiHuw I0/Udx8BLx6nFFLJPYV/5Z4bNE+rXyvippq2QipvMUNkWPhtNe2BRj9OTZlSJcM+RNJjOhdgs3P v7iwi2/1drY9PWkz7mZ7dPXjv91rr9dpDXctC948ntsOVH9+5yTyklQbdFeIGg+Nybs1XgYUcHf z1LGj1G3aHumWPRHsn0viuhFpYMm/sWgy2Dm37pvv1RRNLEFR8lj1SztFvc= X-Google-Smtp-Source: AGHT+IHxCmEWT1kjMcnNxE37KiANdTmbauEYGd7bI0JIgofbcW4hN4PsjcB7cmjsji5zjQocsURLtg== X-Received: by 2002:a17:907:6d12:b0:aab:9607:b6b1 with SMTP id a640c23a62f3a-aabf1980db6mr11560966b.26.1734455206715; Tue, 17 Dec 2024 09:06:46 -0800 (PST) From: Carlo Nonato To: xen-devel@lists.xenproject.org Cc: andrea.bastoni@minervasys.tech, marco.solieri@minervasys.tech, Carlo Nonato , Anthony PERARD , Nick Rosbrook , George Dunlap , Juergen Gross Subject: [PATCH v13 06/12] tools: add support for cache coloring configuration Date: Tue, 17 Dec 2024 18:06:31 +0100 Message-ID: <20241217170637.233097-7-carlo.nonato@minervasys.tech> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241217170637.233097-1-carlo.nonato@minervasys.tech> References: <20241217170637.233097-1-carlo.nonato@minervasys.tech> MIME-Version: 1.0 Add a new "llc_colors" parameter that defines the LLC color assignment for a domain. The user can specify one or more color ranges using the same syntax used everywhere else for color config described in the documentation. The parameter is defined as a list of strings that represent the color ranges. Documentation is also added. Golang bindings are regenerated. Based on original work from: Luca Miccio Signed-off-by: Carlo Nonato Signed-off-by: Marco Solieri Reviewed-by: Anthony PERARD --- v13: - no changes v12: - no changes v11: - turned unsigned int to uint32_t in xc_domain_set_llc_colors() - return -1 in case of error instead of ENOMEM in xc_domain_set_llc_colors() - added regenerated go bindings v10: - no changes v9: - turned warning into error in case of coloring not enabled v8: - warn the user in case of coloring not supported at hypervisor level v7: - removed unneeded NULL check before xc_hypercall_buffer_free() in xc_domain_set_llc_colors() v6: - no edits v5: - added LIBXL_HAVE_BUILDINFO_LLC_COLORS - moved color configuration in xc_domain_set_llc_colors() cause of the new hypercall v4: - removed overlapping color ranges checks during parsing - moved hypercall buffer initialization in libxenctrl --- docs/man/xl.cfg.5.pod.in | 6 +++++ tools/golang/xenlight/helpers.gen.go | 16 ++++++++++++ tools/golang/xenlight/types.gen.go | 1 + tools/include/libxl.h | 5 ++++ tools/include/xenctrl.h | 9 +++++++ tools/libs/ctrl/xc_domain.c | 34 +++++++++++++++++++++++++ tools/libs/light/libxl_create.c | 18 +++++++++++++ tools/libs/light/libxl_types.idl | 1 + tools/xl/xl_parse.c | 38 +++++++++++++++++++++++++++- 9 files changed, 127 insertions(+), 1 deletion(-) diff --git a/docs/man/xl.cfg.5.pod.in b/docs/man/xl.cfg.5.pod.in index ac3f88fd57..8e1422104e 100644 --- a/docs/man/xl.cfg.5.pod.in +++ b/docs/man/xl.cfg.5.pod.in @@ -3074,6 +3074,12 @@ raised. =over 4 +=item B + +Specify the Last Level Cache (LLC) color configuration for the guest. +B can be either a single color value or a hypen-separated closed +interval of colors (such as "0-4"). + =item B An optional integer parameter specifying the number of SPIs (Shared diff --git a/tools/golang/xenlight/helpers.gen.go b/tools/golang/xenlight/helpers.gen.go index fe5110474d..90846ea8e8 100644 --- a/tools/golang/xenlight/helpers.gen.go +++ b/tools/golang/xenlight/helpers.gen.go @@ -1097,6 +1097,14 @@ if err := x.Iomem[i].fromC(&v); err != nil { return fmt.Errorf("converting field Iomem: %v", err) } } } +x.LlcColors = nil +if n := int(xc.num_llc_colors); n > 0 { +cLlcColors := (*[1<<28]C.uint32_t)(unsafe.Pointer(xc.llc_colors))[:n:n] +x.LlcColors = make([]uint32, n) +for i, v := range cLlcColors { +x.LlcColors[i] = uint32(v) +} +} if err := x.ClaimMode.fromC(&xc.claim_mode);err != nil { return fmt.Errorf("converting field ClaimMode: %v", err) } @@ -1453,6 +1461,14 @@ return fmt.Errorf("converting field Iomem: %v", err) } } } +if numLlcColors := len(x.LlcColors); numLlcColors > 0 { +xc.llc_colors = (*C.uint32_t)(C.malloc(C.size_t(numLlcColors*numLlcColors))) +xc.num_llc_colors = C.int(numLlcColors) +cLlcColors := (*[1<<28]C.uint32_t)(unsafe.Pointer(xc.llc_colors))[:numLlcColors:numLlcColors] +for i,v := range x.LlcColors { +cLlcColors[i] = C.uint32_t(v) +} +} if err := x.ClaimMode.toC(&xc.claim_mode); err != nil { return fmt.Errorf("converting field ClaimMode: %v", err) } diff --git a/tools/golang/xenlight/types.gen.go b/tools/golang/xenlight/types.gen.go index c9e45b306f..e7667f1ce3 100644 --- a/tools/golang/xenlight/types.gen.go +++ b/tools/golang/xenlight/types.gen.go @@ -575,6 +575,7 @@ SchedParams DomainSchedParams Ioports []IoportRange Irqs []uint32 Iomem []IomemRange +LlcColors []uint32 ClaimMode Defbool EventChannels uint32 Kernel string diff --git a/tools/include/libxl.h b/tools/include/libxl.h index 8d32428ea9..f8fe4afd7d 100644 --- a/tools/include/libxl.h +++ b/tools/include/libxl.h @@ -1379,6 +1379,11 @@ void libxl_mac_copy(libxl_ctx *ctx, libxl_mac *dst, const libxl_mac *src); */ #define LIBXL_HAVE_BUILDINFO_HVM_SYSTEM_FIRMWARE +/* + * The libxl_domain_build_info has the llc_colors array. + */ +#define LIBXL_HAVE_BUILDINFO_LLC_COLORS 1 + /* * ERROR_REMUS_XXX error code only exists from Xen 4.5, Xen 4.6 and it * is changed to ERROR_CHECKPOINT_XXX in Xen 4.7 diff --git a/tools/include/xenctrl.h b/tools/include/xenctrl.h index 29617585c5..d6d93a2e8f 100644 --- a/tools/include/xenctrl.h +++ b/tools/include/xenctrl.h @@ -2667,6 +2667,15 @@ int xc_livepatch_replace(xc_interface *xch, char *name, uint32_t timeout, uint32 int xc_domain_cacheflush(xc_interface *xch, uint32_t domid, xen_pfn_t start_pfn, xen_pfn_t nr_pfns); +/* + * Set LLC colors for a domain. + * It can only be used directly after domain creation. An attempt to use it + * afterwards will result in an error. + */ +int xc_domain_set_llc_colors(xc_interface *xch, uint32_t domid, + const uint32_t *llc_colors, + uint32_t num_llc_colors); + #if defined(__arm__) || defined(__aarch64__) int xc_dt_overlay(xc_interface *xch, void *overlay_fdt, uint32_t overlay_fdt_size, uint8_t overlay_op); diff --git a/tools/libs/ctrl/xc_domain.c b/tools/libs/ctrl/xc_domain.c index e3538ec0ba..2ddc3f4f42 100644 --- a/tools/libs/ctrl/xc_domain.c +++ b/tools/libs/ctrl/xc_domain.c @@ -2195,6 +2195,40 @@ int xc_domain_soft_reset(xc_interface *xch, domctl.domain = domid; return do_domctl(xch, &domctl); } + +int xc_domain_set_llc_colors(xc_interface *xch, uint32_t domid, + const uint32_t *llc_colors, + uint32_t num_llc_colors) +{ + struct xen_domctl domctl = {}; + DECLARE_HYPERCALL_BUFFER(uint32_t, local); + int ret = -1; + + if ( num_llc_colors ) + { + size_t bytes = sizeof(uint32_t) * num_llc_colors; + + local = xc_hypercall_buffer_alloc(xch, local, bytes); + if ( local == NULL ) + { + PERROR("Could not allocate LLC colors for set_llc_colors"); + goto out; + } + memcpy(local, llc_colors, bytes); + set_xen_guest_handle(domctl.u.set_llc_colors.llc_colors, local); + } + + domctl.cmd = XEN_DOMCTL_set_llc_colors; + domctl.domain = domid; + domctl.u.set_llc_colors.num_llc_colors = num_llc_colors; + + ret = do_domctl(xch, &domctl); + +out: + xc_hypercall_buffer_free(xch, local); + + return ret; +} /* * Local variables: * mode: C diff --git a/tools/libs/light/libxl_create.c b/tools/libs/light/libxl_create.c index edeadd57ef..e03599ea99 100644 --- a/tools/libs/light/libxl_create.c +++ b/tools/libs/light/libxl_create.c @@ -747,6 +747,24 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_config *d_config, /* A new domain now exists */ *domid = local_domid; + ret = xc_domain_set_llc_colors(ctx->xch, local_domid, + b_info->llc_colors, + b_info->num_llc_colors); + if (ret < 0) { + if (errno == EOPNOTSUPP) { + if (b_info->num_llc_colors > 0) { + LOGED(ERROR, local_domid, + "LLC coloring not enabled in the hypervisor"); + rc = ERROR_FAIL; + goto out; + } + } else { + LOGED(ERROR, local_domid, "LLC colors allocation failed"); + rc = ERROR_FAIL; + goto out; + } + } + rc = libxl__is_domid_recent(gc, local_domid, &recent); if (rc) goto out; diff --git a/tools/libs/light/libxl_types.idl b/tools/libs/light/libxl_types.idl index 4e65e6fda5..bd4b8721ff 100644 --- a/tools/libs/light/libxl_types.idl +++ b/tools/libs/light/libxl_types.idl @@ -616,6 +616,7 @@ libxl_domain_build_info = Struct("domain_build_info",[ ("ioports", Array(libxl_ioport_range, "num_ioports")), ("irqs", Array(uint32, "num_irqs")), ("iomem", Array(libxl_iomem_range, "num_iomem")), + ("llc_colors", Array(uint32, "num_llc_colors")), ("claim_mode", libxl_defbool), ("event_channels", uint32), ("kernel", string), diff --git a/tools/xl/xl_parse.c b/tools/xl/xl_parse.c index e3a4800f6e..3d85be7dd4 100644 --- a/tools/xl/xl_parse.c +++ b/tools/xl/xl_parse.c @@ -1296,7 +1296,7 @@ void parse_config_data(const char *config_source, XLU_ConfigList *cpus, *vbds, *nics, *pcis, *cvfbs, *cpuids, *vtpms, *usbctrls, *usbdevs, *p9devs, *vdispls, *pvcallsifs_devs; XLU_ConfigList *channels, *ioports, *irqs, *iomem, *viridian, *dtdevs, - *mca_caps, *smbios; + *mca_caps, *smbios, *llc_colors; int num_ioports, num_irqs, num_iomem, num_cpus, num_viridian, num_mca_caps; int num_smbios; int pci_power_mgmt = 0; @@ -1304,6 +1304,7 @@ void parse_config_data(const char *config_source, int pci_permissive = 0; int pci_seize = 0; int i, e; + int num_llc_colors; char *kernel_basename; libxl_domain_create_info *c_info = &d_config->c_info; @@ -1447,6 +1448,41 @@ void parse_config_data(const char *config_source, if (!xlu_cfg_get_long (config, "maxmem", &l, 0)) b_info->max_memkb = l * 1024; + if (!xlu_cfg_get_list(config, "llc_colors", &llc_colors, &num_llc_colors, 0)) { + int cur_index = 0; + + b_info->num_llc_colors = 0; + for (i = 0; i < num_llc_colors; i++) { + uint32_t start = 0, end = 0, k; + + buf = xlu_cfg_get_listitem(llc_colors, i); + if (!buf) { + fprintf(stderr, + "xl: Can't get element %d in LLC color list\n", i); + exit(1); + } + + if (sscanf(buf, "%" SCNu32 "-%" SCNu32, &start, &end) != 2) { + if (sscanf(buf, "%" SCNu32, &start) != 1) { + fprintf(stderr, "xl: Invalid LLC color range: %s\n", buf); + exit(1); + } + end = start; + } else if (start > end) { + fprintf(stderr, + "xl: Start LLC color is greater than end: %s\n", buf); + exit(1); + } + + b_info->num_llc_colors += (end - start) + 1; + b_info->llc_colors = (uint32_t *)realloc(b_info->llc_colors, + sizeof(*b_info->llc_colors) * b_info->num_llc_colors); + + for (k = start; k <= end; k++) + b_info->llc_colors[cur_index++] = k; + } + } + if (!xlu_cfg_get_long (config, "vcpus", &l, 0)) { vcpus = l; if (libxl_cpu_bitmap_alloc(ctx, &b_info->avail_vcpus, l)) { From patchwork Tue Dec 17 17:06:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlo Nonato X-Patchwork-Id: 13912216 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 AF6C3E77187 for ; Tue, 17 Dec 2024 17:06:59 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.859581.1271762 (Exim 4.92) (envelope-from ) id 1tNb24-00080e-7A; Tue, 17 Dec 2024 17:06:52 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 859581.1271762; Tue, 17 Dec 2024 17:06:52 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tNb24-0007z5-2G; Tue, 17 Dec 2024 17:06:52 +0000 Received: by outflank-mailman (input) for mailman id 859581; Tue, 17 Dec 2024 17:06:50 +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 1tNb22-0006gw-Ag for xen-devel@lists.xenproject.org; Tue, 17 Dec 2024 17:06:50 +0000 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [2a00:1450:4864:20::532]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 4d63f883-bc99-11ef-99a3-01e77a169b0f; Tue, 17 Dec 2024 18:06:48 +0100 (CET) Received: by mail-ed1-x532.google.com with SMTP id 4fb4d7f45d1cf-5d3d0205bd5so7902468a12.3 for ; Tue, 17 Dec 2024 09:06:48 -0800 (PST) Received: from carlo-ubuntu.home.arpa (host-95-230-250-178.business.telecomitalia.it. [95.230.250.178]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aab963910f7sm461362666b.166.2024.12.17.09.06.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Dec 2024 09:06:47 -0800 (PST) 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: 4d63f883-bc99-11ef-99a3-01e77a169b0f DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=minervasys-tech.20230601.gappssmtp.com; s=20230601; t=1734455208; x=1735060008; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vsljsmkJSCPil0FUckH5NAu8dI/+5ZLlI9O6PG7m0uw=; b=GuhN/j/LmgHJgbMtPKI9IsvW2pCvo2Xe7zPOkFcL0L5b2ldnj9zeFV0w3+ZbziFoj6 a+B2RgRqMLKCm56gr8uKrorcAbwT4L6zNWmbk3CMFbKmjiVMSrNZD37OywVGQG51xtXl TQxdKexZVhXQjPQJuJST2IOvTwwYGYQVE7o41VFTuZWsMQqqm3hqm381fybDRFl0tcwK u7MEMEEiTENUQQcVavEqMuW2pVGlf2uY2sAeD/Oan7xEWuZKt2BIjTHJdeNm7dBcK+CY DmTeVn1CEly/J1lkAbf0IxeV2Mzj9FzcWzhX7w4TufRaj/D6yt7ci7UCwBTHezQm47oQ aiFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734455208; x=1735060008; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vsljsmkJSCPil0FUckH5NAu8dI/+5ZLlI9O6PG7m0uw=; b=GISx5fy3kkcKovTJjCeWTt2x3mvRfqC1RrcnxA9w1v+YLCZNvl9RmdOkEYZfvdxRhT Be0QLXqDxsJaGaTI7x1Mo4bdCg90tLYGZKbixT12m4EoGOWuEDbm6GqZT8wz/HcnMWKi Cl9M1Afgw1ot1DwqgI4HgwS7BID+1B2q8e1uzc6hYU6/EWZbvhQbqWq3teFhCvlK90zN eyUWey185Exzw720OxY44dzjUExzP04uTSXXYrSZr9ivlH4okKn61rPBhzb3c1tucCnw 1axPqwaGGhsDefvabVnCTtvnB7CLzK1XOMqCfG/1haKO0fymr27pahmJQqW7Qw/7p1vl JwUQ== X-Gm-Message-State: AOJu0Yzcg0qHFy69ZGDqcQ1K8BY+Q2qaHIrqTV8020SyMr+JPjocxPBn uuDiKJb8lioDYapdnIYnOOeDODedU3k9YCF/sA01GZgYY7QhFU1YC+76Is2qoJ93LrgXHXHufyT GF2c= X-Gm-Gg: ASbGncvEElxDXEBpt5mSwAJ8N7chmwudEZwjKN0ubdWHzb9Obti+KRCePHZEHp00W+f MUaEJLHRr23KhIESmSERkX1FDjUFqxHsdbBxNSvhCR2d2sjhJxSeuZCG06ApVOiuRYLN5OYrnXg BYO4qGrEvp6ZD8iEC2s3SUVL37TO+o6ilXDbYYUwyGzhl23vMeUy+Yj+inI9/dzQZFBG8yaGyZL 4uJrY5+6dUScHSlPUnGj6I9UnopLet/raPBIceg5e4Bi4GceCbkCUzmoyyJN9upBV5fKSafPpyQ /u3LXPkHu9NXgU/xsCp3tq4jkk+M6Ph1FvUD3NcGCDL/AT5GCoGuWAOCFaA= X-Google-Smtp-Source: AGHT+IFGVoSRTy2RFPUBcqn7vq+gbGl6ReZMS0vT3I2961MFB8rp3RC9M1kHq5f65lA6Btxnmmqa7g== X-Received: by 2002:a17:907:3f1b:b0:aa6:7165:504b with SMTP id a640c23a62f3a-aab779b3ff3mr2029549166b.31.1734455207774; Tue, 17 Dec 2024 09:06:47 -0800 (PST) From: Carlo Nonato To: xen-devel@lists.xenproject.org Cc: andrea.bastoni@minervasys.tech, marco.solieri@minervasys.tech, Carlo Nonato , Stefano Stabellini , Julien Grall , Bertrand Marquis , Michal Orzel , Volodymyr Babchuk , Andrew Cooper , Anthony PERARD , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= Subject: [PATCH v13 07/12] xen/arm: add support for cache coloring configuration via device-tree Date: Tue, 17 Dec 2024 18:06:32 +0100 Message-ID: <20241217170637.233097-8-carlo.nonato@minervasys.tech> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241217170637.233097-1-carlo.nonato@minervasys.tech> References: <20241217170637.233097-1-carlo.nonato@minervasys.tech> MIME-Version: 1.0 Add the "llc-colors" Device Tree property to express DomUs and Dom0less color configurations. Based on original work from: Luca Miccio Signed-off-by: Carlo Nonato Signed-off-by: Marco Solieri Reviewed-by: Jan Beulich # non-Arm Reviewed-by: Michal Orzel --- v13: - no changes v12: - no changes v11: - made clear that llc-colors device-tree property is Arm64-only in booting.txt v10: - no changes v9: - use best-effort allocation in domain_set_llc_colors_from_str() v8: - fixed memory leak on error path of domain_set_llc_colors_from_str() - realloc colors array after parsing from string to reduce memory usage v7: - removed alloc_colors() helper usage from domain_set_llc_colors_from_str() v6: - rewrote domain_set_llc_colors_from_str() to be more explicit v5: - static-mem check has been moved in a previous patch - added domain_set_llc_colors_from_str() to set colors after domain creation --- docs/misc/arm/device-tree/booting.txt | 5 +++ docs/misc/cache-coloring.rst | 48 +++++++++++++++++++++++++++ xen/arch/arm/dom0less-build.c | 10 ++++++ xen/common/llc-coloring.c | 40 ++++++++++++++++++++++ xen/include/xen/llc-coloring.h | 1 + xen/include/xen/xmalloc.h | 12 +++++++ 6 files changed, 116 insertions(+) diff --git a/docs/misc/arm/device-tree/booting.txt b/docs/misc/arm/device-tree/booting.txt index 3a04f5c57f..9c881baccc 100644 --- a/docs/misc/arm/device-tree/booting.txt +++ b/docs/misc/arm/device-tree/booting.txt @@ -162,6 +162,11 @@ with the following properties: An integer specifying the number of vcpus to allocate to the guest. +- llc-colors + A string specifying the LLC color configuration for the guest. + Refer to docs/misc/cache_coloring.rst for syntax. This option is applicable + only to Arm64 guests. + - vpl011 An empty property to enable/disable a virtual pl011 for the guest to diff --git a/docs/misc/cache-coloring.rst b/docs/misc/cache-coloring.rst index 7b47d0ed92..e097e74032 100644 --- a/docs/misc/cache-coloring.rst +++ b/docs/misc/cache-coloring.rst @@ -14,6 +14,7 @@ If needed, change the maximum number of colors with ``CONFIG_LLC_COLORS_ORDER=``. Runtime configuration is done via `Command line parameters`_. +For DomUs follow `DomUs configuration`_. Background ********** @@ -149,6 +150,53 @@ LLC specs can be manually set via the above command line parameters. This bypasses any auto-probing and it's used to overcome failing situations, such as flawed probing logic, or for debugging/testing purposes. +DomUs configuration +******************* + +DomUs colors can be set either in the ``xl`` configuration file (documentation +at `docs/man/xl.cfg.pod.5.in`) or via Device Tree (documentation at +`docs/misc/arm/device-tree/booting.txt`) using the ``llc-colors`` option. +For example: + +:: + + xen,xen-bootargs = "console=dtuart dtuart=serial0 dom0_mem=1G dom0_max_vcpus=1 sched=null llc-coloring=on dom0-llc-colors=2-6"; + xen,dom0-bootargs "console=hvc0 earlycon=xen earlyprintk=xen root=/dev/ram0" + + dom0 { + compatible = "xen,linux-zimage" "xen,multiboot-module"; + reg = <0x0 0x1000000 0x0 15858176>; + }; + + dom0-ramdisk { + compatible = "xen,linux-initrd" "xen,multiboot-module"; + reg = <0x0 0x2000000 0x0 20638062>; + }; + + domU0 { + #address-cells = <0x1>; + #size-cells = <0x1>; + compatible = "xen,domain"; + memory = <0x0 0x40000>; + llc-colors = "4-8,10,11,12"; + cpus = <0x1>; + vpl011 = <0x1>; + + module@2000000 { + compatible = "multiboot,kernel", "multiboot,module"; + reg = <0x2000000 0xffffff>; + bootargs = "console=ttyAMA0"; + }; + + module@30000000 { + compatible = "multiboot,ramdisk", "multiboot,module"; + reg = <0x3000000 0xffffff>; + }; + }; + +**Note:** If no color configuration is provided for a domain, the default one, +which corresponds to all available colors is used instead. + Known issues and limitations **************************** diff --git a/xen/arch/arm/dom0less-build.c b/xen/arch/arm/dom0less-build.c index 67b1503647..49d1f14d65 100644 --- a/xen/arch/arm/dom0less-build.c +++ b/xen/arch/arm/dom0less-build.c @@ -817,6 +817,7 @@ void __init create_domUs(void) bool iommu = false; const struct dt_device_node *cpupool_node, *chosen = dt_find_node_by_path("/chosen"); + const char *llc_colors_str = NULL; BUG_ON(chosen == NULL); dt_for_each_child_node(chosen, node) @@ -965,6 +966,10 @@ void __init create_domUs(void) #endif } + dt_property_read_string(node, "llc-colors", &llc_colors_str); + if ( !llc_coloring_enabled && llc_colors_str ) + panic("'llc-colors' found, but LLC coloring is disabled\n"); + /* * The variable max_init_domid is initialized with zero, so here it's * very important to use the pre-increment operator to call @@ -975,6 +980,11 @@ void __init create_domUs(void) panic("Error creating domain %s (rc = %ld)\n", dt_node_name(node), PTR_ERR(d)); + if ( llc_coloring_enabled && + (rc = domain_set_llc_colors_from_str(d, llc_colors_str)) ) + panic("Error initializing LLC coloring for domain %s (rc = %d)\n", + dt_node_name(node), rc); + d->is_console = true; dt_device_set_used_by(node, d->domain_id); diff --git a/xen/common/llc-coloring.c b/xen/common/llc-coloring.c index 3dd1aaa4df..5a0e3bac59 100644 --- a/xen/common/llc-coloring.c +++ b/xen/common/llc-coloring.c @@ -282,6 +282,46 @@ void domain_llc_coloring_free(struct domain *d) xfree(__va(__pa(d->llc_colors))); } +int __init domain_set_llc_colors_from_str(struct domain *d, const char *str) +{ + int err; + unsigned int *colors, num_colors; + + if ( !str ) + { + domain_set_default_colors(d); + return 0; + } + + colors = xmalloc_array(unsigned int, max_nr_colors); + if ( !colors ) + return -ENOMEM; + + err = parse_color_config(str, colors, max_nr_colors, &num_colors); + if ( err ) + { + printk(XENLOG_ERR "Error parsing LLC color configuration"); + xfree(colors); + return err; + } + + if ( !check_colors(colors, num_colors) ) + { + printk(XENLOG_ERR "%pd: bad LLC color config\n", d); + xfree(colors); + return -EINVAL; + } + + /* Adjust the size cause it was initially set to max_nr_colors */ + d->llc_colors = xrealloc_array(colors, num_colors); + if ( !d->llc_colors ) + d->llc_colors = colors; + + d->num_llc_colors = num_colors; + + return 0; +} + /* * Local variables: * mode: C diff --git a/xen/include/xen/llc-coloring.h b/xen/include/xen/llc-coloring.h index 26353c808a..5d1355b3c9 100644 --- a/xen/include/xen/llc-coloring.h +++ b/xen/include/xen/llc-coloring.h @@ -34,6 +34,7 @@ void arch_llc_coloring_init(void); int dom0_set_llc_colors(struct domain *d); int domain_set_llc_colors(struct domain *d, const struct xen_domctl_set_llc_colors *config); +int domain_set_llc_colors_from_str(struct domain *d, const char *str); #endif /* __XEN_LLC_COLORING_H__ */ diff --git a/xen/include/xen/xmalloc.h b/xen/include/xen/xmalloc.h index b903fa2e26..f0412fb4e0 100644 --- a/xen/include/xen/xmalloc.h +++ b/xen/include/xen/xmalloc.h @@ -37,6 +37,9 @@ ((_type *)_xmalloc_array(sizeof(_type), __alignof__(_type), _num)) #define xzalloc_array(_type, _num) \ ((_type *)_xzalloc_array(sizeof(_type), __alignof__(_type), _num)) +#define xrealloc_array(_ptr, _num) \ + ((typeof(_ptr))_xrealloc_array(_ptr, sizeof(typeof(*(_ptr))), \ + __alignof__(typeof(*(_ptr))), _num)) /* Allocate space for a structure with a flexible array of typed objects. */ #define xzalloc_flex_struct(type, field, nr) \ @@ -98,6 +101,15 @@ static inline void *_xzalloc_array( return _xzalloc(size * num, align); } +static inline void *_xrealloc_array( + void *ptr, unsigned long size, unsigned long align, unsigned long num) +{ + /* Check for overflow. */ + if ( size && num > UINT_MAX / size ) + return NULL; + return _xrealloc(ptr, size * num, align); +} + /* * Pooled allocator interface. */ From patchwork Tue Dec 17 17:06:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlo Nonato X-Patchwork-Id: 13912215 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 8E78DE7717F for ; Tue, 17 Dec 2024 17:06:59 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.859580.1271758 (Exim 4.92) (envelope-from ) id 1tNb23-0007wa-SD; Tue, 17 Dec 2024 17:06:51 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 859580.1271758; Tue, 17 Dec 2024 17:06:51 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tNb23-0007wM-LQ; Tue, 17 Dec 2024 17:06:51 +0000 Received: by outflank-mailman (input) for mailman id 859580; Tue, 17 Dec 2024 17:06:50 +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 1tNb22-0006B0-22 for xen-devel@lists.xenproject.org; Tue, 17 Dec 2024 17:06:50 +0000 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [2a00:1450:4864:20::636]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 4de667a8-bc99-11ef-a0d6-8be0dac302b0; Tue, 17 Dec 2024 18:06:49 +0100 (CET) Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-aa689a37dd4so1053199566b.3 for ; Tue, 17 Dec 2024 09:06:49 -0800 (PST) Received: from carlo-ubuntu.home.arpa (host-95-230-250-178.business.telecomitalia.it. [95.230.250.178]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aab963910f7sm461362666b.166.2024.12.17.09.06.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Dec 2024 09:06:48 -0800 (PST) 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: 4de667a8-bc99-11ef-a0d6-8be0dac302b0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=minervasys-tech.20230601.gappssmtp.com; s=20230601; t=1734455209; x=1735060009; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=TrreipUOqvu+e4eZhr8owwgZAEd1TzrMBSTgToeJEUs=; b=u5tVwYtQFHX0aTcLsu/aXOfStxknP2Af0R7Wi92nfYNeUDHIxpFSfW9gKVzMc2goAH wuUXan7318HACfcXb2fYQ0OxFHl59qdl8waAimMu3RcCMHjECA1+VuKSeod89SKjz/cP ICTI5MAQc01l5qjwSyyO7QGNxsEL6T4CHdJOZtYnyh86q6B77xkLOqspWEBcxb6WX+0S sySpJknP1oMi5lonnYj1Su7GO7ALwIJaDZ1NFmnBBCUMm36gAywvg3yvsb64DqDubUJ0 2S9uPsU7F4lR7OSnSgCLxzTB9SVSAjuEQoBIMaS0m1FXG1nWOtXh0nr7IV3Yhk/WyCtH fXTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734455209; x=1735060009; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TrreipUOqvu+e4eZhr8owwgZAEd1TzrMBSTgToeJEUs=; b=ODNbS5mf4AjbkPVUjGayu2g+38JANRpL1np7CtCVMbGHUwgmXjg4KpwtMq51zNnlLy rMda/nizn11NH9nT/lHQe6XyY7p9Md1zydFa+idcnX8e6CjADSBE2og6JPnqYig+y5/U 7ae2iCvP7F/Gd2I45LegYPYrQ+hJEJew8OsXoaYQXF5FOOFzuCajp1M2wEI36xQ+4nr/ ktgMe7IpDkMrSfw7LloSaPYSAtlQqsMOEJf2QDKLPQ6I6hjYqdfW+67GbmbSEIdhFEwQ lv8MB0Ewh1x07imWUBxI+Djo3thcA/HwqJQIrJIGjKCV49rEclY6zkNw0bRujw75rcCc qvSQ== X-Gm-Message-State: AOJu0YwidKP2XkGC8JD1Lu1zuY0h6+LoWKQyLg2LPV/ufxoWbfwrYAvn EEFZm7eH3XBhMzZXYEpm4esNGNeOafl2wTAmVQTjE228jmyKSgec+Ia+s/WMGEld+uGPsIT/6ky Q+p4= X-Gm-Gg: ASbGncsQ69F5bUj8miI/DZGU0KAZJZ0z5dtTkLz+Wvq5K69jRnYGS6/fWon98SS7rUH uNIBh7o+Fyfrs6KY9BFJP4d8H8ojYZ3tN8/hGpAuGv3mBdp7QPhRsSwdBC5X+RG9mZDnmuH+N9O 19fVvLmErrn4ylj3KBgxJVGcJEa6ySXlXb3TnzMhWDSQi2LoGBXAIMw7XIL6DzL7t4fyWMOpzgc kNdfccmtxlGOyyO/ULZpyCT67wl5t7L91IfYFgzccA3DBgjlxWwmLI2AUP4oS6xIf+p4XCuqutu Z2Gb7Inyx3+IMXWM63gYyd9adIV4UcRwYWtYCn8+P7g2/6GSuGyYzkCyTZA= X-Google-Smtp-Source: AGHT+IFuCSa53dAt3X2uYHYt3kSIWw1yJmJ/rrSQ+FLSFp9jh3JwOm6m4Wkm74+jmHFUlcpIQOHKYA== X-Received: by 2002:a17:906:23e1:b0:aa6:96be:2bed with SMTP id a640c23a62f3a-aab77ee8ae6mr1461073066b.59.1734455208785; Tue, 17 Dec 2024 09:06:48 -0800 (PST) From: Carlo Nonato To: xen-devel@lists.xenproject.org Cc: andrea.bastoni@minervasys.tech, marco.solieri@minervasys.tech, Carlo Nonato , Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Stefano Stabellini Subject: [PATCH v13 08/12] xen/page_alloc: introduce preserved page flags macro Date: Tue, 17 Dec 2024 18:06:33 +0100 Message-ID: <20241217170637.233097-9-carlo.nonato@minervasys.tech> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241217170637.233097-1-carlo.nonato@minervasys.tech> References: <20241217170637.233097-1-carlo.nonato@minervasys.tech> MIME-Version: 1.0 PGC_static and PGC_extra need to be preserved when assigning a page. Define a new macro that groups those flags and use it instead of or'ing every time. Signed-off-by: Carlo Nonato Reviewed-by: Jan Beulich --- v13: - no changes v12: - fixed wrong patch sent in v11 - added comment on PGC_preserved v11: - removed PGC_broken from PGC_preserved - removed PGC preservation from mark_page_free() v10: - fixed commit message v9: - add PGC_broken to PGC_preserved - clear PGC_extra in alloc_domheap_pages() only if MEMF_no_refcount is set v8: - fixed PGC_extra ASSERT fail in alloc_domheap_pages() by removing PGC_extra before freeing v7: - PGC_preserved used also in mark_page_free() v6: - preserved_flags renamed to PGC_preserved - PGC_preserved is used only in assign_pages() v5: - new patch --- xen/common/page_alloc.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c index da505a79f6..6dc8e0d5c9 100644 --- a/xen/common/page_alloc.c +++ b/xen/common/page_alloc.c @@ -161,6 +161,10 @@ #endif #define PGC_no_buddy_merge PGC_static +/* + * Flags that are preserved in assign_pages() (and only there) + */ +#define PGC_preserved (PGC_extra | PGC_static) #ifndef PGT_TYPE_INFO_INITIALIZER #define PGT_TYPE_INFO_INITIALIZER 0 @@ -2385,7 +2389,7 @@ int assign_pages( for ( i = 0; i < nr; i++ ) { - ASSERT(!(pg[i].count_info & ~(PGC_extra | PGC_static))); + ASSERT(!(pg[i].count_info & ~PGC_preserved)); if ( pg[i].count_info & PGC_extra ) extra_pages++; } @@ -2445,7 +2449,7 @@ int assign_pages( page_set_owner(&pg[i], d); smp_wmb(); /* Domain pointer must be visible before updating refcnt. */ pg[i].count_info = - (pg[i].count_info & (PGC_extra | PGC_static)) | PGC_allocated | 1; + (pg[i].count_info & PGC_preserved) | PGC_allocated | 1; page_list_add_tail(&pg[i], page_to_list(d, &pg[i])); } From patchwork Tue Dec 17 17:06:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlo Nonato X-Patchwork-Id: 13912225 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 DD9BDE77187 for ; Tue, 17 Dec 2024 17:07:03 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.859586.1271783 (Exim 4.92) (envelope-from ) id 1tNb26-00008L-FM; Tue, 17 Dec 2024 17:06:54 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 859586.1271783; Tue, 17 Dec 2024 17:06:54 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tNb26-00006U-5y; Tue, 17 Dec 2024 17:06:54 +0000 Received: by outflank-mailman (input) for mailman id 859586; Tue, 17 Dec 2024 17:06:53 +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 1tNb24-0006gw-PN for xen-devel@lists.xenproject.org; Tue, 17 Dec 2024 17:06:52 +0000 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [2a00:1450:4864:20::42e]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 4eb8922c-bc99-11ef-99a3-01e77a169b0f; Tue, 17 Dec 2024 18:06:51 +0100 (CET) Received: by mail-wr1-x42e.google.com with SMTP id ffacd0b85a97d-3862ca8e0bbso4776832f8f.0 for ; Tue, 17 Dec 2024 09:06:51 -0800 (PST) Received: from carlo-ubuntu.home.arpa (host-95-230-250-178.business.telecomitalia.it. [95.230.250.178]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aab963910f7sm461362666b.166.2024.12.17.09.06.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Dec 2024 09:06:49 -0800 (PST) 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: 4eb8922c-bc99-11ef-99a3-01e77a169b0f DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=minervasys-tech.20230601.gappssmtp.com; s=20230601; t=1734455210; x=1735060010; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gl0EWK+FsqsA8S/JacV6oWuRXTqVhZbd3MTd0ok1lAk=; b=OqWc9TGVVVLWMl/pOgNjo8+SexQ8XMbiR9DHGARq5IxPiRFPi1OdB+H2OR3inqFEFo Z6UVJs8q4Y1+A0ypBZou4fiPeA4T0pfF86CD3W3H4rK8EJ2zXpwTsj04D5Jg8wmhpPc/ QgSncnMt5zyn5psPFY9fHS/n/1MjhuuEHMGZGPwGWbCLufjUDOLHgIEd3RNdo0WYA2zU nNR/gqox2ih+cdx9Z076VKdXqS0qVwnjrbTDHeJrJUSdhLiMvrN+x3lpsuACyp/Kj13n JoPSo34XAFTBG+Hu81z4QSG4oP4mjJtOW/Svx5sAwiAjep7S6xLznJ/ZDn187UUVTyRi H7bQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734455210; x=1735060010; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gl0EWK+FsqsA8S/JacV6oWuRXTqVhZbd3MTd0ok1lAk=; b=mmXRJk5rW1LZCGtoIyYDTRCRyadmOm22P+Wyn8SEpblgg/WaViwTOp80Uw6Pv4cnUV UYSk4NexR4uxf2w8BuqwMqmme2s3bMCq7ILNO11M5JDPgAvfTMD7po07AlNmYPQWSniZ bW7e+AKUAk+xda9Lp8W7ENXlnRNsZfVErrZj44C7byT2IEx9xggm5RiVgXw7HqwTf7tK zsP5lzXScFPoGEGBaKkl+aUbb4XrrF4gD3IknzH7/W1w6rl9ZVnqLe7DQpBtOM3iDStn TPwUK6R7cZztZLOvbgd3mt4KhF0EWbMRtGdIOHlpG3w/SQxrtHmbRXSSf7KgDhUw+Zj0 Fo7g== X-Gm-Message-State: AOJu0Yzo4VPYzNNtJWxloEd4to7qvYA9XSjYAPQVFg/mtzWOH5pAIDj7 nhW+uZ9Tn8Ykz3PqfLkN0bV4KpSIKNohZ2wOScAUlxWl5sJC0hI/v4QWLC6w2ME4qbjRhvyI0nI dMuU= X-Gm-Gg: ASbGnctPCAWmXke5huvMS35a//SgUi+ywA7Xfl0lSuNbw4UhMeAttYfaTvlEhVhhuv7 wnl5EQgzI7x+NE6Vy+HWRKqEGd+zq1+Z+AI6HEc6RqQ2VT4hRXw1YJETWTtJrWhGMFwrTTyJmHz /hwdtNkgYnSE9aKS1IyfFfRql4cJzuP7VFMYtiiNyX8NDES+XNSdGtIHo3Vbz27Te7m1K0sUGSw 0McYsDz15QnsfxrViyfl+Ogbupab0jDJptWSq/xJ2Gho7Dnm8lg7TyJi1brD+j1KmzBD5a4hBHM E5O/XPejyzgEDJTuSmQbn2EfDEMRdAvmUsukUFWAiWdKtW6v0MzFbGc4VMg= X-Google-Smtp-Source: AGHT+IEA6MIhAvKEwTYcmgKvX7UoPESWauyCPfWXwAADDZ9H4qmQygfaiBWhcPFXfMEQVZCURopTVg== X-Received: by 2002:a05:6000:4028:b0:385:e2d5:cdf2 with SMTP id ffacd0b85a97d-38880ad9012mr15621029f8f.19.1734455209858; Tue, 17 Dec 2024 09:06:49 -0800 (PST) From: Carlo Nonato To: xen-devel@lists.xenproject.org Cc: andrea.bastoni@minervasys.tech, marco.solieri@minervasys.tech, Carlo Nonato , Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Stefano Stabellini , Bertrand Marquis , Volodymyr Babchuk Subject: [PATCH v13 09/12] xen: add cache coloring allocator for domains Date: Tue, 17 Dec 2024 18:06:34 +0100 Message-ID: <20241217170637.233097-10-carlo.nonato@minervasys.tech> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241217170637.233097-1-carlo.nonato@minervasys.tech> References: <20241217170637.233097-1-carlo.nonato@minervasys.tech> MIME-Version: 1.0 Add a new memory page allocator that implements the cache coloring mechanism. The allocation algorithm enforces equal frequency distribution of cache partitions, following the coloring configuration of a domain. This allows for an even utilization of cache sets for every domain. Pages are stored in a color-indexed array of lists. Those lists are filled by a simple init function which computes the color of each page. When a domain requests a page, the allocator extracts the page from the list with the maximum number of free pages among those that the domain can access, given its coloring configuration. The allocator can only handle requests of order-0 pages. This allows for easier implementation and since cache coloring targets only embedded systems, it's assumed not to be a major problem. The buddy allocator must coexist with the colored one because the Xen heap isn't colored. For this reason a new Kconfig option and a command line parameter are added to let the user set the amount of memory reserved for the buddy allocator. Even when cache coloring is enabled, this memory isn't managed by the colored allocator. Colored heap information is dumped in the dump_heap() debug-key function. Based on original work from: Luca Miccio Signed-off-by: Marco Solieri Signed-off-by: Carlo Nonato Reviewed-by: Jan Beulich Acked-by: Michal Orzel --- v13: - no changes v12: - fixed #ifdef CONFIG_LLC_COLORING in init_color_heap_pages() v11: - CONFIG_BUDDY_ALLOCATOR_SIZE depends on CONFIG_LLC_COLORING - buddy_alloc_size is defined only if CONFIG_LLC_COLORING - buddy-alloc-size param is parsed only if CONFIG_LLC_COLORING v10: - stated explicit dependency on CONFIG_LLC_COLORING for buddy-alloc-size - fix for MISRA rule 20.7 parenthesis v9: - added ASSERT(order == 0) when freeing a colored page - moved buddy_alloc_size initialization logic in Kconfig v8: - requests that uses MEMF_* flags that can't be served are now going to fail - free_color_heap_page() is called directly from free_heap_pages() v7: - requests to alloc_color_heap_page() now fail if MEMF_bits is used v6: - colored allocator functions are now static v5: - Carlo Nonato as the new author - the colored allocator balances color usage for each domain and it searches linearly only in the number of colors (FIXME removed) - addedd scrub functionality - removed stub functions (still requires some macro definition) - addr_to_color turned to mfn_to_color for easier operations - removed BUG_ON in init_color_heap_pages() in favor of panic() - only non empty page lists are logged in dump_color_heap() v4: - moved colored allocator code after buddy allocator because it now has some dependencies on buddy functions - buddy_alloc_size is now used only by the colored allocator - fixed a bug that allowed the buddy to merge pages when they were colored - free_color_heap_page() now calls mark_page_free() - free_color_heap_page() uses of the frametable array for faster searches - added FIXME comment for the linear search in free_color_heap_page() - removed alloc_color_domheap_page() to let the colored allocator exploit some more buddy allocator code - alloc_color_heap_page() now allocs min address pages first - reduced the mess in end_boot_allocator(): use the first loop for init_color_heap_pages() - fixed page_list_add_prev() (list.h) since it was doing the opposite of what it was supposed to do - fixed page_list_add_prev() (non list.h) to check also for next existence - removed unused page_list_add_next() - moved p2m code in another patch --- docs/misc/cache-coloring.rst | 37 ++++++ docs/misc/xen-command-line.pandoc | 14 +++ xen/arch/arm/include/asm/mm.h | 5 + xen/common/Kconfig | 8 ++ xen/common/llc-coloring.c | 13 ++ xen/common/page_alloc.c | 191 +++++++++++++++++++++++++++++- xen/include/xen/llc-coloring.h | 3 + 7 files changed, 267 insertions(+), 4 deletions(-) diff --git a/docs/misc/cache-coloring.rst b/docs/misc/cache-coloring.rst index e097e74032..5224b27afe 100644 --- a/docs/misc/cache-coloring.rst +++ b/docs/misc/cache-coloring.rst @@ -13,6 +13,9 @@ To compile LLC coloring support set ``CONFIG_LLC_COLORING=y``. If needed, change the maximum number of colors with ``CONFIG_LLC_COLORS_ORDER=``. +If needed, change the buddy allocator reserved size with +``CONFIG_BUDDY_ALLOCATOR_SIZE=``. + Runtime configuration is done via `Command line parameters`_. For DomUs follow `DomUs configuration`_. @@ -110,6 +113,8 @@ Specific documentation is available at `docs/misc/xen-command-line.pandoc`. +----------------------+-------------------------------+ | ``dom0-llc-colors`` | Dom0 color configuration | +----------------------+-------------------------------+ +| ``buddy-alloc-size`` | Buddy allocator reserved size | ++----------------------+-------------------------------+ Colors selection format *********************** @@ -197,6 +202,17 @@ For example: **Note:** If no color configuration is provided for a domain, the default one, which corresponds to all available colors is used instead. +Colored allocator and buddy allocator +************************************* + +The colored allocator distributes pages based on color configurations of +domains so that each domains only gets pages of its own colors. +The colored allocator is meant as an alternative to the buddy allocator because +its allocation policy is by definition incompatible with the generic one. Since +the Xen heap is not colored yet, we need to support the coexistence of the two +allocators and some memory must be left for the buddy one. Buddy memory +reservation is configured via Kconfig or via command-line. + Known issues and limitations **************************** @@ -207,3 +223,24 @@ In the domain configuration, "xen,static-mem" allows memory to be statically allocated to the domain. This isn't possible when LLC coloring is enabled, because that memory can't be guaranteed to use only colors assigned to the domain. + +Cache coloring is intended only for embedded systems +#################################################### + +The current implementation aims to satisfy the need of predictability in +embedded systems with small amount of memory to be managed in a colored way. +Given that, some shortcuts are taken in the development. Expect worse +performances on larger systems. + +Colored allocator can only make use of order-0 pages +#################################################### + +The cache coloring technique relies on memory mappings and on the smallest +mapping granularity to achieve the maximum number of colors (cache partitions) +possible. This granularity is what is normally called a page and, in Xen +terminology, the order-0 page is the smallest one. The fairly simple +colored allocator currently implemented, makes use only of such pages. +It must be said that a more complex one could, in theory, adopt higher order +pages if the colors selection contained adjacent colors. Two subsequent colors, +for example, can be represented by an order-1 page, four colors correspond to +an order-2 page, etc. diff --git a/docs/misc/xen-command-line.pandoc b/docs/misc/xen-command-line.pandoc index be70d6af61..de5d68a452 100644 --- a/docs/misc/xen-command-line.pandoc +++ b/docs/misc/xen-command-line.pandoc @@ -270,6 +270,20 @@ and not running softirqs. Reduce this if softirqs are not being run frequently enough. Setting this to a high value may cause boot failure, particularly if the NMI watchdog is also enabled. +### buddy-alloc-size (arm64) +> `= ` + +> Default: `64M` + +Amount of memory reserved for the buddy allocator when colored allocator is +active. This option is available only when `CONFIG_LLC_COLORING` is enabled. +The colored allocator is meant as an alternative to the buddy allocator, +because its allocation policy is by definition incompatible with the generic +one. Since the Xen heap systems is not colored yet, we need to support the +coexistence of the two allocators for now. This parameter, which is optional +and for expert only, it's used to set the amount of memory reserved to the +buddy allocator. + ### cet = List of [ shstk=, ibt= ] diff --git a/xen/arch/arm/include/asm/mm.h b/xen/arch/arm/include/asm/mm.h index 59b774b7b8..f91ff088f6 100644 --- a/xen/arch/arm/include/asm/mm.h +++ b/xen/arch/arm/include/asm/mm.h @@ -145,6 +145,11 @@ struct page_info #else #define PGC_static 0 #endif +#ifdef CONFIG_LLC_COLORING +/* Page is cache colored */ +#define _PGC_colored PG_shift(4) +#define PGC_colored PG_mask(1, 4) +#endif /* ... */ /* Page is broken? */ #define _PGC_broken PG_shift(7) diff --git a/xen/common/Kconfig b/xen/common/Kconfig index b4ec6893be..6166327f4d 100644 --- a/xen/common/Kconfig +++ b/xen/common/Kconfig @@ -537,4 +537,12 @@ config LLC_COLORS_ORDER The default value corresponds to an 8 MiB 16-ways LLC, which should be more than what's needed in the general case. +config BUDDY_ALLOCATOR_SIZE + int "Buddy allocator reserved memory size (MiB)" + default "64" + depends on LLC_COLORING + help + Amount of memory reserved for the buddy allocator to serve Xen heap, + working alongside the colored one. + endmenu diff --git a/xen/common/llc-coloring.c b/xen/common/llc-coloring.c index 5a0e3bac59..bebd6e2056 100644 --- a/xen/common/llc-coloring.c +++ b/xen/common/llc-coloring.c @@ -36,6 +36,9 @@ static unsigned int __ro_after_init default_colors[NR_LLC_COLORS]; static unsigned int __initdata dom0_colors[NR_LLC_COLORS]; static unsigned int __initdata dom0_num_colors; +#define mfn_color_mask (max_nr_colors - 1) +#define mfn_to_color(mfn) (mfn_x(mfn) & mfn_color_mask) + /* * Parse the coloring configuration given in the buf string, following the * syntax below. @@ -322,6 +325,16 @@ int __init domain_set_llc_colors_from_str(struct domain *d, const char *str) return 0; } +unsigned int page_to_llc_color(const struct page_info *pg) +{ + return mfn_to_color(page_to_mfn(pg)); +} + +unsigned int get_max_nr_llc_colors(void) +{ + return max_nr_colors; +} + /* * Local variables: * mode: C diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c index 6dc8e0d5c9..1bf070c8c5 100644 --- a/xen/common/page_alloc.c +++ b/xen/common/page_alloc.c @@ -139,6 +139,7 @@ #include #include #include +#include #include #include @@ -160,11 +161,15 @@ #define PGC_static 0 #endif -#define PGC_no_buddy_merge PGC_static +#ifndef PGC_colored +#define PGC_colored 0 +#endif + +#define PGC_no_buddy_merge (PGC_static | PGC_colored) /* * Flags that are preserved in assign_pages() (and only there) */ -#define PGC_preserved (PGC_extra | PGC_static) +#define PGC_preserved (PGC_extra | PGC_static | PGC_colored) #ifndef PGT_TYPE_INFO_INITIALIZER #define PGT_TYPE_INFO_INITIALIZER 0 @@ -1480,6 +1485,8 @@ static bool mark_page_free(struct page_info *pg, mfn_t mfn) return pg_offlined; } +static void free_color_heap_page(struct page_info *pg, bool need_scrub); + /* Free 2^@order set of pages. */ static void free_heap_pages( struct page_info *pg, unsigned int order, bool need_scrub) @@ -1504,6 +1511,15 @@ static void free_heap_pages( pg[i].count_info |= PGC_need_scrub; poison_one_page(&pg[i]); } + + if ( pg->count_info & PGC_colored ) + { + ASSERT(order == 0); + + free_color_heap_page(pg, need_scrub); + spin_unlock(&heap_lock); + return; + } } avail[node][zone] += 1 << order; @@ -1968,6 +1984,157 @@ static unsigned long avail_heap_pages( return free_pages; } +/************************* + * COLORED SIDE-ALLOCATOR + * + * Pages are grouped by LLC color in lists which are globally referred to as the + * color heap. Lists are populated in end_boot_allocator(). + * After initialization there will be N lists where N is the number of + * available colors on the platform. + */ +static struct page_list_head *__ro_after_init _color_heap; +#define color_heap(color) (&_color_heap[color]) + +static unsigned long *__ro_after_init free_colored_pages; + +#ifdef CONFIG_LLC_COLORING +#define domain_num_llc_colors(d) ((d)->num_llc_colors) +#define domain_llc_color(d, i) ((d)->llc_colors[i]) + +/* Memory required for buddy allocator to work with colored one */ +static unsigned long __initdata buddy_alloc_size = + MB(CONFIG_BUDDY_ALLOCATOR_SIZE); +size_param("buddy-alloc-size", buddy_alloc_size); +#else +#define domain_num_llc_colors(d) 0 +#define domain_llc_color(d, i) 0 +#endif + +static void free_color_heap_page(struct page_info *pg, bool need_scrub) +{ + unsigned int color; + + color = page_to_llc_color(pg); + free_colored_pages[color]++; + /* + * Head insertion allows re-using cache-hot pages in configurations without + * sharing of colors. + */ + page_list_add(pg, color_heap(color)); +} + +static struct page_info *alloc_color_heap_page(unsigned int memflags, + const struct domain *d) +{ + struct page_info *pg = NULL; + unsigned int i, color = 0; + unsigned long max = 0; + bool need_tlbflush = false; + uint32_t tlbflush_timestamp = 0; + bool need_scrub; + + if ( memflags & ~(MEMF_no_refcount | MEMF_no_owner | MEMF_no_tlbflush | + MEMF_no_icache_flush | MEMF_no_scrub) ) + return NULL; + + spin_lock(&heap_lock); + + for ( i = 0; i < domain_num_llc_colors(d); i++ ) + { + unsigned long free = free_colored_pages[domain_llc_color(d, i)]; + + if ( free > max ) + { + color = domain_llc_color(d, i); + pg = page_list_first(color_heap(color)); + max = free; + } + } + + if ( !pg ) + { + spin_unlock(&heap_lock); + return NULL; + } + + need_scrub = pg->count_info & PGC_need_scrub; + pg->count_info = PGC_state_inuse | (pg->count_info & PGC_colored); + free_colored_pages[color]--; + page_list_del(pg, color_heap(color)); + + if ( !(memflags & MEMF_no_tlbflush) ) + accumulate_tlbflush(&need_tlbflush, pg, &tlbflush_timestamp); + + init_free_page_fields(pg); + + spin_unlock(&heap_lock); + + if ( !(memflags & MEMF_no_scrub) ) + { + if ( need_scrub ) + scrub_one_page(pg); + else + check_one_page(pg); + } + + if ( need_tlbflush ) + filtered_flush_tlb_mask(tlbflush_timestamp); + + flush_page_to_ram(mfn_x(page_to_mfn(pg)), + !(memflags & MEMF_no_icache_flush)); + + return pg; +} + +static void __init init_color_heap_pages(struct page_info *pg, + unsigned long nr_pages) +{ + unsigned long i; + bool need_scrub = opt_bootscrub == BOOTSCRUB_IDLE; + +#ifdef CONFIG_LLC_COLORING + if ( buddy_alloc_size >= PAGE_SIZE ) + { + unsigned long buddy_pages = min(PFN_DOWN(buddy_alloc_size), nr_pages); + + init_heap_pages(pg, buddy_pages); + nr_pages -= buddy_pages; + buddy_alloc_size -= buddy_pages << PAGE_SHIFT; + pg += buddy_pages; + } +#endif + + if ( !_color_heap ) + { + unsigned int max_nr_colors = get_max_nr_llc_colors(); + + _color_heap = xvmalloc_array(struct page_list_head, max_nr_colors); + free_colored_pages = xvzalloc_array(unsigned long, max_nr_colors); + if ( !_color_heap || !free_colored_pages ) + panic("Can't allocate colored heap. Buddy reserved size is too low"); + + for ( i = 0; i < max_nr_colors; i++ ) + INIT_PAGE_LIST_HEAD(color_heap(i)); + } + + for ( i = 0; i < nr_pages; i++ ) + { + pg[i].count_info = PGC_colored; + free_color_heap_page(&pg[i], need_scrub); + } +} + +static void dump_color_heap(void) +{ + unsigned int color; + + printk("Dumping color heap info\n"); + for ( color = 0; color < get_max_nr_llc_colors(); color++ ) + if ( free_colored_pages[color] > 0 ) + printk("Color heap[%u]: %lu pages\n", + color, free_colored_pages[color]); +} + void __init end_boot_allocator(void) { unsigned int i; @@ -1987,7 +2154,13 @@ void __init end_boot_allocator(void) for ( i = nr_bootmem_regions; i-- > 0; ) { struct bootmem_region *r = &bootmem_region_list[i]; - if ( r->s < r->e ) + + if ( r->s >= r->e ) + continue; + + if ( llc_coloring_enabled ) + init_color_heap_pages(mfn_to_page(_mfn(r->s)), r->e - r->s); + else init_heap_pages(mfn_to_page(_mfn(r->s)), r->e - r->s); } nr_bootmem_regions = 0; @@ -2483,7 +2656,14 @@ struct page_info *alloc_domheap_pages( if ( memflags & MEMF_no_owner ) memflags |= MEMF_no_refcount; - if ( !dma_bitsize ) + /* Only domains are supported for coloring */ + if ( d && llc_coloring_enabled ) + { + /* Colored allocation must be done on 0 order */ + if ( order || (pg = alloc_color_heap_page(memflags, d)) == NULL ) + return NULL; + } + else if ( !dma_bitsize ) memflags &= ~MEMF_no_dma; else if ( (dma_zone = bits_to_zone(dma_bitsize)) < zone_hi ) pg = alloc_heap_pages(dma_zone + 1, zone_hi, order, memflags, d); @@ -2686,6 +2866,9 @@ static void cf_check dump_heap(unsigned char key) continue; printk("Node %d has %lu unscrubbed pages\n", i, node_need_scrub[i]); } + + if ( llc_coloring_enabled ) + dump_color_heap(); } static __init int cf_check register_heap_trigger(void) diff --git a/xen/include/xen/llc-coloring.h b/xen/include/xen/llc-coloring.h index 5d1355b3c9..1216d1fbd4 100644 --- a/xen/include/xen/llc-coloring.h +++ b/xen/include/xen/llc-coloring.h @@ -11,6 +11,7 @@ #include struct domain; +struct page_info; struct xen_domctl_set_llc_colors; #ifdef CONFIG_LLC_COLORING @@ -35,6 +36,8 @@ int dom0_set_llc_colors(struct domain *d); int domain_set_llc_colors(struct domain *d, const struct xen_domctl_set_llc_colors *config); int domain_set_llc_colors_from_str(struct domain *d, const char *str); +unsigned int page_to_llc_color(const struct page_info *pg); +unsigned int get_max_nr_llc_colors(void); #endif /* __XEN_LLC_COLORING_H__ */ From patchwork Tue Dec 17 17:06:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlo Nonato X-Patchwork-Id: 13912224 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 1582FE7717F for ; Tue, 17 Dec 2024 17:07:02 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.859585.1271777 (Exim 4.92) (envelope-from ) id 1tNb25-0008TY-Te; Tue, 17 Dec 2024 17:06:53 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 859585.1271777; Tue, 17 Dec 2024 17:06:53 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tNb25-0008Rw-Mh; Tue, 17 Dec 2024 17:06:53 +0000 Received: by outflank-mailman (input) for mailman id 859585; Tue, 17 Dec 2024 17:06:52 +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 1tNb24-0006B0-Bg for xen-devel@lists.xenproject.org; Tue, 17 Dec 2024 17:06:52 +0000 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [2a00:1450:4864:20::630]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 4f2c7e7a-bc99-11ef-a0d6-8be0dac302b0; Tue, 17 Dec 2024 18:06:51 +0100 (CET) Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-aa67333f7d2so827497566b.0 for ; Tue, 17 Dec 2024 09:06:51 -0800 (PST) Received: from carlo-ubuntu.home.arpa (host-95-230-250-178.business.telecomitalia.it. [95.230.250.178]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aab963910f7sm461362666b.166.2024.12.17.09.06.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Dec 2024 09:06:50 -0800 (PST) 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: 4f2c7e7a-bc99-11ef-a0d6-8be0dac302b0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=minervasys-tech.20230601.gappssmtp.com; s=20230601; t=1734455211; x=1735060011; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+yFCjWByWBlLWohsvm0nzKlA6dcBQUEk8oUoZi9smWw=; b=ygsN4MQQdbZFfzJamLemuXmUg4Jtk0YHJLsPDcPLVBA1CsQPkNBgacxd9txHiiGVlZ 1Q7XU/oziZHE8gW3pt/zqoE/9dymbAmHAj30Hlv99ugO1tQDVyLBEJHGiYzLXgujQxTZ 3eS0EE3lF6qTESR2LsNsnpcIhcIHscgtO/iKSCcICyTkYJ4ebCJumCFKRcHI3Q3rwIsa OMWp+ouqtSUHUaiMYN0ltnvnvt4iL9h8fVtLkLJ8IVm98H7WDiXuaqvubQs6hTP0GaTM EjRE6Vv1XKBQG2GQWb5p8O+SVWc9s5Qv0xgckk8Tp8f1HP8lQlOjooiVuDVPO7uSDxFm hTew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734455211; x=1735060011; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+yFCjWByWBlLWohsvm0nzKlA6dcBQUEk8oUoZi9smWw=; b=komAIIYbmppXpXuz50F5Hk2W53XEnXaX9cLDVdQp3PluK5UQCa66yWjhv+l7OfR8YT 8BmYJ/ydLSYbf+yjdEvRhE7sYwnyfyR6EAu+uhYJzFd0L6lO/1Pk50SMUG/Jhd5nHvfy u1E/C63zGJJOdhVKuLtVWTAbztC9tVYkK9T7ztFQDx+87hVqj1MhKoXNsBOxe6FhxVWR e2MOyMqC1LzJEF7cq6YQXNENKxqQ4glqSPkQfu2/G9UaW4iUe6g2AoW0Omkey+pXEZRQ HUuOWoYSzqMyZAUYPj0t4sh8KAd6YkquVCm2c0r1kTWssUj+I8YXXeAj9fmNJGpDnqAn g3gA== X-Gm-Message-State: AOJu0YyREIHe8Eu0OTRIZu0S4zZB16Z7OIgutMJPwJ6D+Z/RStJq52Ru DTOUMU63mS50A97VQPGdNyLYbw5KWIzP2Uj+JqrcG700oP/tjQyOup7bMPUFGBIrHFJ+ep2lf+7 nfMw= X-Gm-Gg: ASbGncv1idgT3CDKCtk5WRaMlraNU51Q+0TirWnmCtgIXEqJrRNXmtbEeIingXxngwf Lr1HLtI+l53bC4iusq/05Xe7zuBtFr2VlB3w3pSPCM3x8RDXcifCbxddTAsQxHhieP2Ymf8hxdv 6siwChG/U3MVQIzU7FQQFN6VU97OLj/uXB6QL9bMX0dwLThN9XKwPA8EqhG9AlHQgrs8hPV9pjh P8KgsDt1SMLJ8K2yKuEFwZSWiO+dBUkAegRhX+3ecFXZZCW6F6H026NXJgFtJ4d8W0SZvjDgsUv dXCf0nYm/Nf0Bzu57BvW6k1Oyq+ntHJGzKVztQuhNJ+bp99ml3pV85f3ixI= X-Google-Smtp-Source: AGHT+IHABKJ8nQpchEGx4HHrgDQ1znx3h1yf5DTmFdzEj9pREyPSZ0pPO4Cj5rldUVQkqa0/rsr9zQ== X-Received: by 2002:a17:907:d24:b0:aa6:6b7c:8a62 with SMTP id a640c23a62f3a-aabf1c8552emr9481566b.38.1734455210924; Tue, 17 Dec 2024 09:06:50 -0800 (PST) From: Carlo Nonato To: xen-devel@lists.xenproject.org Cc: andrea.bastoni@minervasys.tech, marco.solieri@minervasys.tech, Luca Miccio , Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , Stefano Stabellini , Carlo Nonato Subject: [PATCH v13 10/12] xen/arm: add Xen cache colors command line parameter Date: Tue, 17 Dec 2024 18:06:35 +0100 Message-ID: <20241217170637.233097-11-carlo.nonato@minervasys.tech> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241217170637.233097-1-carlo.nonato@minervasys.tech> References: <20241217170637.233097-1-carlo.nonato@minervasys.tech> MIME-Version: 1.0 From: Luca Miccio Add a new command line parameter to configure Xen cache colors. These colors are dumped together with other coloring info. Benchmarking the VM interrupt response time provides an estimation of LLC usage by Xen's most latency-critical runtime task. Results on Arm Cortex-A53 on Xilinx Zynq UltraScale+ XCZU9EG show that one color, which reserves 64 KiB of L2, is enough to attain best responsiveness: - Xen 1 color latency: 3.1 us - Xen 2 color latency: 3.1 us Since this is the most common target for Arm cache coloring, the default amount of Xen colors is set to one. More colors are instead very likely to be needed on processors whose L1 cache is physically-indexed and physically-tagged, such as Cortex-A57. In such cases, coloring applies to L1 also, and there typically are two distinct L1-colors. Therefore, reserving only one color for Xen would senselessly partitions a cache memory that is already private, i.e. underutilize it. Signed-off-by: Luca Miccio Signed-off-by: Marco Solieri Signed-off-by: Carlo Nonato Reviewed-by: Jan Beulich --- v13: - no changes v12: - no changes v11: - no changes v10: - no changes v9: - no changes v8: - added bound check on xen_colors in llc_coloring_init() v7: - removed XEN_DEFAULT_COLOR - XEN_DEFAULT_NUM_COLORS is now used in a for loop to set xen default colors --- docs/misc/cache-coloring.rst | 2 ++ docs/misc/xen-command-line.pandoc | 10 ++++++++++ xen/common/llc-coloring.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/docs/misc/cache-coloring.rst b/docs/misc/cache-coloring.rst index 5224b27afe..e156062aa2 100644 --- a/docs/misc/cache-coloring.rst +++ b/docs/misc/cache-coloring.rst @@ -115,6 +115,8 @@ Specific documentation is available at `docs/misc/xen-command-line.pandoc`. +----------------------+-------------------------------+ | ``buddy-alloc-size`` | Buddy allocator reserved size | +----------------------+-------------------------------+ +| ``xen-llc-colors`` | Xen color configuration | ++----------------------+-------------------------------+ Colors selection format *********************** diff --git a/docs/misc/xen-command-line.pandoc b/docs/misc/xen-command-line.pandoc index de5d68a452..4f1fde84df 100644 --- a/docs/misc/xen-command-line.pandoc +++ b/docs/misc/xen-command-line.pandoc @@ -2925,6 +2925,16 @@ mode. **WARNING: `x2apic_phys` is deprecated and superseded by `x2apic-mode`. The latter takes precedence if both are set.** +### xen-llc-colors (arm64) +> `= List of [ | - ]` + +> Default: `0: the lowermost color` + +Specify Xen LLC color configuration. This options is available only when +`CONFIG_LLC_COLORING` is enabled. +Two colors are most likely needed on platforms where private caches are +physically indexed, e.g. the L1 instruction cache of the Arm Cortex-A57. + ### xenheap_megabytes (arm32) > `= ` diff --git a/xen/common/llc-coloring.c b/xen/common/llc-coloring.c index bebd6e2056..0cfaa77a2b 100644 --- a/xen/common/llc-coloring.c +++ b/xen/common/llc-coloring.c @@ -13,6 +13,7 @@ #include #define NR_LLC_COLORS (1U << CONFIG_LLC_COLORS_ORDER) +#define XEN_DEFAULT_NUM_COLORS 1 /* * -1: not specified (disabled unless llc-size and llc-nr-ways present) @@ -36,6 +37,9 @@ static unsigned int __ro_after_init default_colors[NR_LLC_COLORS]; static unsigned int __initdata dom0_colors[NR_LLC_COLORS]; static unsigned int __initdata dom0_num_colors; +static unsigned int __ro_after_init xen_colors[NR_LLC_COLORS]; +static unsigned int __ro_after_init xen_num_colors; + #define mfn_color_mask (max_nr_colors - 1) #define mfn_to_color(mfn) (mfn_x(mfn) & mfn_color_mask) @@ -94,6 +98,13 @@ static int __init parse_dom0_colors(const char *s) } custom_param("dom0-llc-colors", parse_dom0_colors); +static int __init parse_xen_colors(const char *s) +{ + return parse_color_config(s, xen_colors, ARRAY_SIZE(xen_colors), + &xen_num_colors); +} +custom_param("xen-llc-colors", parse_xen_colors); + static void print_colors(const unsigned int colors[], unsigned int num_colors) { unsigned int i; @@ -178,6 +189,22 @@ void __init llc_coloring_init(void) for ( i = 0; i < max_nr_colors; i++ ) default_colors[i] = i; + if ( !xen_num_colors ) + { + unsigned int i; + + xen_num_colors = MIN(XEN_DEFAULT_NUM_COLORS, max_nr_colors); + + printk(XENLOG_WARNING + "Xen LLC color config not found. Using first %u colors\n", + xen_num_colors); + for ( i = 0; i < xen_num_colors; i++ ) + xen_colors[i] = i; + } + else if ( xen_num_colors > max_nr_colors || + !check_colors(xen_colors, xen_num_colors) ) + panic("Bad LLC color config for Xen\n"); + arch_llc_coloring_init(); } @@ -188,6 +215,8 @@ void dump_llc_coloring_info(void) printk("LLC coloring info:\n"); printk(" Number of LLC colors supported: %u\n", max_nr_colors); + printk(" Xen LLC colors (%u): ", xen_num_colors); + print_colors(xen_colors, xen_num_colors); } void domain_dump_llc_colors(const struct domain *d) From patchwork Tue Dec 17 17:06:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlo Nonato X-Patchwork-Id: 13912240 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 51615E77184 for ; Tue, 17 Dec 2024 17:14:02 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.859690.1271818 (Exim 4.92) (envelope-from ) id 1tNb8t-0006Nv-6O; Tue, 17 Dec 2024 17:13:55 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 859690.1271818; Tue, 17 Dec 2024 17:13:55 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tNb8t-0006Nn-2f; Tue, 17 Dec 2024 17:13:55 +0000 Received: by outflank-mailman (input) for mailman id 859690; Tue, 17 Dec 2024 17:13:54 +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 1tNb25-0006B0-EB for xen-devel@lists.xenproject.org; Tue, 17 Dec 2024 17:06:53 +0000 Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [2a00:1450:4864:20::530]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 4fc67774-bc99-11ef-a0d6-8be0dac302b0; Tue, 17 Dec 2024 18:06:52 +0100 (CET) Received: by mail-ed1-x530.google.com with SMTP id 4fb4d7f45d1cf-5d3e6f6cf69so2358997a12.1 for ; Tue, 17 Dec 2024 09:06:52 -0800 (PST) Received: from carlo-ubuntu.home.arpa (host-95-230-250-178.business.telecomitalia.it. [95.230.250.178]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aab963910f7sm461362666b.166.2024.12.17.09.06.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Dec 2024 09:06:51 -0800 (PST) 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: 4fc67774-bc99-11ef-a0d6-8be0dac302b0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=minervasys-tech.20230601.gappssmtp.com; s=20230601; t=1734455212; x=1735060012; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=uCbAePzsxXQgANJpC00dfwQGE1Jow0ZuHgQ13Xppu60=; b=j/ZzIEnB4VV4cXfFGPY3ch1vkcWy38dNKUiCM9QKNGoItZH+9jU7zPpnbg3Mq6sPQI gBz8MUozu60sMbQsIlOawGf5dYunKKral3Hic8UVcbc3cO0UtvYoaamL1N/4TzQ/Rz9e OQU0kHptcZZH1kWE/dUbhp1OoybLCm+3qkhcX5jlNn8tau0bainmzTvk3/niDlUCPv2r dzocQnw21O7SKd/xAxUi5G9Fv6+JFHJnzEa3y2vhMfhxhUHAQ5hkS6vDvA9ztV5PjQ2l vo0uUq5cJh/FdsqXVzlgZIy2QAxFVFKndPX4Qt5ZzyqRgdeCuXQrX52qVFniPi8S2LGF ltxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734455212; x=1735060012; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uCbAePzsxXQgANJpC00dfwQGE1Jow0ZuHgQ13Xppu60=; b=O76mdhAWM4Dv1AInCE7xfiCZ5RyA1FvZKJpzdP9RT5rDocQKLkYZArezk6B6lSWY4X mQxSwGksOdiKTATbE4K9NzNFFAKCm6Ea/G8cfYgWomHwJE8HpLxBTy7QnD5XSiPwz8Rj z/3mAbuthn4TcKKqa05t5UNhwOMt8MSMlJdBOK053SL9R43ky+jZ4mHkMRjEgqtiHpVT p0p97Vd5fWMvCBpZHopLynM/S9QDB9gmolUi83ceuLtu5cz9y4bJfrpfVkSEuANP7/CW Ln3SwPI0c/g8ME9mFLfNt95Ih/zA8jKZlNd4At8qk0+BNBB+LDgV4OCRPqnRa51dANXN p17g== X-Gm-Message-State: AOJu0YxNPxVfgfdN+m32Wn4F+Npeytoi38VXSBAp+kR6Js5L+j/TMOtg AlqHALLwHhUKhC5Gg9fKui6YVRH5gqmY9TKe86xFS0a5KN0KCt+SmGPRnqYZyxZ78eFtWKHjUIi xCns= X-Gm-Gg: ASbGnctKGWAPsC8NizkdhW2shxIySnpY4I1/KrLu7jFnQfTpAd3rAJgSjlxpgslrhOp GNydDNQbdyBeibKvraaKC9x9VQ7VRGXtUs/bi0ZQlDyQ5MZkWJ+n0aAXOuOpq//oBFAFbwUx6lp QsH1ggC9m0OPSrRTCi/FmC33ebcMIlQVwOOfVs9OAU1gkZ1Eu3yJUzWoV3VCXsgEBDrqd0EeJA5 vJcpMxKBCKog3eKsXrQMXljmQ4bOMAfz8hGcg8Yi63BfU6NIlZWSaEYMnyIOK9ztql3ODe3Qq31 PW2ewv6lFR/9cY2yDL9PsWHCtjyf/PSWPVxjuOSX3iw87J3FgeYIgbu7dHc= X-Google-Smtp-Source: AGHT+IHANdNcEuSk3JUmNF8b1IphrGJ40kUtQEAyVIqtsJvzWh7W5bLvH9nHff5RehXKbMMNmLeUAA== X-Received: by 2002:a17:907:3e1d:b0:aa6:800a:1292 with SMTP id a640c23a62f3a-aab779ab53dmr1811455166b.25.1734455211849; Tue, 17 Dec 2024 09:06:51 -0800 (PST) From: Carlo Nonato To: xen-devel@lists.xenproject.org Cc: andrea.bastoni@minervasys.tech, marco.solieri@minervasys.tech, Carlo Nonato , Stefano Stabellini , Julien Grall , Bertrand Marquis , Michal Orzel , Volodymyr Babchuk Subject: [PATCH v13 11/12] xen/arm: make consider_modules() available for xen relocation Date: Tue, 17 Dec 2024 18:06:36 +0100 Message-ID: <20241217170637.233097-12-carlo.nonato@minervasys.tech> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241217170637.233097-1-carlo.nonato@minervasys.tech> References: <20241217170637.233097-1-carlo.nonato@minervasys.tech> MIME-Version: 1.0 Cache coloring must physically relocate Xen in order to color the hypervisor and consider_modules() is a key function that is needed to find a new available physical address. 672d67f339c0 ("xen/arm: Split MMU-specific setup_mm() and related code out") moved consider_modules() under arm32. Move it to mmu/setup.c and make it non-static so that it can be used outside. Signed-off-by: Carlo Nonato Reviewed-by: Michal Orzel --- v13: - no changes v12: - no changes v11: - removed useless #include v10: - no changes v9: - no changes v8: - patch adapted to new changes to consider_modules() v7: - moved consider_modules() to arm/mmu/setup.c v6: - new patch --- xen/arch/arm/arm32/mmu/mm.c | 95 +------------------------------- xen/arch/arm/include/asm/setup.h | 3 + xen/arch/arm/mmu/setup.c | 94 +++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 94 deletions(-) diff --git a/xen/arch/arm/arm32/mmu/mm.c b/xen/arch/arm/arm32/mmu/mm.c index 0824d61323..956693232a 100644 --- a/xen/arch/arm/arm32/mmu/mm.c +++ b/xen/arch/arm/arm32/mmu/mm.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -31,100 +32,6 @@ static void __init setup_directmap_mappings(unsigned long base_mfn, directmap_virt_end = XENHEAP_VIRT_START + nr_mfns * PAGE_SIZE; } -/* - * Returns the end address of the highest region in the range s..e - * with required size and alignment that does not conflict with the - * modules from first_mod to nr_modules. - * - * For non-recursive callers first_mod should normally be 0 (all - * modules and Xen itself) or 1 (all modules but not Xen). - */ -static paddr_t __init consider_modules(paddr_t s, paddr_t e, - uint32_t size, paddr_t align, - int first_mod) -{ - const struct membanks *reserved_mem = bootinfo_get_reserved_mem(); -#ifdef CONFIG_STATIC_SHM - const struct membanks *shmem = bootinfo_get_shmem(); -#endif - const struct bootmodules *mi = &bootinfo.modules; - int i; - int nr; - - s = (s+align-1) & ~(align-1); - e = e & ~(align-1); - - if ( s > e || e - s < size ) - return 0; - - /* First check the boot modules */ - for ( i = first_mod; i < mi->nr_mods; i++ ) - { - paddr_t mod_s = mi->module[i].start; - paddr_t mod_e = mod_s + mi->module[i].size; - - if ( s < mod_e && mod_s < e ) - { - mod_e = consider_modules(mod_e, e, size, align, i+1); - if ( mod_e ) - return mod_e; - - return consider_modules(s, mod_s, size, align, i+1); - } - } - - /* - * i is the current bootmodule we are evaluating, across all - * possible kinds of bootmodules. - * - * When retrieving the corresponding reserved-memory addresses, we - * need to index the reserved_mem bank starting from 0, and only counting - * the reserved-memory modules. Hence, we need to use i - nr. - */ - nr = mi->nr_mods; - for ( ; i - nr < reserved_mem->nr_banks; i++ ) - { - paddr_t r_s = reserved_mem->bank[i - nr].start; - paddr_t r_e = r_s + reserved_mem->bank[i - nr].size; - - if ( s < r_e && r_s < e ) - { - r_e = consider_modules(r_e, e, size, align, i + 1); - if ( r_e ) - return r_e; - - return consider_modules(s, r_s, size, align, i + 1); - } - } - -#ifdef CONFIG_STATIC_SHM - nr += reserved_mem->nr_banks; - for ( ; i - nr < shmem->nr_banks; i++ ) - { - paddr_t r_s, r_e; - - r_s = shmem->bank[i - nr].start; - - /* Shared memory banks can contain INVALID_PADDR as start */ - if ( INVALID_PADDR == r_s ) - continue; - - r_e = r_s + shmem->bank[i - nr].size; - - if ( s < r_e && r_s < e ) - { - r_e = consider_modules(r_e, e, size, align, i + 1); - if ( r_e ) - return r_e; - - return consider_modules(s, r_s, size, align, i + 1); - } - } -#endif - - return e; -} - /* * Find a contiguous region that fits in the static heap region with * required size and alignment, and return the end address of the region diff --git a/xen/arch/arm/include/asm/setup.h b/xen/arch/arm/include/asm/setup.h index a5a80d9b47..6cf272c160 100644 --- a/xen/arch/arm/include/asm/setup.h +++ b/xen/arch/arm/include/asm/setup.h @@ -81,6 +81,9 @@ struct init_info unsigned int cpuid; }; +paddr_t consider_modules(paddr_t s, paddr_t e, uint32_t size, paddr_t align, + int first_mod); + #endif /* * Local variables: diff --git a/xen/arch/arm/mmu/setup.c b/xen/arch/arm/mmu/setup.c index 8c87649bc8..041e3fc1b6 100644 --- a/xen/arch/arm/mmu/setup.c +++ b/xen/arch/arm/mmu/setup.c @@ -222,6 +222,100 @@ static void xen_pt_enforce_wnx(void) flush_xen_tlb_local(); } +/* + * Returns the end address of the highest region in the range s..e + * with required size and alignment that does not conflict with the + * modules from first_mod to nr_modules. + * + * For non-recursive callers first_mod should normally be 0 (all + * modules and Xen itself) or 1 (all modules but not Xen). + */ +paddr_t __init consider_modules(paddr_t s, paddr_t e, + uint32_t size, paddr_t align, + int first_mod) +{ + const struct membanks *reserved_mem = bootinfo_get_reserved_mem(); +#ifdef CONFIG_STATIC_SHM + const struct membanks *shmem = bootinfo_get_shmem(); +#endif + const struct bootmodules *mi = &bootinfo.modules; + int i; + int nr; + + s = (s+align-1) & ~(align-1); + e = e & ~(align-1); + + if ( s > e || e - s < size ) + return 0; + + /* First check the boot modules */ + for ( i = first_mod; i < mi->nr_mods; i++ ) + { + paddr_t mod_s = mi->module[i].start; + paddr_t mod_e = mod_s + mi->module[i].size; + + if ( s < mod_e && mod_s < e ) + { + mod_e = consider_modules(mod_e, e, size, align, i+1); + if ( mod_e ) + return mod_e; + + return consider_modules(s, mod_s, size, align, i+1); + } + } + + /* + * i is the current bootmodule we are evaluating, across all + * possible kinds of bootmodules. + * + * When retrieving the corresponding reserved-memory addresses, we + * need to index the reserved_mem bank starting from 0, and only counting + * the reserved-memory modules. Hence, we need to use i - nr. + */ + nr = mi->nr_mods; + for ( ; i - nr < reserved_mem->nr_banks; i++ ) + { + paddr_t r_s = reserved_mem->bank[i - nr].start; + paddr_t r_e = r_s + reserved_mem->bank[i - nr].size; + + if ( s < r_e && r_s < e ) + { + r_e = consider_modules(r_e, e, size, align, i + 1); + if ( r_e ) + return r_e; + + return consider_modules(s, r_s, size, align, i + 1); + } + } + +#ifdef CONFIG_STATIC_SHM + nr += reserved_mem->nr_banks; + for ( ; i - nr < shmem->nr_banks; i++ ) + { + paddr_t r_s, r_e; + + r_s = shmem->bank[i - nr].start; + + /* Shared memory banks can contain INVALID_PADDR as start */ + if ( INVALID_PADDR == r_s ) + continue; + + r_e = r_s + shmem->bank[i - nr].size; + + if ( s < r_e && r_s < e ) + { + r_e = consider_modules(r_e, e, size, align, i + 1); + if ( r_e ) + return r_e; + + return consider_modules(s, r_s, size, align, i + 1); + } + } +#endif + + return e; +} + /* * Boot-time pagetable setup. * Changes here may need matching changes in head.S From patchwork Tue Dec 17 17:06:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlo Nonato X-Patchwork-Id: 13912226 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 7466DE7717F for ; Tue, 17 Dec 2024 17:07:07 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.859589.1271798 (Exim 4.92) (envelope-from ) id 1tNb2A-0000pl-59; Tue, 17 Dec 2024 17:06:58 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 859589.1271798; Tue, 17 Dec 2024 17:06:58 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tNb29-0000pO-Ul; Tue, 17 Dec 2024 17:06:57 +0000 Received: by outflank-mailman (input) for mailman id 859589; Tue, 17 Dec 2024 17:06:56 +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 1tNb27-0006gw-TH for xen-devel@lists.xenproject.org; Tue, 17 Dec 2024 17:06:56 +0000 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [2a00:1450:4864:20::636]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 50847243-bc99-11ef-99a3-01e77a169b0f; Tue, 17 Dec 2024 18:06:54 +0100 (CET) Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-aa66c1345caso209845366b.3 for ; Tue, 17 Dec 2024 09:06:54 -0800 (PST) Received: from carlo-ubuntu.home.arpa (host-95-230-250-178.business.telecomitalia.it. [95.230.250.178]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aab963910f7sm461362666b.166.2024.12.17.09.06.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Dec 2024 09:06:52 -0800 (PST) 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: 50847243-bc99-11ef-99a3-01e77a169b0f DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=minervasys-tech.20230601.gappssmtp.com; s=20230601; t=1734455213; x=1735060013; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=K9t6ImaeRttaisbzX9gWIlpV2Z0Ew1PDxnW05IrKzpA=; b=Q51WU3hGifrgevmiLHSnVqXMqoPImp+f7HpMsilpRDGSiFRFYoOE1+QXMhqrrTKZi6 9Y24wptNmMUs7LQrGTYho3Wg5hsI6QIZ6R/W2YWQQ07V93fHFMMtKgoggfe8Fagc636B j+bRP77cqCXmAPphdj/iZlhJ41L5q8xm3WmgbDxa3gt5t4hO7PzFCW9kTCKA1CuUp4mR ZApjnyEWnlwZsFq2L3yXId+6yIafIFuUxK/Ft0pl9rFR0E8Wv5nROlUItoI1Vr701wHu msZO6+lNb/CTjMWu7Eshun4tVLxWv33d/eHdZsy1lIDikYS35hXiX8Wip/zEU6ZIm1QP EBcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734455213; x=1735060013; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=K9t6ImaeRttaisbzX9gWIlpV2Z0Ew1PDxnW05IrKzpA=; b=k1OmSNEXvsFz4an0unfsYYKMq8b1aHvGO2+nT2Q1CU0VFIUXM/2myiUG6jAJ5ki+5l OC7REkt3Wx25y9LCecbSDU62H4amX3q6vEo476PJFg6hzWg26AYUC+/owNQulMKAFxsj +Hi+5tLOJjQ+BNTeiZladc2wQs7opPkDy84Mb04VCWRCArQsCql4nACttC04DC7zqLA0 tzM6Lk96SQDxSv61N/FfUbPeQkfDhkdVGNb1twXdX3wOieDJmBWxyw4L3zd8MC+ool/B nZiAoR57j/u3uIUXOs9EcS3LQtbY/EKjLAEzYykIU9GuM7FGv8vJGFlm09y4/Qj6T0+2 zKXQ== X-Gm-Message-State: AOJu0YzS93Tlt0ebtlFWqgqVazXCnNZAzwnr5nQlpJD2WiRYQkQVA+ZN 2JsGvUHofEIfHrJyohbaY6q6o/q7bSCsaGiQDvELyvsl2pncaKVxk9bvUCi70xWr2lfENUj5A2T YKgs= X-Gm-Gg: ASbGncsPR7F/+vOoo5qV/iZm0G1t7UVqoi4WC8gHtp5QXrMViKxdvTRHMk8EH869rq+ M++Lxx8eIew9MoGFQN9mh3dehHxkxL1G1pbl5pwXVjAUgUwt4tB89W+N4HTM6mX3JVv5yAk7XEy iF4mZChaPzvGtglOg3gQG+PP3S4OEP9B2Gk0a3COwUGgFflM54Qq5YggO+TN5Gv1BADR97MEwCR ThaqGEbCdKt76lHSS6C5BaM6r5YVqbuLG53uBbG/PabPGYlFPwGJfLTZMlOFiM17HT/rzHtKmQJ HKktgDm5JfWwqMneyHEXB+MKMqXTtuRGV+KkHo7N+B5AZYHg5R+HYgb3JdA= X-Google-Smtp-Source: AGHT+IGScJw9Sd0Pvm04BktSNQzqxYAna5DuBi7NfoxOj9YSlgw60A8Nuhpi4/g5zWSAxyjW3sslaw== X-Received: by 2002:a17:907:7843:b0:aa6:8a1b:8b84 with SMTP id a640c23a62f3a-aab77ed7774mr1454364266b.57.1734455212968; Tue, 17 Dec 2024 09:06:52 -0800 (PST) From: Carlo Nonato To: xen-devel@lists.xenproject.org Cc: andrea.bastoni@minervasys.tech, marco.solieri@minervasys.tech, Carlo Nonato , Stefano Stabellini , Julien Grall , Bertrand Marquis , Michal Orzel , Volodymyr Babchuk , Andrew Cooper , Anthony PERARD , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= Subject: [PATCH v13 12/12] xen/arm: add cache coloring support for Xen image Date: Tue, 17 Dec 2024 18:06:37 +0100 Message-ID: <20241217170637.233097-13-carlo.nonato@minervasys.tech> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241217170637.233097-1-carlo.nonato@minervasys.tech> References: <20241217170637.233097-1-carlo.nonato@minervasys.tech> MIME-Version: 1.0 Xen image is relocated to a new colored physical space. Some relocation functionalities must be brought back: - the virtual address of the new space is taken from 0c18fb76323b ("xen/arm: Remove unused BOOT_RELOC_VIRT_START"). - relocate_xen() and get_xen_paddr() are taken from f60658c6ae47 ("xen/arm: Stop relocating Xen"). setup_pagetables() must be adapted for coloring and for relocation. Runtime page tables are used to map the colored space, but they are also linked in boot tables so that the new space is temporarily available for relocation. This implies that Xen protection must happen after the copy. Finally, since the alternative framework needs to remap the Xen text and inittext sections, this operation must be done in a coloring-aware way. The function xen_remap_colored() is introduced for that. Signed-off-by: Carlo Nonato Signed-off-by: Marco Solieri Reviewed-by: Jan Beulich # common Reviewed-by: Michal Orzel --- v13: - added stub and #ifdef CONFIG_LLC_COLORING for relocate_and_switch_ttbr() v12: - FUNC instead of ENTRY in head.S for relocate_xen - dc cvau instead of dc cvav in relocate_xen() - added relocate_and_switch_ttbr() to make setup_pagetables() code more clear v11: - else if -> if in xen_colored_mfn() v10: - no changes v9: - patch adapted to changes to setup_pagetables() v8: - moved xen_colored_map_size() to arm/llc-coloring.c v7: - added BUG_ON() checks to arch_llc_coloring_init() and create_llc_coloring_mappings() v6: - squashed with BOOT_RELOC_VIRT_START patch - consider_modules() moved in another patch - removed psci and smpboot code because of new idmap work already handles that - moved xen_remap_colored() in alternative.c since it's only used there - removed xen_colored_temp[] in favor of xen_xenmap[] usage for mapping - use of boot_module_find_by_kind() to remove the need of extra parameter in setup_pagetables() - moved get_xen_paddr() in arm/llc-coloring.c since it's only used there v5: - FIXME: consider_modules copy pasted since it got moved v4: - removed set_value_for_secondary() because it was wrongly cleaning cache - relocate_xen() now calls switch_ttbr_id() --- xen/arch/arm/alternative.c | 26 ++++++- xen/arch/arm/arm64/mmu/head.S | 58 +++++++++++++- xen/arch/arm/arm64/mmu/mm.c | 34 ++++++++ xen/arch/arm/include/asm/mmu/layout.h | 3 + xen/arch/arm/include/asm/mmu/mm.h | 1 + xen/arch/arm/llc-coloring.c | 63 +++++++++++++++ xen/arch/arm/mmu/setup.c | 107 ++++++++++++++++++++++---- xen/arch/arm/setup.c | 10 ++- xen/common/llc-coloring.c | 18 +++++ xen/include/xen/llc-coloring.h | 13 ++++ 10 files changed, 313 insertions(+), 20 deletions(-) diff --git a/xen/arch/arm/alternative.c b/xen/arch/arm/alternative.c index fec7dbd2cd..2c1af6e7e4 100644 --- a/xen/arch/arm/alternative.c +++ b/xen/arch/arm/alternative.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -191,6 +192,25 @@ static int __apply_alternatives_multi_stop(void *xenmap) return 0; } +static void __init *xen_remap_colored(mfn_t xen_mfn, paddr_t xen_size) +{ + unsigned int i; + void *xenmap; + mfn_t *xen_colored_mfns, mfn; + + xen_colored_mfns = xmalloc_array(mfn_t, xen_size >> PAGE_SHIFT); + if ( !xen_colored_mfns ) + panic("Can't allocate LLC colored MFNs\n"); + + for_each_xen_colored_mfn ( xen_mfn, mfn, i ) + xen_colored_mfns[i] = mfn; + + xenmap = vmap(xen_colored_mfns, xen_size >> PAGE_SHIFT); + xfree(xen_colored_mfns); + + return xenmap; +} + /* * This function should only be called during boot and before CPU0 jump * into the idle_loop. @@ -209,7 +229,11 @@ void __init apply_alternatives_all(void) * The text and inittext section are read-only. So re-map Xen to * be able to patch the code. */ - xenmap = vmap_contig(xen_mfn, 1U << xen_order); + if ( llc_coloring_enabled ) + xenmap = xen_remap_colored(xen_mfn, xen_size); + else + xenmap = vmap_contig(xen_mfn, 1U << xen_order); + /* Re-mapping Xen is not expected to fail during boot. */ BUG_ON(!xenmap); diff --git a/xen/arch/arm/arm64/mmu/head.S b/xen/arch/arm/arm64/mmu/head.S index 665a51a337..634156f83d 100644 --- a/xen/arch/arm/arm64/mmu/head.S +++ b/xen/arch/arm/arm64/mmu/head.S @@ -428,6 +428,61 @@ FUNC_LOCAL(fail) b 1b END(fail) +/* + * Copy Xen to new location and switch TTBR + * x0 ttbr + * x1 source address + * x2 destination address + * x3 length + * + * Source and destination must be word aligned, length is rounded up + * to a 16 byte boundary. + * + * MUST BE VERY CAREFUL when saving things to RAM over the copy + */ +FUNC(relocate_xen) + /* + * Copy 16 bytes at a time using: + * x9: counter + * x10: data + * x11: data + * x12: source + * x13: destination + */ + mov x9, x3 + mov x12, x1 + mov x13, x2 + +1: ldp x10, x11, [x12], #16 + stp x10, x11, [x13], #16 + + subs x9, x9, #16 + bgt 1b + + /* + * Flush destination from dcache using: + * x9: counter + * x10: step + * x11: vaddr + * + * This is to ensure data is visible to the instruction cache + */ + dsb sy + + mov x9, x3 + ldr x10, =dcache_line_bytes /* x10 := step */ + ldr x10, [x10] + mov x11, x2 + +1: dc cvau, x11 + + add x11, x11, x10 + subs x9, x9, x10 + bgt 1b + + /* No need for dsb/isb because they are alredy done in switch_ttbr_id */ + b switch_ttbr_id + /* * Switch TTBR * @@ -453,7 +508,8 @@ FUNC(switch_ttbr_id) /* * 5) Flush I-cache - * This should not be necessary but it is kept for safety. + * This should not be necessary in the general case, but it's needed + * for cache coloring because code is relocated in that case. */ ic iallu isb diff --git a/xen/arch/arm/arm64/mmu/mm.c b/xen/arch/arm/arm64/mmu/mm.c index 671eaadbc1..26361c4fe4 100644 --- a/xen/arch/arm/arm64/mmu/mm.c +++ b/xen/arch/arm/arm64/mmu/mm.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0-only */ #include +#include #include #include @@ -138,8 +139,41 @@ void update_boot_mapping(bool enable) } extern void switch_ttbr_id(uint64_t ttbr); +extern void relocate_xen(uint64_t ttbr, void *src, void *dst, size_t len); typedef void (switch_ttbr_fn)(uint64_t ttbr); +typedef void (relocate_xen_fn)(uint64_t ttbr, void *src, void *dst, size_t len); + +#ifdef CONFIG_LLC_COLORING +void __init relocate_and_switch_ttbr(uint64_t ttbr) +{ + vaddr_t id_addr = virt_to_maddr(relocate_xen); + relocate_xen_fn *fn = (relocate_xen_fn *)id_addr; + lpae_t pte; + + /* Enable the identity mapping in the boot page tables */ + update_identity_mapping(true); + + /* Enable the identity mapping in the runtime page tables */ + pte = pte_of_xenaddr((vaddr_t)relocate_xen); + pte.pt.table = 1; + pte.pt.xn = 0; + pte.pt.ro = 1; + write_pte(&xen_third_id[third_table_offset(id_addr)], pte); + + /* Relocate Xen and switch TTBR */ + fn(ttbr, _start, (void *)BOOT_RELOC_VIRT_START, _end - _start); + + /* + * Disable the identity mapping in the runtime page tables. + * Note it is not necessary to disable it in the boot page tables + * because they are not going to be used by this CPU anymore. + */ + update_identity_mapping(false); +} +#else +void __init relocate_and_switch_ttbr(uint64_t ttbr) {} +#endif void __init switch_ttbr(uint64_t ttbr) { diff --git a/xen/arch/arm/include/asm/mmu/layout.h b/xen/arch/arm/include/asm/mmu/layout.h index a3b546465b..19c0ec63a5 100644 --- a/xen/arch/arm/include/asm/mmu/layout.h +++ b/xen/arch/arm/include/asm/mmu/layout.h @@ -30,6 +30,7 @@ * 10M - 12M Fixmap: special-purpose 4K mapping slots * 12M - 16M Early boot mapping of FDT * 16M - 18M Livepatch vmap (if compiled in) + * 16M - 24M Cache-colored Xen text, data, bss (temporary, if compiled in) * * 1G - 2G VMAP: ioremap and early_ioremap * @@ -74,6 +75,8 @@ #define BOOT_FDT_VIRT_START (FIXMAP_VIRT_START + FIXMAP_VIRT_SIZE) #define BOOT_FDT_VIRT_SIZE _AT(vaddr_t, MB(4)) +#define BOOT_RELOC_VIRT_START (BOOT_FDT_VIRT_START + BOOT_FDT_VIRT_SIZE) + #ifdef CONFIG_LIVEPATCH #define LIVEPATCH_VMAP_START (BOOT_FDT_VIRT_START + BOOT_FDT_VIRT_SIZE) #define LIVEPATCH_VMAP_SIZE _AT(vaddr_t, MB(2)) diff --git a/xen/arch/arm/include/asm/mmu/mm.h b/xen/arch/arm/include/asm/mmu/mm.h index c5e03a66bf..f5a00558c4 100644 --- a/xen/arch/arm/include/asm/mmu/mm.h +++ b/xen/arch/arm/include/asm/mmu/mm.h @@ -31,6 +31,7 @@ void dump_pt_walk(paddr_t ttbr, paddr_t addr, /* Switch to a new root page-tables */ extern void switch_ttbr(uint64_t ttbr); +extern void relocate_and_switch_ttbr(uint64_t ttbr); #endif /* __ARM_MMU_MM_H__ */ diff --git a/xen/arch/arm/llc-coloring.c b/xen/arch/arm/llc-coloring.c index 1c7b92bc45..99ac10b610 100644 --- a/xen/arch/arm/llc-coloring.c +++ b/xen/arch/arm/llc-coloring.c @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -64,8 +65,70 @@ unsigned int __init get_llc_way_size(void) return line_size * num_sets; } +/* + * get_xen_paddr - get physical address to relocate Xen to + * + * Xen is relocated to as near to the top of RAM as possible and + * aligned to a XEN_PADDR_ALIGN boundary. + */ +static paddr_t __init get_xen_paddr(paddr_t xen_size) +{ + const struct membanks *mem = bootinfo_get_mem(); + paddr_t min_size, paddr = 0; + unsigned int i; + + min_size = ROUNDUP(xen_size, XEN_PADDR_ALIGN); + + /* Find the highest bank with enough space. */ + for ( i = 0; i < mem->nr_banks; i++ ) + { + const struct membank *bank = &mem->bank[i]; + paddr_t s, e; + + if ( bank->size >= min_size ) + { + e = consider_modules(bank->start, bank->start + bank->size, + min_size, XEN_PADDR_ALIGN, 0); + if ( !e ) + continue; + +#ifdef CONFIG_ARM_32 + /* Xen must be under 4GB */ + if ( e > GB(4) ) + e = GB(4); + if ( e < bank->start ) + continue; +#endif + + s = e - min_size; + + if ( s > paddr ) + paddr = s; + } + } + + if ( !paddr ) + panic("Not enough memory to relocate Xen\n"); + + printk("Placing Xen at 0x%"PRIpaddr"-0x%"PRIpaddr"\n", + paddr, paddr + min_size); + + return paddr; +} + +static paddr_t __init xen_colored_map_size(void) +{ + return ROUNDUP((_end - _start) * get_max_nr_llc_colors(), XEN_PADDR_ALIGN); +} + void __init arch_llc_coloring_init(void) { + struct bootmodule *xen_bootmodule = boot_module_find_by_kind(BOOTMOD_XEN); + + BUG_ON(!xen_bootmodule); + + xen_bootmodule->size = xen_colored_map_size(); + xen_bootmodule->start = get_xen_paddr(xen_bootmodule->size); } /* diff --git a/xen/arch/arm/mmu/setup.c b/xen/arch/arm/mmu/setup.c index 041e3fc1b6..30afe97781 100644 --- a/xen/arch/arm/mmu/setup.c +++ b/xen/arch/arm/mmu/setup.c @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -20,6 +21,9 @@ #undef virt_to_mfn #define virt_to_mfn(va) _mfn(__virt_to_mfn(va)) +#define virt_to_reloc_virt(virt) \ + (((vaddr_t)(virt)) - XEN_VIRT_START + BOOT_RELOC_VIRT_START) + /* Main runtime page tables */ /* @@ -69,6 +73,7 @@ static void __init __maybe_unused build_assertions(void) /* 2MB aligned regions */ BUILD_BUG_ON(XEN_VIRT_START & ~SECOND_MASK); BUILD_BUG_ON(FIXMAP_ADDR(0) & ~SECOND_MASK); + BUILD_BUG_ON(BOOT_RELOC_VIRT_START & ~SECOND_MASK); /* 1GB aligned regions */ #ifdef CONFIG_ARM_32 BUILD_BUG_ON(XENHEAP_VIRT_START & ~FIRST_MASK); @@ -138,6 +143,9 @@ static void __init __maybe_unused build_assertions(void) lpae_t __init pte_of_xenaddr(vaddr_t va) { + if ( llc_coloring_enabled ) + va = virt_to_reloc_virt(va); + return mfn_to_xen_entry(virt_to_mfn(va), MT_NORMAL); } @@ -316,6 +324,32 @@ paddr_t __init consider_modules(paddr_t s, paddr_t e, return e; } +static void __init create_llc_coloring_mappings(void) +{ + lpae_t pte; + unsigned int i; + struct bootmodule *xen_bootmodule = boot_module_find_by_kind(BOOTMOD_XEN); + mfn_t start_mfn = maddr_to_mfn(xen_bootmodule->start), mfn; + + for_each_xen_colored_mfn ( start_mfn, mfn, i ) + { + pte = mfn_to_xen_entry(mfn, MT_NORMAL); + pte.pt.table = 1; /* level 3 mappings always have this bit set */ + xen_xenmap[i] = pte; + } + + for ( i = 0; i < XEN_NR_ENTRIES(2); i++ ) + { + vaddr_t va = BOOT_RELOC_VIRT_START + (i << XEN_PT_LEVEL_SHIFT(2)); + + pte = mfn_to_xen_entry(virt_to_mfn(xen_xenmap + + i * XEN_PT_LPAE_ENTRIES), + MT_NORMAL); + pte.pt.table = 1; + write_pte(&boot_second[second_table_offset(va)], pte); + } +} + /* * Boot-time pagetable setup. * Changes here may need matching changes in head.S @@ -326,6 +360,14 @@ void __init setup_pagetables(void) lpae_t pte, *p; int i; + /* + * In case of cache coloring, map the new physical space in the boot page + * tables. From now on, pte_of_xenaddr() will translate addresses to this + * new space. + */ + if ( llc_coloring_enabled ) + create_llc_coloring_mappings(); + arch_setup_page_tables(); #ifdef CONFIG_ARM_64 @@ -353,13 +395,7 @@ void __init setup_pagetables(void) break; pte = pte_of_xenaddr(va); pte.pt.table = 1; /* third level mappings always have this bit set */ - if ( is_kernel_text(va) || is_kernel_inittext(va) ) - { - pte.pt.xn = 0; - pte.pt.ro = 1; - } - if ( is_kernel_rodata(va) ) - pte.pt.ro = 1; + pte.pt.xn = 0; /* Permissions will be enforced later. Allow execution */ xen_xenmap[i] = pte; } @@ -379,19 +415,58 @@ void __init setup_pagetables(void) pte.pt.table = 1; xen_second[second_table_offset(FIXMAP_ADDR(0))] = pte; -#ifdef CONFIG_ARM_64 - ttbr = virt_to_maddr(xen_pgtable); -#else - ttbr = virt_to_maddr(cpu0_pgtable); +#ifdef CONFIG_ARM_32 + per_cpu(xen_pgtable, 0) = cpu0_pgtable; #endif - switch_ttbr(ttbr); + if ( llc_coloring_enabled ) + { + ttbr = virt_to_maddr(virt_to_reloc_virt(THIS_CPU_PGTABLE)); + relocate_and_switch_ttbr(ttbr); + } + else + { + ttbr = virt_to_maddr(THIS_CPU_PGTABLE); + switch_ttbr(ttbr); + } - xen_pt_enforce_wnx(); + /* Protect Xen */ + for ( i = 0; i < XEN_NR_ENTRIES(3); i++ ) + { + vaddr_t va = XEN_VIRT_START + (i << PAGE_SHIFT); + lpae_t *entry = xen_xenmap + i; -#ifdef CONFIG_ARM_32 - per_cpu(xen_pgtable, 0) = cpu0_pgtable; -#endif + if ( !is_kernel(va) ) + break; + + pte = read_atomic(entry); + + if ( is_kernel_text(va) || is_kernel_inittext(va) ) + { + pte.pt.xn = 0; + pte.pt.ro = 1; + } + else if ( is_kernel_rodata(va) ) + { + pte.pt.ro = 1; + pte.pt.xn = 1; + } + else + { + pte.pt.xn = 1; + pte.pt.ro = 0; + } + + write_pte(entry, pte); + } + + /* + * We modified live page-tables. Ensure the TLBs are invalidated + * before setting enforcing the WnX permissions. + */ + flush_xen_tlb_local(); + + xen_pt_enforce_wnx(); } void *__init arch_vmap_virt_end(void) diff --git a/xen/arch/arm/setup.c b/xen/arch/arm/setup.c index 5f1993ffed..68c1f30920 100644 --- a/xen/arch/arm/setup.c +++ b/xen/arch/arm/setup.c @@ -312,8 +312,6 @@ void asmlinkage __init start_xen(unsigned long fdt_paddr) /* Initialize traps early allow us to get backtrace when an error occurred */ init_traps(); - setup_pagetables(); - smp_clear_cpu_maps(); device_tree_flattened = early_fdt_map(fdt_paddr); @@ -337,6 +335,14 @@ void asmlinkage __init start_xen(unsigned long fdt_paddr) llc_coloring_init(); + /* + * Page tables must be setup after LLC coloring initialization because + * coloring info are required in order to create colored mappings + */ + setup_pagetables(); + /* Device-tree was mapped in boot page tables, remap it in the new tables */ + device_tree_flattened = early_fdt_map(fdt_paddr); + setup_mm(); vm_init(); diff --git a/xen/common/llc-coloring.c b/xen/common/llc-coloring.c index 0cfaa77a2b..a572f77a09 100644 --- a/xen/common/llc-coloring.c +++ b/xen/common/llc-coloring.c @@ -42,6 +42,8 @@ static unsigned int __ro_after_init xen_num_colors; #define mfn_color_mask (max_nr_colors - 1) #define mfn_to_color(mfn) (mfn_x(mfn) & mfn_color_mask) +#define get_mfn_with_color(mfn, color) \ + (_mfn((mfn_x(mfn) & ~mfn_color_mask) | (color))) /* * Parse the coloring configuration given in the buf string, following the @@ -364,6 +366,22 @@ unsigned int get_max_nr_llc_colors(void) return max_nr_colors; } +mfn_t __init xen_colored_mfn(mfn_t mfn) +{ + unsigned int i, color = mfn_to_color(mfn); + + for ( i = 0; i < xen_num_colors; i++ ) + { + if ( color == xen_colors[i] ) + return mfn; + if ( color < xen_colors[i] ) + return get_mfn_with_color(mfn, xen_colors[i]); + } + + /* Jump to next color space (max_nr_colors mfns) and use the first color */ + return get_mfn_with_color(mfn_add(mfn, max_nr_colors), xen_colors[0]); +} + /* * Local variables: * mode: C diff --git a/xen/include/xen/llc-coloring.h b/xen/include/xen/llc-coloring.h index 1216d1fbd4..45f250f9f3 100644 --- a/xen/include/xen/llc-coloring.h +++ b/xen/include/xen/llc-coloring.h @@ -8,6 +8,7 @@ #ifndef __XEN_LLC_COLORING_H__ #define __XEN_LLC_COLORING_H__ +#include #include struct domain; @@ -30,6 +31,17 @@ static inline void domain_dump_llc_colors(const struct domain *d) {} static inline void domain_llc_coloring_free(struct domain *d) {} #endif +/* + * Iterate over each Xen mfn in the colored space. + * @start_mfn: the first mfn that needs to be colored. + * @mfn: the current mfn. + * @i: loop index. + */ +#define for_each_xen_colored_mfn(start_mfn, mfn, i) \ + for ( (i) = 0, (mfn) = xen_colored_mfn(start_mfn); \ + (i) < (_end - _start) >> PAGE_SHIFT; \ + (i)++, (mfn) = xen_colored_mfn(mfn_add(mfn, 1)) ) + unsigned int get_llc_way_size(void); void arch_llc_coloring_init(void); int dom0_set_llc_colors(struct domain *d); @@ -38,6 +50,7 @@ int domain_set_llc_colors(struct domain *d, int domain_set_llc_colors_from_str(struct domain *d, const char *str); unsigned int page_to_llc_color(const struct page_info *pg); unsigned int get_max_nr_llc_colors(void); +mfn_t xen_colored_mfn(mfn_t mfn); #endif /* __XEN_LLC_COLORING_H__ */