Skip to content

Commit b795854

Browse files
Mel Gormaningomolnar
authored andcommitted
sched/numa: Set preferred NUMA node based on number of private faults
Ideally it would be possible to distinguish between NUMA hinting faults that are private to a task and those that are shared. If treated identically there is a risk that shared pages bounce between nodes depending on the order they are referenced by tasks. Ultimately what is desirable is that task private pages remain local to the task while shared pages are interleaved between sharing tasks running on different nodes to give good average performance. This is further complicated by THP as even applications that partition their data may not be partitioning on a huge page boundary. To start with, this patch assumes that multi-threaded or multi-process applications partition their data and that in general the private accesses are more important for cpu->memory locality in the general case. Also, no new infrastructure is required to treat private pages properly but interleaving for shared pages requires additional infrastructure. To detect private accesses the pid of the last accessing task is required but the storage requirements are a high. This patch borrows heavily from Ingo Molnar's patch "numa, mm, sched: Implement last-CPU+PID hash tracking" to encode some bits from the last accessing task in the page flags as well as the node information. Collisions will occur but it is better than just depending on the node information. Node information is then used to determine if a page needs to migrate. The PID information is used to detect private/shared accesses. The preferred NUMA node is selected based on where the maximum number of approximately private faults were measured. Shared faults are not taken into consideration for a few reasons. First, if there are many tasks sharing the page then they'll all move towards the same node. The node will be compute overloaded and then scheduled away later only to bounce back again. Alternatively the shared tasks would just bounce around nodes because the fault information is effectively noise. Either way accounting for shared faults the same as private faults can result in lower performance overall. The second reason is based on a hypothetical workload that has a small number of very important, heavily accessed private pages but a large shared array. The shared array would dominate the number of faults and be selected as a preferred node even though it's the wrong decision. The third reason is that multiple threads in a process will race each other to fault the shared page making the fault information unreliable. Signed-off-by: Mel Gorman <mgorman@suse.de> [ Fix complication error when !NUMA_BALANCING. ] Reviewed-by: Rik van Riel <riel@redhat.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Srikar Dronamraju <srikar@linux.vnet.ibm.com> Signed-off-by: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/1381141781-10992-30-git-send-email-mgorman@suse.de Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 073b5be commit b795854

12 files changed

Lines changed: 149 additions & 82 deletions

File tree

‎include/linux/mm.h‎

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -581,11 +581,11 @@ static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
581581
* sets it, so none of the operations on it need to be atomic.
582582
*/
583583

584-
/* Page flags: | [SECTION] | [NODE] | ZONE | [LAST_NID] | ... | FLAGS | */
584+
/* Page flags: | [SECTION] | [NODE] | ZONE | [LAST_NIDPID] | ... | FLAGS | */
585585
#define SECTIONS_PGOFF ((sizeof(unsigned long)*8) - SECTIONS_WIDTH)
586586
#define NODES_PGOFF (SECTIONS_PGOFF - NODES_WIDTH)
587587
#define ZONES_PGOFF (NODES_PGOFF - ZONES_WIDTH)
588-
#define LAST_NID_PGOFF (ZONES_PGOFF - LAST_NID_WIDTH)
588+
#define LAST_NIDPID_PGOFF (ZONES_PGOFF - LAST_NIDPID_WIDTH)
589589

590590
/*
591591
* Define the bit shifts to access each section. For non-existent
@@ -595,7 +595,7 @@ static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
595595
#define SECTIONS_PGSHIFT (SECTIONS_PGOFF * (SECTIONS_WIDTH != 0))
596596
#define NODES_PGSHIFT (NODES_PGOFF * (NODES_WIDTH != 0))
597597
#define ZONES_PGSHIFT (ZONES_PGOFF * (ZONES_WIDTH != 0))
598-
#define LAST_NID_PGSHIFT (LAST_NID_PGOFF * (LAST_NID_WIDTH != 0))
598+
#define LAST_NIDPID_PGSHIFT (LAST_NIDPID_PGOFF * (LAST_NIDPID_WIDTH != 0))
599599

600600
/* NODE:ZONE or SECTION:ZONE is used to ID a zone for the buddy allocator */
601601
#ifdef NODE_NOT_IN_PAGE_FLAGS
@@ -617,7 +617,7 @@ static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
617617
#define ZONES_MASK ((1UL << ZONES_WIDTH) - 1)
618618
#define NODES_MASK ((1UL << NODES_WIDTH) - 1)
619619
#define SECTIONS_MASK ((1UL << SECTIONS_WIDTH) - 1)
620-
#define LAST_NID_MASK ((1UL << LAST_NID_WIDTH) - 1)
620+
#define LAST_NIDPID_MASK ((1UL << LAST_NIDPID_WIDTH) - 1)
621621
#define ZONEID_MASK ((1UL << ZONEID_SHIFT) - 1)
622622

