1MBIND(2) Linux Programmer's Manual MBIND(2)
2
3
4
6 mbind - set memory policy for a memory range
7
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
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 ap‐
28 plication writes (stores) to the page. For anonymous regions, an ini‐
29 tial 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 be‐
54 low.
55
56 The mode argument must specify one of MPOL_DEFAULT, MPOL_BIND, MPOL_IN‐
57 TERLEAVE, MPOL_PREFERRED, or MPOL_LOCAL (which are described in detail
58 below). All policy modes except MPOL_DEFAULT require the caller to
59 specify the node or nodes to which the mode applies, via the nodemask
60 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 ig‐
80 nored. Where a nodemask is required, it must contain at least one node
81 that is on-line, allowed by the thread's current cpuset context (unless
82 the MPOL_F_STATIC_NODES mode flag is specified), and contains memory.
83
84 The mode argument must include one of the following values:
85
86 MPOL_DEFAULT
87 This mode requests that any nondefault policy be removed,
88 restoring default behavior. When applied to a range of memory
89 via mbind(), this means to use the thread memory policy, which
90 may have been set with set_mempolicy(2). If the mode of the
91 thread memory policy is also MPOL_DEFAULT, the system-wide de‐
92 fault policy will be used. The system-wide default policy allo‐
93 cates pages on the node of the CPU that triggers the allocation.
94 For MPOL_DEFAULT, the nodemask and maxnode arguments must be
95 specify the empty set of nodes.
96
97 MPOL_BIND
98 This mode specifies a strict policy that restricts memory allo‐
99 cation to the nodes specified in nodemask. If nodemask speci‐
100 fies more than one node, page allocations will come from the
101 node with sufficient free memory that is closest to the node
102 where the allocation takes place. Pages will not be allocated
103 from any node not specified in the IR nodemask . (Before Linux
104 2.6.26, page allocations came from the node with the lowest nu‐
105 meric node ID first, until that node contained no free memory.
106 Allocations then came from the node with the next highest node
107 ID specified in nodemask and so forth, until none of the speci‐
108 fied nodes contained free memory.)
109
110 MPOL_INTERLEAVE
111 This mode specifies that page allocations be interleaved across
112 the set of nodes specified in nodemask. This optimizes for
113 bandwidth instead of latency by spreading out pages and memory
114 accesses to those pages across multiple nodes. To be effective
115 the memory area should be fairly large, at least 1 MB or bigger
116 with a fairly uniform access pattern. Accesses to a single page
117 of the area will still be limited to the memory bandwidth of a
118 single node.
119
120 MPOL_PREFERRED
121 This mode sets the preferred node for allocation. The kernel
122 will try to allocate pages from this node first and fall back to
123 other nodes if the preferred nodes is low on free memory. If
124 nodemask specifies more than one node ID, the first node in the
125 mask will be selected as the preferred node. If the nodemask
126 and maxnode arguments specify the empty set, then the memory is
127 allocated on the node of the CPU that triggered the allocation.
128
129 MPOL_LOCAL (since Linux 3.8)
130 This mode specifies "local allocation"; the memory is allocated
131 on the node of the CPU that triggered the allocation (the "local
132 node"). The nodemask and maxnode arguments must specify the
133 empty set. If the "local node" is low on free memory, the ker‐
134 nel will try to allocate memory from other nodes. The kernel
135 will allocate memory from the "local node" whenever memory for
136 this node is available. If the "local node" is not allowed by
137 the thread's current cpuset context, the kernel will try to al‐
138 locate memory from other nodes. The kernel will allocate memory
139 from the "local node" whenever it becomes allowed by the
140 thread's current cpuset context. By contrast, MPOL_DEFAULT re‐
141 verts to the memory policy of the thread (which may be set via
142 set_mempolicy(2)); that policy may be something other than "lo‐
143 cal allocation".
144
145 If MPOL_MF_STRICT is passed in flags and mode is not MPOL_DEFAULT, then
146 the call fails with the error EIO if the existing pages in the memory
147 range don't follow the policy.
148
149 If MPOL_MF_MOVE is specified in flags, then the kernel will attempt to
150 move all the existing pages in the memory range so that they follow the
151 policy. Pages that are shared with other processes will not be moved.
152 If MPOL_MF_STRICT is also specified, then the call fails with the error
153 EIO if some pages could not be moved.
154
155 If MPOL_MF_MOVE_ALL is passed in flags, then the kernel will attempt to
156 move all existing pages in the memory range regardless of whether other
157 processes use the pages. The calling thread must be privileged
158 (CAP_SYS_NICE) to use this flag. If MPOL_MF_STRICT is also specified,
159 then the call fails with the error EIO if some pages could not be
160 moved.
161
163 On success, mbind() returns 0; on error, -1 is returned and errno is
164 set to indicate the error.
165
167 EFAULT Part or all of the memory range specified by nodemask and maxn‐
168 ode points outside your accessible address space. Or, there was
169 an unmapped hole in the specified memory range specified by addr
170 and len.
171
172 EINVAL An invalid value was specified for flags or mode; or addr + len
173 was less than addr; or addr is not a multiple of the system page
174 size. Or, mode is MPOL_DEFAULT and nodemask specified a
175 nonempty set; or mode is MPOL_BIND or MPOL_INTERLEAVE and node‐
176 mask is empty. Or, maxnode exceeds a kernel-imposed limit. Or,
177 nodemask specifies one or more node IDs that are greater than
178 the maximum supported node ID. Or, none of the node IDs speci‐
179 fied by nodemask are on-line and allowed by the thread's current
180 cpuset context, or none of the specified nodes contain memory.
181 Or, the mode argument specified both MPOL_F_STATIC_NODES and
182 MPOL_F_RELATIVE_NODES.
183
184 EIO MPOL_MF_STRICT was specified and an existing page was already on
185 a node that does not follow the policy; or MPOL_MF_MOVE or
186 MPOL_MF_MOVE_ALL was specified and the kernel was unable to move
187 all existing pages in the range.
188
189 ENOMEM Insufficient kernel memory was available.
190
191 EPERM The flags argument included the MPOL_MF_MOVE_ALL flag and the
192 caller does not have the CAP_SYS_NICE privilege.
193
195 The mbind() system call was added to the Linux kernel in version 2.6.7.
196
198 This system call is Linux-specific.
199
201 For information on library support, see numa(7).
202
203 NUMA policy is not supported on a memory-mapped file range that was
204 mapped with the MAP_SHARED flag.
205
206 The MPOL_DEFAULT mode can have different effects for mbind() and
207 set_mempolicy(2). When MPOL_DEFAULT is specified for set_mempolicy(2),
208 the thread's memory policy reverts to the system default policy or lo‐
209 cal allocation. When MPOL_DEFAULT is specified for a range of memory
210 using mbind(), any pages subsequently allocated for that range will use
211 the thread's memory policy, as set by set_mempolicy(2). This effec‐
212 tively removes the explicit policy from the specified range, "falling
213 back" to a possibly nondefault policy. To select explicit "local allo‐
214 cation" for a memory range, specify a mode of MPOL_LOCAL or MPOL_PRE‐
215 FERRED with an empty set of nodes. This method will work for set_mem‐
216 policy(2), as well.
217
218 Support for huge page policy was added with 2.6.16. For interleave
219 policy to be effective on huge page mappings the policied memory needs
220 to be tens of megabytes or larger.
221
222 Before Linux 5.7. MPOL_MF_STRICT was ignored on huge page mappings.
223
224 MPOL_MF_MOVE and MPOL_MF_MOVE_ALL are available only on Linux 2.6.16
225 and later.
226
228 get_mempolicy(2), getcpu(2), mmap(2), set_mempolicy(2), shmat(2),
229 shmget(2), numa(3), cpuset(7), numa(7), numactl(8)
230
232 This page is part of release 5.10 of the Linux man-pages project. A
233 description of the project, information about reporting bugs, and the
234 latest version of this page, can be found at
235 https://www.kernel.org/doc/man-pages/.
236
237
238
239Linux 2020-06-09 MBIND(2)