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

NAME

6       mbind - set memory policy for a memory range
7

SYNOPSIS

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

DESCRIPTION

18       mbind()  sets  the  NUMA memory policy, which consists of a policy mode
19       and zero or more nodes, for the memory range  starting  with  addr  and
20       continuing  for  len  bytes.  The memory policy defines from which node
21       memory is allocated.
22
23       If the memory range specified by the addr and len arguments includes an
24       "anonymous"  region  of memory—that is a region of memory created using
25       the mmap(2) system call with the MAP_ANONYMOUS—or a memory-mapped file,
26       mapped  using  the mmap(2) system call with the MAP_PRIVATE flag, pages
27       will be allocated only according  to  the  specified  policy  when  the
28       application  writes  (stores)  to  the page.  For anonymous regions, an
29       initial read access will use a shared page in the kernel containing all
30       zeros.  For a file mapped with MAP_PRIVATE, an initial read access will
31       allocate pages according to the memory policy of the thread that causes
32       the  page  to  be  allocated.   This  may not be the thread that called
33       mbind().
34
35       The specified policy will be ignored for any MAP_SHARED mappings in the
36       specified  memory  range.  Rather the pages will be allocated according
37       to the memory policy of the thread that caused the  page  to  be  allo‐
38       cated.  Again, this may not be the thread that called mbind().
39
40       If  the  specified memory range includes a shared memory region created
41       using the shmget(2) system call and attached using the shmat(2)  system
42       call, pages allocated for the anonymous or shared memory region will be
43       allocated according  to  the  policy  specified,  regardless  of  which
44       process  attached  to  the shared memory segment causes the allocation.
45       If, however, the shared memory region was created with the  SHM_HUGETLB
46       flag,  the  huge pages will be allocated according to the policy speci‐
47       fied only if the page allocation is caused by the  process  that  calls
48       mbind() for that region.
49
50       By  default,  mbind()  has  an  effect only for new allocations; if the
51       pages inside the range have been already  touched  before  setting  the
52       policy,  then  the  policy has no effect.  This default behavior may be
53       overridden by the MPOL_MF_MOVE  and  MPOL_MF_MOVE_ALL  flags  described
54       below.
55
56       The   mode  argument  must  specify  one  of  MPOL_DEFAULT,  MPOL_BIND,
57       MPOL_INTERLEAVE, MPOL_PREFERRED, or MPOL_LOCAL (which are described  in
58       detail below).  All policy modes except MPOL_DEFAULT require the caller
59       to specify the node or nodes to which the mode applies, via  the  node‐
60       mask argument.
61
62       The  mode  argument  may  also include an optional mode flag.  The sup‐
63       ported mode flags are:
64
65       MPOL_F_STATIC_NODES (since Linux-2.6.26)
66              A nonempty nodemask specifies physical node IDs.  Linux does not
67              remap  the  nodemask when the thread moves to a different cpuset
68              context, nor when the set of nodes allowed by the thread's  cur‐
69              rent cpuset context changes.
70
71       MPOL_F_RELATIVE_NODES (since Linux-2.6.26)
72              A  nonempty nodemask specifies node IDs that are relative to the
73              set of node IDs allowed by the thread's current cpuset.
74
75       nodemask points to a bit mask of nodes containing up to  maxnode  bits.
76       The  bit  mask  size is rounded to the next multiple of sizeof(unsigned
77       long), but the kernel will use bits only up to maxnode.  A  NULL  value
78       of  nodemask  or  a  maxnode  value  of zero specifies the empty set of
79       nodes.  If the value of maxnode  is  zero,  the  nodemask  argument  is
80       ignored.   Where  a  nodemask is required, it must contain at least one
81       node that is on-line, allowed by the thread's  current  cpuset  context
82       (unless  the  MPOL_F_STATIC_NODES mode flag is specified), and contains
83       memory.
84
85       The mode argument must include one of the following values:
86
87       MPOL_DEFAULT
88              This mode  requests  that  any  nondefault  policy  be  removed,
89              restoring  default  behavior.  When applied to a range of memory
90              via mbind(), this means to use the thread memory  policy,  which
91              may  have  been  set  with set_mempolicy(2).  If the mode of the
92              thread memory  policy  is  also  MPOL_DEFAULT,  the  system-wide
93              default  policy  will  be  used.  The system-wide default policy
94              allocates pages on the node of the CPU that triggers the alloca‐
95              tion.  For MPOL_DEFAULT, the nodemask and maxnode arguments must
96              be specify the empty set of nodes.
97
98       MPOL_BIND
99              This mode specifies a strict policy that restricts memory  allo‐
100              cation  to  the nodes specified in nodemask.  If nodemask speci‐
101              fies more than one node, page allocations  will  come  from  the
102              node  with  sufficient  free  memory that is closest to the node
103              where the allocation takes place.  Pages will not  be  allocated
104              from  any node not specified in the IR nodemask .  (Before Linux
105              2.6.26, page allocations came from  the  node  with  the  lowest
106              numeric node ID first, until that node contained no free memory.
107              Allocations then came from the node with the next  highest  node
108              ID  specified in nodemask and so forth, until none of the speci‐
109              fied nodes contained free memory.)
110
111       MPOL_INTERLEAVE
112              This mode specifies that page allocations be interleaved  across
113              the  set  of  nodes  specified  in nodemask.  This optimizes for
114              bandwidth instead of latency by spreading out pages  and  memory
115              accesses  to those pages across multiple nodes.  To be effective
116              the memory area should be fairly large, at least 1 MB or  bigger
117              with a fairly uniform access pattern.  Accesses to a single page
118              of the area will still be limited to the memory bandwidth  of  a
119              single node.
120
121       MPOL_PREFERRED
122              This  mode  sets  the preferred node for allocation.  The kernel
123              will try to allocate pages from this node first and fall back to
124              other  nodes  if  the preferred nodes is low on free memory.  If
125              nodemask specifies more than one node ID, the first node in  the
126              mask  will  be  selected as the preferred node.  If the nodemask
127              and maxnode arguments specify the empty set, then the memory  is
128              allocated on the node of the CPU that triggered the allocation.
129
130       MPOL_LOCAL (since Linux 3.8)
131              This  mode specifies "local allocation"; the memory is allocated
132              on the node of the CPU that triggered the allocation (the "local
133              node").   The  nodemask  and  maxnode arguments must specify the
134              empty set.  If the "local node" is low on free memory, the  ker‐
135              nel  will  try  to allocate memory from other nodes.  The kernel
136              will allocate memory from the "local node" whenever  memory  for
137              this  node  is available.  If the "local node" is not allowed by
138              the thread's current cpuset context,  the  kernel  will  try  to
139              allocate memory from other nodes.  The kernel will allocate mem‐
140              ory from the "local node" whenever it  becomes  allowed  by  the
141              thread's  current  cpuset  context.   By  contrast, MPOL_DEFAULT
142              reverts to the memory policy of the thread (which may be set via
143              set_mempolicy(2));  that  policy  may  be  something  other than
144              "local allocation".
145
146       If MPOL_MF_STRICT is passed in flags and mode is not MPOL_DEFAULT, then
147       the  call  fails with the error EIO if the existing pages in the memory
148       range don't follow the policy.
149
150       If MPOL_MF_MOVE is specified in flags, then the kernel will attempt  to
151       move all the existing pages in the memory range so that they follow the
152       policy.  Pages that are shared with other processes will not be  moved.
153       If MPOL_MF_STRICT is also specified, then the call fails with the error
154       EIO if some pages could not be moved.
155
156       If MPOL_MF_MOVE_ALL is passed in flags, then the kernel will attempt to
157       move all existing pages in the memory range regardless of whether other
158       processes use  the  pages.   The  calling  thread  must  be  privileged
159       (CAP_SYS_NICE)  to use this flag.  If MPOL_MF_STRICT is also specified,
160       then the call fails with the error EIO  if  some  pages  could  not  be
161       moved.
162

RETURN VALUE

164       On  success,  mbind()  returns 0; on error, -1 is returned and errno is
165       set to indicate the error.
166

ERRORS

168       EFAULT Part or all of the memory range specified by nodemask and  maxn‐
169              ode points outside your accessible address space.  Or, there was
170              an unmapped hole in the specified memory range specified by addr
171              and len.
172
173       EINVAL An  invalid value was specified for flags or mode; or addr + len
174              was less than addr; or addr is not a multiple of the system page
175              size.   Or,  mode  is  MPOL_DEFAULT  and  nodemask  specified  a
176              nonempty set; or mode is MPOL_BIND or MPOL_INTERLEAVE and  node‐
177              mask is empty.  Or, maxnode exceeds a kernel-imposed limit.  Or,
178              nodemask specifies one or more node IDs that  are  greater  than
179              the  maximum supported node ID.  Or, none of the node IDs speci‐
180              fied by nodemask are on-line and allowed by the thread's current
181              cpuset  context,  or none of the specified nodes contain memory.
182              Or, the mode argument  specified  both  MPOL_F_STATIC_NODES  and
183              MPOL_F_RELATIVE_NODES.
184
185       EIO    MPOL_MF_STRICT was specified and an existing page was already on
186              a node that does not  follow  the  policy;  or  MPOL_MF_MOVE  or
187              MPOL_MF_MOVE_ALL was specified and the kernel was unable to move
188              all existing pages in the range.
189
190       ENOMEM Insufficient kernel memory was available.
191
192       EPERM  The flags argument included the MPOL_MF_MOVE_ALL  flag  and  the
193              caller does not have the CAP_SYS_NICE privilege.
194

VERSIONS

196       The mbind() system call was added to the Linux kernel in version 2.6.7.
197

CONFORMING TO

199       This system call is Linux-specific.
200

NOTES

202       For information on library support, see numa(7).
203
204       NUMA  policy  is  not  supported on a memory-mapped file range that was
205       mapped with the MAP_SHARED flag.
206
207       The MPOL_DEFAULT mode  can  have  different  effects  for  mbind()  and
208       set_mempolicy(2).  When MPOL_DEFAULT is specified for set_mempolicy(2),
209       the thread's memory policy reverts to  the  system  default  policy  or
210       local allocation.  When MPOL_DEFAULT is specified for a range of memory
211       using mbind(), any pages subsequently allocated for that range will use
212       the  thread's  memory  policy, as set by set_mempolicy(2).  This effec‐
213       tively removes the explicit policy from the specified  range,  "falling
214       back" to a possibly nondefault policy.  To select explicit "local allo‐
215       cation" for a memory range, specify a mode of MPOL_LOCAL  or  MPOL_PRE‐
216       FERRED  with an empty set of nodes.  This method will work for set_mem‐
217       policy(2), as well.
218
219       Support for huge page policy was added  with  2.6.16.   For  interleave
220       policy  to be effective on huge page mappings the policied memory needs
221       to be tens of megabytes or larger.
222
223       Before Linux 5.7.  MPOL_MF_STRICT was ignored on huge page mappings.
224
225       MPOL_MF_MOVE and MPOL_MF_MOVE_ALL are available only  on  Linux  2.6.16
226       and later.
227

SEE ALSO

229       get_mempolicy(2),   getcpu(2),   mmap(2),  set_mempolicy(2),  shmat(2),
230       shmget(2), numa(3), cpuset(7), numa(7), numactl(8)
231

COLOPHON

233       This page is part of release 5.07 of the Linux  man-pages  project.   A
234       description  of  the project, information about reporting bugs, and the
235       latest    version    of    this    page,    can     be     found     at
236       https://www.kernel.org/doc/man-pages/.
237
238
239
240Linux                             2020-06-09                          MBIND(2)
Impressum