From: "Jan Beulich" Subject: [PATCH] x86: fix NUMA handling (c/s 20599) Date: Wed, 06 Jan 2010 15:36:03 +0000 Message-ID: <4B44BBF30200007800028783@vpn.id2.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part39133BF3.1__=" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: xen-devel@lists.xensource.com Cc: Yunhong Jiang List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part39133BF3.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline c/s 20599 caused the hash shift to become significantly smaller on systems with an SRAT like this (XEN) SRAT: Node 0 PXM 0 0-a0000 (XEN) SRAT: Node 0 PXM 0 100000-80000000 (XEN) SRAT: Node 1 PXM 1 80000000-d0000000 (XEN) SRAT: Node 1 PXM 1 100000000-130000000 Comined with the static size of the memnodemap[] array, NUMA got therefore disabled on such systems. The backport from Linux was really incomplete, as Linux much earlier had already introduced a dynamcially allocated memnodemap[]. Further, doing to/from pdx translations on addresses just past a valid range is not correct, as it may strip/fail to insert non-zero bits in this case. Finally, using 63 as the cover-it-all shift value is invalid on 32bit, since pdx values are unsigned long. Signed-off-by: Jan Beulich --- 2010-01-06.orig/xen/arch/x86/numa.c 2010-01-06 15:16:18.000000000 = +0100 +++ 2010-01-06/xen/arch/x86/numa.c 2010-01-06 16:10:59.000000000 = +0100 @@ -30,7 +30,9 @@ struct node_data node_data[MAX_NUMNODES] =20 /* Mapping from pdx to node id */ int memnode_shift; -u8 memnodemap[NODEMAPSIZE]; +static typeof(*memnodemap) _memnodemap[2]; +unsigned long memnodemapsize; +u8 *memnodemap; =20 unsigned char cpu_to_node[NR_CPUS] __read_mostly =3D { [0 ... NR_CPUS-1] =3D NUMA_NO_NODE @@ -62,13 +64,13 @@ static int __init populate_memnodemap(co unsigned long spdx, epdx; int i, res =3D -1; =20 - memset(memnodemap, NUMA_NO_NODE, sizeof(memnodemap)); + memset(memnodemap, NUMA_NO_NODE, memnodemapsize * sizeof(*memnodema= p)); for (i =3D 0; i < numnodes; i++) { spdx =3D paddr_to_pdx(nodes[i].start); - epdx =3D paddr_to_pdx(nodes[i].end); + epdx =3D paddr_to_pdx(nodes[i].end - 1) + 1; if (spdx >=3D epdx) continue; - if ((epdx >> shift) >=3D NODEMAPSIZE) + if ((epdx >> shift) >=3D memnodemapsize) return 0; do { if (memnodemap[spdx >> shift] !=3D NUMA_NO_NODE) @@ -86,6 +88,28 @@ static int __init populate_memnodemap(co return res; } =20 +static int __init allocate_cachealigned_memnodemap(void) +{ + unsigned long size =3D PFN_UP(memnodemapsize * sizeof(*memnodemap))= ; + unsigned long mfn =3D alloc_boot_pages(size, 1); + + if (!mfn) { + printk(KERN_ERR + "NUMA: Unable to allocate Memory to Node hash = map\n"); + memnodemapsize =3D 0; + return -1; + } + + memnodemap =3D mfn_to_virt(mfn); + mfn <<=3D PAGE_SHIFT; + size <<=3D PAGE_SHIFT; + printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n", + mfn, mfn + size); + memnodemapsize =3D size / sizeof(*memnodemap); + + return 0; +} + /* * The LSB of all start and end addresses in the node map is the value of = the * maximum possible shift. @@ -99,7 +123,7 @@ static int __init extract_lsb_from_nodes =20 for (i =3D 0; i < numnodes; i++) { spdx =3D paddr_to_pdx(nodes[i].start); - epdx =3D paddr_to_pdx(nodes[i].end); + epdx =3D paddr_to_pdx(nodes[i].end - 1) + 1; if (spdx >=3D epdx) continue; bitfield |=3D spdx; @@ -108,9 +132,10 @@ static int __init extract_lsb_from_nodes memtop =3D epdx; } if (nodes_used <=3D 1) - i =3D 63; + i =3D BITS_PER_LONG - 1; else i =3D find_first_bit(&bitfield, sizeof(unsigned long)*8); + memnodemapsize =3D (memtop >> i) + 1; return i; } =20 @@ -120,6 +145,10 @@ int __init compute_hash_shift(struct nod int shift; =20 shift =3D extract_lsb_from_nodes(nodes, numnodes); + if (memnodemapsize <=3D ARRAY_SIZE(_memnodemap)) + memnodemap =3D _memnodemap; + else if (allocate_cachealigned_memnodemap()) + return -1; printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n", shift); =20 @@ -233,8 +262,8 @@ void __init numa_initmem_init(unsigned l (u64)start_pfn << PAGE_SHIFT, (u64)end_pfn << PAGE_SHIFT); /* setup dummy node covering all memory */=20 - memnode_shift =3D 63;=20 - memnodemap[0] =3D 0; + memnode_shift =3D BITS_PER_LONG - 1; + memnodemap =3D _memnodemap; nodes_clear(node_online_map); node_set_online(0); for (i =3D 0; i < NR_CPUS; i++) --- 2010-01-06.orig/xen/include/asm-x86/numa.h 2010-01-06 15:16:18.0000000= 00 +0100 +++ 2010-01-06/xen/include/asm-x86/numa.h 2010-01-06 15:15:41.0000000= 00 +0100 @@ -25,7 +25,6 @@ extern int pxm_to_node(int nid); =20 #define ZONE_ALIGN (1UL << (MAX_ORDER+PAGE_SHIFT)) #define VIRTUAL_BUG_ON(x)=20 -#define NODEMAPSIZE 0xfff =20 extern void numa_add_cpu(int cpu); extern void numa_init_array(void); @@ -51,7 +50,8 @@ static inline void clear_node_cpumask(in =20 /* Simple perfect hash to map pdx to node numbers */ extern int memnode_shift;=20 -extern u8 memnodemap[NODEMAPSIZE];=20 +extern unsigned long memnodemapsize; +extern u8 *memnodemap; =20 struct node_data { unsigned long node_start_pfn; @@ -64,7 +64,7 @@ extern struct node_data node_data[]; static inline __attribute__((pure)) int phys_to_nid(paddr_t addr)=20 {=20 unsigned nid; - VIRTUAL_BUG_ON((paddr_to_pdx(addr) >> memnode_shift) >=3D = NODEMAPSIZE); + VIRTUAL_BUG_ON((paddr_to_pdx(addr) >> memnode_shift) >=3D = memnodemapsize); nid =3D memnodemap[paddr_to_pdx(addr) >> memnode_shift];=20 VIRTUAL_BUG_ON(nid >=3D MAX_NUMNODES || !node_data[nid]);=20 return nid;=20 --=__Part39133BF3.1__= Content-Type: text/plain; name="xen-x86-numa-node-map.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="xen-x86-numa-node-map.patch" c/s 20599 caused the hash shift to become significantly smaller on=0Asystem= s with an SRAT like this=0A=0A(XEN) SRAT: Node 0 PXM 0 0-a0000=0A(XEN) = SRAT: Node 0 PXM 0 100000-80000000=0A(XEN) SRAT: Node 1 PXM 1 80000000-d000= 0000=0A(XEN) SRAT: Node 1 PXM 1 100000000-130000000=0A=0AComined with the = static size of the memnodemap[] array, NUMA got=0Atherefore disabled on = such systems. The backport from Linux was really=0Aincomplete, as Linux = much earlier had already introduced a dynamcially=0Aallocated memnodemap[].= =0A=0AFurther, doing to/from pdx translations on addresses just past a = valid=0Arange is not correct, as it may strip/fail to insert non-zero bits = in=0Athis case.=0A=0AFinally, using 63 as the cover-it-all shift value is = invalid on 32bit,=0Asince pdx values are unsigned long.=0A=0ASigned-off-by:= Jan Beulich =0A=0A--- 2010-01-06.orig/xen/arch/x86/nu= ma.c 2010-01-06 15:16:18.000000000 +0100=0A+++ 2010-01-06/xen/arch/x86/n= uma.c 2010-01-06 16:10:59.000000000 +0100=0A@@ -30,7 +30,9 @@ struct = node_data node_data[MAX_NUMNODES]=0A =0A /* Mapping from pdx to node id = */=0A int memnode_shift;=0A-u8 memnodemap[NODEMAPSIZE];=0A+static = typeof(*memnodemap) _memnodemap[2];=0A+unsigned long memnodemapsize;=0A+u8 = *memnodemap;=0A =0A unsigned char cpu_to_node[NR_CPUS] __read_mostly =3D = {=0A [0 ... NR_CPUS-1] =3D NUMA_NO_NODE=0A@@ -62,13 +64,13 @@ static = int __init populate_memnodemap(co=0A unsigned long spdx, epdx;=0A = int i, res =3D -1;=0A =0A- memset(memnodemap, NUMA_NO_NODE, sizeof(mem= nodemap));=0A+ memset(memnodemap, NUMA_NO_NODE, memnodemapsize * = sizeof(*memnodemap));=0A for (i =3D 0; i < numnodes; i++) {=0A = spdx =3D paddr_to_pdx(nodes[i].start);=0A- epdx =3D paddr_to_p= dx(nodes[i].end);=0A+ epdx =3D paddr_to_pdx(nodes[i].end - 1) + = 1;=0A if (spdx >=3D epdx)=0A continue;=0A- = if ((epdx >> shift) >=3D NODEMAPSIZE)=0A+ if ((epdx >> = shift) >=3D memnodemapsize)=0A return 0;=0A do = {=0A if (memnodemap[spdx >> shift] !=3D NUMA_NO_NODE)=0A= @@ -86,6 +88,28 @@ static int __init populate_memnodemap(co=0A return = res;=0A }=0A =0A+static int __init allocate_cachealigned_memnodemap(void)= =0A+{=0A+ unsigned long size =3D PFN_UP(memnodemapsize * sizeof(*memn= odemap));=0A+ unsigned long mfn =3D alloc_boot_pages(size, 1);=0A+=0A+ = if (!mfn) {=0A+ printk(KERN_ERR=0A+ "NUMA: = Unable to allocate Memory to Node hash map\n");=0A+ memnodemaps= ize =3D 0;=0A+ return -1;=0A+ }=0A+=0A+ memnodemap =3D = mfn_to_virt(mfn);=0A+ mfn <<=3D PAGE_SHIFT;=0A+ size <<=3D = PAGE_SHIFT;=0A+ printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - = %lx\n",=0A+ mfn, mfn + size);=0A+ memnodemapsize =3D size / = sizeof(*memnodemap);=0A+=0A+ return 0;=0A+}=0A+=0A /*=0A * The LSB of = all start and end addresses in the node map is the value of the=0A * = maximum possible shift.=0A@@ -99,7 +123,7 @@ static int __init extract_lsb_= from_nodes=0A =0A for (i =3D 0; i < numnodes; i++) {=0A = spdx =3D paddr_to_pdx(nodes[i].start);=0A- epdx =3D paddr_to_p= dx(nodes[i].end);=0A+ epdx =3D paddr_to_pdx(nodes[i].end - 1) + = 1;=0A if (spdx >=3D epdx)=0A continue;=0A = bitfield |=3D spdx;=0A@@ -108,9 +132,10 @@ static int __init extract_lsb_fr= om_nodes=0A memtop =3D epdx;=0A }=0A if = (nodes_used <=3D 1)=0A- i =3D 63;=0A+ i =3D BITS_PER_LONG= - 1;=0A else=0A i =3D find_first_bit(&bitfield, = sizeof(unsigned long)*8);=0A+ memnodemapsize =3D (memtop >> i) + 1;=0A = return i;=0A }=0A =0A@@ -120,6 +145,10 @@ int __init compute_hash_shift(str= uct nod=0A int shift;=0A =0A shift =3D extract_lsb_from_nodes(no= des, numnodes);=0A+ if (memnodemapsize <=3D ARRAY_SIZE(_memnodemap))=0A= + memnodemap =3D _memnodemap;=0A+ else if (allocate_cachealig= ned_memnodemap())=0A+ return -1;=0A printk(KERN_DEBUG "NUMA: = Using %d for the hash shift.\n",=0A shift);=0A =0A@@ -233,8 = +262,8 @@ void __init numa_initmem_init(unsigned l=0A (u64)start_p= fn << PAGE_SHIFT,=0A (u64)end_pfn << PAGE_SHIFT);=0A /* setup = dummy node covering all memory */ =0A- memnode_shift =3D 63; =0A- = memnodemap[0] =3D 0;=0A+ memnode_shift =3D BITS_PER_LONG - 1;=0A+ = memnodemap =3D _memnodemap;=0A nodes_clear(node_online_map);=0A = node_set_online(0);=0A for (i =3D 0; i < NR_CPUS; i++)=0A--- 2010-01-06.or= ig/xen/include/asm-x86/numa.h 2010-01-06 15:16:18.000000000 +0100=0A+++ = 2010-01-06/xen/include/asm-x86/numa.h 2010-01-06 15:15:41.000000000 = +0100=0A@@ -25,7 +25,6 @@ extern int pxm_to_node(int nid);=0A =0A #define = ZONE_ALIGN (1UL << (MAX_ORDER+PAGE_SHIFT))=0A #define VIRTUAL_BUG_ON(x) = =0A-#define NODEMAPSIZE 0xfff=0A =0A extern void numa_add_cpu(int cpu);=0A = extern void numa_init_array(void);=0A@@ -51,7 +50,8 @@ static inline void = clear_node_cpumask(in=0A =0A /* Simple perfect hash to map pdx to node = numbers */=0A extern int memnode_shift; =0A-extern u8 memnodemap[NODEMAPSI= ZE]; =0A+extern unsigned long memnodemapsize;=0A+extern u8 *memnodemap;=0A = =0A struct node_data {=0A unsigned long node_start_pfn;=0A@@ -64,7 = +64,7 @@ extern struct node_data node_data[];=0A static inline __attribute_= _((pure)) int phys_to_nid(paddr_t addr) =0A { =0A unsigned nid;=0A- = VIRTUAL_BUG_ON((paddr_to_pdx(addr) >> memnode_shift) >=3D NODEMAPSIZE);=0A+= VIRTUAL_BUG_ON((paddr_to_pdx(addr) >> memnode_shift) >=3D = memnodemapsize);=0A nid =3D memnodemap[paddr_to_pdx(addr) >> memnode_sh= ift]; =0A VIRTUAL_BUG_ON(nid >=3D MAX_NUMNODES || !node_data[nid]); = =0A return nid; =0A --=__Part39133BF3.1__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --=__Part39133BF3.1__=--