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_STATIC_NODES (since Linux 2.6.26)
46              A nonempty nodemask specifies physical node IDs.  Linux will not
47              remap the nodemask when the process moves to a different  cpuset
48              context, nor when the set of nodes allowed by the process's cur‐
49              rent cpuset context changes.
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       nodemask  points  to a bit mask of node IDs that contains up to maxnode
56       bits.  The bit mask size is rounded to the next multiple of  sizeof(un‐
57       signed  long), but the kernel will use bits only up to maxnode.  A NULL
58       value of nodemask or a maxnode value of zero specifies the empty set of
59       nodes.   If  the value of maxnode is zero, the nodemask argument is ig‐
60       nored.
61
62       Where a nodemask is required, it must contain at least one node that is
63       on-line,  allowed  by the process's current cpuset context, (unless the
64       MPOL_F_STATIC_NODES mode flag is specified), and contains  memory.   If
65       the MPOL_F_STATIC_NODES is set in mode and a required nodemask contains
66       no nodes that are allowed by the process's current cpuset context,  the
67       memory  policy reverts to local allocation.  This effectively overrides
68       the specified policy until the process's cpuset context includes one or
69       more of the nodes specified by nodemask.
70
71       The mode argument must include one of the following values:
72
73       MPOL_DEFAULT
74              This  mode specifies that any nondefault thread memory policy be
75              removed, so that the memory policy "falls back"  to  the  system
76              default  policy.   The  system  default policy is "local alloca‐
77              tion"—that is, allocate memory on the node of the CPU that trig‐
78              gered  the  allocation.  nodemask must be specified as NULL.  If
79              the "local node" contains no free memory, the  system  will  at‐
80              tempt to allocate memory from a "near by" node.
81
82       MPOL_BIND
83              This  mode defines a strict policy that restricts memory alloca‐
84              tion to the nodes specified in nodemask.  If nodemask  specifies
85              more  than  one  node,  page allocations will come from the node
86              with the lowest numeric node ID first, until that node  contains
87              no  free  memory.  Allocations will then come from the node with
88              the next highest node ID specified in nodemask and so forth, un‐
89              til none of the specified nodes contain free memory.  Pages will
90              not be allocated from any node not specified in the nodemask.
91
92       MPOL_INTERLEAVE
93              This mode interleaves page allocations across the  nodes  speci‐
94              fied  in  nodemask in numeric node ID order.  This optimizes for
95              bandwidth instead of latency by spreading out pages  and  memory
96              accesses  to  those  pages  across multiple nodes.  However, ac‐
97              cesses to a single page will still  be  limited  to  the  memory
98              bandwidth of a single node.
99
100       MPOL_PREFERRED
101              This  mode  sets  the preferred node for allocation.  The kernel
102              will try to allocate pages from this node first and fall back to
103              "near by" nodes if the preferred node is low on free memory.  If
104              nodemask specifies more than one node ID, the first node in  the
105              mask  will  be  selected as the preferred node.  If the nodemask
106              and maxnode arguments specify the empty  set,  then  the  policy
107              specifies  "local  allocation"  (like  the system default policy
108              discussed above).
109
110       MPOL_LOCAL (since Linux 3.8)
111              This mode specifies "local allocation"; the memory is  allocated
112              on the node of the CPU that triggered the allocation (the "local
113              node").  The nodemask and maxnode  arguments  must  specify  the
114              empty  set.  If the "local node" is low on free memory, the ker‐
115              nel will try to allocate memory from other  nodes.   The  kernel
116              will  allocate  memory from the "local node" whenever memory for
117              this node is available.  If the "local node" is not  allowed  by
118              the process's current cpuset context, the kernel will try to al‐
119              locate memory from other nodes.  The kernel will allocate memory
120              from  the  "local  node"  whenever  it  becomes  allowed  by the
121              process's current cpuset context.
122
123       The thread memory policy is preserved across an execve(2), and  is  in‐
124       herited by child threads created using fork(2) or clone(2).
125

RETURN VALUE

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

ERRORS

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

VERSIONS

147       The  set_mempolicy()  system call was added to the Linux kernel in ver‐
148       sion 2.6.7.
149

CONFORMING TO

151       This system call is Linux-specific.
152

NOTES

154       Memory policy is not remembered if the page is swapped out.  When  such
155       a page is paged back in, it will use the policy of the thread or memory
156       range that is in effect at the time the page is allocated.
157
158       For information on library support, see numa(7).
159

SEE ALSO

161       get_mempolicy(2), getcpu(2),  mbind(2),  mmap(2),  numa(3),  cpuset(7),
162       numa(7), numactl(8)
163

COLOPHON

165       This  page  is  part of release 5.10 of the Linux man-pages project.  A
166       description of the project, information about reporting bugs,  and  the
167       latest     version     of     this    page,    can    be    found    at
168       https://www.kernel.org/doc/man-pages/.
169
170
171
172Linux                             2020-12-21                  SET_MEMPOLICY(2)
Impressum