numa_memory_policy.txt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. What is Linux Memory Policy?
  2. In the Linux kernel, "memory policy" determines from which node the kernel will
  3. allocate memory in a NUMA system or in an emulated NUMA system. Linux has
  4. supported platforms with Non-Uniform Memory Access architectures since 2.4.?.
  5. The current memory policy support was added to Linux 2.6 around May 2004. This
  6. document attempts to describe the concepts and APIs of the 2.6 memory policy
  7. support.
  8. Memory policies should not be confused with cpusets (Documentation/cpusets.txt)
  9. which is an administrative mechanism for restricting the nodes from which
  10. memory may be allocated by a set of processes. Memory policies are a
  11. programming interface that a NUMA-aware application can take advantage of. When
  12. both cpusets and policies are applied to a task, the restrictions of the cpuset
  13. takes priority. See "MEMORY POLICIES AND CPUSETS" below for more details.
  14. MEMORY POLICY CONCEPTS
  15. Scope of Memory Policies
  16. The Linux kernel supports _scopes_ of memory policy, described here from
  17. most general to most specific:
  18. System Default Policy: this policy is "hard coded" into the kernel. It
  19. is the policy that governs all page allocations that aren't controlled
  20. by one of the more specific policy scopes discussed below. When the
  21. system is "up and running", the system default policy will use "local
  22. allocation" described below. However, during boot up, the system
  23. default policy will be set to interleave allocations across all nodes
  24. with "sufficient" memory, so as not to overload the initial boot node
  25. with boot-time allocations.
  26. Task/Process Policy: this is an optional, per-task policy. When defined
  27. for a specific task, this policy controls all page allocations made by or
  28. on behalf of the task that aren't controlled by a more specific scope.
  29. If a task does not define a task policy, then all page allocations that
  30. would have been controlled by the task policy "fall back" to the System
  31. Default Policy.
  32. The task policy applies to the entire address space of a task. Thus,
  33. it is inheritable, and indeed is inherited, across both fork()
  34. [clone() w/o the CLONE_VM flag] and exec*(). This allows a parent task
  35. to establish the task policy for a child task exec()'d from an
  36. executable image that has no awareness of memory policy. See the
  37. MEMORY POLICY APIS section, below, for an overview of the system call
  38. that a task may use to set/change it's task/process policy.
  39. In a multi-threaded task, task policies apply only to the thread
  40. [Linux kernel task] that installs the policy and any threads
  41. subsequently created by that thread. Any sibling threads existing
  42. at the time a new task policy is installed retain their current
  43. policy.
  44. A task policy applies only to pages allocated after the policy is
  45. installed. Any pages already faulted in by the task when the task
  46. changes its task policy remain where they were allocated based on
  47. the policy at the time they were allocated.
  48. VMA Policy: A "VMA" or "Virtual Memory Area" refers to a range of a task's
  49. virtual adddress space. A task may define a specific policy for a range
  50. of its virtual address space. See the MEMORY POLICIES APIS section,
  51. below, for an overview of the mbind() system call used to set a VMA
  52. policy.
  53. A VMA policy will govern the allocation of pages that back this region of
  54. the address space. Any regions of the task's address space that don't
  55. have an explicit VMA policy will fall back to the task policy, which may
  56. itself fall back to the System Default Policy.
  57. VMA policies have a few complicating details:
  58. VMA policy applies ONLY to anonymous pages. These include pages
  59. allocated for anonymous segments, such as the task stack and heap, and
  60. any regions of the address space mmap()ed with the MAP_ANONYMOUS flag.
  61. If a VMA policy is applied to a file mapping, it will be ignored if
  62. the mapping used the MAP_SHARED flag. If the file mapping used the
  63. MAP_PRIVATE flag, the VMA policy will only be applied when an
  64. anonymous page is allocated on an attempt to write to the mapping--
  65. i.e., at Copy-On-Write.
  66. VMA policies are shared between all tasks that share a virtual address
  67. space--a.k.a. threads--independent of when the policy is installed; and
  68. they are inherited across fork(). However, because VMA policies refer
  69. to a specific region of a task's address space, and because the address
  70. space is discarded and recreated on exec*(), VMA policies are NOT
  71. inheritable across exec(). Thus, only NUMA-aware applications may
  72. use VMA policies.
  73. A task may install a new VMA policy on a sub-range of a previously
  74. mmap()ed region. When this happens, Linux splits the existing virtual
  75. memory area into 2 or 3 VMAs, each with it's own policy.
  76. By default, VMA policy applies only to pages allocated after the policy
  77. is installed. Any pages already faulted into the VMA range remain
  78. where they were allocated based on the policy at the time they were
  79. allocated. However, since 2.6.16, Linux supports page migration via
  80. the mbind() system call, so that page contents can be moved to match
  81. a newly installed policy.
  82. Shared Policy: Conceptually, shared policies apply to "memory objects"
  83. mapped shared into one or more tasks' distinct address spaces. An
  84. application installs a shared policies the same way as VMA policies--using
  85. the mbind() system call specifying a range of virtual addresses that map
  86. the shared object. However, unlike VMA policies, which can be considered
  87. to be an attribute of a range of a task's address space, shared policies
  88. apply directly to the shared object. Thus, all tasks that attach to the
  89. object share the policy, and all pages allocated for the shared object,
  90. by any task, will obey the shared policy.
  91. As of 2.6.22, only shared memory segments, created by shmget() or
  92. mmap(MAP_ANONYMOUS|MAP_SHARED), support shared policy. When shared
  93. policy support was added to Linux, the associated data structures were
  94. added to hugetlbfs shmem segments. At the time, hugetlbfs did not
  95. support allocation at fault time--a.k.a lazy allocation--so hugetlbfs
  96. shmem segments were never "hooked up" to the shared policy support.
  97. Although hugetlbfs segments now support lazy allocation, their support
  98. for shared policy has not been completed.
  99. As mentioned above [re: VMA policies], allocations of page cache
  100. pages for regular files mmap()ed with MAP_SHARED ignore any VMA
  101. policy installed on the virtual address range backed by the shared
  102. file mapping. Rather, shared page cache pages, including pages backing
  103. private mappings that have not yet been written by the task, follow
  104. task policy, if any, else System Default Policy.
  105. The shared policy infrastructure supports different policies on subset
  106. ranges of the shared object. However, Linux still splits the VMA of
  107. the task that installs the policy for each range of distinct policy.
  108. Thus, different tasks that attach to a shared memory segment can have
  109. different VMA configurations mapping that one shared object. This
  110. can be seen by examining the /proc/<pid>/numa_maps of tasks sharing
  111. a shared memory region, when one task has installed shared policy on
  112. one or more ranges of the region.
  113. Components of Memory Policies
  114. A Linux memory policy is a tuple consisting of a "mode" and an optional set
  115. of nodes. The mode determine the behavior of the policy, while the
  116. optional set of nodes can be viewed as the arguments to the behavior.
  117. Internally, memory policies are implemented by a reference counted
  118. structure, struct mempolicy. Details of this structure will be discussed
  119. in context, below, as required to explain the behavior.
  120. Note: in some functions AND in the struct mempolicy itself, the mode
  121. is called "policy". However, to avoid confusion with the policy tuple,
  122. this document will continue to use the term "mode".
  123. Linux memory policy supports the following 4 behavioral modes:
  124. Default Mode--MPOL_DEFAULT: The behavior specified by this mode is
  125. context or scope dependent.
  126. As mentioned in the Policy Scope section above, during normal
  127. system operation, the System Default Policy is hard coded to
  128. contain the Default mode.
  129. In this context, default mode means "local" allocation--that is
  130. attempt to allocate the page from the node associated with the cpu
  131. where the fault occurs. If the "local" node has no memory, or the
  132. node's memory can be exhausted [no free pages available], local
  133. allocation will "fallback to"--attempt to allocate pages from--
  134. "nearby" nodes, in order of increasing "distance".
  135. Implementation detail -- subject to change: "Fallback" uses
  136. a per node list of sibling nodes--called zonelists--built at
  137. boot time, or when nodes or memory are added or removed from
  138. the system [memory hotplug]. These per node zonelist are
  139. constructed with nodes in order of increasing distance based
  140. on information provided by the platform firmware.
  141. When a task/process policy or a shared policy contains the Default
  142. mode, this also means "local allocation", as described above.
  143. In the context of a VMA, Default mode means "fall back to task
  144. policy"--which may or may not specify Default mode. Thus, Default
  145. mode can not be counted on to mean local allocation when used
  146. on a non-shared region of the address space. However, see
  147. MPOL_PREFERRED below.
  148. The Default mode does not use the optional set of nodes.
  149. MPOL_BIND: This mode specifies that memory must come from the
  150. set of nodes specified by the policy.
  151. The memory policy APIs do not specify an order in which the nodes
  152. will be searched. However, unlike "local allocation", the Bind
  153. policy does not consider the distance between the nodes. Rather,
  154. allocations will fallback to the nodes specified by the policy in
  155. order of numeric node id. Like everything in Linux, this is subject
  156. to change.
  157. MPOL_PREFERRED: This mode specifies that the allocation should be
  158. attempted from the single node specified in the policy. If that
  159. allocation fails, the kernel will search other nodes, exactly as
  160. it would for a local allocation that started at the preferred node
  161. in increasing distance from the preferred node. "Local" allocation
  162. policy can be viewed as a Preferred policy that starts at the node
  163. containing the cpu where the allocation takes place.
  164. Internally, the Preferred policy uses a single node--the
  165. preferred_node member of struct mempolicy. A "distinguished
  166. value of this preferred_node, currently '-1', is interpreted
  167. as "the node containing the cpu where the allocation takes
  168. place"--local allocation. This is the way to specify
  169. local allocation for a specific range of addresses--i.e. for
  170. VMA policies.
  171. MPOL_INTERLEAVED: This mode specifies that page allocations be
  172. interleaved, on a page granularity, across the nodes specified in
  173. the policy. This mode also behaves slightly differently, based on
  174. the context where it is used:
  175. For allocation of anonymous pages and shared memory pages,
  176. Interleave mode indexes the set of nodes specified by the policy
  177. using the page offset of the faulting address into the segment
  178. [VMA] containing the address modulo the number of nodes specified
  179. by the policy. It then attempts to allocate a page, starting at
  180. the selected node, as if the node had been specified by a Preferred
  181. policy or had been selected by a local allocation. That is,
  182. allocation will follow the per node zonelist.
  183. For allocation of page cache pages, Interleave mode indexes the set
  184. of nodes specified by the policy using a node counter maintained
  185. per task. This counter wraps around to the lowest specified node
  186. after it reaches the highest specified node. This will tend to
  187. spread the pages out over the nodes specified by the policy based
  188. on the order in which they are allocated, rather than based on any
  189. page offset into an address range or file. During system boot up,
  190. the temporary interleaved system default policy works in this
  191. mode.
  192. MEMORY POLICY APIs
  193. Linux supports 3 system calls for controlling memory policy. These APIS
  194. always affect only the calling task, the calling task's address space, or
  195. some shared object mapped into the calling task's address space.
  196. Note: the headers that define these APIs and the parameter data types
  197. for user space applications reside in a package that is not part of
  198. the Linux kernel. The kernel system call interfaces, with the 'sys_'
  199. prefix, are defined in <linux/syscalls.h>; the mode and flag
  200. definitions are defined in <linux/mempolicy.h>.
  201. Set [Task] Memory Policy:
  202. long set_mempolicy(int mode, const unsigned long *nmask,
  203. unsigned long maxnode);
  204. Set's the calling task's "task/process memory policy" to mode
  205. specified by the 'mode' argument and the set of nodes defined
  206. by 'nmask'. 'nmask' points to a bit mask of node ids containing
  207. at least 'maxnode' ids.
  208. See the set_mempolicy(2) man page for more details
  209. Get [Task] Memory Policy or Related Information
  210. long get_mempolicy(int *mode,
  211. const unsigned long *nmask, unsigned long maxnode,
  212. void *addr, int flags);
  213. Queries the "task/process memory policy" of the calling task, or
  214. the policy or location of a specified virtual address, depending
  215. on the 'flags' argument.
  216. See the get_mempolicy(2) man page for more details
  217. Install VMA/Shared Policy for a Range of Task's Address Space
  218. long mbind(void *start, unsigned long len, int mode,
  219. const unsigned long *nmask, unsigned long maxnode,
  220. unsigned flags);
  221. mbind() installs the policy specified by (mode, nmask, maxnodes) as
  222. a VMA policy for the range of the calling task's address space
  223. specified by the 'start' and 'len' arguments. Additional actions
  224. may be requested via the 'flags' argument.
  225. See the mbind(2) man page for more details.
  226. MEMORY POLICY COMMAND LINE INTERFACE
  227. Although not strictly part of the Linux implementation of memory policy,
  228. a command line tool, numactl(8), exists that allows one to:
  229. + set the task policy for a specified program via set_mempolicy(2), fork(2) and
  230. exec(2)
  231. + set the shared policy for a shared memory segment via mbind(2)
  232. The numactl(8) tool is packages with the run-time version of the library
  233. containing the memory policy system call wrappers. Some distributions
  234. package the headers and compile-time libraries in a separate development
  235. package.
  236. MEMORY POLICIES AND CPUSETS
  237. Memory policies work within cpusets as described above. For memory policies
  238. that require a node or set of nodes, the nodes are restricted to the set of
  239. nodes whose memories are allowed by the cpuset constraints. If the nodemask
  240. specified for the policy contains nodes that are not allowed by the cpuset, or
  241. the intersection of the set of nodes specified for the policy and the set of
  242. nodes with memory is the empty set, the policy is considered invalid
  243. and cannot be installed.
  244. The interaction of memory policies and cpusets can be problematic for a
  245. couple of reasons:
  246. 1) the memory policy APIs take physical node id's as arguments. As mentioned
  247. above, it is illegal to specify nodes that are not allowed in the cpuset.
  248. The application must query the allowed nodes using the get_mempolicy()
  249. API with the MPOL_F_MEMS_ALLOWED flag to determine the allowed nodes and
  250. restrict itself to those nodes. However, the resources available to a
  251. cpuset can be changed by the system administrator, or a workload manager
  252. application, at any time. So, a task may still get errors attempting to
  253. specify policy nodes, and must query the allowed memories again.
  254. 2) when tasks in two cpusets share access to a memory region, such as shared
  255. memory segments created by shmget() of mmap() with the MAP_ANONYMOUS and
  256. MAP_SHARED flags, and any of the tasks install shared policy on the region,
  257. only nodes whose memories are allowed in both cpusets may be used in the
  258. policies. Obtaining this information requires "stepping outside" the
  259. memory policy APIs to use the cpuset information and requires that one
  260. know in what cpusets other task might be attaching to the shared region.
  261. Furthermore, if the cpusets' allowed memory sets are disjoint, "local"
  262. allocation is the only valid policy.