1SET_MEMPOLICY(2)           Linux Programmer's Manual          SET_MEMPOLICY(2)
2
3
4

NAME

6       set_mempolicy  -  set  default  NUMA memory policy for a thread and its
7       children
8

SYNOPSIS

10       #include <numaif.h>
11
12       long set_mempolicy(int mode, const unsigned long *nodemask,
13                          unsigned long maxnode);
14
15       Link with -lnuma.
16

DESCRIPTION

18       set_mempolicy() sets the NUMA memory  policy  of  the  calling  thread,
19       which  consists  of a policy mode and zero or more nodes, to the values
20       specified by the mode, nodemask, and maxnode arguments.
21
22       A NUMA machine has different memory  controllers  with  different  dis‐
23       tances  to  specific  CPUs.   The memory policy defines from which node
24       memory is allocated for the thread.
25
26       This system call defines the default policy for the thread.  The thread
27       policy  governs allocation of pages in the process's address space out‐
28       side of memory ranges controlled by  a  more  specific  policy  set  by
29       mbind(2).   The  thread  default policy also controls allocation of any
30       pages for memory-mapped files mapped using the mmap(2)  call  with  the
31       MAP_PRIVATE flag and that are only read (loaded) from by the thread and
32       of  memory-mapped  files  mapped  using  the  mmap(2)  call  with   the
33       MAP_SHARED  flag, regardless of the access type.  The policy is applied
34       only when a new page is allocated for the thread.  For anonymous memory
35       this is when the page is first touched by the thread.
36
37       The mode argument must specify one of MPOL_DEFAULT, MPOL_BIND, MPOL_IN‐
38       TERLEAVE, MPOL_PREFERRED, or MPOL_LOCAL (which are described in  detail
39       below).   All  modes  except MPOL_DEFAULT require the caller to specify
40       the node or nodes to which the mode applies, via the nodemask argument.
41
42       The mode argument may also include an optional  mode  flag.   The  sup‐
43       ported mode flags are:
44
45       MPOL_F_NUMA_BALANCING (since Linux 5.12)
46              When mode is MPOL_BIND, enable the kernel NUMA balancing for the
47              task if it is supported by the kernel.  If the flag  isn't  sup‐
48              ported by the kernel, or is used with mode other than MPOL_BIND,
49              -1 is returned and errno is set to EINVAL.
50
51       MPOL_F_RELATIVE_NODES (since Linux 2.6.26)
52              A nonempty nodemask specifies node IDs that are relative to  the
53              set of node IDs allowed by the process's current cpuset.
54
55       MPOL_F_STATIC_NODES (since Linux 2.6.26)
56              A nonempty nodemask specifies physical node IDs.  Linux will not
57              remap the nodemask when the process moves to a different  cpuset
58              context, nor when the set of nodes allowed by the process's cur‐
59              rent cpuset context changes.
60
61       nodemask points to a bit mask of node IDs that contains up  to  maxnode
62       bits.   The bit mask size is rounded to the next multiple of sizeof(un‐
63       signed long), but the kernel will use bits only up to maxnode.  A  NULL
64       value of nodemask or a maxnode value of zero specifies the empty set of
65       nodes.  If the value of maxnode is zero, the nodemask argument  is  ig‐
66       nored.
67
68       Where a nodemask is required, it must contain at least one node that is
69       on-line, allowed by the process's current cpuset context,  (unless  the
70       MPOL_F_STATIC_NODES  mode  flag is specified), and contains memory.  If
71       the MPOL_F_STATIC_NODES is set in mode and a required nodemask contains
72       no  nodes that are allowed by the process's current cpuset context, the
73       memory policy reverts to local allocation.  This effectively  overrides
74       the specified policy until the process's cpuset context includes one or
75       more of the nodes specified by nodemask.
76
77       The mode argument must include one of the following values:
78
79       MPOL_DEFAULT
80              This mode specifies that any nondefault thread memory policy  be
81              removed,  so  that  the memory policy "falls back" to the system
82              default policy.  The system default  policy  is  "local  alloca‐
83              tion"—that is, allocate memory on the node of the CPU that trig‐
84              gered the allocation.  nodemask must be specified as  NULL.   If
85              the  "local  node"  contains no free memory, the system will at‐
86              tempt to allocate memory from a "near by" node.
87
88       MPOL_BIND
89              This mode defines a strict policy that restricts memory  alloca‐
90              tion  to the nodes specified in nodemask.  If nodemask specifies
91              more than one node, page allocations will  come  from  the  node
92              with  the lowest numeric node ID first, until that node contains
93              no free memory.  Allocations will then come from the  node  with
94              the next highest node ID specified in nodemask and so forth, un‐
95              til none of the specified nodes contain free memory.  Pages will
96              not be allocated from any node not specified in the nodemask.
97
98       MPOL_INTERLEAVE
99              This  mode  interleaves page allocations across the nodes speci‐
100              fied in nodemask in numeric node ID order.  This  optimizes  for
101              bandwidth  instead  of latency by spreading out pages and memory
102              accesses to those pages across  multiple  nodes.   However,  ac‐
103              cesses  to  a  single  page  will still be limited to the memory
104              bandwidth of a single node.
105
106       MPOL_PREFERRED
107              This mode sets the preferred node for  allocation.   The  kernel
108              will try to allocate pages from this node first and fall back to
109              "near by" nodes if the preferred node is low on free memory.  If
110              nodemask  specifies more than one node ID, the first node in the
111              mask will be selected as the preferred node.   If  the  nodemask
112              and  maxnode  arguments  specify  the empty set, then the policy
113              specifies "local allocation" (like  the  system  default  policy
114              discussed above).
115
116       MPOL_LOCAL (since Linux 3.8)
117              This  mode specifies "local allocation"; the memory is allocated
118              on the node of the CPU that triggered the allocation (the "local
119              node").   The  nodemask  and  maxnode arguments must specify the
120              empty set.  If the "local node" is low on free memory, the  ker‐
121              nel  will  try  to allocate memory from other nodes.  The kernel
122              will allocate memory from the "local node" whenever  memory  for
123              this  node  is available.  If the "local node" is not allowed by
124              the process's current cpuset context, the kernel will try to al‐
125              locate memory from other nodes.  The kernel will allocate memory
126              from the  "local  node"  whenever  it  becomes  allowed  by  the
127              process's current cpuset context.
128
129       The  thread  memory policy is preserved across an execve(2), and is in‐
130       herited by child threads created using fork(2) or clone(2).
131

RETURN VALUE

133       On success, set_mempolicy() returns 0; on error, -1 is returned and er‐
134       rno is set to indicate the error.
135

ERRORS

137       EFAULT Part  of all of the memory range specified by nodemask and maxn‐
138              ode points outside your accessible address space.
139
140       EINVAL mode is invalid.  Or,  mode  is  MPOL_DEFAULT  and  nodemask  is
141              nonempty,  or  mode is MPOL_BIND or MPOL_INTERLEAVE and nodemask
142              is empty.  Or, maxnode specifies more than a page worth of bits.
143              Or,  nodemask  specifies  one  or more node IDs that are greater
144              than the maximum supported node ID.  Or, none of  the  node  IDs
145              specified  by  nodemask are on-line and allowed by the process's
146              current cpuset context, or none of the specified  nodes  contain
147              memory.      Or,    the    mode    argument    specified    both
148              MPOL_F_STATIC_NODES   and   MPOL_F_RELATIVE_NODES.    Or,    the
149              MPOL_F_NUMA_BALANCING  isn't supported by the kernel, or is used
150              with mode other than MPOL_BIND.
151
152       ENOMEM Insufficient kernel memory was available.
153

VERSIONS

155       The set_mempolicy() system call was added to the Linux kernel  in  ver‐
156       sion 2.6.7.
157

CONFORMING TO

159       This system call is Linux-specific.
160

NOTES

162       Memory  policy is not remembered if the page is swapped out.  When such
163       a page is paged back in, it will use the policy of the thread or memory
164       range that is in effect at the time the page is allocated.
165
166       For information on library support, see numa(7).
167

SEE ALSO

169       get_mempolicy(2),  getcpu(2),  mbind(2),  mmap(2),  numa(3), cpuset(7),
170       numa(7), numactl(8)
171

COLOPHON

173       This page is part of release 5.13 of the Linux  man-pages  project.   A
174       description  of  the project, information about reporting bugs, and the
175       latest    version    of    this    page,    can     be     found     at
176       https://www.kernel.org/doc/man-pages/.
177
178
179
180Linux                             2021-06-20                  SET_MEMPOLICY(2)
Impressum