]> xenbits.xensource.com Git - xen.git/commitdiff
NUMA: Introduce NODE_DATA->node_present_pages (RAM)
authorBernhard Kaindl <bernhard.kaindl@cloud.com>
Thu, 7 Nov 2024 11:51:52 +0000 (12:51 +0100)
committerJan Beulich <jbeulich@suse.com>
Thu, 7 Nov 2024 11:51:52 +0000 (12:51 +0100)
Xen tracks the total span of physical memory space of each NUMA node,
but the PFN span includes large MMIO holes in e.g. the first NUMA node.
Thus, the span is not the same as the amount of usable RAM of a node.

Xen does not need it, but NUMA node memory amount 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/

Introduce node_present_pages to node_data[]:
On boot, set the count of usable PFNs and update it on memory_add().

Signed-off-by: Bernhard Kaindl <bernhard.kaindl@cloud.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>
xen/arch/x86/numa.c
xen/arch/x86/x86_64/mm.c
xen/common/numa.c
xen/include/xen/numa.h

index 3f2fe54874a3128093852bd3ea2a88c3d2bcbedf..ae3cc7a8d06051f45a9e95988bcbdd96bd3efacb 100644 (file)
@@ -100,6 +100,19 @@ 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 starting and ending addresses 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 starting address of the RAM range.
+ * @param end Pointer to store the exclusive ending 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 )
index 0d8e602529628dc70e0c7407e0dc1f0c74ee1696..389d813ebe63cc90b4bf01b474eb8ebe0f7223dd 100644 (file)
@@ -1336,6 +1336,9 @@ 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:
index ce3991929ce5964f33a7c7d974b2b95e7235bcd3..ad75955a1622b3f41541c8197fdb53ee6cc54672 100644 (file)
@@ -10,6 +10,7 @@
 #include <xen/nodemask.h>
 #include <xen/numa.h>
 #include <xen/param.h>
+#include <xen/pfn.h>
 #include <xen/sched.h>
 #include <xen/softirq.h>
 
@@ -499,14 +500,41 @@ 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);
-
-    NODE_DATA(nodeid)->node_start_pfn = start_pfn;
-    NODE_DATA(nodeid)->node_spanned_pages = end_pfn - start_pfn;
+    struct node_data *node = NODE_DATA(nodeid);
+    unsigned int idx = 0;
+    int err;
+
+    node->node_start_pfn = start_pfn;
+    node->node_spanned_pages = end_pfn - start_pfn;
+    node->node_present_pages = 0;
+
+    /* Calculate the number of present RAM pages within the node */
+    do {
+        paddr_t ram_start, ram_end;
+
+        err = arch_get_ram_range(idx++, &ram_start, &ram_end);
+        if ( !err && ram_start < end && ram_end > start )
+            node->node_present_pages += PFN_DOWN(min(ram_end, end)) -
+                                        PFN_UP(max(ram_start, start));
+    } while ( err != -ENOENT );
 
     node_set_online(nodeid);
 }
index fd1511a6fb6eac8f8d19d4b38984f378795cd8ac..f6c1f27ca10510d9d7e64863dc2650f2f0fb967c 100644 (file)
@@ -68,9 +68,24 @@ extern unsigned int memnode_shift;
 extern unsigned long memnodemapsize;
 extern nodeid_t *memnodemap;
 
+/* The memory information of NUMA nodes in the node_data[] array */
 struct node_data {
+    /* The starting page frame number (lowest pfn) of the NUMA node */
     unsigned long node_start_pfn;
+
+    /*
+     * The number of pages spanned by the NUMA node, including memory holes.
+     * Used for the pyhsical memory range when scrubbing unallocated memory.
+     */
     unsigned long node_spanned_pages;
+
+    /*
+     * Number of usable memory pages that are available in this NUMA node.
+     * The sum of these values from all NUMA nodes reflects total_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.
+     */
+    unsigned long node_present_pages;
 };
 
 extern struct node_data node_data[];
@@ -91,6 +106,7 @@ 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)
 
@@ -123,6 +139,7 @@ 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