623623
static inline enum zone_type page_zonenum(const struct page *page)
@@ -661,48 +661,93 @@ static inline int page_to_nid(const struct page *page)
661661
#endif
662662

663663
#ifdef CONFIG_NUMA_BALANCING
664-
#ifdef LAST_NID_NOT_IN_PAGE_FLAGS
665-
static inline int page_nid_xchg_last(struct page *page, int nid)
664+
static inline int nid_pid_to_nidpid(int nid, int pid)
666665
{
667-
return xchg(&page->_last_nid, nid);
666+
return ((nid & LAST__NID_MASK) << LAST__PID_SHIFT) | (pid & LAST__PID_MASK);
668667
}
669668

670-
static inline int page_nid_last(struct page *page)
669+
static inline int nidpid_to_pid(int nidpid)
671670
{
672-
return page->_last_nid;
671+
return nidpid & LAST__PID_MASK;
673672
}
674-
static inline void page_nid_reset_last(struct page *page)
673+
674+
static inline int nidpid_to_nid(int nidpid)
675+
{
676+
return (nidpid >> LAST__PID_SHIFT) & LAST__NID_MASK;
677+
}
678+
679+
static inline bool nidpid_pid_unset(int nidpid)
680+
{
681+
return nidpid_to_pid(nidpid) == (-1 & LAST__PID_MASK);
682+
}
683+
684+
static inline bool nidpid_nid_unset(int nidpid)
675685
{
676-
page->_last_nid = -1;
686+
return nidpid_to_nid(nidpid) == (-1 & LAST__NID_MASK);
687+
}
688+
689+
#ifdef LAST_NIDPID_NOT_IN_PAGE_FLAGS
690+
static inline int page_nidpid_xchg_last(struct page *page, int nid)
691+
{
692+
return xchg(&page->_last_nidpid, nid);
693+
}
694+
695+
static inline int page_nidpid_last(struct page *page)
696+
{
697+
return page->_last_nidpid;
698+
}
699+
static inline void page_nidpid_reset_last(struct page *page)
700+
{
701+
page->_last_nidpid = -1;
677702
}
678703
#else
679-
static inline int page_nid_last(struct page *page)
704+
static inline int page_nidpid_last(struct page *page)
680705
{
681-
return (page->flags >> LAST_NID_PGSHIFT) & LAST_NID_MASK;
706+
return (page->flags >> LAST_NIDPID_PGSHIFT) & LAST_NIDPID_MASK;
682707
}
683708

684-
extern int page_nid_xchg_last(struct page *page, int nid);
709+
extern int page_nidpid_xchg_last(struct page *page, int nidpid);
685710

686-
static inline void page_nid_reset_last(struct page *page)
711+
static inline void page_nidpid_reset_last(struct page *page)
687712
{
688-
int nid = (1 << LAST_NID_SHIFT) - 1;
713+
int nidpid = (1 << LAST_NIDPID_SHIFT) - 1;
689714

690-
page->flags &= ~(LAST_NID_MASK << LAST_NID_PGSHIFT);
691-
page->flags |= (nid & LAST_NID_MASK) << LAST_NID_PGSHIFT;
715+
page->flags &= ~(LAST_NIDPID_MASK << LAST_NIDPID_PGSHIFT);
716+
page->flags |= (nidpid & LAST_NIDPID_MASK) << LAST_NIDPID_PGSHIFT;
692717
}
693-
#endif /* LAST_NID_NOT_IN_PAGE_FLAGS */
718+
#endif /* LAST_NIDPID_NOT_IN_PAGE_FLAGS */
694719
#else
695-
static inline int page_nid_xchg_last(struct page *page, int nid)
720+
static inline int page_nidpid_xchg_last(struct page *page, int nidpid)
696721
{
697722
return page_to_nid(page);
698723
}
699724

