:p
atchew
Login
From: Bernhard Kaindl <bernhard.kaindl@cloud.com> Changes in v2: - Remove update of numainfo call, only calculate RAM for each node. - Calculate RAM based on page boundaries, coding style fixes Some admin tools like 'xl info -n' like to display the total memory for each NUMA node. The Xen backend[1] of hwloc comes to mind too. The total amount of RAM on a NUMA node is not needed by Xen internally: Xen only uses NODE_DATA->node_spanned_pages, but that can be confusing for users as it includes memory holes (can be as large as 2GB on x86). Calculate the RAM per NUMA node by iterating over arch_get_ram_range() which returns the e820 RAM entries on x86 and update it on memory_add(). Use NODE_DATA->node_present_pages (like in the Linux kernel) to hold this info and in a later commit, find a way for tools to read it. [1] hwloc with Xen backend: https://github.com/xenserver-next/hwloc/ Signed-off-by: Bernhard Kaindl <bernhard.kaindl@cloud.com> --- xen/arch/x86/x86_64/mm.c | 3 +++ xen/common/numa.c | 28 +++++++++++++++++++++++++++- xen/include/xen/numa.h | 3 +++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/xen/arch/x86/x86_64/mm.c b/xen/arch/x86/x86_64/mm.c index XXXXXXX..XXXXXXX 100644 --- a/xen/arch/x86/x86_64/mm.c +++ b/xen/arch/x86/x86_64/mm.c @@ -XXX,XX +XXX,XX @@ int memory_add(unsigned long spfn, unsigned long epfn, unsigned int pxm) share_hotadd_m2p_table(&info); transfer_pages_to_heap(&info); + /* Update the node's present pages (like the total_pages of the system) */ + NODE_DATA(node)->node_present_pages += epfn - spfn; + return 0; destroy_m2p: diff --git a/xen/common/numa.c b/xen/common/numa.c index XXXXXXX..XXXXXXX 100644 --- a/xen/common/numa.c +++ b/xen/common/numa.c @@ -XXX,XX +XXX,XX @@ int __init compute_hash_shift(const struct node *nodes, return shift; } -/* Initialize NODE_DATA given nodeid and start/end */ +/** + * @brief Initialize a NUMA node's NODE_DATA given nodeid and start/end addrs. + * + * This function sets up the boot memory for a given NUMA node by calculating + * the node's start and end page frame numbers (PFNs) and determining + * the number of present RAM pages within the node's memory range. + * + * @param nodeid The identifier of the node to initialize. + * @param start The starting physical address of the node's memory range. + * @param end The ending physical address of the node's memory range. + */ void __init setup_node_bootmem(nodeid_t nodeid, paddr_t start, paddr_t end) { unsigned long start_pfn = paddr_to_pfn(start); unsigned long end_pfn = paddr_to_pfn(end); + unsigned long ram_start_pfn, ram_end_pfn; + unsigned int idx = 0; + int err; + paddr_t ram_start, ram_end; NODE_DATA(nodeid)->node_start_pfn = start_pfn; NODE_DATA(nodeid)->node_spanned_pages = end_pfn - start_pfn; + /* Calculate the numer of present RAM pages within the node: */ + NODE_DATA(nodeid)->node_present_pages = 0; + while ( (err = arch_get_ram_range(idx++, &ram_start, &ram_end)) != -ENOENT ) + { + if ( err || ram_start >= end || ram_end <= start ) + continue; /* Not RAM (err != 0) or range is outside the node */ + + /* Add RAM that in the node's memory range (within start and end): */ + ram_end_pfn = min(ram_end, end) >> PAGE_SHIFT; + ram_start_pfn = (PAGE_ALIGN(max(ram_start, start))) >> PAGE_SHIFT; + NODE_DATA(nodeid)->node_present_pages += ram_end_pfn - ram_start_pfn; + } node_set_online(nodeid); } diff --git a/xen/include/xen/numa.h b/xen/include/xen/numa.h index XXXXXXX..XXXXXXX 100644 --- a/xen/include/xen/numa.h +++ b/xen/include/xen/numa.h @@ -XXX,XX +XXX,XX @@ extern nodeid_t *memnodemap; struct node_data { unsigned long node_start_pfn; unsigned long node_spanned_pages; + unsigned long node_present_pages; }; extern struct node_data node_data[]; @@ -XXX,XX +XXX,XX @@ static inline nodeid_t mfn_to_nid(mfn_t mfn) #define node_start_pfn(nid) (NODE_DATA(nid)->node_start_pfn) #define node_spanned_pages(nid) (NODE_DATA(nid)->node_spanned_pages) +#define node_present_pages(nid) (NODE_DATA(nid)->node_present_pages) #define node_end_pfn(nid) (NODE_DATA(nid)->node_start_pfn + \ NODE_DATA(nid)->node_spanned_pages) @@ -XXX,XX +XXX,XX @@ extern void numa_set_processor_nodes_parsed(nodeid_t node); extern mfn_t first_valid_mfn; #define node_spanned_pages(nid) (max_page - mfn_x(first_valid_mfn)) +#define node_present_pages(nid) total_pages #define node_start_pfn(nid) mfn_x(first_valid_mfn) #define __node_distance(a, b) 20 -- 2.43.0
From: Bernhard Kaindl <bernhard.kaindl@cloud.com> At the moment, Xen keeps track of the spans of PFNs of the NUMA nodes. But the PFN span sometimes includes large MMIO holes, so these values might not be an exact representation of the total usable RAM of nodes. Xen does not need it, but the size of the NUMA node's memory can be helpful for management tools and HW information tools like hwloc/lstopo with its Xen backend for Dom0: https://github.com/xenserver-next/hwloc/ First, introduce NODE_DATA(nodeid)->node_present_pages to node_data[], determine the sum of usable PFNs at boot and update them on memory_add(). (The Linux kernel handles NODE_DATA->node_present_pages likewise) Signed-off-by: Bernhard Kaindl <bernhard.kaindl@cloud.com> --- Changes in v3: - Use PFN_UP/DOWN, refactored further to simplify the code while leaving compiler-level optimisations to the compiler's optimisation passes. Changes in v4: - Refactored code and doxygen documentation according to the review. --- xen/arch/x86/numa.c | 13 +++++++++++++ xen/arch/x86/x86_64/mm.c | 3 +++ xen/common/numa.c | 36 +++++++++++++++++++++++++++++++++--- xen/include/xen/numa.h | 21 +++++++++++++++++++++ 4 files changed, 70 insertions(+), 3 deletions(-) diff --git a/xen/arch/x86/numa.c b/xen/arch/x86/numa.c index XXXXXXX..XXXXXXX 100644 --- a/xen/arch/x86/numa.c +++ b/xen/arch/x86/numa.c @@ -XXX,XX +XXX,XX @@ unsigned int __init arch_get_dma_bitsize(void) + PAGE_SHIFT, 32); } +/** + * @brief Retrieves the RAM range for a given index from the e820 memory map. + * + * This function fetches the start and end address (exclusive) of a RAM range + * specified by the given index idx from the e820 memory map. + * + * @param idx The index of the RAM range in the e820 memory map to retrieve. + * @param start Pointer to store the start address of the RAM range. + * @param end Pointer to store the end address of the RAM range. + * + * @return 0 on success, -ENOENT if the index is out of bounds, + * or -ENODATA if the memory map at index idx is not of type E820_RAM. + */ int __init arch_get_ram_range(unsigned int idx, paddr_t *start, paddr_t *end) { if ( idx >= e820.nr_map ) diff --git a/xen/arch/x86/x86_64/mm.c b/xen/arch/x86/x86_64/mm.c index XXXXXXX..XXXXXXX 100644 --- a/xen/arch/x86/x86_64/mm.c +++ b/xen/arch/x86/x86_64/mm.c @@ -XXX,XX +XXX,XX @@ int memory_add(unsigned long spfn, unsigned long epfn, unsigned int pxm) share_hotadd_m2p_table(&info); transfer_pages_to_heap(&info); + /* Update the node's present pages (like the total_pages of the system) */ + NODE_DATA(node)->node_present_pages += epfn - spfn; + return 0; destroy_m2p: diff --git a/xen/common/numa.c b/xen/common/numa.c index XXXXXXX..XXXXXXX 100644 --- a/xen/common/numa.c +++ b/xen/common/numa.c @@ -XXX,XX +XXX,XX @@ * Adapted for Xen: Ryan Harper <ryanh@us.ibm.com> */ +#include "xen/pfn.h" #include <xen/init.h> #include <xen/keyhandler.h> #include <xen/mm.h> @@ -XXX,XX +XXX,XX @@ int __init compute_hash_shift(const struct node *nodes, return shift; } -/* Initialize NODE_DATA given nodeid and start/end */ +/** + * @brief Initialize a NUMA node's node_data structure at boot. + * + * It is given the NUMA node's index in the node_data array as well + * as the start and exclusive end address of the node's memory span + * as arguments and initializes the node_data entry with this information. + * + * It then initializes the total number of usable memory pages within + * the NUMA node's memory span using the arch_get_ram_range() function. + * + * @param nodeid The index into the node_data array for the node. + * @param start The starting physical address of the node's memory range. + * @param end The exclusive ending physical address of the node's memory range. + */ void __init setup_node_bootmem(nodeid_t nodeid, paddr_t start, paddr_t end) { unsigned long start_pfn = paddr_to_pfn(start); unsigned long end_pfn = paddr_to_pfn(end); + struct node_data *numa_node = NODE_DATA(nodeid); + paddr_t start_ram, end_ram; + unsigned int idx = 0; + unsigned long *pages = &numa_node->node_present_pages; - NODE_DATA(nodeid)->node_start_pfn = start_pfn; - NODE_DATA(nodeid)->node_spanned_pages = end_pfn - start_pfn; + numa_node->node_start_pfn = start_pfn; + numa_node->node_spanned_pages = end_pfn - start_pfn; + + /* Calculate the number of present RAM pages within the node: */ + *pages = 0; + do { + int err = arch_get_ram_range(idx++, &start_ram, &end_ram); + + if (err == -ENOENT) + break; + if ( err || start_ram >= end || end_ram <= start ) + continue; /* range is outside of the node, or not usable RAM */ + *pages += PFN_DOWN(min(end_ram, end)) - PFN_UP(max(start_ram, start)); + } while (1); node_set_online(nodeid); } diff --git a/xen/include/xen/numa.h b/xen/include/xen/numa.h index XXXXXXX..XXXXXXX 100644 --- a/xen/include/xen/numa.h +++ b/xen/include/xen/numa.h @@ -XXX,XX +XXX,XX @@ extern unsigned int memnode_shift; extern unsigned long memnodemapsize; extern nodeid_t *memnodemap; +/** + * @struct numa_node + * @brief Represents the memory information of a NUMA node. + * + * @var numa_node::node_start_pfn + * The starting page frame number (lowest pfn) of the NUMA node. + * + * @var numa_node::node_spanned_pages + * The number of pages spanned by the NUMA node, including memory holes. + * Used to get the end of the node memory when scrubbing unallocated memory. + * + * @var numa_node::node_present_pages + * The total number of usable memory pages that are available in this NUMA node. + * The value of total_pages would be the sum of all node's node_present_pages. + * + * The Xen Hypervisor does not use this field internally, but it is useful + * for reporting the memory information of NUMA nodes to management tools. + */ struct node_data { unsigned long node_start_pfn; unsigned long node_spanned_pages; + unsigned long node_present_pages; }; extern struct node_data node_data[]; @@ -XXX,XX +XXX,XX @@ static inline nodeid_t mfn_to_nid(mfn_t mfn) #define node_start_pfn(nid) (NODE_DATA(nid)->node_start_pfn) #define node_spanned_pages(nid) (NODE_DATA(nid)->node_spanned_pages) +#define node_present_pages(nid) (NODE_DATA(nid)->node_present_pages) #define node_end_pfn(nid) (NODE_DATA(nid)->node_start_pfn + \ NODE_DATA(nid)->node_spanned_pages) @@ -XXX,XX +XXX,XX @@ extern void numa_set_processor_nodes_parsed(nodeid_t node); extern mfn_t first_valid_mfn; #define node_spanned_pages(nid) (max_page - mfn_x(first_valid_mfn)) +#define node_present_pages(nid) total_pages #define node_start_pfn(nid) mfn_x(first_valid_mfn) #define __node_distance(a, b) 20 -- 2.43.0