diff mbox series

[v2,6/9] xen/x86: move NUMA scan nodes codes from x86 to common

Message ID 20220708145424.1848572-7-wei.chen@arm.com (mailing list archive)
State Superseded
Headers show
Series Device tree based NUMA support for Arm - Part#2 | expand

Commit Message

Wei Chen July 8, 2022, 2:54 p.m. UTC
x86 has implemented a set of codes to scan NUMA nodes. These
codes will parse NUMA memory and processor information from
ACPI SRAT table. But except some ACPI specific codes, most
of the scan codes like memory blocks validation, node memory
range updates and some sanity check can be reused by other
NUMA implementation.

So in this patch, we move some variables and related functions
for NUMA memory and processor to common code. At the same time,
numa_set_processor_nodes_parsed has been introduced for ACPI
specific code to update processor parsing results. With this
helper, we can move most of NUMA memory affinity init code from
ACPI. And bad_srat and node_to_pxm functions have been exported
for common code to do architectural fallback and node to proximity
converting. For those NUMA implementations haven't supported
proximity domain to node map. A simple 1-1 mapping helper can help
us to avoid the modification of some very valuable print messages.

Signed-off-by: Wei Chen <wei.chen@arm.com>
---
v1 -> v2:
1. Add code comment for numa_update_node_memblks to explain:
   Assumes all memory regions belonging to a single node
   are in one chunk. Holes between them will be included
   in the node.
2. Merge this single patch instead of serval patches to move
   x86 SRAT code to common.
3. Export node_to_pxm to keep pxm information in NUMA scan
   nodes error messages.
4. Change the code style to target file's Xen code-style.
5. Adjust some __init and __initdata for some functions and
   variables.
---
 xen/arch/x86/include/asm/numa.h |   3 +-
 xen/arch/x86/srat.c             | 288 ++----------------------------
 xen/common/numa.c               | 307 ++++++++++++++++++++++++++++++++
 xen/include/xen/numa.h          |   7 +
 4 files changed, 328 insertions(+), 277 deletions(-)

Comments

Jan Beulich July 12, 2022, 2:21 p.m. UTC | #1
On 08.07.2022 16:54, Wei Chen wrote:
> x86 has implemented a set of codes to scan NUMA nodes. These
> codes will parse NUMA memory and processor information from
> ACPI SRAT table. But except some ACPI specific codes, most
> of the scan codes like memory blocks validation, node memory
> range updates and some sanity check can be reused by other
> NUMA implementation.
> 
> So in this patch, we move some variables and related functions
> for NUMA memory and processor to common code. At the same time,
> numa_set_processor_nodes_parsed has been introduced for ACPI
> specific code to update processor parsing results. With this
> helper, we can move most of NUMA memory affinity init code from
> ACPI. And bad_srat and node_to_pxm functions have been exported
> for common code to do architectural fallback and node to proximity
> converting.

I consider it wrong for generic (ACPI-independent) code to use
terms like "srat" or "pxm". This wants abstracting in some way,
albeit I have to admit I lack a good idea for a suggestion right
now.

> For those NUMA implementations haven't supported
> proximity domain to node map. A simple 1-1 mapping helper can help
> us to avoid the modification of some very valuable print messages.

I don't think a simple 1:1 mapping can do. Surely firmware
represents nodes by some identifiers everywhere. And I don't
consider it very likely that we would want to blindly re-use
such numbering inside Xen.

> --- a/xen/common/numa.c
> +++ b/xen/common/numa.c
> @@ -14,6 +14,21 @@
>  #include <xen/softirq.h>
>  #include <asm/acpi.h>
>  
> +static nodemask_t __initdata processor_nodes_parsed;
> +static nodemask_t __initdata memory_nodes_parsed;
> +static struct node __initdata nodes[MAX_NUMNODES];
> +
> +static int num_node_memblks;
> +static struct node node_memblk_range[NR_NODE_MEMBLKS];
> +static nodeid_t memblk_nodeid[NR_NODE_MEMBLKS];

Some (all) of these likely want to become __read_mostly again, at
this occasion.

