numa_memory_policy.txt 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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
  9. (Documentation/cgroups/cpusets.txt)
  10. which is an administrative mechanism for restricting the nodes from which
  11. memory may be allocated by a set of processes. Memory policies are a
  12. programming interface that a NUMA-aware application can take advantage of. When
  13. both cpusets and policies are applied to a task, the restrictions of the cpuset
  14. takes priority. See "MEMORY POLICIES AND CPUSETS" below for more details.
  15. MEMORY POLICY CONCEPTS
  16. Scope of Memory Policies
  17. The Linux kernel supports _scopes_ of memory policy, described here from
  18. most general to most specific:
  19. System Default Policy: this policy is "hard coded" into the kernel. It
  20. is the policy that governs all page allocations that aren't controlled
  21. by one of the more specific policy scopes discussed below. When the
  22. system is "up and running", the system default policy will use "local
  23. allocation" described below. However, during boot up, the system
  24. default policy will be set to interleave allocations across all nodes
  25. with "sufficient" memory, so as not to overload the initial boot node
  26. with boot-time allocations.
  27. Task/Process Policy: this is an optional, per-task policy. When defined
  28. for a specific task, this policy controls all page allocations made by or
  29. on behalf of the task that aren't controlled by a more specific scope.
  30. If a task does not define a task policy, then all page allocations that
  31. would have been controlled by the task policy "fall back" to the System
  32. Default Policy.
  33. The task policy applies to the entire address space of a task. Thus,
  34. it is inheritable, and indeed is inherited, across both fork()
  35. [clone() w/o the CLONE_VM flag] and exec*(). This allows a parent task
  36. to establish the task policy for a child task exec()'d from an
  37. executable image that has no awareness of memory policy. See the
  38. MEMORY POLICY APIS section, below, for an overview of the system call
  39. that a task may use to set/change its task/process policy.
  40. In a multi-threaded task, task policies apply only to the thread
  41. [Linux kernel task] that installs the policy and any threads
  42. subsequently created by that thread. Any sibling threads existing
  43. at the time a new task policy is installed retain their current
  44. policy.
  45. A task policy applies only to pages allocated after the policy is
  46. installed. Any pages already faulted in by the task when the task
  47. changes its task policy remain where they were allocated based on
  48. the policy at the time they were allocated.
  49. VMA Policy: A "VMA" or "Virtual Memory Area" refers to a range of a task's
  50. virtual address space. A task may define a specific policy for a range
  51. of its virtual address space. See the MEMORY POLICIES APIS section,
  52. below, for an overview of the mbind() system call used to set a VMA
  53. policy.
  54. A VMA policy will govern the allocation of pages that back this region of
  55. the address space. Any regions of the task's address space that don't
  56. have an explicit VMA policy will fall back to the task policy, which may
  57. itself fall back to the System Default Policy.
  58. VMA policies have a few complicating details:
  59. VMA policy applies ONLY to anonymous pages. These include pages
  60. allocated for anonymous segments, such as the task stack and heap, and
  61. any regions of the address space mmap()ed with the MAP_ANONYMOUS flag.
  62. If a VMA policy is applied to a file mapping, it will be ignored if
  63. the mapping used the MAP_SHARED flag. If the file mapping used the
  64. MAP_PRIVATE flag, the VMA policy will only be applied when an
  65. anonymous page is allocated on an attempt to write to the mapping--
  66. i.e., at Copy-On-Write.
  67. VMA policies are shared between all tasks that share a virtual address
  68. space--a.k.a. threads--independent of when the policy is installed; and
  69. they are inherited across fork(). However, because VMA policies refer
  70. to a specific region of a task's address space, and because the address
  71. space is discarded and recreated on exec*(), VMA policies are NOT
  72. inheritable across exec(). Thus, only NUMA-aware applications may
  73. use VMA policies.
  74. A task may install a new VMA policy on a sub-range of a previously
  75. mmap()ed region. When this happens, Linux splits the existing virtual
  76. memory area into 2 or 3 VMAs, each with it's own policy.
  77. By default, VMA policy applies only to pages allocated after the policy
  78. is installed. Any pages already faulted into the VMA range remain
  79. where they were allocated based on the policy at the time they were
  80. allocated. However, since 2.6.16, Linux supports page migration via
  81. the mbind() system call, so that page contents can be moved to match
  82. a newly installed policy.
  83. Shared Policy: Conceptually, shared policies apply to "memory objects"
  84. mapped shared into one or more tasks' distinct address spaces. An
  85. application installs a shared policies the same way as VMA policies--using
  86. the mbind() system call specifying a range of virtual addresses that map
  87. the shared object. However, unlike VMA policies, which can be considered
  88. to be an attribute of a range of a task's address space, shared policies
  89. apply directly to the shared object. Thus, all tasks that attach to the
  90. object share the policy, and all pages allocated for the shared object,
  91. by any task, will obey the shared policy.
  92. As of 2.6.22, only shared memory segments, created by shmget() or
  93. mmap(MAP_ANONYMOUS|MAP_SHARED), support shared policy. When shared
  94. policy support was added to Linux, the associated data structures were
  95. added to hugetlbfs shmem segments. At the time, hugetlbfs did not
  96. support allocation at fault time--a.k.a lazy allocation--so hugetlbfs
  97. shmem segments were never "hooked up" to the shared policy support.
  98. Although hugetlbfs segments now support lazy allocation, their support
  99. for shared policy has not been completed.
  100. As mentioned above [re: VMA policies], allocations of page cache
  101. pages for regular files mmap()ed with MAP_SHARED ignore any VMA
  102. policy installed on the virtual address range backed by the shared
  103. file mapping. Rather, shared page cache pages, including pages backing
  104. private mappings that have not yet been written by the task, follow
  105. task policy, if any, else System Default Policy.
  106. The shared policy infrastructure supports different policies on subset
  107. ranges of the shared object. However, Linux still splits the VMA of
  108. the task that installs the policy for each range of distinct policy.
  109. Thus, different tasks that attach to a shared memory segment can have
  110. different VMA configurations mapping that one shared object. This
  111. can be seen by examining the /proc/<pid>/numa_maps of tasks sharing
  112. a shared memory region, when one task has installed shared policy on
  113. one or more ranges of the region.
  114. Components of Memory Policies
  115. A Linux memory policy consists of a "mode", optional mode flags, and an
  116. optional set of nodes. The mode determines the behavior of the policy,
  117. the optional mode flags determine the behavior of the mode, and the
  118. optional set of nodes can be viewed as the arguments to the policy
  119. behavior.
  120. Internally, memory policies are implemented by a reference counted
  121. structure, struct mempolicy. Details of this structure will be discussed
  122. in context, below, as required to explain the behavior.
  123. Linux memory policy supports the following 4 behavioral modes:
  124. Default Mode--MPOL_DEFAULT: This mode is only used in the memory
  125. policy APIs. Internally, MPOL_DEFAULT is converted to the NULL
  126. memory policy in all policy scopes. Any existing non-default policy
  127. will simply be removed when MPOL_DEFAULT is specified. As a result,
  128. MPOL_DEFAULT means "fall back to the next most specific policy scope."
  129. For example, a NULL or default task policy will fall back to the
  130. system default policy. A NULL or default vma policy will fall
  131. back to the task policy.
  132. When specified in one of the memory policy APIs, the Default mode
  133. does not use the optional set of nodes.
  134. It is an error for the set of nodes specified for this policy to
  135. be non-empty.
  136. MPOL_BIND: This mode specifies that memory must come from the
  137. set of nodes specified by the policy. Memory will be allocated from
  138. the node in the set with sufficient free memory that is closest to
  139. the node where the allocation takes place.
  140. MPOL_PREFERRED: This mode specifies that the allocation should be
  141. attempted from the single node specified in the policy. If that
  142. allocation fails, the kernel will search other nodes, in order of
  143. increasing distance from the preferred node based on information
  144. provided by the platform firmware.
  145. Internally, the Preferred policy uses a single node--the
  146. preferred_node member of struct mempolicy. When the internal
  147. mode flag MPOL_F_LOCAL is set, the preferred_node is ignored and
  148. the policy is interpreted as local allocation. "Local" allocation
  149. policy can be viewed as a Preferred policy that starts at the node
  150. containing the cpu where the allocation takes place.
  151. It is possible for the user to specify that local allocation is
  152. always preferred by passing an empty nodemask with this mode.
  153. If an empty nodemask is passed, the policy cannot use the
  154. MPOL_F_STATIC_NODES or MPOL_F_RELATIVE_NODES flags described
  155. below.
  156. MPOL_INTERLEAVED: This mode specifies that page allocations be
  157. interleaved, on a page granularity, across the nodes specified in
  158. the policy. This mode also behaves slightly differently, based on
  159. the context where it is used:
  160. For allocation of anonymous pages and shared memory pages,
  161. Interleave mode indexes the set of nodes specified by the policy
  162. using the page offset of the faulting address into the segment
  163. [VMA] containing the address modulo the number of nodes specified
  164. by the policy. It then attempts to allocate a page, starting at
  165. the selected node, as if the node had been specified by a Preferred
  166. policy or had been selected by a local allocation. That is,
  167. allocation will follow the per node zonelist.
  168. For allocation of page cache pages, Interleave mode indexes the set
  169. of nodes specified by the policy using a node counter maintained
  170. per task. This counter wraps around to the lowest specified node
  171. after it reaches the highest specified node. This will tend to
  172. spread the pages out over the nodes specified by the policy based
  173. on the order in which they are allocated, rather than based on any
  174. page offset into an address range or file. During system boot up,
  175. the temporary interleaved system default policy works in this
  176. mode.
  177. Linux memory policy supports the following optional mode flags:
  178. MPOL_F_STATIC_NODES: This flag specifies that the nodemask passed by
  179. the user should not be remapped if the task or VMA's set of allowed
  180. nodes changes after the memory policy has been defined.
  181. Without this flag, anytime a mempolicy is rebound because of a
  182. change in the set of allowed nodes, the node (Preferred) or
  183. nodemask (Bind, Interleave) is remapped to the new set of
  184. allowed nodes. This may result in nodes being used that were
  185. previously undesired.
  186. With this flag, if the user-specified nodes overlap with the
  187. nodes allowed by the task's cpuset, then the memory policy is
  188. applied to their intersection. If the two sets of nodes do not
  189. overlap, the Default policy is used.
  190. For example, consider a task that is attached to a cpuset with
  191. mems 1-3 that sets an Interleave policy over the same set. If
  192. the cpuset's mems change to 3-5, the Interleave will now occur
  193. over nodes 3, 4, and 5. With this flag, however, since only node
  194. 3 is allowed from the user's nodemask, the "interleave" only
  195. occurs over that node. If no nodes from the user's nodemask are
  196. now allowed, the Default behavior is used.
  197. MPOL_F_STATIC_NODES cannot be combined with the
  198. MPOL_F_RELATIVE_NODES flag. It also cannot be used for
  199. MPOL_PREFERRED policies that were created with an empty nodemask
  200. (local allocation).
  201. MPOL_F_RELATIVE_NODES: This flag specifies that the nodemask passed
  202. by the user will be mapped relative to the set of the task or VMA's
  203. set of allowed nodes. The kernel stores the user-passed nodemask,
  204. and if the allowed nodes changes, then that original nodemask will
  205. be remapped relative to the new set of allowed nodes.
  206. Without this flag (and without MPOL_F_STATIC_NODES), anytime a
  207. mempolicy is rebound because of a change in the set of allowed
  208. nodes, the node (Preferred) or nodemask (Bind, Interleave) is
  209. remapped to the new set of allowed nodes. That remap may not
  210. preserve the relative nature of the user's passed nodemask to its
  211. set of allowed nodes upon successive rebinds: a nodemask of
  212. 1,3,5 may be remapped to 7-9 and then to 1-3 if the set of
  213. allowed nodes is restored to its original state.
  214. With this flag, the remap is done so that the node numbers from
  215. the user's passed nodemask are relative to the set of allowed
  216. nodes. In other words, if nodes 0, 2, and 4 are set in the user's
  217. nodemask, the policy will be effected over the first (and in the
  218. Bind or Interleave case, the third and fifth) nodes in the set of
  219. allowed nodes. The nodemask passed by the user represents nodes
  220. relative to task or VMA's set of allowed nodes.
  221. If the user's nodemask includes nodes that are outside the range
  222. of the new set of allowed nodes (for example, node 5 is set in
  223. the user's nodemask when the set of allowed nodes is only 0-3),
  224. then the remap wraps around to the beginning of the nodemask and,
  225. if not already set, sets the node in the mempolicy nodemask.
  226. For example, consider a task that is attached to a cpuset with
  227. mems 2-5 that sets an Interleave policy over the same set with
  228. MPOL_F_RELATIVE_NODES. If the cpuset's mems change to 3-7, the
  229. interleave now occurs over nodes 3,5-7. If the cpuset's mems
  230. then change to 0,2-3,5, then the interleave occurs over nodes
  231. 0,2-3,5.
  232. Thanks to the consistent remapping, applications preparing
  233. nodemasks to specify memory policies using this flag should
  234. disregard their current, actual cpuset imposed memory placement
  235. and prepare the nodemask as if they were always located on
  236. memory nodes 0 to N-1, where N is the number of memory nodes the
  237. policy is intended to manage. Let the kernel then remap to the
  238. set of memory nodes allowed by the task's cpuset, as that may
  239. change over time.
  240. MPOL_F_RELATIVE_NODES cannot be combined with the
  241. MPOL_F_STATIC_NODES flag. It also cannot be used for
  242. MPOL_PREFERRED policies that were created with an empty nodemask
  243. (local allocation).
  244. MEMORY POLICY REFERENCE COUNTING
  245. To resolve use/free races, struct mempolicy contains an atomic reference
  246. count field. Internal interfaces, mpol_get()/mpol_put() increment and
  247. decrement this reference count, respectively. mpol_put() will only free
  248. the structure back to the mempolicy kmem cache when the reference count
  249. goes to zero.
  250. When a new memory policy is allocated, its reference count is initialized
  251. to '1', representing the reference held by the task that is installing the
  252. new policy. When a pointer to a memory policy structure is stored in another
  253. structure, another reference is added, as the task's reference will be dropped
  254. on completion of the policy installation.
  255. During run-time "usage" of the policy, we attempt to minimize atomic operations
  256. on the reference count, as this can lead to cache lines bouncing between cpus
  257. and NUMA nodes. "Usage" here means one of the following:
  258. 1) querying of the policy, either by the task itself [using the get_mempolicy()
  259. API discussed below] or by another task using the /proc/<pid>/numa_maps
  260. interface.
  261. 2) examination of the policy to determine the policy mode and associated node
  262. or node lists, if any, for page allocation. This is considered a "hot
  263. path". Note that for MPOL_BIND, the "usage" extends across the entire
  264. allocation process, which may sleep during page reclaimation, because the
  265. BIND policy nodemask is used, by reference, to filter ineligible nodes.
  266. We can avoid taking an extra reference during the usages listed above as
  267. follows:
  268. 1) we never need to get/free the system default policy as this is never
  269. changed nor freed, once the system is up and running.
  270. 2) for querying the policy, we do not need to take an extra reference on the
  271. target task's task policy nor vma policies because we always acquire the
  272. task's mm's mmap_sem for read during the query. The set_mempolicy() and
  273. mbind() APIs [see below] always acquire the mmap_sem for write when
  274. installing or replacing task or vma policies. Thus, there is no possibility
  275. of a task or thread freeing a policy while another task or thread is
  276. querying it.
  277. 3) Page allocation usage of task or vma policy occurs in the fault path where
  278. we hold them mmap_sem for read. Again, because replacing the task or vma
  279. policy requires that the mmap_sem be held for write, the policy can't be
  280. freed out from under us while we're using it for page allocation.
  281. 4) Shared policies require special consideration. One task can replace a
  282. shared memory policy while another task, with a distinct mmap_sem, is
  283. querying or allocating a page based on the policy. To resolve this
  284. potential race, the shared policy infrastructure adds an extra reference
  285. to the shared policy during lookup while holding a spin lock on the shared
  286. policy management structure. This requires that we drop this extra
  287. reference when we're finished "using" the policy. We must drop the
  288. extra reference on shared policies in the same query/allocation paths
  289. used for non-shared policies. For this reason, shared policies are marked
  290. as such, and the extra reference is dropped "conditionally"--i.e., only
  291. for shared policies.
  292. Because of this extra reference counting, and because we must lookup
  293. shared policies in a tree structure under spinlock, shared policies are
  294. more expensive to use in the page allocation path. This is especially
  295. true for shared policies on shared memory regions shared by tasks running
  296. on different NUMA nodes. This extra overhead can be avoided by always
  297. falling back to task or system default policy for shared memory regions,
  298. or by prefaulting the entire shared memory region into memory and locking
  299. it down. However, this might not be appropriate for all applications.
  300. MEMORY POLICY APIs
  301. Linux supports 3 system calls for controlling memory policy. These APIS
  302. always affect only the calling task, the calling task's address space, or
  303. some shared object mapped into the calling task's address space.
  304. Note: the headers that define these APIs and the parameter data types
  305. for user space applications reside in a package that is not part of
  306. the Linux kernel. The kernel system call interfaces, with the 'sys_'
  307. prefix, are defined in <linux/syscalls.h>; the mode and flag
  308. definitions are defined in <linux/mempolicy.h>.
  309. Set [Task] Memory Policy:
  310. long set_mempolicy(int mode, const unsigned long *nmask,
  311. unsigned long maxnode);
  312. Set's the calling task's "task/process memory policy" to mode
  313. specified by the 'mode' argument and the set of nodes defined
  314. by 'nmask'. 'nmask' points to a bit mask of node ids containing
  315. at least 'maxnode' ids. Optional mode flags may be passed by
  316. combining the 'mode' argument with the flag (for example:
  317. MPOL_INTERLEAVE | MPOL_F_STATIC_NODES).
  318. See the set_mempolicy(2) man page for more details
  319. Get [Task] Memory Policy or Related Information
  320. long get_mempolicy(int *mode,
  321. const unsigned long *nmask, unsigned long maxnode,
  322. void *addr, int flags);
  323. Queries the "task/process memory policy" of the calling task, or
  324. the policy or location of a specified virtual address, depending
  325. on the 'flags' argument.
  326. See the get_mempolicy(2) man page for more details
  327. Install VMA/Shared Policy for a Range of Task's Address Space
  328. long mbind(void *start, unsigned long len, int mode,
  329. const unsigned long *nmask, unsigned long maxnode,
  330. unsigned flags);
  331. mbind() installs the policy specified by (mode, nmask, maxnodes) as
  332. a VMA policy for the range of the calling task's address space
  333. specified by the 'start' and 'len' arguments. Additional actions
  334. may be requested via the 'flags' argument.
  335. See the mbind(2) man page for more details.
  336. MEMORY POLICY COMMAND LINE INTERFACE
  337. Although not strictly part of the Linux implementation of memory policy,
  338. a command line tool, numactl(8), exists that allows one to:
  339. + set the task policy for a specified program via set_mempolicy(2), fork(2) and
  340. exec(2)
  341. + set the shared policy for a shared memory segment via mbind(2)
  342. The numactl(8) tool is packaged with the run-time version of the library
  343. containing the memory policy system call wrappers. Some distributions
  344. package the headers and compile-time libraries in a separate development
  345. package.
  346. MEMORY POLICIES AND CPUSETS
  347. Memory policies work within cpusets as described above. For memory policies
  348. that require a node or set of nodes, the nodes are restricted to the set of
  349. nodes whose memories are allowed by the cpuset constraints. If the nodemask
  350. specified for the policy contains nodes that are not allowed by the cpuset and
  351. MPOL_F_RELATIVE_NODES is not used, the intersection of the set of nodes
  352. specified for the policy and the set of nodes with memory is used. If the
  353. result is the empty set, the policy is considered invalid and cannot be
  354. installed. If MPOL_F_RELATIVE_NODES is used, the policy's nodes are mapped
  355. onto and folded into the task's set of allowed nodes as previously described.
  356. The interaction of memory policies and cpusets can be problematic when tasks
  357. in two cpusets share access to a memory region, such as shared memory segments
  358. created by shmget() of mmap() with the MAP_ANONYMOUS and MAP_SHARED flags, and
  359. any of the tasks install shared policy on the region, only nodes whose
  360. memories are allowed in both cpusets may be used in the policies. Obtaining
  361. this information requires "stepping outside" the memory policy APIs to use the
  362. cpuset information and requires that one know in what cpusets other task might
  363. be attaching to the shared region. Furthermore, if the cpusets' allowed
  364. memory sets are disjoint, "local" allocation is the only valid policy.