700-
static inline int page_nid_last(struct page *page)
725+
static inline int page_nidpid_last(struct page *page)
701726
{
702727
return page_to_nid(page);
703728
}
704729

705-
static inline void page_nid_reset_last(struct page *page)
730+
static inline int nidpid_to_nid(int nidpid)
731+
{
732+
return -1;
733+
}
734+
735+
static inline int nidpid_to_pid(int nidpid)
736+
{
737+
return -1;
738+
}
739+
740+
static inline int nid_pid_to_nidpid(int nid, int pid)
741+
{
742+
return -1;
743+
}
744+
745+
static inline bool nidpid_pid_unset(int nidpid)
746+
{
747+
return 1;
748+
}
749+
750+
static inline void page_nidpid_reset_last(struct page *page)
706751
{
707752
}
708753
#endif

‎include/linux/mm_types.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ struct page {
174174
void *shadow;
175175
#endif
176176

177-
#ifdef LAST_NID_NOT_IN_PAGE_FLAGS
178-
int _last_nid;
177+
#ifdef LAST_NIDPID_NOT_IN_PAGE_FLAGS
178+
int _last_nidpid;
179179
#endif
180180
}
181181
/*

‎include/linux/page-flags-layout.h‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
* The last is when there is insufficient space in page->flags and a separate
3939
* lookup is necessary.
4040
*
41-
* No sparsemem or sparsemem vmemmap: | NODE | ZONE | ... | FLAGS |
42-
* " plus space for last_nid: | NODE | ZONE | LAST_NID ... | FLAGS |
43-
* classic sparse with space for node:| SECTION | NODE | ZONE | ... | FLAGS |
44-
* " plus space for last_nid: | SECTION | NODE | ZONE | LAST_NID ... | FLAGS |
41+
* No sparsemem or sparsemem vmemmap: | NODE | ZONE | ... | FLAGS |
42+
* " plus space for last_nidpid: | NODE | ZONE | LAST_NIDPID ... | FLAGS |
43+
* classic sparse with space for node:| SECTION | NODE | ZONE | ... | FLAGS |
44+
* " plus space for last_nidpid: | SECTION | NODE | ZONE | LAST_NIDPID ... | FLAGS |
4545
* classic sparse no space for node: | SECTION | ZONE | ... | FLAGS |
4646
*/
4747
#if defined(CONFIG_SPARSEMEM) && !defined(CONFIG_SPARSEMEM_VMEMMAP)
@@ -62,15 +62,21 @@
6262
#endif
6363

6464
#ifdef CONFIG_NUMA_BALANCING
65-
#define LAST_NID_SHIFT NODES_SHIFT
65+
#define LAST__PID_SHIFT 8
66+
#define LAST__PID_MASK ((1 << LAST__PID_SHIFT)-1)
67+
68+
#define LAST__NID_SHIFT NODES_SHIFT
69+
#define LAST__NID_MASK ((1 << LAST__NID_SHIFT)-1)
70+
71+
#define LAST_NIDPID_SHIFT (LAST__PID_SHIFT+LAST__NID_SHIFT)
6672
#else
67-
#define LAST_NID_SHIFT 0
73+
#define LAST_NIDPID_SHIFT 0
6874
#endif
6975

70-
#if SECTIONS_WIDTH+ZONES_WIDTH+NODES_SHIFT+LAST_NID_SHIFT <= BITS_PER_LONG - NR_PAGEFLAGS
71-
#define LAST_NID_WIDTH LAST_NID_SHIFT
76+
#if SECTIONS_WIDTH+ZONES_WIDTH+NODES_SHIFT+LAST_NIDPID_SHIFT <= BITS_PER_LONG - NR_PAGEFLAGS
77+
#define LAST_NIDPID_WIDTH LAST_NIDPID_SHIFT
7278
#else
73-
#define LAST_NID_WIDTH 0
79+
#define LAST_NIDPID_WIDTH 0
7480
#endif
7581

