Skip to contentSkip to conversation

sched/numa/mm: Improve migration

tip:numa/core tip-bot for Peter Zijlstra 4 participants 8 messages Oct 18, 2012 – Oct 22, 2012

Subsystems (18)
lore
Share & tools
lore
#0 lore raw
Commit-ID:  713f937655c4b15131b5a0eae4610918a4febe17
Gitweb:     http://git.kernel.org/tip/713f937655c4b15131b5a0eae4610918a4febe17
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Fri, 12 Oct 2012 19:30:14 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 15 Oct 2012 14:18:40 +0200

sched/numa/mm: Improve migration

Add THP migration. Extend task_numa_fault() to absorb THP faults.

[ Would be nice if the gents on Cc: expressed their opinion about
this change. A missing detail might be cgroup page accounting, plus the fact that some architectures might cache PMD_NONE pmds in their TLBs, needing some extra TLB magic beyond what we already do here? ]

Cc Johannes Weiner <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Thomas Gleixner <[email protected]>
[ Significant fixes ]
---
 include/linux/sched.h |    4 +-
 kernel/sched/fair.c   |    4 +-
 mm/huge_memory.c      |  142 +++++++++++++++++++++++++++++++++++++++---------
 mm/migrate.c          |    2 +-
 4 files changed, 120 insertions(+), 32 deletions(-)
include/linux/sched.h +2 -2
index 22be2d6..2c3009b 100644
@@ -1609,7 +1609,7 @@ static inline int tsk_home_node(struct task_struct *p)
 }
 
 extern void task_numa_placement(void);
-extern void task_numa_fault(int node);
+extern void task_numa_fault(int node, int pages);
 #else
 static inline int tsk_home_node(struct task_struct *p)
 {
@@ -1620,7 +1620,7 @@ static inline void task_numa_placement(void)
 {
 }
 
-static inline void task_numa_fault(int node)
+static inline void task_numa_fault(int node, int pages)
 {
 }
 #endif /* CONFIG_SCHED_NUMA */
kernel/sched/fair.c +2 -2
index 7d522d0..df35c8d 100644
@@ -835,7 +835,7 @@ unsigned int sysctl_sched_numa_settle_count = 2;
 /*
  * Got a PROT_NONE fault for a page on @node.
  */
-void task_numa_fault(int node)
+void task_numa_fault(int node, int pages)
 {
 	struct task_struct *p = current;
 
@@ -846,7 +846,7 @@ void task_numa_fault(int node)
 			return;
 	}
 
-	p->numa_faults[node]++;
+	p->numa_faults[node] += pages;
 }
 
 void task_numa_placement(void)
mm/huge_memory.c +115 -27

170 lines not on this page · raw message

