1 .\" Copyright 2003,2004 Andi Kleen, SuSE Labs.
2 .\" and Copyright 2007 Lee Schermerhorn, Hewlett Packard
4 .\" %%%LICENSE_START(VERBATIM_PROF)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.
19 .\" Formatted or processed versions of this manual, if unaccompanied by
20 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" 2006-02-03, mtk, substantial wording changes and other improvements
24 .\" 2007-08-27, Lee Schermerhorn <Lee.Schermerhorn@hp.com>
25 .\" more precise specification of behavior.
28 .\" Linux 3.8 added MPOL_MF_LAZY, which needs to be documented.
29 .\" Does it also apply for move_pages()?
31 .\" commit b24f53a0bea38b266d219ee651b22dba727c44ae
32 .\" Author: Lee Schermerhorn <lee.schermerhorn@hp.com>
33 .\" Date: Thu Oct 25 14:16:32 2012 +0200
35 .TH MBIND 2 2021-03-22 Linux "Linux Programmer's Manual"
37 mbind \- set memory policy for a memory range
39 NUMA (Non-Uniform Memory Access) policy library
40 .RI ( libnuma ", " \-lnuma )
43 .B "#include <numaif.h>"
45 .BI "long mbind(void *" addr ", unsigned long " len ", int " mode ,
46 .BI " const unsigned long *" nodemask ", unsigned long " maxnode ,
47 .BI " unsigned int " flags );
51 sets the NUMA memory policy,
52 which consists of a policy mode and zero or more nodes,
53 for the memory range starting with
58 The memory policy defines from which node memory is allocated.
60 If the memory range specified by the
62 arguments includes an "anonymous" region of memory\(emthat is
63 a region of memory created using the
66 .BR MAP_ANONYMOUS \(emor
67 a memory-mapped file, mapped using the
71 flag, pages will be allocated only according to the specified
72 policy when the application writes (stores) to the page.
73 For anonymous regions, an initial read access will use a shared
74 page in the kernel containing all zeros.
75 For a file mapped with
77 an initial read access will allocate pages according to the
78 memory policy of the thread that causes the page to be allocated.
79 This may not be the thread that called
82 The specified policy will be ignored for any
84 mappings in the specified memory range.
85 Rather the pages will be allocated according to the memory policy
86 of the thread that caused the page to be allocated.
87 Again, this may not be the thread that called
90 If the specified memory range includes a shared memory region
93 system call and attached using the
96 pages allocated for the anonymous or shared memory region will
97 be allocated according to the policy specified, regardless of which
98 process attached to the shared memory segment causes the allocation.
99 If, however, the shared memory region was created with the
102 the huge pages will be allocated according to the policy specified
103 only if the page allocation is caused by the process that calls
109 has an effect only for new allocations; if the pages inside
110 the range have been already touched before setting the policy,
111 then the policy has no effect.
112 This default behavior may be overridden by the
116 flags described below.
120 argument must specify one of
123 .BR MPOL_INTERLEAVE ,
127 (which are described in detail below).
128 All policy modes except
130 require the caller to specify the node or nodes to which the mode applies,
137 argument may also include an optional
143 .BR MPOL_F_STATIC_NODES " (since Linux-2.6.26)"
146 specifies physical node IDs.
147 Linux does not remap the
149 when the thread moves to a different cpuset context,
150 nor when the set of nodes allowed by the thread's
151 current cpuset context changes.
153 .BR MPOL_F_RELATIVE_NODES " (since Linux-2.6.26)"
156 specifies node IDs that are relative to the set of
157 node IDs allowed by the thread's current cpuset.
160 points to a bit mask of nodes containing up to
163 The bit mask size is rounded to the next multiple of
164 .IR "sizeof(unsigned long)" ,
165 but the kernel will use bits only up to
171 value of zero specifies the empty set of nodes.
180 is required, it must contain at least one node that is on-line,
181 allowed by the thread's current cpuset context
183 .B MPOL_F_STATIC_NODES
184 mode flag is specified),
189 argument must include one of the following values:
192 This mode requests that any nondefault policy be removed,
193 restoring default behavior.
194 When applied to a range of memory via
196 this means to use the thread memory policy,
197 which may have been set with
198 .BR set_mempolicy (2).
199 If the mode of the thread memory policy is also
201 the system-wide default policy will be used.
202 The system-wide default policy allocates
203 pages on the node of the CPU that triggers the allocation.
210 arguments must be specify the empty set of nodes.
213 This mode specifies a strict policy that restricts memory allocation to
214 the nodes specified in
218 specifies more than one node, page allocations will come from
219 the node with sufficient free memory that is closest to
220 the node where the allocation takes place.
221 Pages will not be allocated from any node not specified in the
223 (Before Linux 2.6.26,
224 .\" commit 19770b32609b6bf97a3dece2529089494cbfc549
225 page allocations came from
226 the node with the lowest numeric node ID first, until that node
227 contained no free memory.
228 Allocations then came from the node with the next highest
231 and so forth, until none of the specified nodes contained free memory.)
234 This mode specifies that page allocations be interleaved across the
235 set of nodes specified in
237 This optimizes for bandwidth instead of latency
238 by spreading out pages and memory accesses to those pages across
240 To be effective the memory area should be fairly large,
241 at least 1\ MB or bigger with a fairly uniform access pattern.
242 Accesses to a single page of the area will still be limited to
243 the memory bandwidth of a single node.
246 This mode sets the preferred node for allocation.
247 The kernel will try to allocate pages from this
248 node first and fall back to other nodes if the
249 preferred nodes is low on free memory.
252 specifies more than one node ID, the first node in the
253 mask will be selected as the preferred node.
258 arguments specify the empty set, then the memory is allocated on
259 the node of the CPU that triggered the allocation.
261 .BR MPOL_LOCAL " (since Linux 3.8)"
262 .\" commit 479e2802d09f1e18a97262c4c6f8f17ae5884bd8
263 .\" commit f2a07f40dbc603c15f8b06e6ec7f768af67b424f
264 This mode specifies "local allocation"; the memory is allocated on
265 the node of the CPU that triggered the allocation (the "local node").
270 arguments must specify the empty set.
271 If the "local node" is low on free memory,
272 the kernel will try to allocate memory from other nodes.
273 The kernel will allocate memory from the "local node"
274 whenever memory for this node is available.
275 If the "local node" is not allowed by the thread's current cpuset context,
276 the kernel will try to allocate memory from other nodes.
277 The kernel will allocate memory from the "local node" whenever
278 it becomes allowed by the thread's current cpuset context.
281 reverts to the memory policy of the thread (which may be set via
282 .BR set_mempolicy (2));
283 that policy may be something other than "local allocation".
293 then the call fails with the error
295 if the existing pages in the memory range don't follow the policy.
296 .\" According to the kernel code, the following is not true
297 .\" --Lee Schermerhorn
298 .\" In 2.6.16 or later the kernel will also try to move pages
299 .\" to the requested node with this flag.
305 then the kernel will attempt to move all the existing pages
306 in the memory range so that they follow the policy.
307 Pages that are shared with other processes will not be moved.
310 is also specified, then the call fails with the error
312 if some pages could not be moved.
318 then the kernel will attempt to move all existing pages in the memory range
319 regardless of whether other processes use the pages.
320 The calling thread must be privileged
325 is also specified, then the call fails with the error
327 if some pages could not be moved.
328 .\" ---------------------------------------------------------------
333 on error, \-1 is returned and
335 is set to indicate the error.
336 .\" ---------------------------------------------------------------
338 .\" I think I got all of the error returns. --Lee Schermerhorn
341 Part or all of the memory range specified by
345 points outside your accessible address space.
346 Or, there was an unmapped hole in the specified memory range specified by
352 An invalid value was specified for
362 is not a multiple of the system page size.
369 specified a nonempty set;
381 exceeds a kernel-imposed limit.
382 .\" As at 2.6.23, this limit is "a page worth of bits", e.g.,
383 .\" 8 * 4096 bits, assuming a 4kB page size.
386 specifies one or more node IDs that are
387 greater than the maximum supported node ID.
388 Or, none of the node IDs specified by
390 are on-line and allowed by the thread's current cpuset context,
391 or none of the specified nodes contain memory.
394 argument specified both
395 .B MPOL_F_STATIC_NODES
397 .BR MPOL_F_RELATIVE_NODES .
401 was specified and an existing page was already on a node
402 that does not follow the policy;
407 was specified and the kernel was unable to move all existing
411 Insufficient kernel memory was available.
416 argument included the
418 flag and the caller does not have the
421 .\" ---------------------------------------------------------------
425 system call was added to the Linux kernel in version 2.6.7.
427 This system call is Linux-specific.
429 For information on library support, see
432 NUMA policy is not supported on a memory-mapped file range
433 that was mapped with the
439 mode can have different effects for
442 .BR set_mempolicy (2).
446 .BR set_mempolicy (2),
447 the thread's memory policy reverts to the system default policy
451 is specified for a range of memory using
453 any pages subsequently allocated for that range will use
454 the thread's memory policy, as set by
455 .BR set_mempolicy (2).
456 This effectively removes the explicit policy from the
457 specified range, "falling back" to a possibly nondefault
459 To select explicit "local allocation" for a memory range,
466 with an empty set of nodes.
467 This method will work for
468 .BR set_mempolicy (2),
471 Support for huge page policy was added with 2.6.16.
472 For interleave policy to be effective on huge page mappings the
473 policied memory needs to be tens of megabytes or larger.
476 .\" commit dcf1763546d76c372f3136c8d6b2b6e77f140cf0
478 was ignored on huge page mappings.
483 are available only on Linux 2.6.16 and later.
485 .BR get_mempolicy (2),
488 .BR set_mempolicy (2),