Skip to content

Commit

Permalink
sched/numa: Set preferred NUMA node based on number of private faults
Browse files Browse the repository at this point in the history
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 <[email protected]>
[ Fix complication error when !NUMA_BALANCING. ]
Reviewed-by: Rik van Riel <[email protected]>
Cc: Andrea Arcangeli <[email protected]>
Cc: Johannes Weiner <[email protected]>
Cc: Srikar Dronamraju <[email protected]>
Signed-off-by: Peter Zijlstra <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Ingo Molnar <[email protected]>
  • Loading branch information
Mel Gorman authored and Ingo Molnar committed Oct 9, 2013
1 parent 073b5be commit b795854
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 82 deletions.
89 changes: 67 additions & 22 deletions include/linux/mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -581,11 +581,11 @@ static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
* sets it, so none of the operations on it need to be atomic.
*/

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

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

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

static inline enum zone_type page_zonenum(const struct page *page)
Expand Down Expand Up @@ -661,48 +661,93 @@ static inline int page_to_nid(const struct page *page)
#endif

#ifdef CONFIG_NUMA_BALANCING
#ifdef LAST_NID_NOT_IN_PAGE_FLAGS
static inline int page_nid_xchg_last(struct page *page, int nid)
static inline int nid_pid_to_nidpid(int nid, int pid)
{
return xchg(&page->_last_nid, nid);
return ((nid & LAST__NID_MASK) << LAST__PID_SHIFT) | (pid & LAST__PID_MASK);
}

static inline int page_nid_last(struct page *page)
static inline int nidpid_to_pid(int nidpid)
{
return page->_last_nid;
return nidpid & LAST__PID_MASK;
}
static inline void page_nid_reset_last(struct page *page)

static inline int nidpid_to_nid(int nidpid)
{
return (nidpid >> LAST__PID_SHIFT) & LAST__NID_MASK;
}

static inline bool nidpid_pid_unset(int nidpid)
{
return nidpid_to_pid(nidpid) == (-1 & LAST__PID_MASK);
}

static inline bool nidpid_nid_unset(int nidpid)
{
page->_last_nid = -1;
return nidpid_to_nid(nidpid) == (-1 & LAST__NID_MASK);
}

#ifdef LAST_NIDPID_NOT_IN_PAGE_FLAGS
static inline int page_nidpid_xchg_last(struct page *page, int nid)
{
return xchg(&page->_last_nidpid, nid);
}

static inline int page_nidpid_last(struct page *page)
{
return page->_last_nidpid;
}
static inline void page_nidpid_reset_last(struct page *page)
{
page->_last_nidpid = -1;
}
#else
static inline int page_nid_last(struct page *page)
static inline int page_nidpid_last(struct page *page)
{
return (page->flags >> LAST_NID_PGSHIFT) & LAST_NID_MASK;
return (page->flags >> LAST_NIDPID_PGSHIFT) & LAST_NIDPID_MASK;
}

extern int page_nid_xchg_last(struct page *page, int nid);
extern int page_nidpid_xchg_last(struct page *page, int nidpid);

static inline void page_nid_reset_last(struct page *page)
static inline void page_nidpid_reset_last(struct page *page)
{
int nid = (1 << LAST_NID_SHIFT) - 1;
int nidpid = (1 << LAST_NIDPID_SHIFT) - 1;

page->flags &= ~(LAST_NID_MASK << LAST_NID_PGSHIFT);
page->flags |= (nid & LAST_NID_MASK) << LAST_NID_PGSHIFT;
page->flags &= ~(LAST_NIDPID_MASK << LAST_NIDPID_PGSHIFT);
page->flags |= (nidpid & LAST_NIDPID_MASK) << LAST_NIDPID_PGSHIFT;
}
#endif /* LAST_NID_NOT_IN_PAGE_FLAGS */
#endif /* LAST_NIDPID_NOT_IN_PAGE_FLAGS */
#else
static inline int page_nid_xchg_last(struct page *page, int nid)
static inline int page_nidpid_xchg_last(struct page *page, int nidpid)
{
return page_to_nid(page);
}

static inline int page_nid_last(struct page *page)
static inline int page_nidpid_last(struct page *page)
{
return page_to_nid(page);
}

