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 int mbind(void *addr, unsigned long len, int mode,
12 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 only be allocated 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 process policy of the process that
32 causes the page to be allocated. This may not be the process that
33 called 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 process policy of the process that caused the page to be allo‐
38 cated. Again, this may not be the process 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 which process
44 attached to the shared memory segment causes the allocation. If, how‐
45 ever, the shared memory region was created with the SHM_HUGETLB flag,
46 the huge pages will be allocated according to the policy specified only
47 if the page allocation is caused by the process that calls mbind() for
48 that region.
49
50 By default, mbind() only has an effect 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 or MPOL_PREFERRED. All policy modes except
58 MPOL_DEFAULT require the caller to specify via the nodemask argument,
59 the node or nodes to which the mode applies.
60
61 The mode argument may also include an optional mode flag . The sup‐
62 ported mode flags are:
63
64 MPOL_F_STATIC_NODES (since Linux-2.6.26)
65 A nonempty nodemask specifies physical node ids. Linux does not
66 remap the nodemask when the process moves to a different cpuset
67 context, nor when the set of nodes allowed by the process's cur‐
68 rent cpuset context changes.
69
70 MPOL_F_RELATIVE_NODES (since Linux-2.6.26)
71 A nonempty nodemask specifies node ids that are relative to the
72 set of node ids allowed by the process's current cpuset.
73
74 nodemask points to a bitmask of nodes containing up to maxnode bits.
75 The bit mask size is rounded to the next multiple of sizeof(unsigned
76 long), but the kernel will only use bits up to maxnode. A NULL value
77 of nodemask or a maxnode value of zero specifies the empty set of
78 nodes. If the value of maxnode is zero, the nodemask argument is
79 ignored. Where a nodemask is required, it must contain at least one
80 node that is on-line, allowed by the process's current cpuset context
81 [unless the MPOL_F_STATIC_NODES mode flag is specified], and contains
82 memory.
83
84 The MPOL_DEFAULT mode requests that any nondefault policy be removed,
85 restoring default behavior. When applied to a range of memory via
86 mbind(), this means to use the process policy, which may have been set
87 with set_mempolicy(2). If the mode of the process policy is also
88 MPOL_DEFAULT, the system-wide default policy will be used. The system-
89 wide default policy allocates pages on the node of the CPU that trig‐
90 gers the allocation. For MPOL_DEFAULT, the nodemask and maxnode argu‐
91 ments must be specify the empty set of nodes.
92
93 The MPOL_BIND mode specifies a strict policy that restricts memory
94 allocation to the nodes specified in nodemask. If nodemask specifies
95 more than one node, page allocations will come from the node with the
96 lowest numeric node ID first, until that node contains no free memory.
97 Allocations will then come from the node with the next highest node ID
98 specified in nodemask and so forth, until none of the specified nodes
99 contain free memory. Pages will not be allocated from any node not
100 specified in the nodemask.
101
102 The MPOL_INTERLEAVE mode specifies that page allocations be interleaved
103 across the set of nodes specified in nodemask. This optimizes for
104 bandwidth instead of latency by spreading out pages and memory accesses
105 to those pages across multiple nodes. To be effective the memory area
106 should be fairly large, at least 1MB or bigger with a fairly uniform
107 access pattern. Accesses to a single page of the area will still be
108 limited to the memory bandwidth of a single node.
109
110 MPOL_PREFERRED sets the preferred node for allocation. The kernel will
111 try to allocate pages from this node first and fall back to other nodes
112 if the preferred nodes is low on free memory. If nodemask specifies
113 more than one node ID, the first node in the mask will be selected as
114 the preferred node. If the nodemask and maxnode arguments specify the
115 empty set, then the memory is allocated on the node of the CPU that
116 triggered the allocation. This is the only way to specify "local allo‐
117 cation" for a range of memory via mbind().
118
119 If MPOL_MF_STRICT is passed in flags and policy is not MPOL_DEFAULT,
120 then the call will fail with the error EIO if the existing pages in the
121 memory range don't follow the policy.
122
123 If MPOL_MF_MOVE is specified in flags, then the kernel will attempt to
124 move all the existing pages in the memory range so that they follow the
125 policy. Pages that are shared with other processes will not be moved.
126 If MPOL_MF_STRICT is also specified, then the call will fail with the
127 error EIO if some pages could not be moved.
128
129 If MPOL_MF_MOVE_ALL is passed in flags, then the kernel will attempt to
130 move all existing pages in the memory range regardless of whether other
131 processes use the pages. The calling process must be privileged
132 (CAP_SYS_NICE) to use this flag. If MPOL_MF_STRICT is also specified,
133 then the call will fail with the error EIO if some pages could not be
134 moved.
135
137 On success, mbind() returns 0; on error, -1 is returned and errno is
138 set to indicate the error.
139
141 EFAULT Part or all of the memory range specified by nodemask and maxn‐
142 ode points outside your accessible address space. Or, there was
143 an unmapped hole in the specified memory range.
144
145 EINVAL An invalid value was specified for flags or mode; or addr + len
146 was less than addr; or addr is not a multiple of the system page
147 size. Or, mode is MPOL_DEFAULT and nodemask specified a
148 nonempty set; or mode is MPOL_BIND or MPOL_INTERLEAVE and node‐
149 mask is empty. Or, maxnode exceeds a kernel-imposed limit. Or,
150 nodemask specifies one or more node IDs that are greater than
151 the maximum supported node ID. Or, none of the node IDs speci‐
152 fied by nodemask are on-line and allowed by the process's cur‐
153 rent cpuset context, or none of the specified nodes contain mem‐
154 ory. Or, the mode argument specified both MPOL_F_STATIC_NODES
155 and MPOL_F_RELATIVE_NODES.
156
157 EIO MPOL_MF_STRICT was specified and an existing page was already on
158 a node that does not follow the policy; or MPOL_MF_MOVE or
159 MPOL_MF_MOVE_ALL was specified and the kernel was unable to move
160 all existing pages in the range.
161
162 ENOMEM Insufficient kernel memory was available.
163
164 EPERM The flags argument included the MPOL_MF_MOVE_ALL flag and the
165 caller does not have the CAP_SYS_NICE privilege.
166
168 The mbind() system call was added to the Linux kernel in version 2.6.7.
169
171 This system call is Linux-specific.
172
174 For information on library support, see numa(7).
175
176 NUMA policy is not supported on a memory mapped file range that was
177 mapped with the MAP_SHARED flag.
178
179 The MPOL_DEFAULT mode can have different effects for mbind() and
180 set_mempolicy(2). When MPOL_DEFAULT is specified for set_mempolicy(2),
181 the process's policy reverts to system default policy or local alloca‐
182 tion. When MPOL_DEFAULT is specified for a range of memory using
183 mbind(), any pages subsequently allocated for that range will use the
184 process's policy, as set by set_mempolicy(2). This effectively removes
185 the explicit policy from the specified range, "falling back" to a pos‐
186 sibly nondefault policy. To select explicit "local allocation" for a
187 memory range, specify a mode of MPOL_PREFERRED with an empty set of
188 nodes. This method will work for set_mempolicy(2), as well.
189
190 Support for huge page policy was added with 2.6.16. For interleave
191 policy to be effective on huge page mappings the policied memory needs
192 to be tens of megabytes or larger.
193
194 MPOL_MF_STRICT is ignored on huge page mappings.
195
196 MPOL_MF_MOVE and MPOL_MF_MOVE_ALL are only available on Linux 2.6.16
197 and later.
198
200 get_mempolicy(2), getcpu(2), mmap(2), set_mempolicy(2), shmat(2),
201 shmget(2), numa(3), cpuset(7), numa(7), numactl(8)
202
204 This page is part of release 3.25 of the Linux man-pages project. A
205 description of the project, and information about reporting bugs, can
206 be found at http://www.kernel.org/doc/man-pages/.
207
208
209
210Linux 2008-08-15 MBIND(2)