Skip to contentSkip to conversation

sched/numa: Add fault driven placement policy

tip:sched/numa tip-bot for Peter Zijlstra 1 participant 1 message Oct 12, 2012

Subsystems (4)
lore
Share & tools
lore
#0 lore raw
Commit-ID:  8bf1f58fcfbd1b60bb9e687244f7804d2c503537
Gitweb:     http://git.kernel.org/tip/8bf1f58fcfbd1b60bb9e687244f7804d2c503537
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Tue, 9 Oct 2012 13:46:22 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Fri, 12 Oct 2012 12:07:18 +0200

sched/numa: Add fault driven placement policy

As per the problem/design document Documentation/scheduler/numa-problem.txt implement 3ac & 4.

A pure 3a was found too unstable, I did briefly try 3bc but found no significant improvement. We could add a NUMA_FREQ knob if people want to play further -- but for now implement the simplest form.

---
 include/linux/mm_types.h |    1 +
 include/linux/sched.h    |   30 ++++++++++++++++++++++++++-
 kernel/sched/core.c      |    7 +++++-
 kernel/sched/fair.c      |   49 ++++++++++++++++++++++++++++++++++++++++++++++
 kernel/sched/sched.h     |   10 ++++++++-
 mm/memory.c              |   12 ++++++++--
 mm/mempolicy.c           |   17 +++++++--------
 7 files changed, 110 insertions(+), 16 deletions(-)
include/linux/mm_types.h +1 -0
index d6dc76c..cd38809 100644
@@ -411,6 +411,7 @@ struct mm_struct {
 #endif
 #ifdef CONFIG_SCHED_NUMA
 	unsigned long numa_next_scan;
+	int numa_scan_seq;
 #endif
 	struct uprobes_state uprobes_state;
 };