> @@ -32,6 +47,298 @@ nodemask_t __read_mostly node_online_map = { { [0] = 1UL } };
>  
>  enum numa_mode numa_status;
>  
> +void __init numa_set_processor_nodes_parsed(nodeid_t node)
> +{
> +    node_set(node, processor_nodes_parsed);
> +}
> +
> +int valid_numa_range(paddr_t start, paddr_t end, nodeid_t node)
> +{
> +    int i;

unsigned int? Then matching e.g. ...

> +static
> +enum conflicts __init conflicting_memblks(nodeid_t nid, paddr_t start,
> +                                          paddr_t end, paddr_t nd_start,
> +                                          paddr_t nd_end, unsigned int *mblkid)
> +{
> +    unsigned int i;

... this. But perhaps also elsewhere.

Jan
Wei Chen July 13, 2022, 10:57 a.m. UTC | #2
Hi Jan,

> -----Original Message-----
> From: Jan Beulich <jbeulich@suse.com>
> Sent: 2022年7月12日 22:21
> To: Wei Chen <Wei.Chen@arm.com>
> Cc: nd <nd@arm.com>; Andrew Cooper <andrew.cooper3@citrix.com>; Roger Pau
> Monné <roger.pau@citrix.com>; Wei Liu <wl@xen.org>; George Dunlap
> <george.dunlap@citrix.com>; Julien Grall <julien@xen.org>; Stefano
> Stabellini <sstabellini@kernel.org>; xen-devel@lists.xenproject.org
> Subject: Re: [PATCH v2 6/9] xen/x86: move NUMA scan nodes codes from x86
> to common
> 
> On 08.07.2022 16:54, Wei Chen wrote:
> > x86 has implemented a set of codes to scan NUMA nodes. These
> > codes will parse NUMA memory and processor information from
> > ACPI SRAT table. But except some ACPI specific codes, most
> > of the scan codes like memory blocks validation, node memory
> > range updates and some sanity check can be reused by other
> > NUMA implementation.
> >
> > So in this patch, we move some variables and related functions
> > for NUMA memory and processor to common code. At the same time,
> > numa_set_processor_nodes_parsed has been introduced for ACPI
> > specific code to update processor parsing results. With this
> > helper, we can move most of NUMA memory affinity init code from
> > ACPI. And bad_srat and node_to_pxm functions have been exported
> > for common code to do architectural fallback and node to proximity
> > converting.
> 
> I consider it wrong for generic (ACPI-independent) code to use
> terms like "srat" or "pxm". This wants abstracting in some way,
> albeit I have to admit I lack a good idea for a suggestion right
> now.
> 

Maybe we can use fw_rsc_table or rsc_table to replace srat, because
srat is one kind of NUMA resource description table of ACPI?
For PXM, I had tried to keep PXM in x86 ACPI implementation. But the
cost is that, we have to move some common code to architectural code,
because some messages use pxm for info, and they have different meanings
for each platform, we cannot simply remove them.

> > For those NUMA implementations haven't supported
> > proximity domain to node map. A simple 1-1 mapping helper can help
> > us to avoid the modification of some very valuable print messages.
> 
> I don't think a simple 1:1 mapping can do. Surely firmware
> represents nodes by some identifiers everywhere. And I don't
> consider it very likely that we would want to blindly re-use
> such numbering inside Xen.
> 

If we hide pxm to x86 only now, we can avoid this 1:1 mapping between
Node and PXM.

> > --- a/xen/common/numa.c
> > +++ b/xen/common/numa.c
> > @@ -14,6 +14,21 @@
> >  #include <xen/softirq.h>
> >  #include <asm/acpi.h>
> >
> > +static nodemask_t __initdata processor_nodes_parsed;
> > +static nodemask_t __initdata memory_nodes_parsed;
> > +static struct node __initdata nodes[MAX_NUMNODES];
> > +
> > +static int num_node_memblks;
> > +static struct node node_memblk_range[NR_NODE_MEMBLKS];
> > +static nodeid_t memblk_nodeid[NR_NODE_MEMBLKS];
> 
> Some (all) of these likely want to become __read_mostly again, at
> this occasion.
> 

Ok.

> > @@ -32,6 +47,298 @@ nodemask_t __read_mostly node_online_map = { { [0] =
> 1UL } };
> >
> >  enum numa_mode numa_status;
> >
> > +void __init numa_set_processor_nodes_parsed(nodeid_t node)
> > +{
> > +    node_set(node, processor_nodes_parsed);
> > +}
> > +
> > +int valid_numa_range(paddr_t start, paddr_t end, nodeid_t node)
> > +{
> > +    int i;
> 
> unsigned int? Then matching e.g. ...
> 

Ok.

> > +static
> > +enum conflicts __init conflicting_memblks(nodeid_t nid, paddr_t start,
> > +                                          paddr_t end, paddr_t nd_start,
> > +                                          paddr_t nd_end, unsigned int
> *mblkid)
> > +{
> > +    unsigned int i;
> 
> ... this. But perhaps also elsewhere.
> 

I will check the code an fix them.

Cheers,
Wei Chen

> Jan
Jan Beulich July 13, 2022, 1:49 p.m. UTC | #3
On 13.07.2022 12:57, Wei Chen wrote:
> Hi Jan,
> 
>> -----Original Message-----
>> From: Jan Beulich <jbeulich@suse.com>
>> Sent: 2022年7月12日 22:21
>> To: Wei Chen <Wei.Chen@arm.com>
>> Cc: nd <nd@arm.com>; Andrew Cooper <andrew.cooper3@citrix.com>; Roger Pau
>> Monné <roger.pau@citrix.com>; Wei Liu <wl@xen.org>; George Dunlap
>> <george.dunlap@citrix.com>; Julien Grall <julien@xen.org>; Stefano
>> Stabellini <sstabellini@kernel.org>; xen-devel@lists.xenproject.org
>> Subject: Re: [PATCH v2 6/9] xen/x86: move NUMA scan nodes codes from x86
>> to common
>>
>> On 08.07.2022 16:54, Wei Chen wrote:
>>> x86 has implemented a set of codes to scan NUMA nodes. These
>>> codes will parse NUMA memory and processor information from
>>> ACPI SRAT table. But except some ACPI specific codes, most
>>> of the scan codes like memory blocks validation, node memory
>>> range updates and some sanity check can be reused by other
>>> NUMA implementation.
>>>
>>> So in this patch, we move some variables and related functions
>>> for NUMA memory and processor to common code. At the same time,
>>> numa_set_processor_nodes_parsed has been introduced for ACPI
>>> specific code to update processor parsing results. With this
>>> helper, we can move most of NUMA memory affinity init code from
>>> ACPI. And bad_srat and node_to_pxm functions have been exported
>>> for common code to do architectural fallback and node to proximity
>>> converting.
>>
>> I consider it wrong for generic (ACPI-independent) code to use
>> terms like "srat" or "pxm". This wants abstracting in some way,
>> albeit I have to admit I lack a good idea for a suggestion right
>> now.
>>
> 
> Maybe we can use fw_rsc_table or rsc_table to replace srat, because
> srat is one kind of NUMA resource description table of ACPI?

Is "rsc" meant to stand for "resource"? Would be a somewhat unusual
abbreviation. I could see use using e.g. numa_fw_ as a prefix, as in
e.g. numa_fw_bad() (replacing bad_srat()).

> For PXM, I had tried to keep PXM in x86 ACPI implementation. But the
> cost is that, we have to move some common code to architectural code,
> because some messages use pxm for info, and they have different meanings
> for each platform, we cannot simply remove them.

Well, for functions wanting to emit log messages, suitable abstractions
can likely be made without needing the retain a lot of per-arch code.
E.g. the arch could pass in "PXM" and format strings then would use %s
together with it. Similarly the translation (if any is necessary) could
likely be abstracted by, in the worst case, passing in a function
pointer.

Jan
diff mbox series

Patch

diff --git a/xen/arch/x86/include/asm/numa.h b/xen/arch/x86/include/asm/numa.h
index d8960743d4..eeb431cdb7 100644
--- a/xen/arch/x86/include/asm/numa.h
+++ b/xen/arch/x86/include/asm/numa.h
@@ -10,6 +10,7 @@  typedef u8 nodeid_t;
 extern int srat_rev;
 
 extern nodeid_t pxm_to_node(unsigned int pxm);
+extern unsigned node_to_pxm(nodeid_t n);
 
 #define ZONE_ALIGN (1UL << (MAX_ORDER+PAGE_SHIFT))
 
@@ -22,8 +23,6 @@  extern void init_cpu_to_node(void);
 
 #define arch_want_default_dmazone() (num_online_nodes() > 1)
 
-extern int valid_numa_range(paddr_t start, paddr_t end, nodeid_t node);
-
 void srat_parse_regions(paddr_t addr);
 extern u8 __node_distance(nodeid_t a, nodeid_t b);
 unsigned int arch_get_dma_bitsize(void);
diff --git a/xen/arch/x86/srat.c b/xen/arch/x86/srat.c
index 5bc9096a15..9ae81afdff 100644
--- a/xen/arch/x86/srat.c
+++ b/xen/arch/x86/srat.c
@@ -24,10 +24,6 @@ 
 
 static struct acpi_table_slit *__read_mostly acpi_slit;
 
-static nodemask_t memory_nodes_parsed __initdata;
-static nodemask_t processor_nodes_parsed __initdata;
-static struct node nodes[MAX_NUMNODES] __initdata;
-
 struct pxm2node {
 	unsigned pxm;
 	nodeid_t node;
@@ -35,19 +31,6 @@  struct pxm2node {
 static struct pxm2node __read_mostly pxm2node[MAX_NUMNODES] =
 	{ [0 ... MAX_NUMNODES - 1] = {.node = NUMA_NO_NODE} };
 
-static unsigned node_to_pxm(nodeid_t n);
-
-static int num_node_memblks;
-static struct node node_memblk_range[NR_NODE_MEMBLKS];
-static nodeid_t memblk_nodeid[NR_NODE_MEMBLKS];
-static __initdata DECLARE_BITMAP(memblk_hotplug, NR_NODE_MEMBLKS);
-
-enum conflicts {
-	NO_CONFLICT,
-	OVERLAP,
-	INTERLEAVE,
-};
-
 static inline bool node_found(unsigned idx, unsigned pxm)
 {
 	return ((pxm2node[idx].pxm == pxm) &&
@@ -110,78 +93,7 @@  nodeid_t setup_node(unsigned pxm)
 	return node;
 }
 
-int valid_numa_range(paddr_t start, paddr_t end, nodeid_t node)
-{
-	int i;
-
-	for (i = 0; i < num_node_memblks; i++) {
-		struct node *nd = &node_memblk_range[i];
-
-		if (nd->start <= start && nd->end >= end &&
-			memblk_nodeid[i] == node)
-			return 1;
-	}
-
-	return 0;
-}
-
-static
-enum conflicts __init conflicting_memblks(nodeid_t nid, paddr_t start,
-					  paddr_t end, paddr_t nd_start,
-					  paddr_t nd_end, unsigned int *mblkid)
-{
-	unsigned int i;
-
-	/*
-	 * Scan all recorded nodes' memory blocks to check conflicts:
-	 * Overlap or interleave.
-	 */
-	for (i = 0; i < num_node_memblks; i++) {
-		struct node *nd = &node_memblk_range[i];
-
-		*mblkid = i;
-
-		/* Skip 0 bytes node memory block. */
-		if (nd->start == nd->end)
-			continue;
-		/*
-		 * Use memblk range to check memblk overlaps, include the
-		 * self-overlap case. As nd's range is non-empty, the special
-		 * case "nd->end == end && nd->start == start" also can be covered.
-		 */
-		if (nd->end > start && nd->start < end)
-			return OVERLAP;
-
-		/*
-		 * Use node memory range to check whether new range contains
-		 * memory from other nodes - interleave check. We just need
-		 * to check full contains situation. Because overlaps have
-		 * been checked above.
-		 */
-	        if (nid != memblk_nodeid[i] &&
-		    nd->start >= nd_start && nd->end <= nd_end)
-			return INTERLEAVE;
-	}
-
-	return NO_CONFLICT;
-}
-
-static __init void cutoff_node(int i, paddr_t start, paddr_t end)
-{
-	struct node *nd = &nodes[i];
-	if (nd->start < start) {
-		nd->start = start;
-		if (nd->end < nd->start)
-			nd->start = nd->end;
-	}
-	if (nd->end > end) {
-		nd->end = end;
-		if (nd->start > nd->end)
-			nd->start = nd->end;
-	}
-}
-
-static __init void bad_srat(void)
+void __init bad_srat(void)
 {
 	int i;
 	printk(KERN_ERR "SRAT: SRAT not used.\n");
@@ -259,7 +171,7 @@  acpi_numa_x2apic_affinity_init(const struct acpi_srat_x2apic_cpu_affinity *pa)
 	}
 
 	apicid_to_node[pa->apic_id] = node;
-	node_set(node, processor_nodes_parsed);
+	numa_set_processor_nodes_parsed(node);
 	numa_status = numa_acpi;
 
 	if (opt_acpi_verbose)
@@ -294,7 +206,7 @@  acpi_numa_processor_affinity_init(const struct acpi_srat_cpu_affinity *pa)
 		return;
 	}
 	apicid_to_node[pa->apic_id] = node;
-	node_set(node, processor_nodes_parsed);
+	numa_set_processor_nodes_parsed(node);
 	numa_status = numa_acpi;
 
 	if (opt_acpi_verbose)
@@ -306,12 +218,9 @@  acpi_numa_processor_affinity_init(const struct acpi_srat_cpu_affinity *pa)
 void __init
 acpi_numa_memory_affinity_init(const struct acpi_srat_mem_affinity *ma)
 {
-	struct node *nd;
-	paddr_t nd_start, nd_end;
-	paddr_t start, end;
 	unsigned pxm;
 	nodeid_t node;
-	unsigned int i;
+	int ret;
 
 	if (srat_disabled())
 		return;
@@ -322,15 +231,14 @@  acpi_numa_memory_affinity_init(const struct acpi_srat_mem_affinity *ma)
 	if (!(ma->flags & ACPI_SRAT_MEM_ENABLED))
 		return;
 
-	start = ma->base_address;
-	end = start + ma->length;
 	/* Supplement the heuristics in l1tf_calculations(). */
-	l1tf_safe_maddr = max(l1tf_safe_maddr, ROUNDUP(end, PAGE_SIZE));
+	l1tf_safe_maddr = max(l1tf_safe_maddr,
+			      ROUNDUP(ma->base_address + ma->length,
+			      PAGE_SIZE));
 
-	if (num_node_memblks >= NR_NODE_MEMBLKS)
-	{
+	if (!numa_memblks_available()) {
 		dprintk(XENLOG_WARNING,
-                "Too many numa entry, try bigger NR_NODE_MEMBLKS \n");
+			"Too many numa entries, try bigger NR_NODE_MEMBLKS!\n");
 		bad_srat();
 		return;
 	}
@@ -344,129 +252,10 @@  acpi_numa_memory_affinity_init(const struct acpi_srat_mem_affinity *ma)
 		return;
 	}
 
-	/*
-	 * For the node that already has some memory blocks, we will
-	 * expand the node memory range temporarily to check memory
-	 * interleaves with other nodes. We will not use this node
-	 * temp memory range to check overlaps, because it will mask
-	 * the overlaps in same node.
-	 *
-	 * Node with 0 bytes memory doesn't need this expandsion.
-	 */
-	nd_start = start;
-	nd_end = end;
-	nd = &nodes[node];
-	if (nd->start != nd->end) {
-		if (nd_start > nd->start)
-			nd_start = nd->start;
-
-		if (nd_end < nd->end)
-			nd_end = nd->end;
-	}
-
-	/* It is fine to add this area to the nodes data it will be used later*/
-	switch (conflicting_memblks(node, start, end, nd_start, nd_end, &i)) {
-	case OVERLAP:
-		if (memblk_nodeid[i] == node) {
-			bool mismatch = !(ma->flags &
-					  ACPI_SRAT_MEM_HOT_PLUGGABLE) !=
-			                !test_bit(i, memblk_hotplug);
-
-			printk("%sSRAT: PXM %u [%"PRIpaddr", %"PRIpaddr"] overlaps with itself [%"PRIpaddr", %"PRIpaddr"]\n",
-			       mismatch ? KERN_ERR : KERN_WARNING, pxm, start,
-			       end - 1, node_memblk_range[i].start,
-			       node_memblk_range[i].end - 1);
-			if (mismatch) {
-				bad_srat();
-				return;
-			}
-			break;
-		}
-
-		printk(KERN_ERR
-		       "SRAT: PXM %u [%"PRIpaddr", %"PRIpaddr"] overlaps with PXM %u [%"PRIpaddr", %"PRIpaddr"]\n",
-		       pxm, start, end - 1, node_to_pxm(memblk_nodeid[i]),
-		       node_memblk_range[i].start,
-		       node_memblk_range[i].end - 1);
+	ret = numa_update_node_memblks(node, pxm, ma->base_address, ma->length,
+				       ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE);
+	if (ret)
 		bad_srat();
-		return;
-
-	case INTERLEAVE:
-		printk(KERN_ERR
-		       "SRAT: PXM %u: [%"PRIpaddr", %"PRIpaddr"] interleaves with PXM %u memblk [%"PRIpaddr", %"PRIpaddr"]\n",
-		       pxm, nd_start, nd_end - 1, node_to_pxm(memblk_nodeid[i]),
-		       node_memblk_range[i].start, node_memblk_range[i].end - 1);
-		bad_srat();
-		return;
-
-	case NO_CONFLICT:
-		break;
-	}
-
-	if (!(ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE)) {
-		node_set(node, memory_nodes_parsed);
-		nd->start = nd_start;
-		nd->end = nd_end;
-	}
-
-	printk(KERN_INFO "SRAT: Node %u PXM %u [%"PRIpaddr", %"PRIpaddr"]%s\n",
-	       node, pxm, start, end - 1,
-	       ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE ? " (hotplug)" : "");
-
-	node_memblk_range[num_node_memblks].start = start;
-	node_memblk_range[num_node_memblks].end = end;
-	memblk_nodeid[num_node_memblks] = node;
-	if (ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) {
-		__set_bit(num_node_memblks, memblk_hotplug);
-		mem_hotplug_update_boundary(end);
-	}
-	num_node_memblks++;
-}
-
-/* Sanity check to catch more bad SRATs (they are amazingly common).
-   Make sure the PXMs cover all memory. */
-static int __init nodes_cover_memory(void)
-{
-	unsigned int i;
-
-	for (i = 0; ; i++) {
-		int j, found;
-		paddr_t start, end;
-
-		/* Try to loop memory map from index 0 to end. */
-		found = arch_get_memory_map(i, &start, &end);
-
-		/* Index relate entry is not RAM, skip it. */
-		if (found == -EINVAL)
-			continue;
-
-		/* Reach the end of arch's memory map */
-		if (found == -ENODEV)
-			break;
-
-		do {
-			found = 0;
-			for_each_node_mask(j, memory_nodes_parsed)
-				if (start < nodes[j].end
-				    && end > nodes[j].start) {
-					if (start >= nodes[j].start) {
-						start = nodes[j].end;
-						found = 1;
-					}
-					if (end <= nodes[j].end) {
-						end = nodes[j].start;
-						found = 1;
-					}
-				}
-		} while (found && start < end);
-
-		if (start < end) {
-			printk(KERN_ERR "NUMA: No node for memory map range: "
-				"[%"PRIpaddr", %"PRIpaddr"]\n", start, end - 1);
-			return 0;
-		}
-	}
-	return 1;
 }
 
 void __init acpi_numa_arch_fixup(void) {}
@@ -522,58 +311,7 @@  void __init srat_parse_regions(paddr_t addr)
 	pfn_pdx_hole_setup(mask >> PAGE_SHIFT);
 }
 
-/* Use the information discovered above to actually set up the nodes. */
-int __init acpi_scan_nodes(paddr_t start, paddr_t end)
-{
-	int i;
-	nodemask_t all_nodes_parsed;
-
-	/* First clean up the node list */
-	for (i = 0; i < MAX_NUMNODES; i++)
-		cutoff_node(i, start, end);
-
-	/* Only when numa_on with good firmware, we can do numa scan nodes. */
-	if (!numa_enabled_with_firmware())
-		return -1;
-
-	if (!nodes_cover_memory()) {
-		bad_srat();
-		return -1;
-	}
-
-	memnode_shift = compute_hash_shift(node_memblk_range, num_node_memblks,
-				memblk_nodeid);
-
-	if (memnode_shift < 0) {
-		printk(KERN_ERR
-		     "SRAT: No NUMA node hash function found. Contact maintainer\n");
-		bad_srat();
-		return -1;
-	}
-
-	nodes_or(all_nodes_parsed, memory_nodes_parsed, processor_nodes_parsed);
-
-	/* Finally register nodes */
-	for_each_node_mask(i, all_nodes_parsed)
-	{
-		uint64_t size = nodes[i].end - nodes[i].start;
-
-		if ( size == 0 )
-			printk(KERN_INFO "SRAT: node %u has no memory\n", i);
-
-		setup_node_bootmem(i, nodes[i].start, nodes[i].end);
-	}
-	for (i = 0; i < nr_cpu_ids; i++) {
-		if (cpu_to_node[i] == NUMA_NO_NODE)
-			continue;
-		if (!nodemask_test(cpu_to_node[i], &processor_nodes_parsed))
-			numa_set_node(i, NUMA_NO_NODE);
-	}
-	numa_init_array();
-	return 0;
-}
-
-static unsigned node_to_pxm(nodeid_t n)
+unsigned node_to_pxm(nodeid_t n)
 {
 	unsigned i;
 
diff --git a/xen/common/numa.c b/xen/common/numa.c
index bc6a2ded14..e3b66c54b5 100644
--- a/xen/common/numa.c
+++ b/xen/common/numa.c
@@ -14,6 +14,21 @@ 
 #include <xen/softirq.h>
 #include <asm/acpi.h>
 
+static nodemask_t __initdata processor_nodes_parsed;
+static nodemask_t __initdata memory_nodes_parsed;
+static struct node __initdata nodes[MAX_NUMNODES];
+
+static int num_node_memblks;
+static struct node node_memblk_range[NR_NODE_MEMBLKS];
+static nodeid_t memblk_nodeid[NR_NODE_MEMBLKS];
+static __initdata DECLARE_BITMAP(memblk_hotplug, NR_NODE_MEMBLKS);
+
+enum conflicts {
+    NO_CONFLICT,
+    OVERLAP,
+    INTERLEAVE,
+};
+
 struct node_data node_data[MAX_NUMNODES];
 
 /* Mapping from pdx to node id */
@@ -32,6 +47,298 @@  nodemask_t __read_mostly node_online_map = { { [0] = 1UL } };
 
 enum numa_mode numa_status;
 
+void __init numa_set_processor_nodes_parsed(nodeid_t node)
+{
+    node_set(node, processor_nodes_parsed);
+}
+
+int valid_numa_range(paddr_t start, paddr_t end, nodeid_t node)
+{
+    int i;
+
+    for ( i = 0; i < num_node_memblks; i++ )
+    {
+        struct node *nd = &node_memblk_range[i];
+
+        if ( nd->start <= start && nd->end >= end &&
+             memblk_nodeid[i] == node )
+            return 1;
+    }
+
+    return 0;
+}
+
+static
+enum conflicts __init conflicting_memblks(nodeid_t nid, paddr_t start,
+                                          paddr_t end, paddr_t nd_start,
+                                          paddr_t nd_end, unsigned int *mblkid)
+{
+    unsigned int i;
+
+    /*
+     * Scan all recorded nodes' memory blocks to check conflicts:
+     * Overlap or interleave.
+     */
+    for ( i = 0; i < num_node_memblks; i++ )
+    {
+        struct node *nd = &node_memblk_range[i];
+
+        *mblkid = i;
+
+        /* Skip 0 bytes node memory block. */
+        if ( nd->start == nd->end )
+            continue;
+        /*
+         * Use memblk range to check memblk overlaps, include the
+         * self-overlap case. As nd's range is non-empty, the special
+         * case "nd->end == end && nd->start == start" also can be covered.
+         */
+        if ( nd->end > start && nd->start < end )
+            return OVERLAP;
+
+        /*
+         * Use node memory range to check whether new range contains
+         * memory from other nodes - interleave check. We just need
+         * to check full contains situation. Because overlaps have
+         * been checked above.
+         */
+        if ( nid != memblk_nodeid[i] &&
+             nd->start >= nd_start && nd->end <= nd_end )
+            return INTERLEAVE;
+    }
+
+    return NO_CONFLICT;
+}
+
+static void __init cutoff_node(int i, paddr_t start, paddr_t end)
+{
+    struct node *nd = &nodes[i];
+
+    if ( nd->start < start )
+    {
+        nd->start = start;
+        if ( nd->end < nd->start )
+            nd->start = nd->end;
+    }
+
+    if ( nd->end > end )
+    {
+        nd->end = end;
+        if ( nd->start > nd->end )
+            nd->start = nd->end;
+    }
+}
+
+bool __init numa_memblks_available(void)
+{
+    return num_node_memblks < NR_NODE_MEMBLKS;
+}
+
+/*
+ * This function will be called by NUMA memory affinity initialization to
+ * update NUMA node's memory range. In this function, we assume all memory
+ * regions belonging to a single node are in one chunk. Holes (or MMIO
+ * ranges) between them will be included in the node.
+ *
+ * So in numa_update_node_memblks, if there are multiple banks for each
+ * node, start and end are stretched to cover the holes between them, and
+ * it works as long as memory banks of different NUMA nodes don't interleave.
+ */
+int __init numa_update_node_memblks(nodeid_t node, unsigned pxm,
+                                    paddr_t start, paddr_t size,
+                                    bool hotplug)
+{
+    unsigned int i;
+    paddr_t end = start + size;
+    paddr_t nd_start = start;
+    paddr_t nd_end = end;
+    struct node *nd = &nodes[node];
+
+    /*
+     * For the node that already has some memory blocks, we will
+     * expand the node memory range temporarily to check memory
+     * interleaves with other nodes. We will not use this node
+     * temp memory range to check overlaps, because it will mask
+     * the overlaps in same node.
+     *
+     * Node with 0 bytes memory doesn't need this expandsion.
+     */
+    if ( nd->start != nd->end )
+    {
+        if ( nd_start > nd->start )
+            nd_start = nd->start;
+
+        if ( nd_end < nd->end )
+            nd_end = nd->end;
+    }
+
+    /* It is fine to add this area to the nodes data it will be used later*/
+    switch ( conflicting_memblks(node, start, end, nd_start, nd_end, &i) )
+    {
+    case OVERLAP:
+        if ( memblk_nodeid[i] == node )
+        {
+            bool mismatch = !(hotplug) != !test_bit(i, memblk_hotplug);
+
+            printk("%sNUMA: PXM %u [%"PRIpaddr", %"PRIpaddr"] overlaps with itself [%"PRIpaddr", %"PRIpaddr"]\n",
+                   mismatch ? KERN_ERR : KERN_WARNING, pxm, start, end - 1,
+                   node_memblk_range[i].start, node_memblk_range[i].end - 1);
+            if ( mismatch )
+                return -EINVAL;
+            break;
+        }
+
+        printk(KERN_ERR
+               "NUMA: PXM %u [%"PRIpaddr", %"PRIpaddr"] overlaps with PXM %u [%"PRIpaddr", %"PRIpaddr"]\n",
+               pxm, start, end - 1, node_to_pxm(memblk_nodeid[i]),
+               node_memblk_range[i].start, node_memblk_range[i].end - 1);
+        return -EINVAL;
+
+
+    case INTERLEAVE:
+        printk(KERN_ERR
+               "NUMA: PXM %u: [%"PRIpaddr", %"PRIpaddr"] interleaves with PXM %u memblk [%"PRIpaddr", %"PRIpaddr"]\n",
+               pxm, nd_start, nd_end - 1, node_to_pxm(memblk_nodeid[i]),
+               node_memblk_range[i].start, node_memblk_range[i].end - 1);
+        return -EINVAL;
+
+    case NO_CONFLICT:
+        break;
+    }
+
+    if ( !hotplug )
+    {
+        node_set(node, memory_nodes_parsed);
+        nd->start = nd_start;
+        nd->end = nd_end;
+    }
+
+    printk(KERN_INFO "NUMA: Node %u PXM %u [%"PRIpaddr", %"PRIpaddr"]%s\n",
+           node, pxm, start, end - 1,
+           hotplug ? " (hotplug)" : "");
+
+    node_memblk_range[num_node_memblks].start = start;
+    node_memblk_range[num_node_memblks].end = end;
+    memblk_nodeid[num_node_memblks] = node;
+    if ( hotplug )
+    {
+        __set_bit(num_node_memblks, memblk_hotplug);
+        mem_hotplug_update_boundary(end);
+    }
+    num_node_memblks++;
+
+    return 0;
+}
+
+/*
+ * Sanity check to catch more bad SRATs (they are amazingly common).
+ * Make sure the PXMs cover all memory.
+ */
+static int __init nodes_cover_memory(void)
+{
+    unsigned int i;
+
+    for ( i = 0; ; i++ )
+    {
+        int j, found;
+        paddr_t start, end;
+
+        /* Try to loop memory map from index 0 to end. */
+        found = arch_get_memory_map(i, &start, &end);
+
+        /* Index relate entry is not RAM, skip it. */
+        if ( found == -EINVAL )
+            continue;
+
+        /* Reach the end of arch's memory map */
+        if ( found == -ENODEV )
+            break;
+
+        do {
+            found = 0;
+            for_each_node_mask( j, memory_nodes_parsed )
+                if ( start < nodes[j].end
+                    && end > nodes[j].start )
+                {
+                    if ( start >= nodes[j].start )
+                    {
+                        start = nodes[j].end;
+                        found = 1;
+                    }
+
+                    if ( end <= nodes[j].end )
+                    {
+                        end = nodes[j].start;
+                        found = 1;
+                    }
+                }
+        } while ( found && start < end );
+
+        if ( start < end )
+        {
+            printk(KERN_ERR "NUMA: No node for memory map range: "
+                   "[%"PRIpaddr", %"PRIpaddr"]\n", start, end - 1);
+            return 0;
+        }
+    }
+    return 1;
+}
+
+/* Use the information discovered above to actually set up the nodes. */
+int __init acpi_scan_nodes(paddr_t start, paddr_t end)
+{
+    int i;
+    nodemask_t all_nodes_parsed;
+
+    /* First clean up the node list */
+    for ( i = 0; i < MAX_NUMNODES; i++ )
+        cutoff_node(i, start, end);
+
+    /* Only when numa_on with good firmware, we can do numa scan nodes. */
+    if ( !numa_enabled_with_firmware() )
+        return -1;
+
+    if ( !nodes_cover_memory() )
+    {
+        bad_srat();
+        return -1;
+    }
+
+    memnode_shift = compute_hash_shift(node_memblk_range, num_node_memblks,
+                                       memblk_nodeid);
+
+    if ( memnode_shift < 0 )
+    {
+        printk(KERN_ERR
+               "SRAT: No NUMA node hash function found. Contact maintainer\n");
+        bad_srat();
+        return -1;
+    }
+
+    nodes_or(all_nodes_parsed, memory_nodes_parsed, processor_nodes_parsed);
+
+    /* Finally register nodes */
+    for_each_node_mask( i, all_nodes_parsed )
+    {
+        paddr_t size = nodes[i].end - nodes[i].start;
+
+        if ( size == 0 )
+            printk(KERN_INFO "SRAT: node %u has no memory\n", i);
+
+        setup_node_bootmem(i, nodes[i].start, nodes[i].end);
+    }
+
+    for ( i = 0; i < nr_cpu_ids; i++ )
+    {
+        if ( cpu_to_node[i] == NUMA_NO_NODE )
+            continue;
+        if ( !nodemask_test(cpu_to_node[i], &processor_nodes_parsed) )
+            numa_set_node(i, NUMA_NO_NODE);
+    }
+    numa_init_array();
+    return 0;
+}
+
 /*
  * Given a shift value, try to populate memnodemap[]
  * Returns :
diff --git a/xen/include/xen/numa.h b/xen/include/xen/numa.h
index 6d02204991..564add430c 100644
--- a/xen/include/xen/numa.h
+++ b/xen/include/xen/numa.h
@@ -52,6 +52,7 @@  extern void numa_initmem_init(unsigned long start_pfn, unsigned long end_pfn);
 extern bool numa_enabled_with_firmware(void);
 extern enum numa_mode numa_status;
 
+extern void bad_srat(void);
 extern void numa_set_node(int cpu, nodeid_t node);
 extern void setup_node_bootmem(nodeid_t nodeid, paddr_t start, paddr_t end);
 
@@ -90,6 +91,12 @@  static inline __attribute__((pure)) nodeid_t phys_to_nid(paddr_t addr)
 
 extern int arch_get_memory_map(unsigned int idx,
                                paddr_t *start, paddr_t *end);
+extern int valid_numa_range(paddr_t start, paddr_t end, nodeid_t node);
+extern bool numa_memblks_available(void);
+extern int numa_update_node_memblks(nodeid_t node, unsigned pxm,
+                                    paddr_t start, paddr_t size,
+                                    bool hotplug);
+extern void numa_set_processor_nodes_parsed(nodeid_t node);
 
 #endif