7682
/*
@@ -81,8 +87,8 @@
8187
#define NODE_NOT_IN_PAGE_FLAGS
8288
#endif
8389

84-
#if defined(CONFIG_NUMA_BALANCING) && LAST_NID_WIDTH == 0
85-
#define LAST_NID_NOT_IN_PAGE_FLAGS
90+
#if defined(CONFIG_NUMA_BALANCING) && LAST_NIDPID_WIDTH == 0
91+
#define LAST_NIDPID_NOT_IN_PAGE_FLAGS
8692
#endif
8793

8894
#endif /* _LINUX_PAGE_FLAGS_LAYOUT */

‎kernel/sched/fair.c‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ static void task_numa_placement(struct task_struct *p)
988988
/*
989989
* Got a PROT_NONE fault for a page on @node.
990990
*/
991-
void task_numa_fault(int last_nid, int node, int pages, bool migrated)
991+
void task_numa_fault(int last_nidpid, int node, int pages, bool migrated)
992992
{
993993
struct task_struct *p = current;
994994
int priv;
@@ -1000,8 +1000,14 @@ void task_numa_fault(int last_nid, int node, int pages, bool migrated)
10001000
if (!p->mm)
10011001
return;
10021002

1003-
/* For now, do not attempt to detect private/shared accesses */
1004-
priv = 1;
1003+
/*
1004+
* First accesses are treated as private, otherwise consider accesses
1005+
* to be private if the accessing pid has not changed
1006+
*/
1007+
if (!nidpid_pid_unset(last_nidpid))
1008+
priv = ((p->pid & LAST__PID_MASK) == nidpid_to_pid(last_nidpid));
1009+
else
1010+
priv = 1;
10051011

10061012
/* Allocate buffer to track faults on a per-node basis */
10071013
if (unlikely(!p->numa_faults)) {

‎mm/huge_memory.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,7 @@ int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
12821282
struct page *page;
12831283
unsigned long haddr = addr & HPAGE_PMD_MASK;
12841284
int page_nid = -1, this_nid = numa_node_id();
1285-
int target_nid, last_nid = -1;
1285+
int target_nid, last_nidpid = -1;
12861286
bool page_locked;
12871287
bool migrated = false;
12881288

@@ -1293,7 +1293,7 @@ int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
12931293
page = pmd_page(pmd);
12941294
BUG_ON(is_huge_zero_page(page));
12951295
page_nid = page_to_nid(page);
1296-
last_nid = page_nid_last(page);
1296+
last_nidpid = page_nidpid_last(page);
12971297
count_vm_numa_event(NUMA_HINT_FAULTS);
12981298
if (page_nid == this_nid)
12991299
count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
@@ -1362,7 +1362,7 @@ int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
13621362
page_unlock_anon_vma_read(anon_vma);
13631363

13641364
if (page_nid != -1)
1365-
task_numa_fault(last_nid, page_nid, HPAGE_PMD_NR, migrated);
1365+
task_numa_fault(last_nidpid, page_nid, HPAGE_PMD_NR, migrated);
13661366

13671367
return 0;
13681368
}
@@ -1682,7 +1682,7 @@ static void __split_huge_page_refcount(struct page *page,
16821682
page_tail->mapping = page->mapping;
16831683

16841684
page_tail->index = page->index + i;
1685-
page_nid_xchg_last(page_tail, page_nid_last(page));
1685+
page_nidpid_xchg_last(page_tail, page_nidpid_last(page));
16861686

16871687
BUG_ON(!PageAnon(page_tail));
16881688
BUG_ON(!PageUptodate(page_tail));

‎mm/memory.c‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@
6969

7070
#include "internal.h"
7171

72-
#ifdef LAST_NID_NOT_IN_PAGE_FLAGS
73-
#warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_nid.
72+
#ifdef LAST_NIDPID_NOT_IN_PAGE_FLAGS
73+
#warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_nidpid.
7474
#endif
7575

7676
#ifndef CONFIG_NEED_MULTIPLE_NODES
@@ -3536,7 +3536,7 @@ int do_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
35363536
struct page *page = NULL;
35373537
spinlock_t *ptl;
35383538
int page_nid = -1;
3539-
int last_nid;
3539+
int last_nidpid;
35403540
int target_nid;
35413541
bool migrated = false;
35423542

@@ -3567,7 +3567,7 @@ int do_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
35673567
}
35683568
BUG_ON(is_zero_pfn(page_to_pfn(page)));
35693569

3570-
last_nid = page_nid_last(page);
3570+
last_nidpid = page_nidpid_last(page);
35713571
page_nid = page_to_nid(page);
35723572
target_nid = numa_migrate_prep(page, vma, addr, page_nid);
35733573
pte_unmap_unlock(ptep, ptl);
@@ -3583,7 +3583,7 @@ int do_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
35833583

35843584
out:
35853585
if (page_nid != -1)
3586-
task_numa_fault(last_nid, page_nid, 1, migrated);
3586+
task_numa_fault(last_nidpid, page_nid, 1, migrated);
35873587
return 0;
35883588
}
35893589

