Lines Matching full:the

10 In the Linux kernel, "memory policy" determines from which node the kernel will
13 The current memory policy support was added to Linux 2.6 around May 2004. This
14 document attempts to describe the concepts and APIs of the 2.6 memory policy
19 which is an administrative mechanism for restricting the nodes from which
22 both cpusets and policies are applied to a task, the restrictions of the cpuset
32 The Linux kernel supports _scopes_ of memory policy, described here from
36 this policy is "hard coded" into the kernel. It is the policy
38 one of the more specific policy scopes discussed below. When
39 the system is "up and running", the system default policy will
41 up, the system default policy will be set to interleave
43 not to overload the initial boot node with boot-time
49 by or on behalf of the task that aren't controlled by a more
51 all page allocations that would have been controlled by the
52 task policy "fall back" to the System Default Policy.
54 The task policy applies to the entire address space of a task. Thus,
56 [clone() w/o the CLONE_VM flag] and exec*(). This allows a parent task
57 to establish the task policy for a child task exec()'d from an
58 executable image that has no awareness of memory policy. See the
60 below, for an overview of the system call
63 In a multi-threaded task, task policies apply only to the thread
64 [Linux kernel task] that installs the policy and any threads
66 at the time a new task policy is installed retain their current
69 A task policy applies only to pages allocated after the policy is
70 installed. Any pages already faulted in by the task when the task
72 the policy at the time they were allocated.
79 of its virtual address space. See the
81 below, for an overview of the mbind() system call used to set a VMA
84 A VMA policy will govern the allocation of pages that back
85 this region of the address space. Any regions of the task's
87 back to the task policy, which may itself fall back to the
93 pages allocated for anonymous segments, such as the task
94 stack and heap, and any regions of the address space
95 mmap()ed with the MAP_ANONYMOUS flag. If a VMA policy is
96 applied to a file mapping, it will be ignored if the mapping
97 used the MAP_SHARED flag. If the file mapping used the
98 MAP_PRIVATE flag, the VMA policy will only be applied when
99 an anonymous page is allocated on an attempt to write to the
104 the policy is installed; and they are inherited across
106 region of a task's address space, and because the address
113 the existing virtual memory area into 2 or 3 VMAs, each with
117 the policy is installed. Any pages already faulted into the
118 VMA range remain where they were allocated based on the
119 policy at the time they were allocated. However, since
120 2.6.16, Linux supports page migration via the mbind() system
127 application installs shared policies the same way as VMA
128 policies--using the mbind() system call specifying a range of
129 virtual addresses that map the shared object. However, unlike
132 directly to the shared object. Thus, all tasks that attach to
133 the object share the policy, and all pages allocated for the
134 shared object, by any task, will obey the shared policy.
138 policy support was added to Linux, the associated data structures were
139 added to hugetlbfs shmem segments. At the time, hugetlbfs did not
141 shmem segments were never "hooked up" to the shared policy support.
147 with MAP_SHARED ignore any VMA policy installed on the virtual
148 address range backed by the shared file mapping. Rather,
150 mappings that have not yet been written by the task, follow
153 The shared policy infrastructure supports different policies on subset
154 ranges of the shared object. However, Linux still splits the VMA of
155 the task that installs the policy for each range of distinct policy.
158 can be seen by examining the /proc/<pid>/numa_maps of tasks sharing
160 one or more ranges of the region.
166 an optional set of nodes. The mode determines the behavior of the
167 policy, the optional mode flags determine the behavior of the mode,
168 and the optional set of nodes can be viewed as the arguments to the
173 discussed in context, below, as required to explain the behavior.
175 NUMA memory policy supports the following 4 behavioral modes:
178 This mode is only used in the memory policy APIs. Internally,
179 MPOL_DEFAULT is converted to the NULL memory policy in all
182 MPOL_DEFAULT means "fall back to the next most specific policy
185 For example, a NULL or default task policy will fall back to the
187 back to the task policy.
189 When specified in one of the memory policy APIs, the Default mode
190 does not use the optional set of nodes.
192 It is an error for the set of nodes specified for this policy to
196 This mode specifies that memory must come from the set of
197 nodes specified by the policy. Memory will be allocated from
198 the node in the set with sufficient free memory that is
199 closest to the node where the allocation takes place.
202 This mode specifies that the allocation should be attempted
203 from the single node specified in the policy. If that
204 allocation fails, the kernel will search other nodes, in order
205 of increasing distance from the preferred node based on
206 information provided by the platform firmware.
208 Internally, the Preferred policy uses a single node--the
209 preferred_node member of struct mempolicy. When the internal
210 mode flag MPOL_F_LOCAL is set, the preferred_node is ignored
211 and the policy is interpreted as local allocation. "Local"
213 starts at the node containing the cpu where the allocation
216 It is possible for the user to specify that local allocation
218 mode. If an empty nodemask is passed, the policy cannot use
219 the MPOL_F_STATIC_NODES or MPOL_F_RELATIVE_NODES flags
224 page granularity, across the nodes specified in the policy.
225 This mode also behaves slightly differently, based on the
229 Interleave mode indexes the set of nodes specified by the
230 policy using the page offset of the faulting address into the
231 segment [VMA] containing the address modulo the number of
232 nodes specified by the policy. It then attempts to allocate a
233 page, starting at the selected node, as if the node had been
235 local allocation. That is, allocation will follow the per
239 the set of nodes specified by the policy using a node counter
240 maintained per task. This counter wraps around to the lowest
241 specified node after it reaches the highest specified node.
242 This will tend to spread the pages out over the nodes
243 specified by the policy based on the order in which they are
245 address range or file. During system boot up, the temporary
249 This mode specifices that the allocation should be preferrably
250 satisfied from the nodemask specified in the policy. If there is
251 a memory pressure on all nodes in the nodemask, the allocation
255 NUMA memory policy supports the following optional mode flags:
258 This flag specifies that the nodemask passed by
259 the user should not be remapped if the task or VMA's set of allowed
260 nodes changes after the memory policy has been defined.
263 change in the set of allowed nodes, the preferred nodemask (Preferred
265 remapped to the new set of allowed nodes. This may result in nodes
268 With this flag, if the user-specified nodes overlap with the
269 nodes allowed by the task's cpuset, then the memory policy is
270 applied to their intersection. If the two sets of nodes do not
271 overlap, the Default policy is used.
274 mems 1-3 that sets an Interleave policy over the same set. If
275 the cpuset's mems change to 3-5, the Interleave will now occur
277 3 is allowed from the user's nodemask, the "interleave" only
278 occurs over that node. If no nodes from the user's nodemask are
279 now allowed, the Default behavior is used.
281 MPOL_F_STATIC_NODES cannot be combined with the
287 This flag specifies that the nodemask passed
288 by the user will be mapped relative to the set of the task or VMA's
289 set of allowed nodes. The kernel stores the user-passed nodemask,
290 and if the allowed nodes changes, then that original nodemask will
291 be remapped relative to the new set of allowed nodes.
294 mempolicy is rebound because of a change in the set of allowed
295 nodes, the node (Preferred) or nodemask (Bind, Interleave) is
296 remapped to the new set of allowed nodes. That remap may not
297 preserve the relative nature of the user's passed nodemask to its
299 1,3,5 may be remapped to 7-9 and then to 1-3 if the set of
302 With this flag, the remap is done so that the node numbers from
303 the user's passed nodemask are relative to the set of allowed
304 nodes. In other words, if nodes 0, 2, and 4 are set in the user's
305 nodemask, the policy will be effected over the first (and in the
306 Bind or Interleave case, the third and fifth) nodes in the set of
307 allowed nodes. The nodemask passed by the user represents nodes
310 If the user's nodemask includes nodes that are outside the range
311 of the new set of allowed nodes (for example, node 5 is set in
312 the user's nodemask when the set of allowed nodes is only 0-3),
313 then the remap wraps around to the beginning of the nodemask and,
314 if not already set, sets the node in the mempolicy nodemask.
317 mems 2-5 that sets an Interleave policy over the same set with
318 MPOL_F_RELATIVE_NODES. If the cpuset's mems change to 3-7, the
319 interleave now occurs over nodes 3,5-7. If the cpuset's mems
320 then change to 0,2-3,5, then the interleave occurs over nodes
323 Thanks to the consistent remapping, applications preparing
326 and prepare the nodemask as if they were always located on
327 memory nodes 0 to N-1, where N is the number of memory nodes the
328 policy is intended to manage. Let the kernel then remap to the
329 set of memory nodes allowed by the task's cpuset, as that may
332 MPOL_F_RELATIVE_NODES cannot be combined with the
343 the structure back to the mempolicy kmem cache when the reference count
347 to '1', representing the reference held by the task that is installing the
349 structure, another reference is added, as the task's reference will be dropped
350 on completion of the policy installation.
352 During run-time "usage" of the policy, we attempt to minimize atomic operations
353 on the reference count, as this can lead to cache lines bouncing between cpus
354 and NUMA nodes. "Usage" here means one of the following:
356 1) querying of the policy, either by the task itself [using the get_mempolicy()
357 API discussed below] or by another task using the /proc/<pid>/numa_maps
360 2) examination of the policy to determine the policy mode and associated node
362 path". Note that for MPOL_BIND, the "usage" extends across the entire
363 allocation process, which may sleep during page reclaimation, because the
366 We can avoid taking an extra reference during the usages listed above as
369 1) we never need to get/free the system default policy as this is never
370 changed nor freed, once the system is up and running.
372 2) for querying the policy, we do not need to take an extra reference on the
373 target task's task policy nor vma policies because we always acquire the
374 task's mm's mmap_lock for read during the query. The set_mempolicy() and
375 mbind() APIs [see below] always acquire the mmap_lock for write when
380 3) Page allocation usage of task or vma policy occurs in the fault path where
381 we hold them mmap_lock for read. Again, because replacing the task or vma
382 policy requires that the mmap_lock be held for write, the policy can't be
387 querying or allocating a page based on the policy. To resolve this
388 potential race, the shared policy infrastructure adds an extra reference
389 to the shared policy during lookup while holding a spin lock on the shared
391 reference when we're finished "using" the policy. We must drop the
392 extra reference on shared policies in the same query/allocation paths
394 as such, and the extra reference is dropped "conditionally"--i.e., only
399 more expensive to use in the page allocation path. This is especially
403 or by prefaulting the entire shared memory region into memory and locking
412 always affect only the calling task, the calling task's address space, or
413 some shared object mapped into the calling task's address space.
416 the headers that define these APIs and the parameter data types for
417 user space applications reside in a package that is not part of the
418 Linux kernel. The kernel system call interfaces, with the 'sys\_'
419 prefix, are defined in <linux/syscalls.h>; the mode and flag
427 Set's the calling task's "task/process memory policy" to mode
428 specified by the 'mode' argument and the set of nodes defined by
430 'maxnode' ids. Optional mode flags may be passed by combining the
431 'mode' argument with the flag (for example: MPOL_INTERLEAVE |
434 See the set_mempolicy(2) man page for more details
443 Queries the "task/process memory policy" of the calling task, or the
444 policy or location of a specified virtual address, depending on the
447 See the get_mempolicy(2) man page for more details
456 mbind() installs the policy specified by (mode, nmask, maxnodes) as a
457 VMA policy for the range of the calling task's address space specified
458 by the 'start' and 'len' arguments. Additional actions may be
459 requested via the 'flags' argument.
461 See the mbind(2) man page for more details.
469 sys_set_mempolicy_home_node set the home node for a VMA policy present in the
470 task's address range. The system call updates the home node only for the existing
471 mempolicy range. Other address ranges are ignored. A home node is the NUMA node
472 closest to which page allocation will come from. Specifying the home node override
473 the default allocation policy to allocate memory close to the local node for an
480 Although not strictly part of the Linux implementation of memory policy,
483 + set the task policy for a specified program via set_mempolicy(2), fork(2) and
486 + set the shared policy for a shared memory segment via mbind(2)
488 The numactl(8) tool is packaged with the run-time version of the library
489 containing the memory policy system call wrappers. Some distributions
490 package the headers and compile-time libraries in a separate development
499 that require a node or set of nodes, the nodes are restricted to the set of
500 nodes whose memories are allowed by the cpuset constraints. If the nodemask
501 specified for the policy contains nodes that are not allowed by the cpuset and
502 MPOL_F_RELATIVE_NODES is not used, the intersection of the set of nodes
503 specified for the policy and the set of nodes with memory is used. If the
504 result is the empty set, the policy is considered invalid and cannot be
505 installed. If MPOL_F_RELATIVE_NODES is used, the policy's nodes are mapped
506 onto and folded into the task's set of allowed nodes as previously described.
508 The interaction of memory policies and cpusets can be problematic when tasks
510 created by shmget() of mmap() with the MAP_ANONYMOUS and MAP_SHARED flags, and
511 any of the tasks install shared policy on the region, only nodes whose
512 memories are allowed in both cpusets may be used in the policies. Obtaining
513 this information requires "stepping outside" the memory policy APIs to use the
515 be attaching to the shared region. Furthermore, if the cpusets' allowed
516 memory sets are disjoint, "local" allocation is the only valid policy.