Subject: PCI: Run PCI driver initialization on local node

From: Andi Kleen <[email-masked]>
Date: Wed, 6 Jul 2005 19:56:03 +0200

Run PCI driver initialization on local node

Instead of adding messy kmalloc_node()s everywhere run the
PCI driver probe on the node local to the device.

This would not have helped for IDE, but should for
other more clean drivers that do more initialization in probe().
It won't help for drivers that do most of the work
on first open (like many network drivers)

Signed-off-by: Andi Kleen <[email-masked]>
Signed-off-by: Greg Kroah-Hartman <[email-masked]>

---
 drivers/pci/pci-driver.c  |   31 ++++++++++++++++++++++++++++++-
 include/linux/mempolicy.h |    1 +
 mm/mempolicy.c            |    2 +-
 3 files changed, 32 insertions(+), 2 deletions(-)

--- gregkh-2.6.orig/drivers/pci/pci-driver.c	2005-07-13 09:45:06.000000000 -0700
+++ gregkh-2.6/drivers/pci/pci-driver.c	2005-07-26 16:19:34.000000000 -0700
@@ -7,6 +7,7 @@
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/device.h>
+#include <linux/mempolicy.h>
 #include "pci.h"
 
 /*
@@ -163,6 +164,34 @@
 	return NULL;
 }
 
+static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
+			  const struct pci_device_id *id)
+{
+	int error;
+#ifdef CONFIG_NUMA
+	/* Execute driver initialization on node where the
+	   device's bus is attached to.  This way the driver likely
+	   allocates its local memory on the right node without
+	   any need to change it. */
+	struct mempolicy *oldpol;
+	cpumask_t oldmask = current->cpus_allowed;
+	int node = pcibus_to_node(dev->bus);
+	if (node >= 0 && node_online(node))
+	    set_cpus_allowed(current, node_to_cpumask(node));
+	/* And set default memory allocation policy */
+	oldpol = current->mempolicy;
+	current->mempolicy = &default_policy;
+	mpol_get(current->mempolicy);
+#endif
+	error = drv->probe(dev, id);
+#ifdef CONFIG_NUMA
+	set_cpus_allowed(current, oldmask);
+	mpol_free(current->mempolicy);
+	current->mempolicy = oldpol;
+#endif
+	return error;
+}
+
 /**
  * __pci_device_probe()
  * 
@@ -180,7 +209,7 @@
 
 		id = pci_match_device(drv, pci_dev);
 		if (id)
-			error = drv->probe(pci_dev, id);
+			error = pci_call_probe(drv, pci_dev, id);
 		if (error >= 0) {
 			pci_dev->driver = drv;
 			error = 0;
--- gregkh-2.6.orig/mm/mempolicy.c	2005-07-13 09:45:10.000000000 -0700
+++ gregkh-2.6/mm/mempolicy.c	2005-07-26 16:19:34.000000000 -0700
@@ -88,7 +88,7 @@
    policied. */
 static int policy_zone;
 
-static struct mempolicy default_policy = {
+struct mempolicy default_policy = {
 	.refcnt = ATOMIC_INIT(1), /* never free it */
 	.policy = MPOL_DEFAULT,
 };
--- gregkh-2.6.orig/include/linux/mempolicy.h	2005-06-17 12:48:29.000000000 -0700
+++ gregkh-2.6/include/linux/mempolicy.h	2005-07-26 16:19:34.000000000 -0700
@@ -152,6 +152,7 @@
 
 extern void numa_default_policy(void);
 extern void numa_policy_init(void);
+extern struct mempolicy default_policy;
 
 #else