kernel:memory_policy
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
kernel:memory_policy [2017/04/05 12:57] – peter | kernel:memory_policy [2019/11/30 12:38] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Kernel - Memory Policy ====== | ||
- | What is Linux Memory Policy? | ||
- | |||
- | In the Linux kernel, " | ||
- | |||
- | <WRAP notice> | ||
- | Memory policies should not be confused with [[Kernel: | ||
- | |||
- | |||
- | ===== Memory Policy Concerpts ===== | ||
- | |||
- | ==== Scope of Memory Policies ==== | ||
- | |||
- | The Linux kernel supports _scopes_ of memory policy, described here from most general to most specific: | ||
- | |||
- | * **System Default Policy**: | ||
- | |||
- | * **Task/ | ||
- | |||
- | * The task policy applies to the entire address space of a task. Thus, it is inheritable, | ||
- | |||
- | * In a multi-threaded task, task policies apply only to the thread [Linux kernel task] that installs the policy and any threads subsequently created by that thread. | ||
- | |||
- | * A task policy applies only to pages allocated after the policy is installed. | ||
- | |||
- | * **VMA Policy**: | ||
- | |||
- | * A VMA policy will govern the allocation of pages that back this region of the address space. | ||
- | |||
- | * VMA policies have a few complicating details: | ||
- | |||
- | * VMA policy applies ONLY to anonymous pages. | ||
- | |||
- | * VMA policies are shared between all tasks that share a virtual address space--a.k.a. threads--independent of when the policy is installed; and they are inherited across fork(). | ||
- | |||
- | * A task may install a new VMA policy on a sub-range of a previously mmap()ed region. | ||
- | |||
- | * By default, VMA policy applies only to pages allocated after the policy is installed. | ||
- | |||
- | * **Shared Policy**: | ||
- | |||
- | * As of 2.6.22, only shared memory segments, created by shmget() or mmap(MAP_ANONYMOUS|MAP_SHARED), | ||
- | |||
- | * As mentioned above [re: VMA policies], allocations of page cache pages for regular files mmap()ed with MAP_SHARED ignore any VMA policy installed on the virtual address range backed by the shared file mapping. | ||
- | |||
- | * The shared policy infrastructure supports different policies on subset ranges of the shared object. | ||
- | |||
- | |||
- | ==== Components of Memory Policies ==== | ||
- | |||
- | A Linux memory policy consists of a " | ||
- | |||
- | Internally, memory policies are implemented by a reference counted structure, struct mempolicy. | ||
- | |||
- | Linux memory policy supports the following 4 behavioral modes: | ||
- | |||
- | Default Mode--MPOL_DEFAULT: | ||
- | policy APIs. Internally, MPOL_DEFAULT is converted to the NULL | ||
- | memory policy in all policy scopes. | ||
- | will simply be removed when MPOL_DEFAULT is specified. | ||
- | MPOL_DEFAULT means "fall back to the next most specific policy scope." | ||
- | |||
- | For example, a NULL or default task policy will fall back to the | ||
- | system default policy. | ||
- | back to the task policy. | ||
- | |||
- | When specified in one of the memory policy APIs, the Default mode | ||
- | does not use the optional set of nodes. | ||
- | |||
- | It is an error for the set of nodes specified for this policy to | ||
- | be non-empty. | ||
- | |||
- | MPOL_BIND: | ||
- | set of nodes specified by the policy. | ||
- | the node in the set with sufficient free memory that is closest to | ||
- | the node where the allocation takes place. | ||
- | |||
- | MPOL_PREFERRED: | ||
- | attempted from the single node specified in the policy. | ||
- | allocation fails, the kernel will search other nodes, in order of | ||
- | increasing distance from the preferred node based on information | ||
- | provided by the platform firmware. | ||
- | containing the cpu where the allocation takes place. | ||
- | |||
- | Internally, the Preferred policy uses a single node--the | ||
- | preferred_node member of struct mempolicy. | ||
- | mode flag MPOL_F_LOCAL is set, the preferred_node is ignored and | ||
- | the policy is interpreted as local allocation. | ||
- | policy can be viewed as a Preferred policy that starts at the node | ||
- | containing the cpu where the allocation takes place. | ||
- | |||
- | It is possible for the user to specify that local allocation is | ||
- | always preferred by passing an empty nodemask with this mode. | ||
- | If an empty nodemask is passed, the policy cannot use the | ||
- | MPOL_F_STATIC_NODES or MPOL_F_RELATIVE_NODES flags described | ||
- | below. | ||
- | |||
- | MPOL_INTERLEAVED: | ||
- | interleaved, | ||
- | the policy. | ||
- | the context where it is used: | ||
- | |||
- | For allocation of anonymous pages and shared memory pages, | ||
- | Interleave mode indexes the set of nodes specified by the policy | ||
- | using the page offset of the faulting address into the segment | ||
- | [VMA] containing the address modulo the number of nodes specified | ||
- | by the policy. | ||
- | the selected node, as if the node had been specified by a Preferred | ||
- | policy or had been selected by a local allocation. | ||
- | allocation will follow the per node zonelist. | ||
- | |||
- | For allocation of page cache pages, Interleave mode indexes the set | ||
- | of nodes specified by the policy using a node counter maintained | ||
- | per task. This counter wraps around to the lowest specified node | ||
- | after it reaches the highest specified node. This will tend to | ||
- | spread the pages out over the nodes specified by the policy based | ||
- | on the order in which they are allocated, rather than based on any | ||
- | page offset into an address range or file. During system boot up, | ||
- | the temporary interleaved system default policy works in this | ||
- | mode. | ||
- | |||
- | Linux memory policy supports the following optional mode flags: | ||
- | |||
- | MPOL_F_STATIC_NODES: | ||
- | the user should not be remapped if the task or VMA's set of allowed | ||
- | nodes changes after the memory policy has been defined. | ||
- | |||
- | Without this flag, anytime a mempolicy is rebound because of a | ||
- | change in the set of allowed nodes, the node (Preferred) or | ||
- | nodemask (Bind, Interleave) is remapped to the new set of | ||
- | allowed nodes. | ||
- | previously undesired. | ||
- | |||
- | With this flag, if the user-specified nodes overlap with the | ||
- | nodes allowed by the task's cpuset, then the memory policy is | ||
- | applied to their intersection. | ||
- | overlap, the Default policy is used. | ||
- | |||
- | For example, consider a task that is attached to a cpuset with | ||
- | mems 1-3 that sets an Interleave policy over the same set. If | ||
- | the cpuset' | ||
- | over nodes 3, 4, and 5. With this flag, however, since only node | ||
- | 3 is allowed from the user's nodemask, the " | ||
- | occurs over that node. If no nodes from the user's nodemask are | ||
- | now allowed, the Default behavior is used. | ||
- | |||
- | MPOL_F_STATIC_NODES cannot be combined with the | ||
- | MPOL_F_RELATIVE_NODES flag. It also cannot be used for | ||
- | MPOL_PREFERRED policies that were created with an empty nodemask | ||
- | (local allocation). | ||
- | |||
- | MPOL_F_RELATIVE_NODES: | ||
- | by the user will be mapped relative to the set of the task or VMA's | ||
- | set of allowed nodes. | ||
- | and if the allowed nodes changes, then that original nodemask will | ||
- | be remapped relative to the new set of allowed nodes. | ||
- | |||
- | Without this flag (and without MPOL_F_STATIC_NODES), | ||
- | mempolicy is rebound because of a change in the set of allowed | ||
- | nodes, the node (Preferred) or nodemask (Bind, Interleave) is | ||
- | remapped to the new set of allowed nodes. | ||
- | preserve the relative nature of the user's passed nodemask to its | ||
- | set of allowed nodes upon successive rebinds: a nodemask of | ||
- | 1,3,5 may be remapped to 7-9 and then to 1-3 if the set of | ||
- | allowed nodes is restored to its original state. | ||
- | |||
- | With this flag, the remap is done so that the node numbers from | ||
- | the user's passed nodemask are relative to the set of allowed | ||
- | nodes. | ||
- | nodemask, the policy will be effected over the first (and in the | ||
- | Bind or Interleave case, the third and fifth) nodes in the set of | ||
- | allowed nodes. | ||
- | relative to task or VMA's set of allowed nodes. | ||
- | |||
- | If the user's nodemask includes nodes that are outside the range | ||
- | of the new set of allowed nodes (for example, node 5 is set in | ||
- | the user's nodemask when the set of allowed nodes is only 0-3), | ||
- | then the remap wraps around to the beginning of the nodemask and, | ||
- | if not already set, sets the node in the mempolicy nodemask. | ||
- | |||
- | For example, consider a task that is attached to a cpuset with | ||
- | mems 2-5 that sets an Interleave policy over the same set with | ||
- | MPOL_F_RELATIVE_NODES. | ||
- | interleave now occurs over nodes 3, | ||
- | then change to 0,2-3,5, then the interleave occurs over nodes | ||
- | 0,3,5. | ||
- | |||
- | Thanks to the consistent remapping, applications preparing | ||
- | nodemasks to specify memory policies using this flag should | ||
- | disregard their current, actual cpuset imposed memory placement | ||
- | and prepare the nodemask as if they were always located on | ||
- | memory nodes 0 to N-1, where N is the number of memory nodes the | ||
- | policy is intended to manage. | ||
- | set of memory nodes allowed by the task's cpuset, as that may | ||
- | change over time. | ||
- | |||
- | MPOL_F_RELATIVE_NODES cannot be combined with the | ||
- | MPOL_F_STATIC_NODES flag. It also cannot be used for | ||
- | MPOL_PREFERRED policies that were created with an empty nodemask | ||
- | (local allocation). | ||
- | |||
- | |||
- | |||
- | ===== Memory Policy Reference Counting ===== | ||
- | |||
- | To resolve use/free races, struct mempolicy contains an atomic reference count field. | ||
- | decrement this reference count, respectively. | ||
- | |||
- | When a new memory policy is allocated, its reference count is initialized to ' | ||
- | |||
- | During run-time " | ||
- | |||
- | 1) querying of the policy, either by the task itself [using the get_mempolicy() API discussed below] or by another task using the / | ||
- | |||
- | 2) examination of the policy to determine the policy mode and associated node or node lists, if any, for page allocation. | ||
- | |||
- | We can avoid taking an extra reference during the usages listed above as follows: | ||
- | |||
- | 1) we never need to get/free the system default policy as this is never changed nor freed, once the system is up and running. | ||
- | |||
- | 2) for querying the policy, we do not need to take an extra reference on the target task's task policy nor vma policies because we always acquire the task's mm's mmap_sem for read during the query. | ||
- | |||
- | 3) Page allocation usage of task or vma policy occurs in the fault path where we hold them mmap_sem for read. Again, because replacing the task or vma policy requires that the mmap_sem be held for write, the policy can't be freed out from under us while we're using it for page allocation. | ||
- | |||
- | 4) Shared policies require special consideration. | ||
- | |||
- | Because of this extra reference counting, and because we must lookup shared policies in a tree structure under spinlock, shared policies are more expensive to use in the page allocation path. This is especially true for shared policies on shared memory regions shared by tasks running on different NUMA nodes. | ||
- | |||
- | |||
- | ===== Memory Policy APIs ===== | ||
- | |||
- | Linux supports 3 system calls for controlling memory policy. | ||
- | always affect only the calling task, the calling task's address space, or | ||
- | some shared object mapped into the calling task's address space. | ||
- | |||
- | Note: | ||
- | for user space applications reside in a package that is not part of | ||
- | the Linux kernel. | ||
- | prefix, are defined in < | ||
- | definitions are defined in < | ||
- | |||
- | Set [Task] Memory Policy: | ||
- | |||
- | long set_mempolicy(int mode, const unsigned long *nmask, | ||
- | unsigned long maxnode); | ||
- | |||
- | Set's the calling task's " | ||
- | specified by the ' | ||
- | by ' | ||
- | at least ' | ||
- | combining the ' | ||
- | MPOL_INTERLEAVE | MPOL_F_STATIC_NODES). | ||
- | |||
- | See the set_mempolicy(2) man page for more details | ||
- | |||
- | |||
- | Get [Task] Memory Policy or Related Information | ||
- | |||
- | long get_mempolicy(int *mode, | ||
- | const unsigned long *nmask, unsigned long maxnode, | ||
- | void *addr, int flags); | ||
- | |||
- | Queries the " | ||
- | the policy or location of a specified virtual address, depending | ||
- | on the ' | ||
- | |||
- | See the get_mempolicy(2) man page for more details | ||
- | |||
- | |||
- | Install VMA/Shared Policy for a Range of Task's Address Space | ||
- | |||
- | long mbind(void *start, unsigned long len, int mode, | ||
- | const unsigned long *nmask, unsigned long maxnode, | ||
- | | ||
- | |||
- | mbind() installs the policy specified by (mode, nmask, maxnodes) as | ||
- | a VMA policy for the range of the calling task's address space | ||
- | specified by the ' | ||
- | may be requested via the ' | ||
- | |||
- | See the mbind(2) man page for more details. | ||
- | |||
- | MEMORY POLICY COMMAND LINE INTERFACE | ||
- | |||
- | Although not strictly part of the Linux implementation of memory policy, | ||
- | a command line tool, numactl(8), exists that allows one to: | ||
- | |||
- | + set the task policy for a specified program via set_mempolicy(2), | ||
- | exec(2) | ||
- | |||
- | + set the shared policy for a shared memory segment via mbind(2) | ||
- | |||
- | The numactl(8) tool is packaged with the run-time version of the library | ||
- | containing the memory policy system call wrappers. | ||
- | package the headers and compile-time libraries in a separate development | ||
- | package. | ||
- | |||
- | |||
- | MEMORY POLICIES AND CPUSETS | ||
- | |||
- | Memory policies work within cpusets as described above. | ||
- | that require a node or set of nodes, the nodes are restricted to the set of | ||
- | nodes whose memories are allowed by the cpuset constraints. | ||
- | specified for the policy contains nodes that are not allowed by the cpuset and | ||
- | MPOL_F_RELATIVE_NODES is not used, the intersection of the set of nodes | ||
- | specified for the policy and the set of nodes with memory is used. If the | ||
- | result is the empty set, the policy is considered invalid and cannot be | ||
- | installed. | ||
- | onto and folded into the task's set of allowed nodes as previously described. | ||
- | |||
- | The interaction of memory policies and cpusets can be problematic when tasks | ||
- | in two cpusets share access to a memory region, such as shared memory segments | ||
- | created by shmget() of mmap() with the MAP_ANONYMOUS and MAP_SHARED flags, and | ||
- | any of the tasks install shared policy on the region, only nodes whose | ||
- | memories are allowed in both cpusets may be used in the policies. | ||
- | this information requires " | ||
- | cpuset information and requires that one know in what cpusets other task might | ||
- | be attaching to the shared region. | ||
- | memory sets are disjoint, " |
kernel/memory_policy.1491397057.txt.gz · Last modified: 2020/07/15 09:30 (external edit)