mm/migrate.c +1 -1
index e03ed0b..e3cff03 100644
@@ -417,7 +417,7 @@ int migrate_huge_page_move_mapping(struct address_space *mapping,
  */
 void migrate_page_copy(struct page *newpage, struct page *page)
 {
-	if (PageHuge(page))
+	if (PageHuge(page) || PageTransHuge(page))
 		copy_huge_page(newpage, page);
 	else
 		copy_highpage(newpage, page);
#1 lore raw
Quoted 9 lines · tip-bot for Peter Zijlstra
Commit-ID:  713f937655c4b15131b5a0eae4610918a4febe17
Gitweb:     http://git.kernel.org/tip/713f937655c4b15131b5a0eae4610918a4febe17
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Fri, 12 Oct 2012 19:30:14 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 15 Oct 2012 14:18:40 +0200

sched/numa/mm: Improve migration

Quoted 7 lines

Add THP migration. Extend task_numa_fault() to absorb THP faults.

[ Would be nice if the gents on Cc: expressed their opinion about
this change. A missing detail might be cgroup page accounting, plus the fact that some architectures might cache PMD_NONE pmds in their TLBs, needing some extra TLB magic beyond what we already do here? ]

Looks good to me, the cgroup fixup should be easy enough as well (added the calls inline below).

Of course I'm banging my head into a wall for not seeing earlier through the existing migration path how easy this could be. It would be great for compaction to have this fastpath in the traditional migration code too.

Right now, unlike the traditional migration path, this breaks COW for every migration, but maybe you don't care about shared pages in the first place. And fixing that should be nothing more than grabbing the anon_vma lock and using rmap to switch more than one pmd over, right?

It won't work for pages in swap, which is only a future problem.

It's slightly ugly that migrate_page_copy() actually modifies the existing page (deactivation, munlock) when you end up having to revert back to it.

The new page needs to be PageUptodate.

mm/huge_memory.c void do_huge_pmd_prot_none(struct mm_struct *mm, struct vm_area_struct *vma, · line 835

Quoted 10 lines
mm/huge_memory.c +10 -0
+	task_numa_placement();
+
+	new_page = alloc_pages_node(node,
+	    (GFP_TRANSHUGE | GFP_THISNODE) & ~(__GFP_NO_KSWAPD | __GFP_WAIT),
+	    HPAGE_PMD_ORDER);
+
+	WARN_ON(PageLRU(new_page));
+
+	if (!new_page)
+		goto alloc_fail;
	mem_cgroup_prepare_migration(page, new_page, &memcg);
Quoted 52 lines
mm/huge_memory.c +38 -9
+	lru = PageLRU(page);
+
+	if (lru && isolate_lru_page(page)) /* does an implicit get_page() */
+		goto alloc_fail;
+
+	if (!trylock_page(new_page))
+		BUG();
+
+	/* anon mapping, we can simply copy page->mapping to the new page: */
+	new_page->mapping = page->mapping;
+	new_page->index = page->index;
+
+	migrate_page_copy(new_page, page);
+
+	WARN_ON(PageLRU(new_page));
 
-do_fixup:
 	spin_lock(&mm->page_table_lock);
-	if (unlikely(!pmd_same(*pmd, entry)))
-		goto out_unlock;
-#endif
+	if (unlikely(!pmd_same(*pmd, entry))) {
+		spin_unlock(&mm->page_table_lock);
+		if (lru)
+			putback_lru_page(page);
 
-	/* change back to regular protection */
-	entry = pmd_modify(entry, vma->vm_page_prot);
-	if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
-		update_mmu_cache(vma, address, entry);
+		unlock_page(new_page);
+		ClearPageActive(new_page);	/* Set by migrate_page_copy() */
+		new_page->mapping = NULL;
+		put_page(new_page);		/* Free it */
 
-out_unlock:
+		unlock_page(page);
+		put_page(page);			/* Drop the local reference */
+
+		return;
+	}
+
+	entry = mk_pmd(new_page, vma->vm_page_prot);
+	entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
+	entry = pmd_mkhuge(entry);
+
+	page_add_new_anon_rmap(new_page, vma, haddr);
+
+	set_pmd_at(mm, haddr, pmd, entry);
+	update_mmu_cache(vma, address, entry);
+	page_remove_rmap(page);
 	spin_unlock(&mm->page_table_lock);

mm/huge_memory.c void do_huge_pmd_prot_none(struct mm_struct *mm, struct vm_area_struct *vma, · line 888

Quoted 10 lines
mm/huge_memory.c +9 -1
-	if (page)
+
+	put_page(page);			/* Drop the rmap reference */
+
+	task_numa_fault(node, HPAGE_PMD_NR);
+
+	if (lru)
+		put_page(page);		/* drop the LRU isolation reference */
+
+	unlock_page(new_page);
	mem_cgroup_end_migration(memcg, page, new_page, true);

mm/huge_memory.c void do_huge_pmd_prot_none(struct mm_struct *mm, struct vm_area_struct *vma, · line 896

Quoted 8 lines
mm/huge_memory.c +8 -0
+	unlock_page(page);
+	put_page(page);			/* Drop the local reference */
+
+	return;
+
+alloc_fail:
+	if (new_page)
+		put_page(new_page);
		mem_cgroup_end_migration(memcg, page, new_page, false);
	}
Quoted 13 lines
mm/huge_memory.c +9 -0
+	task_numa_fault(page_to_nid(page), HPAGE_PMD_NR);
+	unlock_page(page);
+
+	spin_lock(&mm->page_table_lock);
+	if (unlikely(!pmd_same(*pmd, entry))) {
 		put_page(page);
+		page = NULL;
+		goto unlock;
+	}
+	goto fixup;
 }
 
 int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
#2 lore raw
Quoted 2 lines · Johannes Weiner

Of course I'm banging my head into a wall for not seeing earlier through the existing migration path how easy this could be.

There's a reason I keep promoting the idea of 'someone' rewriting all that page-migration code :-) I forever get lost in there.

Also note that the proposed code will do 'wasted' work in case the THP page gets split from under us, given that splits are relatively rare (and if they're not, we should make them so) this didn't seem a problem.

Also, this code very much relies on our PROT_NONE marking, it avoids the whole migration-PTE dance usually done, further the assumption that THP pages are anonymous only did help keep it simpler -- if someone 'fixes' that this needs more TLC.

#3 lore raw
In reply to Johannes Weiner
Quoted 5 lines · Johannes Weiner

Right now, unlike the traditional migration path, this breaks COW for every migration, but maybe you don't care about shared pages in the first place. And fixing that should be nothing more than grabbing the anon_vma lock and using rmap to switch more than one pmd over, right?

This patch was very much about getting _something_ rather than caring (too much) about all the weird corner cases.

And yes, your suggestion sounds about right.

#4 lore raw
In reply to Johannes Weiner
Quoted 3 lines · Johannes Weiner

It's slightly ugly that migrate_page_copy() actually modifies the existing page (deactivation, munlock) when you end up having to revert back to it.

The worst is actually calling copy_huge_page() on a THP.. it seems to work though ;-)

#5 lore raw
In reply to Johannes Weiner
Quoted 39 lines · Johannes Weiner
Quoted 9 lines · tip-bot for Peter Zijlstra
Commit-ID:  713f937655c4b15131b5a0eae4610918a4febe17
Gitweb:     http://git.kernel.org/tip/713f937655c4b15131b5a0eae4610918a4febe17
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Fri, 12 Oct 2012 19:30:14 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 15 Oct 2012 14:18:40 +0200

sched/numa/mm: Improve migration

Quoted 7 lines

Add THP migration. Extend task_numa_fault() to absorb THP faults.

[ Would be nice if the gents on Cc: expressed their opinion about
this change. A missing detail might be cgroup page accounting, plus the fact that some architectures might cache PMD_NONE pmds in their TLBs, needing some extra TLB magic beyond what we already do here? ]

Looks good to me, the cgroup fixup should be easy enough as well (added the calls inline below).

Of course I'm banging my head into a wall for not seeing earlier through the existing migration path how easy this could be. It would be great for compaction to have this fastpath in the traditional migration code too.

Right now, unlike the traditional migration path, this breaks COW for every migration, but maybe you don't care about shared pages in the first place. And fixing that should be nothing more than grabbing the anon_vma lock and using rmap to switch more than one pmd over, right?

It won't work for pages in swap, which is only a future problem.

It's slightly ugly that migrate_page_copy() actually modifies the existing page (deactivation, munlock) when you end up having to revert back to it.

The new page needs to be PageUptodate.

Quoted 7 lines

mm/huge_memory.c void do_huge_pmd_prot_none(struct mm_struct *mm, struct vm_area_struct *vma, · line 832

Quoted 7 lines
mm/huge_memory.c +7 -0
+	task_numa_placement();
+
+	new_page = alloc_pages_node(node,
+	    (GFP_TRANSHUGE | GFP_THISNODE) & ~(__GFP_NO_KSWAPD | __GFP_WAIT),
+	    HPAGE_PMD_ORDER);
+
+	WARN_ON(PageLRU(new_page));

This WARN_ON() is somewhat problematic in OOM or OOLNM situations, so I removed it ;-)

Quoted 82 lines

mm/huge_memory.c void do_huge_pmd_prot_none(struct mm_struct *mm, struct vm_area_struct *vma, · line 835

Quoted 3 lines
mm/huge_memory.c +3 -0
+
+	if (!new_page)
+		goto alloc_fail;
	mem_cgroup_prepare_migration(page, new_page, &memcg);
Quoted 52 lines
mm/huge_memory.c +38 -9
+	lru = PageLRU(page);
+
+	if (lru && isolate_lru_page(page)) /* does an implicit get_page() */
+		goto alloc_fail;
+
+	if (!trylock_page(new_page))
+		BUG();
+
+	/* anon mapping, we can simply copy page->mapping to the new page: */
+	new_page->mapping = page->mapping;
+	new_page->index = page->index;
+
+	migrate_page_copy(new_page, page);
+
+	WARN_ON(PageLRU(new_page));
 
-do_fixup:
 	spin_lock(&mm->page_table_lock);
-	if (unlikely(!pmd_same(*pmd, entry)))
-		goto out_unlock;
-#endif
+	if (unlikely(!pmd_same(*pmd, entry))) {
+		spin_unlock(&mm->page_table_lock);
+		if (lru)
+			putback_lru_page(page);
 
-	/* change back to regular protection */
-	entry = pmd_modify(entry, vma->vm_page_prot);
-	if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
-		update_mmu_cache(vma, address, entry);
+		unlock_page(new_page);
+		ClearPageActive(new_page);	/* Set by migrate_page_copy() */
+		new_page->mapping = NULL;
+		put_page(new_page);		/* Free it */
 
-out_unlock:
+		unlock_page(page);
+		put_page(page);			/* Drop the local reference */
+
+		return;
+	}
+
+	entry = mk_pmd(new_page, vma->vm_page_prot);
+	entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
+	entry = pmd_mkhuge(entry);
+
+	page_add_new_anon_rmap(new_page, vma, haddr);
+
+	set_pmd_at(mm, haddr, pmd, entry);
+	update_mmu_cache(vma, address, entry);
+	page_remove_rmap(page);
 	spin_unlock(&mm->page_table_lock);

mm/huge_memory.c void do_huge_pmd_prot_none(struct mm_struct *mm, struct vm_area_struct *vma, · line 888

Quoted 10 lines
mm/huge_memory.c +9 -1
-	if (page)
+
+	put_page(page);			/* Drop the rmap reference */
+
+	task_numa_fault(node, HPAGE_PMD_NR);
+
+	if (lru)
+		put_page(page);		/* drop the LRU isolation reference */
+
+	unlock_page(new_page);
	mem_cgroup_end_migration(memcg, page, new_page, true);

mm/huge_memory.c void do_huge_pmd_prot_none(struct mm_struct *mm, struct vm_area_struct *vma, · line 896

Quoted 8 lines
mm/huge_memory.c +8 -0
+	unlock_page(page);
+	put_page(page);			/* Drop the local reference */
+
+	return;
+
+alloc_fail:
+	if (new_page)
+		put_page(new_page);
		mem_cgroup_end_migration(memcg, page, new_page, false);
	}
Quoted 11 lines

mm/huge_memory.c void do_huge_pmd_prot_none(struct mm_struct *mm, struct vm_area_struct *vma, · line 908

Quoted 11 lines
mm/huge_memory.c +9 -0
+	task_numa_fault(page_to_nid(page), HPAGE_PMD_NR);
+	unlock_page(page);
+
+	spin_lock(&mm->page_table_lock);
+	if (unlikely(!pmd_same(*pmd, entry))) {
 		put_page(page);
+		page = NULL;
+		goto unlock;
+	}
+	goto fixup;
 }

Cool!

Would any of the gents be interested in turning the suggestions above into a suitable patch against this tree:

git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git numa/core

?

Thanks a ton!

Ingo

reviewer
#6 lore raw
In reply to tip-bot for Peter Zijlstra
Quoted 9 lines · tip-bot for Peter Zijlstra
Commit-ID:  713f937655c4b15131b5a0eae4610918a4febe17
Gitweb:     http://git.kernel.org/tip/713f937655c4b15131b5a0eae4610918a4febe17
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Fri, 12 Oct 2012 19:30:14 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 15 Oct 2012 14:18:40 +0200

sched/numa/mm: Improve migration

Quoted 8 lines

Add THP migration. Extend task_numa_fault() to absorb THP faults.

[ Would be nice if the gents on Cc: expressed their opinion about
this change. A missing detail might be cgroup page accounting, plus the fact that some architectures might cache PMD_NONE pmds in their TLBs, needing some extra TLB magic beyond what we already do here? ]

I'm travelling for a conference at the moment so will not get the chance to properly review this until I get back. Is there any plan to post the schednuma patches to linux-mm so the full series can be reviewed? I can extract the patches from -tip when I get back but it's still less than ideal from a review standpoint.

Superficially, the patch looks ok but as I lack context on what the rest of schednuma looks like I cannot be sure so I'm not going to ack it. Basically this is very similar to __unmap_and_move except it doesn't deal with migration PTEs -- presumably because the PTE is PROT_NONE so it gets queued up behind it. There is a downside of that. With migration PTEs, faults during migration will wait on the PTE. With this approach, I think multiple faults will alloc a hugepage, realise the ptes are no longer the same and back off. It should still work but it's potentially more expensive. Was that considered? Is it deliberate? If so, why?

It also feels like the migration part should have been a helper function called unmap_and_move_thp() in migrate.c instead of being buried in mm/huge_memory.c

Signature · 2 lines
Mel Gorman
SUSE Labs
#7 lore raw
Quoted 19 lines · Mel Gorman
Quoted 9 lines · tip-bot for Peter Zijlstra
Commit-ID:  713f937655c4b15131b5a0eae4610918a4febe17
Gitweb:     http://git.kernel.org/tip/713f937655c4b15131b5a0eae4610918a4febe17
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Fri, 12 Oct 2012 19:30:14 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 15 Oct 2012 14:18:40 +0200

sched/numa/mm: Improve migration

Quoted 8 lines

Add THP migration. Extend task_numa_fault() to absorb THP faults.

[ Would be nice if the gents on Cc: expressed their opinion about
this change. A missing detail might be cgroup page accounting, plus the fact that some architectures might cache PMD_NONE pmds in their TLBs, needing some extra TLB magic beyond what we already do here? ]

Quoted 6 lines

I'm travelling for a conference at the moment so will not get the chance to properly review this until I get back. Is there any plan to post the schednuma patches to linux-mm so the full series can be reviewed? I can extract the patches from -tip when I get back but it's still less than ideal from a review standpoint.

Sure, and I'd suggest to rather (re-)post individual MM patches that you find interesting than sending out a 50+ patch series.

I've also attached below the combo patch, against current -linus, this shows all the mm/ changes in their totality.

Quoted 12 lines

Superficially, the patch looks ok but as I lack context on what the rest of schednuma looks like I cannot be sure so I'm not going to ack it. Basically this is very similar to __unmap_and_move except it doesn't deal with migration PTEs -- presumably because the PTE is PROT_NONE so it gets queued up behind it. There is a downside of that. With migration PTEs, faults during migration will wait on the PTE. With this approach, I think multiple faults will alloc a hugepage, realise the ptes are no longer the same and back off. It should still work but it's potentially more expensive. Was that considered? Is it deliberate? If so, why?

Quoted 3 lines

It also feels like the migration part should have been a helper function called unmap_and_move_thp() in migrate.c instead of being buried in mm/huge_memory.c

Yeah. Peter is looking at this right now and trying to untangle it a bit more. You might want to wait for that to be complete before reviewing the end result.

Thanks,

Ingo

-------------->

 CREDITS                                  |   1 +
 Documentation/scheduler/numa-problem.txt | 230 +++++++++++++
 arch/mips/include/asm/pgtable.h          |   2 +
 arch/s390/include/asm/pgtable.h          |  13 +
 arch/sh/mm/Kconfig                       |   1 +
 arch/x86/include/asm/pgtable.h           |   7 +
 arch/x86/mm/gup.c                        |  23 +-
 arch/x86/mm/pgtable.c                    |  17 +-
 drivers/base/node.c                      |   2 +-
 include/asm-generic/pgtable.h            |   4 +
 include/linux/huge_mm.h                  |  19 ++
 include/linux/init_task.h                |   8 +
 include/linux/mempolicy.h                |  11 +-
 include/linux/migrate.h                  |   7 +
 include/linux/migrate_mode.h             |   3 +
 include/linux/mm.h                       | 122 ++++---
 include/linux/mm_types.h                 |  10 +
 include/linux/mmzone.h                   |  15 +-
 include/linux/page-flags-layout.h        |  83 +++++
 include/linux/sched.h                    |  43 ++-
 include/uapi/linux/mempolicy.h           |  17 +-
 init/Kconfig                             |  14 +
 kernel/sched/core.c                      |  77 ++++-
 kernel/sched/debug.c                     |   3 +
 kernel/sched/fair.c                      | 566 +++++++++++++++++++++++++++++--
 kernel/sched/features.h                  |  14 +
 kernel/sched/sched.h                     |  36 ++
 kernel/sysctl.c                          |  34 +-
 mm/huge_memory.c                         | 254 +++++++++++---
 mm/memory.c                              | 158 ++++++++-
 mm/mempolicy.c                           | 273 +++++++++++----
 mm/migrate.c                             |  89 ++++-
 mm/mprotect.c                            |  31 +-
 mm/pgtable-generic.c                     |   3 +-
 mm/vmstat.c                              |   1 -
 35 files changed, 1929 insertions(+), 262 deletions(-)
CREDITS +1 -0
index d8fe12a..b4cdc8f 100644
@@ -125,6 +125,7 @@ D: Author of pscan that helps to fix lp/parport bugs
 D: Author of lil (Linux Interrupt Latency benchmark)
 D: Fixed the shm swap deallocation at swapoff time (try_to_unuse message)
 D: VM hacker
+D: NUMA task placement
 D: Various other kernel hacks
 S: Imola 40026
 S: Italy
Documentation/scheduler/numa-problem.txt new file +230 -0

230 lines not on this page · raw message

arch/mips/include/asm/pgtable.h +2 -0
index c02158b..bbe4cda 100644
@@ -89,6 +89,8 @@ static inline int is_zero_pfn(unsigned long pfn)
 
 extern void paging_init(void);
 
+#define pmd_pgprot(x)		__pgprot(pmd_val(x) & ~_PAGE_CHG_MASK)
+
 /*
  * Conversion functions: convert a page and protection to a page entry,
  * and a page entry and page directory to the page they refer to.
arch/s390/include/asm/pgtable.h +13 -0
index dd647c9..098fc5a 100644
@@ -1240,6 +1240,19 @@ static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr,
 	*pmdp = entry;
 }
 
+static inline pgprot_t pmd_pgprot(pmd_t pmd)
+{
+	pgprot_t prot = PAGE_RW;
+
+	if (pmd_val(pmd) & _SEGMENT_ENTRY_RO) {
+		if (pmd_val(pmd) & _SEGMENT_ENTRY_INV)
+			prot = PAGE_NONE;
+		else
+			prot = PAGE_RO;
+	}
+	return prot;
+}
+
 static inline unsigned long massage_pgprot_pmd(pgprot_t pgprot)
 {
 	unsigned long pgprot_pmd = 0;
arch/sh/mm/Kconfig +1 -0
index cb8f992..1210cc7 100644
@@ -111,6 +111,7 @@ config VSYSCALL
 config NUMA
 	bool "Non Uniform Memory Access (NUMA) Support"
 	depends on MMU && SYS_SUPPORTS_NUMA && EXPERIMENTAL
+	select EMBEDDED_NUMA
 	default n
 	help
 	  Some SH systems have many various memories scattered around
arch/x86/include/asm/pgtable.h +7 -0
index a1f780d..ecf64b4 100644
@@ -349,6 +349,7 @@ static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
 }
 
 #define pte_pgprot(x) __pgprot(pte_flags(x) & PTE_FLAGS_MASK)
+#define pmd_pgprot(x) __pgprot(pmd_val(x) & ~_HPAGE_CHG_MASK)
 
 #define canon_pgprot(p) __pgprot(massage_pgprot(p))
 
@@ -407,6 +408,12 @@ static inline int pte_present(pte_t a)
 	return pte_flags(a) & (_PAGE_PRESENT | _PAGE_PROTNONE);
 }
 
+#define __HAVE_ARCH_PTE_ACCESSIBLE
+static inline int pte_accessible(pte_t a)
+{
+	return pte_flags(a) & _PAGE_PRESENT;
+}
+
 static inline int pte_hidden(pte_t pte)
 {
 	return pte_flags(pte) & _PAGE_HIDDEN;
arch/x86/mm/gup.c +20 -3
index dd74e46..6dc9921 100644
@@ -150,7 +150,13 @@ static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
 
 	pmdp = pmd_offset(&pud, addr);
 	do {
-		pmd_t pmd = *pmdp;
+		/*
+		 * With THP and hugetlbfs the pmd can change from
+		 * under us and it can be cleared as well by the TLB
+		 * shootdown, so read it with ACCESS_ONCE to do all
+		 * computations on the same sampling.
+		 */
+		pmd_t pmd = ACCESS_ONCE(*pmdp);
 
 		next = pmd_addr_end(addr, end);
 		/*
@@ -220,7 +226,13 @@ static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
 
 	pudp = pud_offset(&pgd, addr);
 	do {
-		pud_t pud = *pudp;
+		/*
+		 * With hugetlbfs giga pages the pud can change from
+		 * under us and it can be cleared as well by the TLB
+		 * shootdown, so read it with ACCESS_ONCE to do all
+		 * computations on the same sampling.
+		 */
+		pud_t pud = ACCESS_ONCE(*pudp);
 
 		next = pud_addr_end(addr, end);
 		if (pud_none(pud))
@@ -280,7 +292,12 @@ int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
 	local_irq_save(flags);
 	pgdp = pgd_offset(mm, addr);
 	do {
-		pgd_t pgd = *pgdp;
+		/*
+		 * The pgd could be cleared by the TLB shootdown from
+		 * under us so read it with ACCESS_ONCE to do all
+		 * computations on the same sampling.
+		 */
+		pgd_t pgd = ACCESS_ONCE(*pgdp);
 
 		next = pgd_addr_end(addr, end);
 		if (pgd_none(pgd))
arch/x86/mm/pgtable.c +16 -1
index 8573b83..fc82cf1 100644
@@ -306,11 +306,26 @@ int ptep_set_access_flags(struct vm_area_struct *vma,
 			  pte_t entry, int dirty)
 {
 	int changed = !pte_same(*ptep, entry);
+	/*
+	 * If the page used to be inaccessible (_PAGE_PROTNONE), or
+	 * this call upgrades the access permissions on the same page,
+	 * it is safe to skip the remote TLB flush.
+	 */
+	bool flush_remote = false;
+	if (!pte_accessible(*ptep))
+		flush_remote = false;
+	else if (pte_pfn(*ptep) != pte_pfn(entry) ||
+			(pte_write(*ptep) && !pte_write(entry)) ||
+			(pte_exec(*ptep) && !pte_exec(entry)))
+		flush_remote = true;
 
 	if (changed && dirty) {
 		*ptep = entry;
 		pte_update_defer(vma->vm_mm, address, ptep);
-		flush_tlb_page(vma, address);
+		if (flush_remote)
+			flush_tlb_page(vma, address);
+		else
+			__flush_tlb_one(address);
 	}
 
 	return changed;
drivers/base/node.c +1 -1
index af1a177..ef51157 100644
@@ -169,7 +169,7 @@ static ssize_t node_read_numastat(struct device *dev,
 		       node_page_state(dev->id, NUMA_HIT),
 		       node_page_state(dev->id, NUMA_MISS),
 		       node_page_state(dev->id, NUMA_FOREIGN),
-		       node_page_state(dev->id, NUMA_INTERLEAVE_HIT),
+		       0UL,
 		       node_page_state(dev->id, NUMA_LOCAL),
 		       node_page_state(dev->id, NUMA_OTHER));
 }
include/asm-generic/pgtable.h +4 -0
index b36ce40..8ce9d4e 100644
@@ -219,6 +219,10 @@ static inline int pmd_same(pmd_t pmd_a, pmd_t pmd_b)
 #define move_pte(pte, prot, old_addr, new_addr)	(pte)
 #endif
 
+#ifndef __HAVE_ARCH_PTE_ACCESSIBLE
+#define pte_accessible(pte)		pte_present(pte)
+#endif
+
 #ifndef flush_tlb_fix_spurious_fault
 #define flush_tlb_fix_spurious_fault(vma, address) flush_tlb_page(vma, address)
 #endif
include/linux/huge_mm.h +19 -0
index b31cb7d..4f0f948 100644
@@ -159,6 +159,13 @@ static inline struct page *compound_trans_head(struct page *page)
 	}
 	return page;
 }
+
+extern bool pmd_numa(struct vm_area_struct *vma, pmd_t pmd);
+
+extern void do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
+				  unsigned long address, pmd_t *pmd,
+				  unsigned int flags, pmd_t orig_pmd);
+
 #else /* CONFIG_TRANSPARENT_HUGEPAGE */
 #define HPAGE_PMD_SHIFT ({ BUILD_BUG(); 0; })
 #define HPAGE_PMD_MASK ({ BUILD_BUG(); 0; })
@@ -195,6 +202,18 @@ static inline int pmd_trans_huge_lock(pmd_t *pmd,
 {
 	return 0;
 }
+
+static inline bool pmd_numa(struct vm_area_struct *vma, pmd_t pmd)
+{
+	return false;
+}
+
+static inline void do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
+				  unsigned long address, pmd_t *pmd,
+				  unsigned int flags, pmd_t orig_pmd)
+{
+}
+
 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
 
 #endif /* _LINUX_HUGE_MM_H */
include/linux/init_task.h +8 -0
index 6d087c5..b4405b6 100644
@@ -143,6 +143,13 @@ extern struct task_group root_task_group;
 
 #define INIT_TASK_COMM "swapper"
 
+#ifdef CONFIG_SCHED_NUMA
+# define INIT_TASK_NUMA(tsk)						\
+	.node = -1,
+#else
+# define INIT_TASK_NUMA(tsk)
+#endif
+
 /*
  *  INIT_TASK is used to set up the first task table, touch at
  * your own risk!. Base=0, limit=0x1fffff (=2MB)
@@ -210,6 +217,7 @@ extern struct task_group root_task_group;
 	INIT_TRACE_RECURSION						\
 	INIT_TASK_RCU_PREEMPT(tsk)					\
 	INIT_CPUSET_SEQ							\
+	INIT_TASK_NUMA(tsk)						\
 }
 
 
include/linux/mempolicy.h +9 -2
index e5ccb9d..d6b1ea1 100644
@@ -5,7 +5,6 @@
 #ifndef _LINUX_MEMPOLICY_H
 #define _LINUX_MEMPOLICY_H 1
 
-
 #include <linux/mmzone.h>
 #include <linux/slab.h>
 #include <linux/rbtree.h>
@@ -198,7 +197,9 @@ static inline int vma_migratable(struct vm_area_struct *vma)
 	return 1;
 }
 
-#else
+extern int mpol_misplaced(struct page *, struct vm_area_struct *, unsigned long);
+
+#else /* CONFIG_NUMA */
 
 struct mempolicy {};
 
@@ -323,5 +324,11 @@ static inline int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol,
 	return 0;
 }
 
+static inline int mpol_misplaced(struct page *page, struct vm_area_struct *vma,
+				 unsigned long address)
+{
+	return -1; /* no node preference */
+}
+
 #endif /* CONFIG_NUMA */
 #endif
include/linux/migrate.h +7 -0
index ce7e667..9a5afea 100644
@@ -30,6 +30,7 @@ extern int migrate_vmas(struct mm_struct *mm,
 extern void migrate_page_copy(struct page *newpage, struct page *page);
 extern int migrate_huge_page_move_mapping(struct address_space *mapping,
 				  struct page *newpage, struct page *page);
+extern int migrate_misplaced_page(struct page *page, int node);
 #else
 
 static inline void putback_lru_pages(struct list_head *l) {}
@@ -63,5 +64,11 @@ static inline int migrate_huge_page_move_mapping(struct address_space *mapping,
 #define migrate_page NULL
 #define fail_migrate_page NULL
 
+static inline
+int migrate_misplaced_page(struct page *page, int node)
+{
+	return -EAGAIN; /* can't migrate now */
+}
 #endif /* CONFIG_MIGRATION */
+
 #endif /* _LINUX_MIGRATE_H */
include/linux/migrate_mode.h +3 -0
index ebf3d89..40b37dc 100644
@@ -6,11 +6,14 @@
  *	on most operations but not ->writepage as the potential stall time
  *	is too significant
  * MIGRATE_SYNC will block when migrating pages
+ * MIGRATE_FAULT called from the fault path to migrate-on-fault for mempolicy
+ *	this path has an extra reference count
  */
 enum migrate_mode {
 	MIGRATE_ASYNC,
 	MIGRATE_SYNC_LIGHT,
 	MIGRATE_SYNC,
+	MIGRATE_FAULT,
 };
 
 #endif		/* MIGRATE_MODE_H_INCLUDED */
include/linux/mm.h +81 -41

173 lines not on this page · raw message

include/linux/mm_types.h +10 -0
index 31f8a3a..01c1d04 100644
@@ -12,6 +12,7 @@
 #include <linux/cpumask.h>
 #include <linux/page-debug-flags.h>
 #include <linux/uprobes.h>
+#include <linux/page-flags-layout.h>
 #include <asm/page.h>
 #include <asm/mmu.h>
 
@@ -175,6 +176,10 @@ struct page {
 	 */
 	void *shadow;
 #endif
+
+#ifdef LAST_NID_NOT_IN_PAGE_FLAGS
+	int _last_nid;
+#endif
 }
 /*
  * The struct page can be forced to be double word aligned so that atomic ops
@@ -398,6 +403,11 @@ struct mm_struct {
 #ifdef CONFIG_CPUMASK_OFFSTACK
 	struct cpumask cpumask_allocation;
 #endif
+#ifdef CONFIG_SCHED_NUMA
+	unsigned long numa_next_scan;
+	unsigned long numa_scan_offset;
+	int numa_scan_seq;
+#endif
 	struct uprobes_state uprobes_state;
 };
 
include/linux/mmzone.h +1 -14
index 50aaca8..c9fcd8f 100644
@@ -15,7 +15,7 @@
 #include <linux/seqlock.h>
 #include <linux/nodemask.h>
 #include <linux/pageblock-flags.h>
-#include <generated/bounds.h>
+#include <linux/page-flags-layout.h>
 #include <linux/atomic.h>
 #include <asm/page.h>
 
@@ -137,7 +137,6 @@ enum zone_stat_item {
 	NUMA_HIT,		/* allocated in intended node */
 	NUMA_MISS,		/* allocated in non intended node */
 	NUMA_FOREIGN,		/* was intended here, hit elsewhere */
-	NUMA_INTERLEAVE_HIT,	/* interleaver preferred this zone */
 	NUMA_LOCAL,		/* allocation from local node */
 	NUMA_OTHER,		/* allocation from other node */
 #endif
@@ -318,16 +317,6 @@ enum zone_type {
  * match the requested limits. See gfp_zone() in include/linux/gfp.h
  */
 
-#if MAX_NR_ZONES < 2
-#define ZONES_SHIFT 0
-#elif MAX_NR_ZONES <= 2
-#define ZONES_SHIFT 1
-#elif MAX_NR_ZONES <= 4
-#define ZONES_SHIFT 2
-#else
-#error ZONES_SHIFT -- too many zones configured adjust calculation
-#endif
-
 struct zone {
 	/* Fields commonly accessed by the page allocator */
 
@@ -1030,8 +1019,6 @@ static inline unsigned long early_pfn_to_nid(unsigned long pfn)
  * PA_SECTION_SHIFT		physical address to/from section number
  * PFN_SECTION_SHIFT		pfn to/from section number
  */
-#define SECTIONS_SHIFT		(MAX_PHYSMEM_BITS - SECTION_SIZE_BITS)
-
 #define PA_SECTION_SHIFT	(SECTION_SIZE_BITS)
 #define PFN_SECTION_SHIFT	(SECTION_SIZE_BITS - PAGE_SHIFT)
 
include/linux/page-flags-layout.h new file +83 -0

83 lines not on this page · raw message

include/linux/sched.h +39 -4

87 lines not on this page · raw message

include/uapi/linux/mempolicy.h +14 -3
index 23e62e0..a2bcd04 100644
@@ -20,6 +20,8 @@ enum {
 	MPOL_PREFERRED,
 	MPOL_BIND,
 	MPOL_INTERLEAVE,
+	MPOL_LOCAL,
+	MPOL_NOOP,		/* retain existing policy for range */
 	MPOL_MAX,	/* always last member of enum */
 };
 
@@ -47,9 +49,16 @@ enum mpol_rebind_step {
 
 /* Flags for mbind */
 #define MPOL_MF_STRICT	(1<<0)	/* Verify existing pages in the mapping */
-#define MPOL_MF_MOVE	(1<<1)	/* Move pages owned by this process to conform to mapping */
-#define MPOL_MF_MOVE_ALL (1<<2)	/* Move every page to conform to mapping */
-#define MPOL_MF_INTERNAL (1<<3)	/* Internal flags start here */
+#define MPOL_MF_MOVE	 (1<<1)	/* Move pages owned by this process to conform
+				   to policy */
+#define MPOL_MF_MOVE_ALL (1<<2)	/* Move every page to conform to policy */
+#define MPOL_MF_LAZY	 (1<<3)	/* Modifies '_MOVE:  lazy migrate on fault */
+#define MPOL_MF_INTERNAL (1<<4)	/* Internal flags start here */
+
+#define MPOL_MF_VALID	(MPOL_MF_STRICT   | 	\
+			 MPOL_MF_MOVE     | 	\
+			 MPOL_MF_MOVE_ALL |	\
+			 MPOL_MF_LAZY)
 
 /*
  * Internal flags that share the struct mempolicy flags word with
@@ -59,6 +68,8 @@ enum mpol_rebind_step {
 #define MPOL_F_SHARED  (1 << 0)	/* identify shared policies */
 #define MPOL_F_LOCAL   (1 << 1)	/* preferred local allocation */
 #define MPOL_F_REBINDING (1 << 2)	/* identify policies in rebinding */
+#define MPOL_F_MOF	(1 << 3) /* this policy wants migrate on fault */
+#define MPOL_F_HOME	(1 << 4) /* this is the home-node policy */
 
 
 #endif /* _UAPI_LINUX_MEMPOLICY_H */
init/Kconfig +14 -0
index 6fdd6e3..c8d1f59 100644
@@ -696,6 +696,20 @@ config LOG_BUF_SHIFT
 config HAVE_UNSTABLE_SCHED_CLOCK
 	bool
 
+#
+# For architectures that (ab)use NUMA to represent different memory regions
+# all cpu-local but of different latencies, such as SuperH.
+#
+config EMBEDDED_NUMA
+	bool
+
+config SCHED_NUMA
+	bool "Memory placement aware NUMA scheduler"
+	default n
+	depends on SMP && NUMA && MIGRATION && !EMBEDDED_NUMA
+	help
+	  This option adds support for automatic NUMA aware memory/task placement.
+
 menuconfig CGROUPS
 	boolean "Control Group support"
 	depends on EVENTFD
kernel/sched/core.c +75 -2

125 lines not on this page · raw message

kernel/sched/debug.c +3 -0
index 6f79596..c9a5f75 100644
@@ -132,6 +132,9 @@ print_task(struct seq_file *m, struct rq *rq, struct task_struct *p)
 	SEQ_printf(m, "%15Ld %15Ld %15Ld.%06ld %15Ld.%06ld %15Ld.%06ld",
 		0LL, 0LL, 0LL, 0L, 0LL, 0L, 0LL, 0L);
 #endif
+#ifdef CONFIG_SCHED_NUMA
+	SEQ_printf(m, " %d/%d", p->node, cpu_to_node(task_cpu(p)));
+#endif
 #ifdef CONFIG_CGROUP_SCHED
 	SEQ_printf(m, " %s", task_group_path(task_group(p)));
 #endif
kernel/sched/fair.c +538 -28

790 lines not on this page · raw message

kernel/sched/features.h +14 -0
index eebefca..1d11f6c 100644
@@ -61,3 +61,17 @@ SCHED_FEAT(TTWU_QUEUE, true)
 SCHED_FEAT(FORCE_SD_OVERLAP, false)
 SCHED_FEAT(RT_RUNTIME_SHARE, true)
 SCHED_FEAT(LB_MIN, false)
+
+#ifdef CONFIG_SCHED_NUMA
+/* Do the working set probing faults: */
+SCHED_FEAT(NUMA,           true)
+/* Do actual migration/placement based on the working set information: */
+SCHED_FEAT(NUMA_MIGRATION, true)
+SCHED_FEAT(NUMA_HOT,       true)
+SCHED_FEAT(NUMA_TTWU_BIAS, false)
+SCHED_FEAT(NUMA_TTWU_TO,   false)
+SCHED_FEAT(NUMA_PULL,      true)
+SCHED_FEAT(NUMA_PULL_BIAS, true)
+SCHED_FEAT(NUMA_SETTLE,    true)
+#endif
+
kernel/sched/sched.h +36 -0
index 7a7db09..e68cef1 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"
 
@@ -418,6 +419,13 @@ struct rq {
 
 	struct list_head cfs_tasks;
 
+#ifdef CONFIG_SCHED_NUMA
+	unsigned long    onnode_running;
+	unsigned long    offnode_running;
+	unsigned long	 offnode_weight;
+	struct list_head offnode_tasks;
+#endif
+
 	u64 rt_avg;
 	u64 age_stamp;
 	u64 idle_stamp;
@@ -486,6 +494,27 @@ DECLARE_PER_CPU(struct rq, runqueues);
 #define cpu_curr(cpu)		(cpu_rq(cpu)->curr)
 #define raw_rq()		(&__raw_get_cpu_var(runqueues))
 
+#ifdef CONFIG_SCHED_NUMA
+static inline struct list_head *offnode_tasks(struct rq *rq)
+{
+	return &rq->offnode_tasks;
+}
+
+static inline void task_numa_free(struct task_struct *p)
+{
+	kfree(p->numa_faults);
+}
+#else /* CONFIG_SCHED_NUMA */
+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
 
 #define rcu_dereference_check_sched_domain(p) \
@@ -529,6 +558,7 @@ static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
 
 DECLARE_PER_CPU(struct sched_domain *, sd_llc);
 DECLARE_PER_CPU(int, sd_llc_id);
+DECLARE_PER_CPU(struct sched_domain *, sd_node);
 
 extern int group_balance_cpu(struct sched_group *sg);
 
@@ -648,6 +678,12 @@ extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
 #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
 #endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */
 
+#ifdef CONFIG_SCHED_NUMA
+#define sched_feat_numa(x) sched_feat(x)
+#else
+#define sched_feat_numa(x) (0)
+#endif
+
 static inline u64 global_rt_period(void)
 {
 	return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
kernel/sysctl.c +33 -1
index 26f65ea..0f0cb60 100644
@@ -301,6 +301,7 @@ static struct ctl_table kern_table[] = {
 		.extra1		= &min_wakeup_granularity_ns,
 		.extra2		= &max_wakeup_granularity_ns,
 	},
+#ifdef CONFIG_SMP
 	{
 		.procname	= "sched_tunable_scaling",
 		.data		= &sysctl_sched_tunable_scaling,
@@ -347,7 +348,38 @@ static struct ctl_table kern_table[] = {
 		.extra1		= &zero,
 		.extra2		= &one,
 	},
-#endif
+#endif /* CONFIG_SMP */
+#ifdef CONFIG_SCHED_NUMA
+	{
+		.procname	= "sched_numa_task_period_min_ms",
+		.data		= &sysctl_sched_numa_task_period_min,
+		.maxlen		= sizeof(unsigned int),
+		.mode		= 0644,
+		.proc_handler	= proc_dointvec,
+	},
+	{
+		.procname	= "sched_numa_task_period_max_ms",
+		.data		= &sysctl_sched_numa_task_period_max,
+		.maxlen		= sizeof(unsigned int),
+		.mode		= 0644,
+		.proc_handler	= proc_dointvec,
+	},
+	{
+		.procname	= "sched_numa_scan_size_mb",
+		.data		= &sysctl_sched_numa_scan_size,
+		.maxlen		= sizeof(unsigned int),
+		.mode		= 0644,
+		.proc_handler	= proc_dointvec,
+	},
+	{
+		.procname	= "sched_numa_settle_count",
+		.data		= &sysctl_sched_numa_settle_count,
+		.maxlen		= sizeof(unsigned int),
+		.mode		= 0644,
+		.proc_handler	= proc_dointvec,
+	},
+#endif /* CONFIG_SCHED_NUMA */
+#endif /* CONFIG_SCHED_DEBUG */
 	{
 		.procname	= "sched_rt_period_us",
 		.data		= &sysctl_sched_rt_period,
mm/huge_memory.c +199 -55

297 lines not on this page · raw message

mm/memory.c +149 -9

241 lines not on this page · raw message

mm/mempolicy.c +200 -73

426 lines not on this page · raw message

mm/migrate.c +80 -9

31 lines not on this page · raw message

mm/mprotect.c +17 -14
index a409926..392b124 100644

Lines of this file are not shown. View the raw message.

mm/pgtable-generic.c +2 -1
index e642627..c1283dd 100644

Lines of this file are not shown. View the raw message.

mm/vmstat.c +0 -1
index c737057..8fafa32 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Part of the diff is not shown here. View the raw message.