@@ -3598,7 +3598,7 @@ static int do_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
35983598
unsigned long offset;
35993599
spinlock_t *ptl;
36003600
bool numa = false;
3601-
int last_nid;
3601+
int last_nidpid;
36023602

36033603
spin_lock(&mm->page_table_lock);
36043604
pmd = *pmdp;
@@ -3643,7 +3643,7 @@ static int do_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
36433643
if (unlikely(!page))
36443644
continue;
36453645

3646-
last_nid = page_nid_last(page);
3646+
last_nidpid = page_nidpid_last(page);
36473647
page_nid = page_to_nid(page);
36483648
target_nid = numa_migrate_prep(page, vma, addr, page_nid);
36493649
pte_unmap_unlock(pte, ptl);
@@ -3656,7 +3656,7 @@ static int do_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
36563656
}
36573657

36583658
if (page_nid != -1)
3659-
task_numa_fault(last_nid, page_nid, 1, migrated);
3659+
task_numa_fault(last_nidpid, page_nid, 1, migrated);
36603660

36613661
pte = pte_offset_map_lock(mm, pmdp, addr, &ptl);
36623662
}

‎mm/mempolicy.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,9 +2348,11 @@ int mpol_misplaced(struct page *page, struct vm_area_struct *vma, unsigned long
23482348

23492349
/* Migrate the page towards the node whose CPU is referencing it */
23502350
if (pol->flags & MPOL_F_MORON) {
2351-
int last_nid;
2351+
int last_nidpid;
2352+
int this_nidpid;
23522353

23532354
polnid = numa_node_id();
2355+
this_nidpid = nid_pid_to_nidpid(polnid, current->pid);
23542356

23552357
/*
23562358
* Multi-stage node selection is used in conjunction
@@ -2373,8 +2375,8 @@ int mpol_misplaced(struct page *page, struct vm_area_struct *vma, unsigned long
23732375
* it less likely we act on an unlikely task<->page
23742376
* relation.
23752377
*/
2376-
last_nid = page_nid_xchg_last(page, polnid);
2377-
if (last_nid != polnid)
2378+
last_nidpid = page_nidpid_xchg_last(page, this_nidpid);
2379+
if (!nidpid_pid_unset(last_nidpid) && nidpid_to_nid(last_nidpid) != polnid)
23782380
goto out;
23792381
}
23802382

‎mm/migrate.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,7 +1498,7 @@ static struct page *alloc_misplaced_dst_page(struct page *page,
14981498
__GFP_NOWARN) &
14991499
~GFP_IOFS, 0);
15001500
if (newpage)
1501-
page_nid_xchg_last(newpage, page_nid_last(page));
1501+
page_nidpid_xchg_last(newpage, page_nidpid_last(page));
15021502

15031503
return newpage;
15041504
}
@@ -1675,7 +1675,7 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm,
16751675
if (!new_page)
16761676
goto out_fail;
16771677

1678-
page_nid_xchg_last(new_page, page_nid_last(page));
1678+
page_nidpid_xchg_last(new_page, page_nidpid_last(page));
16791679

16801680
isolated = numamigrate_isolate_page(pgdat, page);
16811681
if (!isolated) {

0 commit comments

Comments
 (0)