fanotify_mark.2: ERRORS: add missing EBADF error for invalid 'dirfd'
[man-pages.git] / man2 / set_mempolicy.2
blob36e92742d17767134108ac29954f5594f0b6bc9a
1 .\" Copyright 2003,2004 Andi Kleen, SuSE Labs.
2 .\" and Copyright 2007 Lee Schermerhorn, Hewlett Packard
3 .\"
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.
8 .\"
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.
13 .\"
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.
18 .\"
19 .\" Formatted or processed versions of this manual, if unaccompanied by
20 .\" the source, must acknowledge the copyright and authors of this work.
21 .\" %%%LICENSE_END
22 .\"
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.
26 .\"
27 .TH SET_MEMPOLICY 2 2021-06-20 Linux "Linux Programmer's Manual"
28 .SH NAME
29 set_mempolicy \- set default NUMA memory policy for a thread and its children
30 .SH SYNOPSIS
31 .nf
32 .B "#include <numaif.h>"
33 .PP
34 .BI "long set_mempolicy(int " mode ", const unsigned long *" nodemask ,
35 .BI "                   unsigned long " maxnode );
36 .PP
37 Link with \fI\-lnuma\fP.
38 .fi
39 .SH DESCRIPTION
40 .BR set_mempolicy ()
41 sets the NUMA memory policy of the calling thread,
42 which consists of a policy mode and zero or more nodes,
43 to the values specified by the
44 .IR mode ,
45 .IR nodemask ,
46 and
47 .I maxnode
48 arguments.
49 .PP
50 A NUMA machine has different
51 memory controllers with different distances to specific CPUs.
52 The memory policy defines from which node memory is allocated for
53 the thread.
54 .PP
55 This system call defines the default policy for the thread.
56 The thread policy governs allocation of pages in the process's
57 address space outside of memory ranges
58 controlled by a more specific policy set by
59 .BR mbind (2).
60 The thread default policy also controls allocation of any pages for
61 memory-mapped files mapped using the
62 .BR mmap (2)
63 call with the
64 .B MAP_PRIVATE
65 flag and that are only read (loaded) from by the thread
66 and of memory-mapped files mapped using the
67 .BR mmap (2)
68 call with the
69 .B MAP_SHARED
70 flag, regardless of the access type.
71 The policy is applied only when a new page is allocated
72 for the thread.
73 For anonymous memory this is when the page is first
74 touched by the thread.
75 .PP
76 The
77 .I mode
78 argument must specify one of
79 .BR MPOL_DEFAULT ,
80 .BR MPOL_BIND ,
81 .BR MPOL_INTERLEAVE ,
82 .BR MPOL_PREFERRED ,
84 .BR MPOL_LOCAL
85 (which are described in detail below).
86 All modes except
87 .B MPOL_DEFAULT
88 require the caller to specify the node or nodes to which the mode applies,
89 via the
90 .I nodemask
91 argument.
92 .PP
93 The
94 .I mode
95 argument may also include an optional
96 .IR "mode flag" .
97 The supported
98 .I "mode flags"
99 are:
101 .BR MPOL_F_NUMA_BALANCING " (since Linux 5.12)"
102 .\" commit bda420b985054a3badafef23807c4b4fa38a3dff
103 When
104 .I mode
106 .BR MPOL_BIND ,
107 enable the kernel NUMA balancing for the task if it is supported by the kernel.
108 If the flag isn't supported by the kernel, or is used with
109 .I mode
110 other than
111 .BR MPOL_BIND ,
112 \-1 is returned and
113 .I errno
114 is set to
115 .BR EINVAL .
117 .BR MPOL_F_RELATIVE_NODES " (since Linux 2.6.26)"
118 A nonempty
119 .I nodemask
120 specifies node IDs that are relative to the
121 set of node IDs allowed by the process's current cpuset.
123 .BR MPOL_F_STATIC_NODES " (since Linux 2.6.26)"
124 A nonempty
125 .I nodemask
126 specifies physical node IDs.
127 Linux will not remap the
128 .I nodemask
129 when the process moves to a different cpuset context,
130 nor when the set of nodes allowed by the process's
131 current cpuset context changes.
133 .I nodemask
134 points to a bit mask of node IDs that contains up to
135 .I maxnode
136 bits.
137 The bit mask size is rounded to the next multiple of
138 .IR "sizeof(unsigned long)" ,
139 but the kernel will use bits only up to
140 .IR maxnode .
141 A NULL value of
142 .I nodemask
143 or a
144 .I maxnode
145 value of zero specifies the empty set of nodes.
146 If the value of
147 .I maxnode
148 is zero,
150 .I nodemask
151 argument is ignored.
153 Where a
154 .I nodemask
155 is required, it must contain at least one node that is on-line,
156 allowed by the process's current cpuset context,
157 (unless the
158 .B MPOL_F_STATIC_NODES
159 mode flag is specified),
160 and contains memory.
161 If the
162 .B MPOL_F_STATIC_NODES
163 is set in
164 .I mode
165 and a required
166 .I nodemask
167 contains no nodes that are allowed by the process's current cpuset context,
168 the memory policy reverts to
169 .IR "local allocation" .
170 This effectively overrides the specified policy until the process's
171 cpuset context includes one or more of the nodes specified by
172 .IR nodemask .
175 .I mode
176 argument must include one of the following values:
178 .B MPOL_DEFAULT
179 This mode specifies that any nondefault thread memory policy be removed,
180 so that the memory policy "falls back" to the system default policy.
181 The system default policy is "local allocation"\(emthat is,
182 allocate memory on the node of the CPU that triggered the allocation.
183 .I nodemask
184 must be specified as NULL.
185 If the "local node" contains no free memory, the system will
186 attempt to allocate memory from a "near by" node.
188 .B MPOL_BIND
189 This mode defines a strict policy that restricts memory allocation to the
190 nodes specified in
191 .IR nodemask .
193 .I nodemask
194 specifies more than one node, page allocations will come from
195 the node with the lowest numeric node ID first, until that node
196 contains no free memory.
197 Allocations will then come from the node with the next highest
198 node ID specified in
199 .I nodemask
200 and so forth, until none of the specified nodes contain free memory.
201 Pages will not be allocated from any node not specified in the
202 .IR nodemask .
204 .B MPOL_INTERLEAVE
205 This mode interleaves page allocations across the nodes specified in
206 .I nodemask
207 in numeric node ID order.
208 This optimizes for bandwidth instead of latency
209 by spreading out pages and memory accesses to those pages across
210 multiple nodes.
211 However, accesses to a single page will still be limited to
212 the memory bandwidth of a single node.
213 .\" NOTE:  the following sentence doesn't make sense in the context
214 .\" of set_mempolicy() -- no memory area specified.
215 .\" To be effective the memory area should be fairly large,
216 .\" at least 1 MB or bigger.
218 .B MPOL_PREFERRED
219 This mode sets the preferred node for allocation.
220 The kernel will try to allocate pages from this node first
221 and fall back to "near by" nodes if the preferred node is low on free
222 memory.
224 .I nodemask
225 specifies more than one node ID, the first node in the
226 mask will be selected as the preferred node.
227 If the
228 .I nodemask
230 .I maxnode
231 arguments specify the empty set, then the policy
232 specifies "local allocation"
233 (like the system default policy discussed above).
235 .BR MPOL_LOCAL " (since Linux 3.8)"
236 .\" commit 479e2802d09f1e18a97262c4c6f8f17ae5884bd8
237 .\" commit f2a07f40dbc603c15f8b06e6ec7f768af67b424f
238 This mode specifies "local allocation"; the memory is allocated on
239 the node of the CPU that triggered the allocation (the "local node").
241 .I nodemask
243 .I maxnode
244 arguments must specify the empty set.
245 If the "local node" is low on free memory,
246 the kernel will try to allocate memory from other nodes.
247 The kernel will allocate memory from the "local node"
248 whenever memory for this node is available.
249 If the "local node" is not allowed by the process's current cpuset context,
250 the kernel will try to allocate memory from other nodes.
251 The kernel will allocate memory from the "local node" whenever
252 it becomes allowed by the process's current cpuset context.
254 The thread memory policy is preserved across an
255 .BR execve (2),
256 and is inherited by child threads created using
257 .BR fork (2)
259 .BR clone (2).
260 .SH RETURN VALUE
261 On success,
262 .BR set_mempolicy ()
263 returns 0;
264 on error, \-1 is returned and
265 .I errno
266 is set to indicate the error.
267 .SH ERRORS
269 .B EFAULT
270 Part of all of the memory range specified by
271 .I nodemask
273 .I maxnode
274 points outside your accessible address space.
276 .B EINVAL
277 .I mode
278 is invalid.
280 .I mode
282 .B MPOL_DEFAULT
284 .I nodemask
285 is nonempty,
287 .I mode
289 .B MPOL_BIND
291 .B MPOL_INTERLEAVE
293 .I nodemask
294 is empty.
296 .I maxnode
297 specifies more than a page worth of bits.
299 .I nodemask
300 specifies one or more node IDs that are
301 greater than the maximum supported node ID.
302 Or, none of the node IDs specified by
303 .I nodemask
304 are on-line and allowed by the process's current cpuset context,
305 or none of the specified nodes contain memory.
306 Or, the
307 .I mode
308 argument specified both
309 .B MPOL_F_STATIC_NODES
311 .BR MPOL_F_RELATIVE_NODES .
312 Or, the
313 .B MPOL_F_NUMA_BALANCING
314 isn't supported by the kernel, or is used with
315 .I mode
316 other than
317 .BR MPOL_BIND .
319 .B ENOMEM
320 Insufficient kernel memory was available.
321 .SH VERSIONS
323 .BR set_mempolicy ()
324 system call was added to the Linux kernel in version 2.6.7.
325 .SH CONFORMING TO
326 This system call is Linux-specific.
327 .SH NOTES
328 Memory policy is not remembered if the page is swapped out.
329 When such a page is paged back in, it will use the policy of
330 the thread or memory range that is in effect at the time the
331 page is allocated.
333 For information on library support, see
334 .BR numa (7).
335 .SH SEE ALSO
336 .BR get_mempolicy (2),
337 .BR getcpu (2),
338 .BR mbind (2),
339 .BR mmap (2),
340 .BR numa (3),
341 .BR cpuset (7),
342 .BR numa (7),
343 .BR numactl (8)