Commit d3b8924a authored by Andrew Morton's avatar Andrew Morton Committed by Linus Torvalds

[PATCH] numa api: Core NUMA API code

From: Andi Kleen <ak@suse.de>

The following patches add support for configurable NUMA memory policy
for user processes. It is based on the proposal from last kernel summit
with feedback from various people.

This NUMA API doesn't not attempt to implement page migration or anything
else complicated: all it does is to police the allocation when a page
is first allocation or when a page is reallocated after swapping. Currently
only support for shared memory and anonymous memory is there; policy for
file based mappings is not implemented yet (although they get implicitely
policied by the default process policy)

It adds three new system calls: mbind to change the policy of a VMA,
set_mempolicy to change the policy of a process, get_mempolicy to retrieve
memory policy. User tools (numactl, libnuma, test programs, manpages) can be
found in  ftp://ftp.suse.com/pub/people/ak/numa/numactl-0.6.tar.gz

For details on the system calls see the manpages
http://www.firstfloor.org/~andi/mbind.html
http://www.firstfloor.org/~andi/set_mempolicy.html
http://www.firstfloor.org/~andi/get_mempolicy.html
Most user programs should actually not use the system calls directly,
but use the higher level functions in libnuma
(http://www.firstfloor.org/~andi/numa.html) or the command line tools
(http://www.firstfloor.org/~andi/numactl.html

The system calls allow user programs and administors to set various NUMA memory
policies for putting memory on specific nodes. Here is a short description
of the policies copied from the kernel patch:

 * NUMA policy allows the user to give hints in which node(s) memory should
 * be allocated.
 *
 * Support four policies per VMA and per process:
 *
 * The VMA policy has priority over the process policy for a page fault.
 *
 * interleave     Allocate memory interleaved over a set of nodes,
 *                with normal fallback if it fails.
 *                For VMA based allocations this interleaves based on the
 *                offset into the backing object or offset into the mapping
 *                for anonymous memory. For process policy an process counter
 *                is used.
 * bind           Only allocate memory on a specific set of nodes,
 *                no fallback.
 * preferred      Try a specific node first before normal fallback.
 *                As a special case node -1 here means do the allocation
 *                on the local CPU. This is normally identical to default,
 *                but useful to set in a VMA when you have a non default
 *                process policy.
 * default        Allocate on the local node first, or when on a VMA
 *                use the process policy. This is what Linux always did
 *                in a NUMA aware kernel and still does by, ahem, default.
 *
 * The process policy is applied for most non interrupt memory allocations
 * in that process' context. Interrupts ignore the policies and always
 * try to allocate on the local CPU. The VMA policy is only applied for memory
 * allocations for a VMA in the VM.
 *
 * Currently there are a few corner cases in swapping where the policy
 * is not applied, but the majority should be handled. When process policy
 * is used it is not remembered over swap outs/swap ins.
 *
 * Only the highest zone in the zone hierarchy gets policied. Allocations
 * requesting a lower zone just use default policy. This implies that
 * on systems with highmem kernel lowmem allocation don't get policied.
 * Same with GFP_DMA allocations.
 *
 * For shmfs/tmpfs/hugetlbfs shared memory the policy is shared between
 * all users and remembered even when nobody has memory mapped.




This patch:

This is the core NUMA API code. This includes NUMA policy aware
wrappers for get_free_pages and alloc_page_vma(). On non NUMA kernels
these are defined away.

The system calls mbind (see http://www.firstfloor.org/~andi/mbind.html),
get_mempolicy (http://www.firstfloor.org/~andi/get_mempolicy.html) and
set_mempolicy (http://www.firstfloor.org/~andi/set_mempolicy.html) are
implemented here.

Adds a vm_policy field to the VMA and to the process. The process
also has field for interleaving. VMA interleaving uses the offset
into the VMA, but that's not possible for process allocations.

From: Andi Kleen <ak@muc.de>

  > Andi, how come policy_vma() calls ->set_policy under i_shared_sem?

  I think this can be actually dropped now.  In an earlier version I did
  walk the vma shared list to change the policies of other mappings to the
  same shared memory region.  This turned out too complicated with all the
  corner cases, so I eventually gave in and added ->get_policy to the fast
  path.  Also there is still the mmap_sem which prevents races in the same MM.
   

  Patch to remove it attached.  Also adds documentation and removes the
  bogus __alloc_page_vma() prototype noticed by hch.

From: Andi Kleen <ak@suse.de>

  A few incremental fixes for NUMA API.

  - Fix a few comments

  - Add a compat_ function for get_mem_policy I considered changing the
    ABI to avoid this, but that would have made the API too ugly.  I put it
    directly into the file because a mm/compat.c didn't seem worth it just for
    this.

  - Fix the algorithm for VMA interleave.

From: Matthew Dobson <colpatch@us.ibm.com>

  1) Move the extern of alloc_pages_current() into #ifdef CONFIG_NUMA.
    The only references to the function are in NUMA code in mempolicy.c

  2) Remove the definitions of __alloc_page_vma().  They aren't used.

  3) Move forward declaration of struct vm_area_struct to top of file.
parent 8a8e5a38
Numa policy hit/miss statistics
/sys/devices/system/node/node*/numastat
All units are pages. Hugepages have separate counters.
numa_hit A process wanted to allocate memory from this node,
and succeeded.
numa_miss A process wanted to allocate memory from this node,
but ended up with memory from another.
numa_foreign A process wanted to allocate on another node,
but ended up with memory from this one.
local_node A process ran on this node and got memory from it.
other_node A process ran on this node and got memory from another node.
interleave_hit Interleaving wanted to allocate from this node
and succeeded.
For easier reading you can use the numastat utility from the numactl package
(ftp://ftp.suse.com/pub/people/ak/numa/numactl*). Note that it only works
well right now on machines with a small number of CPUs.
...@@ -4,6 +4,10 @@ ...@@ -4,6 +4,10 @@
#include <linux/mmzone.h> #include <linux/mmzone.h>
#include <linux/stddef.h> #include <linux/stddef.h>
#include <linux/linkage.h> #include <linux/linkage.h>
#include <linux/config.h>
struct vm_area_struct;
/* /*
* GFP bitmasks.. * GFP bitmasks..
*/ */
...@@ -69,19 +73,38 @@ ...@@ -69,19 +73,38 @@
* For the normal case of non-DISCONTIGMEM systems the NODE_DATA() gets * For the normal case of non-DISCONTIGMEM systems the NODE_DATA() gets
* optimized to &contig_page_data at compile-time. * optimized to &contig_page_data at compile-time.
*/ */
extern struct page * FASTCALL(__alloc_pages(unsigned int, unsigned int, struct zonelist *)); extern struct page *
static inline struct page * alloc_pages_node(int nid, unsigned int gfp_mask, unsigned int order) FASTCALL(__alloc_pages(unsigned int, unsigned int, struct zonelist *));
static inline struct page *alloc_pages_node(int nid, unsigned int gfp_mask,
unsigned int order)
{ {
if (unlikely(order >= MAX_ORDER)) if (unlikely(order >= MAX_ORDER))
return NULL; return NULL;
return __alloc_pages(gfp_mask, order, NODE_DATA(nid)->node_zonelists + (gfp_mask & GFP_ZONEMASK)); return __alloc_pages(gfp_mask, order,
NODE_DATA(nid)->node_zonelists + (gfp_mask & GFP_ZONEMASK));
} }
#ifdef CONFIG_NUMA
extern struct page *alloc_pages_current(unsigned gfp_mask, unsigned order);
static inline struct page *
alloc_pages(unsigned int gfp_mask, unsigned int order)
{
if (unlikely(order >= MAX_ORDER))
return NULL;
return alloc_pages_current(gfp_mask, order);
}
extern struct page *alloc_page_vma(unsigned gfp_mask,
struct vm_area_struct *vma, unsigned long addr);
#else
#define alloc_pages(gfp_mask, order) \ #define alloc_pages(gfp_mask, order) \
alloc_pages_node(numa_node_id(), gfp_mask, order) alloc_pages_node(numa_node_id(), gfp_mask, order)
#define alloc_page(gfp_mask) \ #define alloc_page_vma(gfp_mask, vma, addr) alloc_pages(gfp_mask, 0)
alloc_pages_node(numa_node_id(), gfp_mask, 0) #endif
#define alloc_page(gfp_mask) alloc_pages(gfp_mask, 0)
extern unsigned long FASTCALL(__get_free_pages(unsigned int gfp_mask, unsigned int order)); extern unsigned long FASTCALL(__get_free_pages(unsigned int gfp_mask, unsigned int order));
extern unsigned long FASTCALL(get_zeroed_page(unsigned int gfp_mask)); extern unsigned long FASTCALL(get_zeroed_page(unsigned int gfp_mask));
......
#ifndef _LINUX_MEMPOLICY_H
#define _LINUX_MEMPOLICY_H 1
#include <linux/errno.h>
/*
* NUMA memory policies for Linux.
* Copyright 2003,2004 Andi Kleen SuSE Labs
*/
/* Policies */
#define MPOL_DEFAULT 0
#define MPOL_PREFERRED 1
#define MPOL_BIND 2
#define MPOL_INTERLEAVE 3
#define MPOL_MAX MPOL_INTERLEAVE
/* Flags for get_mem_policy */
#define MPOL_F_NODE (1<<0) /* return next IL mode instead of node mask */
#define MPOL_F_ADDR (1<<1) /* look up vma using address */
/* Flags for mbind */
#define MPOL_MF_STRICT (1<<0) /* Verify existing pages in the mapping */
#ifdef __KERNEL__
#include <linux/config.h>
#include <linux/mmzone.h>
#include <linux/bitmap.h>
#include <linux/slab.h>
#include <linux/rbtree.h>
#include <asm/semaphore.h>
struct vm_area_struct;
#ifdef CONFIG_NUMA
/*
* Describe a memory policy.
*
* A mempolicy can be either associated with a process or with a VMA.
* For VMA related allocations the VMA policy is preferred, otherwise
* the process policy is used. Interrupts ignore the memory policy
* of the current process.
*
* Locking policy for interlave:
* In process context there is no locking because only the process accesses
* its own state. All vma manipulation is somewhat protected by a down_read on
* mmap_sem. For allocating in the interleave policy the page_table_lock
* must be also aquired to protect il_next.
*
* Freeing policy:
* When policy is MPOL_BIND v.zonelist is kmalloc'ed and must be kfree'd.
* All other policies don't have any external state. mpol_free() handles this.
*
* Copying policy objects:
* For MPOL_BIND the zonelist must be always duplicated. mpol_clone() does this.
*/
struct mempolicy {
atomic_t refcnt;
short policy; /* See MPOL_* above */
union {
struct zonelist *zonelist; /* bind */
short preferred_node; /* preferred */
DECLARE_BITMAP(nodes, MAX_NUMNODES); /* interleave */
/* undefined for default */
} v;
};
/* An NULL mempolicy pointer is a synonym of &default_policy. */
extern struct mempolicy default_policy;
/*
* Support for managing mempolicy data objects (clone, copy, destroy)
* The default fast path of a NULL MPOL_DEFAULT policy is always inlined.
*/
extern void __mpol_free(struct mempolicy *pol);
static inline void mpol_free(struct mempolicy *pol)
{
if (pol)
__mpol_free(pol);
}
extern struct mempolicy *__mpol_copy(struct mempolicy *pol);
static inline struct mempolicy *mpol_copy(struct mempolicy *pol)
{
if (pol)
pol = __mpol_copy(pol);
return pol;
}
#define vma_policy(vma) ((vma)->vm_policy)
#define vma_set_policy(vma, pol) ((vma)->vm_policy = (pol))
static inline void mpol_get(struct mempolicy *pol)
{
if (pol)
atomic_inc(&pol->refcnt);
}
extern int __mpol_equal(struct mempolicy *a, struct mempolicy *b);
static inline int mpol_equal(struct mempolicy *a, struct mempolicy *b)
{
if (a == b)
return 1;
return __mpol_equal(a, b);
}
#define vma_mpol_equal(a,b) mpol_equal(vma_policy(a), vma_policy(b))
/* Could later add inheritance of the process policy here. */
#define mpol_set_vma_default(vma) ((vma)->vm_policy = NULL)
/*
* Hugetlb policy. i386 hugetlb so far works with node numbers
* instead of zone lists, so give it special interfaces for now.
*/
extern int mpol_first_node(struct vm_area_struct *vma, unsigned long addr);
extern int mpol_node_valid(int nid, struct vm_area_struct *vma,
unsigned long addr);
/*
* Tree of shared policies for a shared memory region.
* Maintain the policies in a pseudo mm that contains vmas. The vmas
* carry the policy. As a special twist the pseudo mm is indexed in pages, not
* bytes, so that we can work with shared memory segments bigger than
* unsigned long.
*/
struct sp_node {
struct rb_node nd;
unsigned long start, end;
struct mempolicy *policy;
};
struct shared_policy {
struct rb_root root;
struct semaphore sem;
};
static inline void mpol_shared_policy_init(struct shared_policy *info)
{
info->root = RB_ROOT;
init_MUTEX(&info->sem);
}
int mpol_set_shared_policy(struct shared_policy *info,
struct vm_area_struct *vma,
struct mempolicy *new);
void mpol_free_shared_policy(struct shared_policy *p);
struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp,
unsigned long idx);
#else
struct mempolicy {};
static inline int mpol_equal(struct mempolicy *a, struct mempolicy *b)
{
return 1;
}
#define vma_mpol_equal(a,b) 1
#define mpol_set_vma_default(vma) do {} while(0)
static inline void mpol_free(struct mempolicy *p)
{
}
static inline void mpol_get(struct mempolicy *pol)
{
}
static inline struct mempolicy *mpol_copy(struct mempolicy *old)
{
return NULL;
}
static inline int mpol_first_node(struct vm_area_struct *vma, unsigned long a)
{
return numa_node_id();
}
static inline int
mpol_node_valid(int nid, struct vm_area_struct *vma, unsigned long a)
{
return 1;
}
struct shared_policy {};
static inline int mpol_set_shared_policy(struct shared_policy *info,
struct vm_area_struct *vma,
struct mempolicy *new)
{
return -EINVAL;
}
static inline void mpol_shared_policy_init(struct shared_policy *info)
{
}
static inline void mpol_free_shared_policy(struct shared_policy *p)
{
}
static inline struct mempolicy *
mpol_shared_policy_lookup(struct shared_policy *sp, unsigned long idx)
{
return NULL;
}
#define vma_policy(vma) NULL
#define vma_set_policy(vma, pol) do {} while(0)
#endif /* CONFIG_NUMA */
#endif /* __KERNEL__ */
#endif
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include <linux/mmzone.h> #include <linux/mmzone.h>
#include <linux/rbtree.h> #include <linux/rbtree.h>
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/mempolicy.h>
#ifndef CONFIG_DISCONTIGMEM /* Don't use mapnrs, do it properly */ #ifndef CONFIG_DISCONTIGMEM /* Don't use mapnrs, do it properly */
extern unsigned long max_mapnr; extern unsigned long max_mapnr;
...@@ -47,6 +48,10 @@ extern int page_cluster; ...@@ -47,6 +48,10 @@ extern int page_cluster;
* *
* This structure is exactly 64 bytes on ia32. Please think very, very hard * This structure is exactly 64 bytes on ia32. Please think very, very hard
* before adding anything to it. * before adding anything to it.
* [Now 4 bytes more on 32bit NUMA machines. Sorry. -AK.
* But if you want to recover the 4 bytes justr remove vm_next. It is redundant
* with vm_rb. Will be a lot of editing work though. vm_rb.color is redundant
* too.]
*/ */
struct vm_area_struct { struct vm_area_struct {
struct mm_struct * vm_mm; /* The address space we belong to. */ struct mm_struct * vm_mm; /* The address space we belong to. */
...@@ -77,6 +82,10 @@ struct vm_area_struct { ...@@ -77,6 +82,10 @@ struct vm_area_struct {
units, *not* PAGE_CACHE_SIZE */ units, *not* PAGE_CACHE_SIZE */
struct file * vm_file; /* File we map to (can be NULL). */ struct file * vm_file; /* File we map to (can be NULL). */
void * vm_private_data; /* was vm_pte (shared mem) */ void * vm_private_data; /* was vm_pte (shared mem) */
#ifdef CONFIG_NUMA
struct mempolicy *vm_policy; /* NUMA policy for the VMA */
#endif
}; };
/* /*
...@@ -145,6 +154,11 @@ struct vm_operations_struct { ...@@ -145,6 +154,11 @@ struct vm_operations_struct {
void (*close)(struct vm_area_struct * area); void (*close)(struct vm_area_struct * area);
struct page * (*nopage)(struct vm_area_struct * area, unsigned long address, int *type); struct page * (*nopage)(struct vm_area_struct * area, unsigned long address, int *type);
int (*populate)(struct vm_area_struct * area, unsigned long address, unsigned long len, pgprot_t prot, unsigned long pgoff, int nonblock); int (*populate)(struct vm_area_struct * area, unsigned long address, unsigned long len, pgprot_t prot, unsigned long pgoff, int nonblock);
#ifdef CONFIG_NUMA
int (*set_policy)(struct vm_area_struct *vma, struct mempolicy *new);
struct mempolicy *(*get_policy)(struct vm_area_struct *vma,
unsigned long addr);
#endif
}; };
struct mmu_gather; struct mmu_gather;
...@@ -465,6 +479,9 @@ extern void show_free_areas(void); ...@@ -465,6 +479,9 @@ extern void show_free_areas(void);
struct page *shmem_nopage(struct vm_area_struct * vma, struct page *shmem_nopage(struct vm_area_struct * vma,
unsigned long address, int *type); unsigned long address, int *type);
int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new);
struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
unsigned long addr);
struct file *shmem_file_setup(char * name, loff_t size, unsigned long flags); struct file *shmem_file_setup(char * name, loff_t size, unsigned long flags);
void shmem_lock(struct file * file, int lock); void shmem_lock(struct file * file, int lock);
int shmem_zero_setup(struct vm_area_struct *); int shmem_zero_setup(struct vm_area_struct *);
...@@ -667,6 +684,11 @@ static inline struct vm_area_struct * find_vma_intersection(struct mm_struct * m ...@@ -667,6 +684,11 @@ static inline struct vm_area_struct * find_vma_intersection(struct mm_struct * m
return vma; return vma;
} }
static inline unsigned long vma_pages(struct vm_area_struct *vma)
{
return (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
}
extern struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr); extern struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr);
extern unsigned int nr_used_zone_pages(void); extern unsigned int nr_used_zone_pages(void);
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include <linux/completion.h> #include <linux/completion.h>
#include <linux/pid.h> #include <linux/pid.h>
#include <linux/percpu.h> #include <linux/percpu.h>
#include <linux/mempolicy.h>
struct exec_domain; struct exec_domain;
...@@ -508,6 +509,9 @@ struct task_struct { ...@@ -508,6 +509,9 @@ struct task_struct {
unsigned long ptrace_message; unsigned long ptrace_message;
siginfo_t *last_siginfo; /* For ptrace use. */ siginfo_t *last_siginfo; /* For ptrace use. */
struct mempolicy *mempolicy;
short il_next; /* could be shared with used_math */
}; };
static inline pid_t process_group(struct task_struct *tsk) static inline pid_t process_group(struct task_struct *tsk)
......
...@@ -271,6 +271,9 @@ cond_syscall(compat_sys_mq_timedsend) ...@@ -271,6 +271,9 @@ cond_syscall(compat_sys_mq_timedsend)
cond_syscall(compat_sys_mq_timedreceive) cond_syscall(compat_sys_mq_timedreceive)
cond_syscall(compat_sys_mq_notify) cond_syscall(compat_sys_mq_notify)
cond_syscall(compat_sys_mq_getsetattr) cond_syscall(compat_sys_mq_getsetattr)
cond_syscall(sys_mbind)
cond_syscall(sys_get_mempolicy)
cond_syscall(sys_set_mempolicy)
/* arch-specific weak syscall entries */ /* arch-specific weak syscall entries */
cond_syscall(sys_pciconfig_read) cond_syscall(sys_pciconfig_read)
......
...@@ -13,3 +13,4 @@ obj-y := bootmem.o filemap.o mempool.o oom_kill.o fadvise.o \ ...@@ -13,3 +13,4 @@ obj-y := bootmem.o filemap.o mempool.o oom_kill.o fadvise.o \
obj-$(CONFIG_SWAP) += page_io.o swap_state.o swapfile.o obj-$(CONFIG_SWAP) += page_io.o swap_state.o swapfile.o
obj-$(CONFIG_HUGETLBFS) += hugetlb.o obj-$(CONFIG_HUGETLBFS) += hugetlb.o
obj-$(CONFIG_NUMA) += mempolicy.o
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment