numa_memory_policy.rst 23 KB

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