highmem.rst 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. ====================
  2. High Memory Handling
  3. ====================
  4. By: Peter Zijlstra <a.p.zijlstra@chello.nl>
  5. .. contents:: :local:
  6. What Is High Memory?
  7. ====================
  8. High memory (highmem) is used when the size of physical memory approaches or
  9. exceeds the maximum size of virtual memory. At that point it becomes
  10. impossible for the kernel to keep all of the available physical memory mapped
  11. at all times. This means the kernel needs to start using temporary mappings of
  12. the pieces of physical memory that it wants to access.
  13. The part of (physical) memory not covered by a permanent mapping is what we
  14. refer to as 'highmem'. There are various architecture dependent constraints on
  15. where exactly that border lies.
  16. In the i386 arch, for example, we choose to map the kernel into every process's
  17. VM space so that we don't have to pay the full TLB invalidation costs for
  18. kernel entry/exit. This means the available virtual memory space (4GiB on
  19. i386) has to be divided between user and kernel space.
  20. The traditional split for architectures using this approach is 3:1, 3GiB for
  21. userspace and the top 1GiB for kernel space::
  22. +--------+ 0xffffffff
  23. | Kernel |
  24. +--------+ 0xc0000000
  25. | |
  26. | User |
  27. | |
  28. +--------+ 0x00000000
  29. This means that the kernel can at most map 1GiB of physical memory at any one
  30. time, but because we need virtual address space for other things - including
  31. temporary maps to access the rest of the physical memory - the actual direct
  32. map will typically be less (usually around ~896MiB).
  33. Other architectures that have mm context tagged TLBs can have separate kernel
  34. and user maps. Some hardware (like some ARMs), however, have limited virtual
  35. space when they use mm context tags.
  36. Temporary Virtual Mappings
  37. ==========================
  38. The kernel contains several ways of creating temporary mappings. The following
  39. list shows them in order of preference of use.
  40. * kmap_local_page(), kmap_local_folio() - These functions are used to create
  41. short term mappings. They can be invoked from any context (including
  42. interrupts) but the mappings can only be used in the context which acquired
  43. them. The only differences between them consist in the first taking a pointer
  44. to a struct page and the second taking a pointer to struct folio and the byte
  45. offset within the folio which identifies the page.
  46. These functions should always be used, whereas kmap_atomic() and kmap() have
  47. been deprecated.
  48. These mappings are thread-local and CPU-local, meaning that the mapping
  49. can only be accessed from within this thread and the thread is bound to the
  50. CPU while the mapping is active. Although preemption is never disabled by
  51. this function, the CPU can not be unplugged from the system via
  52. CPU-hotplug until the mapping is disposed.
  53. It's valid to take pagefaults in a local kmap region, unless the context
  54. in which the local mapping is acquired does not allow it for other reasons.
  55. As said, pagefaults and preemption are never disabled. There is no need to
  56. disable preemption because, when context switches to a different task, the
  57. maps of the outgoing task are saved and those of the incoming one are
  58. restored.
  59. kmap_local_page(), as well as kmap_local_folio() always returns valid virtual
  60. kernel addresses and it is assumed that kunmap_local() will never fail.
  61. On CONFIG_HIGHMEM=n kernels and for low memory pages they return the
  62. virtual address of the direct mapping. Only real highmem pages are
  63. temporarily mapped. Therefore, users may call a plain page_address()
  64. for pages which are known to not come from ZONE_HIGHMEM. However, it is
  65. always safe to use kmap_local_{page,folio}() / kunmap_local().
  66. While they are significantly faster than kmap(), for the highmem case they
  67. come with restrictions about the pointers validity. Contrary to kmap()
  68. mappings, the local mappings are only valid in the context of the caller
  69. and cannot be handed to other contexts. This implies that users must
  70. be absolutely sure to keep the use of the return address local to the
  71. thread which mapped it.
  72. Most code can be designed to use thread local mappings. User should
  73. therefore try to design their code to avoid the use of kmap() by mapping
  74. pages in the same thread the address will be used and prefer
  75. kmap_local_page() or kmap_local_folio().
  76. Nesting kmap_local_page() and kmap_atomic() mappings is allowed to a certain
  77. extent (up to KMAP_TYPE_NR) but their invocations have to be strictly ordered
  78. because the map implementation is stack based. See kmap_local_page() kdocs
  79. (included in the "Functions" section) for details on how to manage nested
  80. mappings.
  81. * kmap_atomic(). This function has been deprecated; use kmap_local_page().
  82. NOTE: Conversions to kmap_local_page() must take care to follow the mapping
  83. restrictions imposed on kmap_local_page(). Furthermore, the code between
  84. calls to kmap_atomic() and kunmap_atomic() may implicitly depend on the side
  85. effects of atomic mappings, i.e. disabling page faults or preemption, or both.
  86. In that case, explicit calls to pagefault_disable() or preempt_disable() or
  87. both must be made in conjunction with the use of kmap_local_page().
  88. [Legacy documentation]
  89. This permits a very short duration mapping of a single page. Since the
  90. mapping is restricted to the CPU that issued it, it performs well, but
  91. the issuing task is therefore required to stay on that CPU until it has
  92. finished, lest some other task displace its mappings.
  93. kmap_atomic() may also be used by interrupt contexts, since it does not
  94. sleep and the callers too may not sleep until after kunmap_atomic() is
  95. called.
  96. Each call of kmap_atomic() in the kernel creates a non-preemptible section
  97. and disable pagefaults. This could be a source of unwanted latency. Therefore
  98. users should prefer kmap_local_page() instead of kmap_atomic().
  99. It is assumed that k[un]map_atomic() won't fail.
  100. * kmap(). This function has been deprecated; use kmap_local_page().
  101. NOTE: Conversions to kmap_local_page() must take care to follow the mapping
  102. restrictions imposed on kmap_local_page(). In particular, it is necessary to
  103. make sure that the kernel virtual memory pointer is only valid in the thread
  104. that obtained it.
  105. [Legacy documentation]
  106. This should be used to make short duration mapping of a single page with no
  107. restrictions on preemption or migration. It comes with an overhead as mapping
  108. space is restricted and protected by a global lock for synchronization. When
  109. mapping is no longer needed, the address that the page was mapped to must be
  110. released with kunmap().
  111. Mapping changes must be propagated across all the CPUs. kmap() also
  112. requires global TLB invalidation when the kmap's pool wraps and it might
  113. block when the mapping space is fully utilized until a slot becomes
  114. available. Therefore, kmap() is only callable from preemptible context.
  115. All the above work is necessary if a mapping must last for a relatively
  116. long time but the bulk of high-memory mappings in the kernel are
  117. short-lived and only used in one place. This means that the cost of
  118. kmap() is mostly wasted in such cases. kmap() was not intended for long
  119. term mappings but it has morphed in that direction and its use is
  120. strongly discouraged in newer code and the set of the preceding functions
  121. should be preferred.
  122. On 64-bit systems, calls to kmap_local_page(), kmap_atomic() and kmap() have
  123. no real work to do because a 64-bit address space is more than sufficient to
  124. address all the physical memory whose pages are permanently mapped.
  125. * vmap(). This can be used to make a long duration mapping of multiple
  126. physical pages into a contiguous virtual space. It needs global
  127. synchronization to unmap.
  128. Cost of Temporary Mappings
  129. ==========================
  130. The cost of creating temporary mappings can be quite high. The arch has to
  131. manipulate the kernel's page tables, the data TLB and/or the MMU's registers.
  132. If CONFIG_HIGHMEM is not set, then the kernel will try and create a mapping
  133. simply with a bit of arithmetic that will convert the page struct address into
  134. a pointer to the page contents rather than juggling mappings about. In such a
  135. case, the unmap operation may be a null operation.
  136. If CONFIG_MMU is not set, then there can be no temporary mappings and no
  137. highmem. In such a case, the arithmetic approach will also be used.
  138. i386 PAE
  139. ========
  140. The i386 arch, under some circumstances, will permit you to stick up to 64GiB
  141. of RAM into your 32-bit machine. This has a number of consequences:
  142. * Linux needs a page-frame structure for each page in the system and the
  143. pageframes need to live in the permanent mapping, which means:
  144. * you can have 896M/sizeof(struct page) page-frames at most; with struct
  145. page being 32-bytes that would end up being something in the order of 112G
  146. worth of pages; the kernel, however, needs to store more than just
  147. page-frames in that memory...
  148. * PAE makes your page tables larger - which slows the system down as more
  149. data has to be accessed to traverse in TLB fills and the like. One
  150. advantage is that PAE has more PTE bits and can provide advanced features
  151. like NX and PAT.
  152. The general recommendation is that you don't use more than 8GiB on a 32-bit
  153. machine - although more might work for you and your workload, you're pretty
  154. much on your own - don't expect kernel developers to really care much if things
  155. come apart.
  156. Functions
  157. =========
  158. .. kernel-doc:: include/linux/highmem.h
  159. .. kernel-doc:: mm/highmem.c
  160. .. kernel-doc:: include/linux/highmem-internal.h