physical_memory.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ===============
  3. Physical Memory
  4. ===============
  5. Linux is available for a wide range of architectures so there is a need for an
  6. architecture-independent abstraction to represent the physical memory. This
  7. chapter describes the structures used to manage physical memory in a running
  8. system.
  9. The first principal concept prevalent in the memory management is
  10. `Non-Uniform Memory Access (NUMA)
  11. <https://en.wikipedia.org/wiki/Non-uniform_memory_access>`_.
  12. With multi-core and multi-socket machines, memory may be arranged into banks
  13. that incur a different cost to access depending on the “distance” from the
  14. processor. For example, there might be a bank of memory assigned to each CPU or
  15. a bank of memory very suitable for DMA near peripheral devices.
  16. Each bank is called a node and the concept is represented under Linux by a
  17. ``struct pglist_data`` even if the architecture is UMA. This structure is
  18. always referenced by its typedef ``pg_data_t``. A ``pg_data_t`` structure
  19. for a particular node can be referenced by ``NODE_DATA(nid)`` macro where
  20. ``nid`` is the ID of that node.
  21. For NUMA architectures, the node structures are allocated by the architecture
  22. specific code early during boot. Usually, these structures are allocated
  23. locally on the memory bank they represent. For UMA architectures, only one
  24. static ``pg_data_t`` structure called ``contig_page_data`` is used. Nodes will
  25. be discussed further in Section :ref:`Nodes <nodes>`
  26. The entire physical address space is partitioned into one or more blocks
  27. called zones which represent ranges within memory. These ranges are usually
  28. determined by architectural constraints for accessing the physical memory.
  29. The memory range within a node that corresponds to a particular zone is
  30. described by a ``struct zone``, typedeffed to ``zone_t``. Each zone has
  31. one of the types described below.
  32. * ``ZONE_DMA`` and ``ZONE_DMA32`` historically represented memory suitable for
  33. DMA by peripheral devices that cannot access all of the addressable
  34. memory. For many years there are better more and robust interfaces to get
  35. memory with DMA specific requirements (Documentation/core-api/dma-api.rst),
  36. but ``ZONE_DMA`` and ``ZONE_DMA32`` still represent memory ranges that have
  37. restrictions on how they can be accessed.
  38. Depending on the architecture, either of these zone types or even they both
  39. can be disabled at build time using ``CONFIG_ZONE_DMA`` and
  40. ``CONFIG_ZONE_DMA32`` configuration options. Some 64-bit platforms may need
  41. both zones as they support peripherals with different DMA addressing
  42. limitations.
  43. * ``ZONE_NORMAL`` is for normal memory that can be accessed by the kernel all
  44. the time. DMA operations can be performed on pages in this zone if the DMA
  45. devices support transfers to all addressable memory. ``ZONE_NORMAL`` is
  46. always enabled.
  47. * ``ZONE_HIGHMEM`` is the part of the physical memory that is not covered by a
  48. permanent mapping in the kernel page tables. The memory in this zone is only
  49. accessible to the kernel using temporary mappings. This zone is available
  50. only on some 32-bit architectures and is enabled with ``CONFIG_HIGHMEM``.
  51. * ``ZONE_MOVABLE`` is for normal accessible memory, just like ``ZONE_NORMAL``.
  52. The difference is that the contents of most pages in ``ZONE_MOVABLE`` is
  53. movable. That means that while virtual addresses of these pages do not
  54. change, their content may move between different physical pages. Often
  55. ``ZONE_MOVABLE`` is populated during memory hotplug, but it may be
  56. also populated on boot using one of ``kernelcore``, ``movablecore`` and
  57. ``movable_node`` kernel command line parameters. See
  58. Documentation/mm/page_migration.rst and
  59. Documentation/admin-guide/mm/memory-hotplug.rst for additional details.
  60. * ``ZONE_DEVICE`` represents memory residing on devices such as PMEM and GPU.
  61. It has different characteristics than RAM zone types and it exists to provide
  62. :ref:`struct page <Pages>` and memory map services for device driver
  63. identified physical address ranges. ``ZONE_DEVICE`` is enabled with
  64. configuration option ``CONFIG_ZONE_DEVICE``.
  65. It is important to note that many kernel operations can only take place using
  66. ``ZONE_NORMAL`` so it is the most performance critical zone. Zones are
  67. discussed further in Section :ref:`Zones <zones>`.
  68. The relation between node and zone extents is determined by the physical memory
  69. map reported by the firmware, architectural constraints for memory addressing
  70. and certain parameters in the kernel command line.
  71. For example, with 32-bit kernel on an x86 UMA machine with 2 Gbytes of RAM the
  72. entire memory will be on node 0 and there will be three zones: ``ZONE_DMA``,
  73. ``ZONE_NORMAL`` and ``ZONE_HIGHMEM``::
  74. 0 2G
  75. +-------------------------------------------------------------+
  76. | node 0 |
  77. +-------------------------------------------------------------+
  78. 0 16M 896M 2G
  79. +----------+-----------------------+--------------------------+
  80. | ZONE_DMA | ZONE_NORMAL | ZONE_HIGHMEM |
  81. +----------+-----------------------+--------------------------+
  82. With a kernel built with ``ZONE_DMA`` disabled and ``ZONE_DMA32`` enabled and
  83. booted with ``movablecore=80%`` parameter on an arm64 machine with 16 Gbytes of
  84. RAM equally split between two nodes, there will be ``ZONE_DMA32``,
  85. ``ZONE_NORMAL`` and ``ZONE_MOVABLE`` on node 0, and ``ZONE_NORMAL`` and
  86. ``ZONE_MOVABLE`` on node 1::
  87. 1G 9G 17G
  88. +--------------------------------+ +--------------------------+
  89. | node 0 | | node 1 |
  90. +--------------------------------+ +--------------------------+
  91. 1G 4G 4200M 9G 9320M 17G
  92. +---------+----------+-----------+ +------------+-------------+
  93. | DMA32 | NORMAL | MOVABLE | | NORMAL | MOVABLE |
  94. +---------+----------+-----------+ +------------+-------------+
  95. Memory banks may belong to interleaving nodes. In the example below an x86
  96. machine has 16 Gbytes of RAM in 4 memory banks, even banks belong to node 0
  97. and odd banks belong to node 1::
  98. 0 4G 8G 12G 16G
  99. +-------------+ +-------------+ +-------------+ +-------------+
  100. | node 0 | | node 1 | | node 0 | | node 1 |
  101. +-------------+ +-------------+ +-------------+ +-------------+
  102. 0 16M 4G
  103. +-----+-------+ +-------------+ +-------------+ +-------------+
  104. | DMA | DMA32 | | NORMAL | | NORMAL | | NORMAL |
  105. +-----+-------+ +-------------+ +-------------+ +-------------+
  106. In this case node 0 will span from 0 to 12 Gbytes and node 1 will span from
  107. 4 to 16 Gbytes.
  108. .. _nodes:
  109. Nodes
  110. =====
  111. As we have mentioned, each node in memory is described by a ``pg_data_t`` which
  112. is a typedef for a ``struct pglist_data``. When allocating a page, by default
  113. Linux uses a node-local allocation policy to allocate memory from the node
  114. closest to the running CPU. As processes tend to run on the same CPU, it is
  115. likely the memory from the current node will be used. The allocation policy can
  116. be controlled by users as described in
  117. Documentation/admin-guide/mm/numa_memory_policy.rst.
  118. Most NUMA architectures maintain an array of pointers to the node
  119. structures. The actual structures are allocated early during boot when
  120. architecture specific code parses the physical memory map reported by the
  121. firmware. The bulk of the node initialization happens slightly later in the
  122. boot process by free_area_init() function, described later in Section
  123. :ref:`Initialization <initialization>`.
  124. Along with the node structures, kernel maintains an array of ``nodemask_t``
  125. bitmasks called ``node_states``. Each bitmask in this array represents a set of
  126. nodes with particular properties as defined by ``enum node_states``:
  127. ``N_POSSIBLE``
  128. The node could become online at some point.
  129. ``N_ONLINE``
  130. The node is online.
  131. ``N_NORMAL_MEMORY``
  132. The node has regular memory.
  133. ``N_HIGH_MEMORY``
  134. The node has regular or high memory. When ``CONFIG_HIGHMEM`` is disabled
  135. aliased to ``N_NORMAL_MEMORY``.
  136. ``N_MEMORY``
  137. The node has memory(regular, high, movable)
  138. ``N_CPU``
  139. The node has one or more CPUs
  140. For each node that has a property described above, the bit corresponding to the
  141. node ID in the ``node_states[<property>]`` bitmask is set.
  142. For example, for node 2 with normal memory and CPUs, bit 2 will be set in ::
  143. node_states[N_POSSIBLE]
  144. node_states[N_ONLINE]
  145. node_states[N_NORMAL_MEMORY]
  146. node_states[N_HIGH_MEMORY]
  147. node_states[N_MEMORY]
  148. node_states[N_CPU]
  149. For various operations possible with nodemasks please refer to
  150. ``include/linux/nodemask.h``.
  151. Among other things, nodemasks are used to provide macros for node traversal,
  152. namely ``for_each_node()`` and ``for_each_online_node()``.
  153. For instance, to call a function foo() for each online node::
  154. for_each_online_node(nid) {
  155. pg_data_t *pgdat = NODE_DATA(nid);
  156. foo(pgdat);
  157. }
  158. Node structure
  159. --------------
  160. The nodes structure ``struct pglist_data`` is declared in
  161. ``include/linux/mmzone.h``. Here we briefly describe fields of this
  162. structure:
  163. General
  164. ~~~~~~~
  165. ``node_zones``
  166. The zones for this node. Not all of the zones may be populated, but it is
  167. the full list. It is referenced by this node's node_zonelists as well as
  168. other node's node_zonelists.
  169. ``node_zonelists``
  170. The list of all zones in all nodes. This list defines the order of zones
  171. that allocations are preferred from. The ``node_zonelists`` is set up by
  172. ``build_zonelists()`` in ``mm/page_alloc.c`` during the initialization of
  173. core memory management structures.
  174. ``nr_zones``
  175. Number of populated zones in this node.
  176. ``node_mem_map``
  177. For UMA systems that use FLATMEM memory model the 0's node
  178. ``node_mem_map`` is array of struct pages representing each physical frame.
  179. ``node_page_ext``
  180. For UMA systems that use FLATMEM memory model the 0's node
  181. ``node_page_ext`` is array of extensions of struct pages. Available only
  182. in the kernels built with ``CONFIG_PAGE_EXTENSION`` enabled.
  183. ``node_start_pfn``
  184. The page frame number of the starting page frame in this node.
  185. ``node_present_pages``
  186. Total number of physical pages present in this node.
  187. ``node_spanned_pages``
  188. Total size of physical page range, including holes.
  189. ``node_size_lock``
  190. A lock that protects the fields defining the node extents. Only defined when
  191. at least one of ``CONFIG_MEMORY_HOTPLUG`` or
  192. ``CONFIG_DEFERRED_STRUCT_PAGE_INIT`` configuration options are enabled.
  193. ``pgdat_resize_lock()`` and ``pgdat_resize_unlock()`` are provided to
  194. manipulate ``node_size_lock`` without checking for ``CONFIG_MEMORY_HOTPLUG``
  195. or ``CONFIG_DEFERRED_STRUCT_PAGE_INIT``.
  196. ``node_id``
  197. The Node ID (NID) of the node, starts at 0.
  198. ``totalreserve_pages``
  199. This is a per-node reserve of pages that are not available to userspace
  200. allocations.
  201. ``first_deferred_pfn``
  202. If memory initialization on large machines is deferred then this is the first
  203. PFN that needs to be initialized. Defined only when
  204. ``CONFIG_DEFERRED_STRUCT_PAGE_INIT`` is enabled
  205. ``deferred_split_queue``
  206. Per-node queue of huge pages that their split was deferred. Defined only when ``CONFIG_TRANSPARENT_HUGEPAGE`` is enabled.
  207. ``__lruvec``
  208. Per-node lruvec holding LRU lists and related parameters. Used only when
  209. memory cgroups are disabled. It should not be accessed directly, use
  210. ``mem_cgroup_lruvec()`` to look up lruvecs instead.
  211. Reclaim control
  212. ~~~~~~~~~~~~~~~
  213. See also Documentation/mm/page_reclaim.rst.
  214. ``kswapd``
  215. Per-node instance of kswapd kernel thread.
  216. ``kswapd_wait``, ``pfmemalloc_wait``, ``reclaim_wait``
  217. Workqueues used to synchronize memory reclaim tasks
  218. ``nr_writeback_throttled``
  219. Number of tasks that are throttled waiting on dirty pages to clean.
  220. ``nr_reclaim_start``
  221. Number of pages written while reclaim is throttled waiting for writeback.
  222. ``kswapd_order``
  223. Controls the order kswapd tries to reclaim
  224. ``kswapd_highest_zoneidx``
  225. The highest zone index to be reclaimed by kswapd
  226. ``kswapd_failures``
  227. Number of runs kswapd was unable to reclaim any pages
  228. ``min_unmapped_pages``
  229. Minimal number of unmapped file backed pages that cannot be reclaimed.
  230. Determined by ``vm.min_unmapped_ratio`` sysctl. Only defined when
  231. ``CONFIG_NUMA`` is enabled.
  232. ``min_slab_pages``
  233. Minimal number of SLAB pages that cannot be reclaimed. Determined by
  234. ``vm.min_slab_ratio sysctl``. Only defined when ``CONFIG_NUMA`` is enabled
  235. ``flags``
  236. Flags controlling reclaim behavior.
  237. Compaction control
  238. ~~~~~~~~~~~~~~~~~~
  239. ``kcompactd_max_order``
  240. Page order that kcompactd should try to achieve.
  241. ``kcompactd_highest_zoneidx``
  242. The highest zone index to be compacted by kcompactd.
  243. ``kcompactd_wait``
  244. Workqueue used to synchronize memory compaction tasks.
  245. ``kcompactd``
  246. Per-node instance of kcompactd kernel thread.
  247. ``proactive_compact_trigger``
  248. Determines if proactive compaction is enabled. Controlled by
  249. ``vm.compaction_proactiveness`` sysctl.
  250. Statistics
  251. ~~~~~~~~~~
  252. ``per_cpu_nodestats``
  253. Per-CPU VM statistics for the node
  254. ``vm_stat``
  255. VM statistics for the node.
  256. .. _zones:
  257. Zones
  258. =====
  259. .. admonition:: Stub
  260. This section is incomplete. Please list and describe the appropriate fields.
  261. .. _pages:
  262. Pages
  263. =====
  264. .. admonition:: Stub
  265. This section is incomplete. Please list and describe the appropriate fields.
  266. .. _folios:
  267. Folios
  268. ======
  269. .. admonition:: Stub
  270. This section is incomplete. Please list and describe the appropriate fields.
  271. .. _initialization:
  272. Initialization
  273. ==============
  274. .. admonition:: Stub
  275. This section is incomplete. Please list and describe the appropriate fields.