From: Andi Kleen <ak@suse.de>

Suggested by Manfred Spraul.

__get_free_pages had a hack to do node interleaving allocation at boot
time.  This patch sets an interleave process policy using the NUMA API for
init and the idle threads instead.  Before entering the user space init the
policy is reset to default again.  Result is the same.

Advantage is less code and removing of a check from a fast path.

Removes more code than it adds.

I verified that the memory distribution after boot is roughly the same.

Signed-off-by: Andrew Morton <akpm@osdl.org>
---

 25-akpm/include/linux/mempolicy.h |   11 ++++++++++
 25-akpm/init/main.c               |    4 +++
 25-akpm/mm/mempolicy.c            |   18 +++++++++++++---
 25-akpm/mm/page_alloc.c           |   41 --------------------------------------
 4 files changed, 30 insertions(+), 44 deletions(-)

diff -puN include/linux/mempolicy.h~use-numa-policy-api-for-boot-time-policy include/linux/mempolicy.h
--- 25/include/linux/mempolicy.h~use-numa-policy-api-for-boot-time-policy	Thu Jun 10 13:53:39 2004
+++ 25-akpm/include/linux/mempolicy.h	Thu Jun 10 13:53:39 2004
@@ -153,6 +153,9 @@ void mpol_free_shared_policy(struct shar
 struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp,
 					    unsigned long idx);
 
+extern void numa_default_policy(void);
+extern void numa_policy_init(void);
+
 #else
 
 struct mempolicy {};
@@ -215,6 +218,14 @@ mpol_shared_policy_lookup(struct shared_
 #define vma_policy(vma) NULL
 #define vma_set_policy(vma, pol) do {} while(0)
 
+static inline void numa_policy_init(void)
+{
+}
+
+static inline void numa_default_policy(void)
+{
+}
+
 #endif /* CONFIG_NUMA */
 #endif /* __KERNEL__ */
 
diff -puN init/main.c~use-numa-policy-api-for-boot-time-policy init/main.c
--- 25/init/main.c~use-numa-policy-api-for-boot-time-policy	Thu Jun 10 13:53:39 2004
+++ 25-akpm/init/main.c	Thu Jun 10 13:53:39 2004
@@ -43,6 +43,7 @@
 #include <linux/efi.h>
 #include <linux/unistd.h>
 #include <linux/rmap.h>
+#include <linux/mempolicy.h>
 
 #include <asm/io.h>
 #include <asm/bugs.h>
@@ -394,6 +395,7 @@ static void __init smp_init(void)
 static void noinline rest_init(void)
 {
 	kernel_thread(init, NULL, CLONE_FS | CLONE_SIGHAND);
+	numa_default_policy();
 	unlock_kernel();
  	cpu_idle();
 } 
@@ -497,6 +499,7 @@ asmlinkage void __init start_kernel(void
 #endif
 	mem_init();
 	kmem_cache_init();
+	numa_policy_init();
 	if (late_time_init)
 		late_time_init();
 	calibrate_delay();
@@ -686,6 +689,7 @@ static int init(void * unused)
 	free_initmem();
 	unlock_kernel();
 	system_state = SYSTEM_RUNNING;
+	numa_default_policy();
 
 	if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
 		printk("Warning: unable to open an initial console.\n");
diff -puN mm/mempolicy.c~use-numa-policy-api-for-boot-time-policy mm/mempolicy.c
--- 25/mm/mempolicy.c~use-numa-policy-api-for-boot-time-policy	Thu Jun 10 13:53:39 2004
+++ 25-akpm/mm/mempolicy.c	Thu Jun 10 13:53:39 2004
@@ -1000,7 +1000,8 @@ void mpol_free_shared_policy(struct shar
 	up(&p->sem);
 }
 
-static __init int numa_policy_init(void)
+/* assumes fs == KERNEL_DS */
+void __init numa_policy_init(void)
 {
 	policy_cache = kmem_cache_create("numa_policy",
 					 sizeof(struct mempolicy),
@@ -1009,6 +1010,17 @@ static __init int numa_policy_init(void)
 	sn_cache = kmem_cache_create("shared_policy_node",
 				     sizeof(struct sp_node),
 				     0, SLAB_PANIC, NULL, NULL);
-	return 0;
+
+	/* Set interleaving policy for system init. This way not all
+	   the data structures allocated at system boot end up in node zero. */
+
+	if (sys_set_mempolicy(MPOL_INTERLEAVE, node_online_map, MAX_NUMNODES) < 0)
+		printk("numa_policy_init: interleaving failed\n");
+}
+
+/* Reset policy of current process to default.
+ * Assumes fs == KERNEL_DS */
+void __init numa_default_policy(void)
+{
+	sys_set_mempolicy(MPOL_DEFAULT, NULL, 0);
 }
-module_init(numa_policy_init);
diff -puN mm/page_alloc.c~use-numa-policy-api-for-boot-time-policy mm/page_alloc.c
--- 25/mm/page_alloc.c~use-numa-policy-api-for-boot-time-policy	Thu Jun 10 13:53:39 2004
+++ 25-akpm/mm/page_alloc.c	Thu Jun 10 13:53:39 2004
@@ -732,53 +732,12 @@ got_pg:
 
 EXPORT_SYMBOL(__alloc_pages);
 
-#ifdef CONFIG_NUMA
-/* Early boot: Everything is done by one cpu, but the data structures will be
- * used by all cpus - spread them on all nodes.
- */
-static __init unsigned long get_boot_pages(unsigned int gfp_mask, unsigned int order)
-{
-static int nodenr;
-	int i = nodenr;
-	struct page *page;
-
-	for (;;) {
-		if (i > nodenr + numnodes)
-			return 0;
-		if (node_present_pages(i%numnodes)) {
-			struct zone **z;
-			/* The node contains memory. Check that there is
-			 * memory in the intended zonelist.
-			 */
-			z = NODE_DATA(i%numnodes)->node_zonelists[gfp_mask & GFP_ZONEMASK].zones;
-			while (*z) {
-				if ( (*z)->free_pages > (1UL<<order))
-					goto found_node;
-				z++;
-			}
-		}
-		i++;
-	}
-found_node:
-	nodenr = i+1;
-	page = alloc_pages_node(i%numnodes, gfp_mask, order);
-	if (!page)
-		return 0;
-	return (unsigned long) page_address(page);
-}
-#endif
-
 /*
  * Common helper functions.
  */
 fastcall unsigned long __get_free_pages(unsigned int gfp_mask, unsigned int order)
 {
 	struct page * page;
-
-#ifdef CONFIG_NUMA
-	if (unlikely(system_state == SYSTEM_BOOTING))
-		return get_boot_pages(gfp_mask, order);
-#endif
 	page = alloc_pages(gfp_mask, order);
 	if (!page)
 		return 0;
_