1set_mempolicy(2)              System Calls Manual             set_mempolicy(2)
2
3
4

NAME

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

LIBRARY

10       NUMA (Non-Uniform Memory Access) policy library (libnuma, -lnuma)
11

SYNOPSIS

13       #include <numaif.h>
14
15       long set_mempolicy(int mode, const unsigned long *nodemask,
16                          unsigned long maxnode);
17

DESCRIPTION

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

RETURN VALUE

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

ERRORS

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

STANDARDS

156       Linux.
157

HISTORY

159       Linux 2.6.7.
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
172
173
174Linux man-pages 6.04              2023-03-30                  set_mempolicy(2)
Impressum