include/linux/sched.h +28 -2
index b0a2c73..d6818d7 100644
@@ -1523,8 +1523,10 @@ struct task_struct {
 #endif
 #ifdef CONFIG_SCHED_NUMA
 	int node;			/* task home node   */
+	int numa_scan_seq;
 	u64 node_stamp;			/* migration stamp  */
 	unsigned long numa_contrib;
+	unsigned long *numa_faults;
 #endif /* CONFIG_SCHED_NUMA */
 
 	struct rcu_head rcu;
@@ -1598,15 +1600,38 @@ struct task_struct {
 /* Future-safe accessor for struct task_struct's cpus_allowed. */
 #define tsk_cpus_allowed(tsk) (&(tsk)->cpus_allowed)
 
+#ifdef CONFIG_SCHED_NUMA
 static inline int tsk_home_node(struct task_struct *p)
 {
-#ifdef CONFIG_SCHED_NUMA
 	return p->node;
+}
+
+extern void task_numa_placement(void);
+extern void __task_numa_fault(int node);
+static inline void task_numa_fault(int node)
+{
+	struct task_struct *p = current;
+
+	if (likely(p->numa_faults))
+		p->numa_faults[node]++;
+	else
+		__task_numa_fault(node);
+}
 #else
+static inline int tsk_home_node(struct task_struct *p)
+{
 	return -1;
-#endif
 }
 
+static inline void task_numa_placement(void)
+{
+}
+
+static inline void task_numa_fault(int node)
+{
+}
+#endif /* CONFIG_SCHED_NUMA */
+
 /*
  * Priority of a process goes from 0..MAX_PRIO-1, valid RT
  * priority is 0..MAX_RT_PRIO-1, and SCHED_NORMAL/SCHED_BATCH
@@ -2126,6 +2151,7 @@ extern int sched_setscheduler(struct task_struct *, int,
 			      const struct sched_param *);
 extern int sched_setscheduler_nocheck(struct task_struct *, int,
 				      const struct sched_param *);
+extern void sched_setnode(struct task_struct *p, int node);
 extern struct task_struct *idle_task(int cpu);
 /**
  * is_idle_task - is the specified task an idle task?
kernel/sched/core.c +6 -1
index 5c1be07..b149cad 100644
@@ -1535,11 +1535,15 @@ static void __sched_fork(struct task_struct *p)
 #endif
 
 #ifdef CONFIG_SCHED_NUMA
-	if (p->mm && atomic_read(&p->mm->mm_users) == 1)
+	if (p->mm && atomic_read(&p->mm->mm_users) == 1) {
 		p->mm->numa_next_scan = jiffies;
+		p->mm->numa_scan_seq = 0;
+	}
 
 	p->node = -1;
 	p->node_stamp = 0ULL;
+	p->numa_scan_seq = p->mm ? p->mm->numa_scan_seq : 0;
+	p->numa_faults = NULL;
 #endif /* CONFIG_SCHED_NUMA */
 }
 
@@ -1782,6 +1786,7 @@ static void finish_task_switch(struct rq *rq, struct task_struct *prev)
 	if (mm)
 		mmdrop(mm);
 	if (unlikely(prev_state == TASK_DEAD)) {
+		task_numa_free(prev);
 		/*
 		 * Remove function-return probe instances associated with this
 		 * task and put them back on the free list.
kernel/sched/fair.c +49 -0

61 lines not on this page · raw message

kernel/sched/sched.h +9 -1
index bc5fc3e..3060136 100644
@@ -3,6 +3,7 @@
 #include <linux/mutex.h>
 #include <linux/spinlock.h>
 #include <linux/stop_machine.h>
+#include <linux/slab.h>
 
 #include "cpupri.h"
 
@@ -503,7 +504,10 @@ static inline struct list_head *offnode_tasks(struct rq *rq)
 	return &rq->offnode_tasks;
 }
 
-void sched_setnode(struct task_struct *p, int node);
+static inline void task_numa_free(struct task_struct *p)
+{
+	kfree(p->numa_faults);
+}
 #else /* CONFIG_SCHED_NUMA */
 static inline bool offnode_task(struct task_struct *t)
 {
@@ -514,6 +518,10 @@ static inline struct list_head *offnode_tasks(struct rq *rq)
 {
 	return NULL;
 }
+
+static inline void task_numa_free(struct task_struct *p)
+{
+}
 #endif /* CONFIG_SCHED_NUMA */
 
 #ifdef CONFIG_SMP
mm/memory.c +9 -3
index 1ee7d7c..9ada7ed 100644
@@ -3443,18 +3443,24 @@ static bool pte_prot_none(struct vm_area_struct *vma, pte_t pte)
 }
 
 #ifdef CONFIG_NUMA
+
+
 static void do_prot_none_numa(struct mm_struct *mm, struct vm_area_struct *vma,
 			      unsigned long address, struct page *page)
 {
-	int node;
+	int node, page_nid = page_to_nid(page);
+
+	task_numa_placement();
 
 	/*
 	 * For NUMA systems we use the special PROT_NONE maps to drive
 	 * lazy page migration, see MPOL_MF_LAZY and related.
 	 */
 	node = mpol_misplaced(page, vma, address);
-	if (node != -1)
-		migrate_misplaced_page(mm, page, node);
+	if (node != -1 && !migrate_misplaced_page(mm, page, node))
+		page_nid = node;
+
+	task_numa_fault(page_nid);
 }
 #else
 static void do_prot_none_numa(struct mm_struct *mm, struct vm_area_struct *vma,
mm/mempolicy.c +8 -9
index 9034202..47793ce 100644
@@ -2255,17 +2255,16 @@ int mpol_misplaced(struct page *page, struct vm_area_struct *vma,
 	 *
 	 * This quadric squishes small probabilities, making it less likely
 	 * we act on an unlikely task<->page relation.
-	 *
-	 * NOTE: effectively we're using task-home-node<->page-node relations
-	 * since those are the only thing we can affect.
-	 *
-	 * NOTE: we're using task-home-node as opposed to the current node
-	 * the task might be running on, since the task-home-node is the
-	 * long-term node of this task, further reducing noise. Also see
-	 * task_tick_numa().
 	 */
 	if (pol->flags & MPOL_F_HOME) {
-		int last_nid = page_xchg_last_nid(page, polnid);
+		int last_nid;
+
+		/*
+		 * Migrate towards the current node, depends on
+		 * task_numa_placement() details.
+		 */
+		polnid = numa_node_id();
+		last_nid = page_xchg_last_nid(page, polnid);
 		if (last_nid != polnid)
 			goto out;
 	}