static inline void page_nid_reset_last(struct page *page)
static inline int nidpid_to_nid(int nidpid)
{
return -1;
}

static inline int nidpid_to_pid(int nidpid)
{
return -1;
}

static inline int nid_pid_to_nidpid(int nid, int pid)
{
return -1;
}

static inline bool nidpid_pid_unset(int nidpid)
{
return 1;
}

static inline void page_nidpid_reset_last(struct page *page)
{
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions include/linux/mm_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ struct page {
void *shadow;
#endif

#ifdef LAST_NID_NOT_IN_PAGE_FLAGS
int _last_nid;
#ifdef LAST_NIDPID_NOT_IN_PAGE_FLAGS
int _last_nidpid;
#endif
}
/*
Expand Down
28 changes: 17 additions & 11 deletions include/linux/page-flags-layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
* The last is when there is insufficient space in page->flags and a separate
* lookup is necessary.
*
* No sparsemem or sparsemem vmemmap: | NODE | ZONE | ... | FLAGS |
* " plus space for last_nid: | NODE | ZONE | LAST_NID ... | FLAGS |
* classic sparse with space for node:| SECTION | NODE | ZONE | ... | FLAGS |
* " plus space for last_nid: | SECTION | NODE | ZONE | LAST_NID ... | FLAGS |
* No sparsemem or sparsemem vmemmap: | NODE | ZONE | ... | FLAGS |
* " plus space for last_nidpid: | NODE | ZONE | LAST_NIDPID ... | FLAGS |
* classic sparse with space for node:| SECTION | NODE | ZONE | ... | FLAGS |
* " plus space for last_nidpid: | SECTION | NODE | ZONE | LAST_NIDPID ... | FLAGS |
* classic sparse no space for node: | SECTION | ZONE | ... | FLAGS |
*/
#if defined(CONFIG_SPARSEMEM) && !defined(CONFIG_SPARSEMEM_VMEMMAP)
Expand All @@ -62,15 +62,21 @@
#endif

#ifdef CONFIG_NUMA_BALANCING
#define LAST_NID_SHIFT NODES_SHIFT
#define LAST__PID_SHIFT 8
#define LAST__PID_MASK ((1 << LAST__PID_SHIFT)-1)

#define LAST__NID_SHIFT NODES_SHIFT
#define LAST__NID_MASK ((1 << LAST__NID_SHIFT)-1)

#define LAST_NIDPID_SHIFT (LAST__PID_SHIFT+LAST__NID_SHIFT)
#else
#define LAST_NID_SHIFT 0
#define LAST_NIDPID_SHIFT 0
#endif

#if SECTIONS_WIDTH+ZONES_WIDTH+NODES_SHIFT+LAST_NID_SHIFT <= BITS_PER_LONG - NR_PAGEFLAGS
#define LAST_NID_WIDTH LAST_NID_SHIFT
#if SECTIONS_WIDTH+ZONES_WIDTH+NODES_SHIFT+LAST_NIDPID_SHIFT <= BITS_PER_LONG - NR_PAGEFLAGS
#define LAST_NIDPID_WIDTH LAST_NIDPID_SHIFT
#else
#define LAST_NID_WIDTH 0
#define LAST_NIDPID_WIDTH 0
#endif

/*
Expand All @@ -81,8 +87,8 @@
#define NODE_NOT_IN_PAGE_FLAGS
#endif

#if defined(CONFIG_NUMA_BALANCING) && LAST_NID_WIDTH == 0
#define LAST_NID_NOT_IN_PAGE_FLAGS
#if defined(CONFIG_NUMA_BALANCING) && LAST_NIDPID_WIDTH == 0
#define LAST_NIDPID_NOT_IN_PAGE_FLAGS
#endif

#endif /* _LINUX_PAGE_FLAGS_LAYOUT */
12 changes: 9 additions & 3 deletions kernel/sched/fair.c
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ static void task_numa_placement(struct task_struct *p)
/*
* Got a PROT_NONE fault for a page on @node.
*/
void task_numa_fault(int last_nid, int node, int pages, bool migrated)
void task_numa_fault(int last_nidpid, int node, int pages, bool migrated)
{
struct task_struct *p = current;
int priv;
Expand All @@ -1000,8 +1000,14 @@ void task_numa_fault(int last_nid, int node, int pages, bool migrated)
if (!p->mm)
return;

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

/* Allocate buffer to track faults on a per-node basis */
if (unlikely(!p->numa_faults)) {
Expand Down
8 changes: 4 additions & 4 deletions mm/huge_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
struct page *page;
unsigned long haddr = addr & HPAGE_PMD_MASK;
int page_nid = -1, this_nid = numa_node_id();
int target_nid, last_nid = -1;
int target_nid, last_nidpid = -1;
bool page_locked;
bool migrated = false;

Expand All @@ -1293,7 +1293,7 @@ int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
page = pmd_page(pmd);
BUG_ON(is_huge_zero_page(page));
page_nid = page_to_nid(page);
last_nid = page_nid_last(page);
last_nidpid = page_nidpid_last(page);
count_vm_numa_event(NUMA_HINT_FAULTS);
if (page_nid == this_nid)
count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Expand Down Expand Up @@ -1362,7 +1362,7 @@ int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
page_unlock_anon_vma_read(anon_vma);

if (page_nid != -1)
task_numa_fault(last_nid, page_nid, HPAGE_PMD_NR, migrated);
task_numa_fault(last_nidpid, page_nid, HPAGE_PMD_NR, migrated);

return 0;
}
Expand Down Expand Up @@ -1682,7 +1682,7 @@ static void __split_huge_page_refcount(struct page *page,
page_tail->mapping = page->mapping;

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

BUG_ON(!PageAnon(page_tail));
BUG_ON(!PageUptodate(page_tail));
Expand Down
16 changes: 8 additions & 8 deletions mm/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@

#include "internal.h"

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

#ifndef CONFIG_NEED_MULTIPLE_NODES
Expand Down Expand Up @@ -3536,7 +3536,7 @@ int do_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
struct page *page = NULL;
spinlock_t *ptl;
int page_nid = -1;
int last_nid;
int last_nidpid;
int target_nid;
bool migrated = false;

Expand Down Expand Up @@ -3567,7 +3567,7 @@ int do_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
}
BUG_ON(is_zero_pfn(page_to_pfn(page)));

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

out:
if (page_nid != -1)
task_numa_fault(last_nid, page_nid, 1, migrated);
task_numa_fault(last_nidpid, page_nid, 1, migrated);
return 0;
}

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

spin_lock(&mm->page_table_lock);
pmd = *pmdp;
Expand Down Expand Up @@ -3643,7 +3643,7 @@ static int do_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
if (unlikely(!page))
continue;

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

if (page_nid != -1)
task_numa_fault(last_nid, page_nid, 1, migrated);
task_numa_fault(last_nidpid, page_nid, 1, migrated);

pte = pte_offset_map_lock(mm, pmdp, addr, &ptl);
}
Expand Down
8 changes: 5 additions & 3 deletions mm/mempolicy.c
Original file line number Diff line number Diff line change
Expand Up @@ -2348,9 +2348,11 @@ int mpol_misplaced(struct page *page, struct vm_area_struct *vma, unsigned long

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

polnid = numa_node_id();
this_nidpid = nid_pid_to_nidpid(polnid, current->pid);

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

Expand Down
4 changes: 2 additions & 2 deletions mm/migrate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1498,7 +1498,7 @@ static struct page *alloc_misplaced_dst_page(struct page *page,
__GFP_NOWARN) &
~GFP_IOFS, 0);
if (newpage)
page_nid_xchg_last(newpage, page_nid_last(page));
page_nidpid_xchg_last(newpage, page_nidpid_last(page));

return newpage;
}
Expand Down Expand Up @@ -1675,7 +1675,7 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm,
if (!new_page)
goto out_fail;

page_nid_xchg_last(new_page, page_nid_last(page));
page_nidpid_xchg_last(new_page, page_nidpid_last(page));

isolated = numamigrate_isolate_page(pgdat, page);
if (!isolated) {
Expand Down
Loading

0 comments on commit b795854

Please sign in to comment.