highmem.txt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. ====================
  2. HIGH MEMORY HANDLING
  3. ====================
  4. By: Peter Zijlstra <a.p.zijlstra@chello.nl>
  5. Contents:
  6. (*) What is high memory?
  7. (*) Temporary virtual mappings.
  8. (*) Using kmap_atomic.
  9. (*) Cost of temporary mappings.
  10. (*) i386 PAE.
  11. ====================
  12. WHAT IS HIGH MEMORY?
  13. ====================
  14. High memory (highmem) is used when the size of physical memory approaches or
  15. exceeds the maximum size of virtual memory. At that point it becomes
  16. impossible for the kernel to keep all of the available physical memory mapped
  17. at all times. This means the kernel needs to start using temporary mappings of
  18. the pieces of physical memory that it wants to access.
  19. The part of (physical) memory not covered by a permanent mapping is what we
  20. refer to as 'highmem'. There are various architecture dependent constraints on
  21. where exactly that border lies.
  22. In the i386 arch, for example, we choose to map the kernel into every process's
  23. VM space so that we don't have to pay the full TLB invalidation costs for
  24. kernel entry/exit. This means the available virtual memory space (4GiB on
  25. i386) has to be divided between user and kernel space.
  26. The traditional split for architectures using this approach is 3:1, 3GiB for
  27. userspace and the top 1GiB for kernel space:
  28. +--------+ 0xffffffff
  29. | Kernel |
  30. +--------+ 0xc0000000
  31. | |
  32. | User |
  33. | |
  34. +--------+ 0x00000000
  35. This means that the kernel can at most map 1GiB of physical memory at any one
  36. time, but because we need virtual address space for other things - including
  37. temporary maps to access the rest of the physical memory - the actual direct
  38. map will typically be less (usually around ~896MiB).
  39. Other architectures that have mm context tagged TLBs can have separate kernel
  40. and user maps. Some hardware (like some ARMs), however, have limited virtual
  41. space when they use mm context tags.
  42. ==========================
  43. TEMPORARY VIRTUAL MAPPINGS
  44. ==========================
  45. The kernel contains several ways of creating temporary mappings:
  46. (*) vmap(). This can be used to make a long duration mapping of multiple
  47. physical pages into a contiguous virtual space. It needs global
  48. synchronization to unmap.
  49. (*) kmap(). This permits a short duration mapping of a single page. It needs
  50. global synchronization, but is amortized somewhat. It is also prone to
  51. deadlocks when using in a nested fashion, and so it is not recommended for
  52. new code.
  53. (*) kmap_atomic(). This permits a very short duration mapping of a single
  54. page. Since the mapping is restricted to the CPU that issued it, it
  55. performs well, but the issuing task is therefore required to stay on that
  56. CPU until it has finished, lest some other task displace its mappings.
  57. kmap_atomic() may also be used by interrupt contexts, since it is does not
  58. sleep and the caller may not sleep until after kunmap_atomic() is called.
  59. It may be assumed that k[un]map_atomic() won't fail.
  60. =================
  61. USING KMAP_ATOMIC
  62. =================
  63. When and where to use kmap_atomic() is straightforward. It is used when code
  64. wants to access the contents of a page that might be allocated from high memory
  65. (see __GFP_HIGHMEM), for example a page in the pagecache. The API has two
  66. functions, and they can be used in a manner similar to the following:
  67. /* Find the page of interest. */
  68. struct page *page = find_get_page(mapping, offset);
  69. /* Gain access to the contents of that page. */
  70. void *vaddr = kmap_atomic(page);
  71. /* Do something to the contents of that page. */
  72. memset(vaddr, 0, PAGE_SIZE);
  73. /* Unmap that page. */
  74. kunmap_atomic(vaddr);
  75. Note that the kunmap_atomic() call takes the result of the kmap_atomic() call
  76. not the argument.
  77. If you need to map two pages because you want to copy from one page to
  78. another you need to keep the kmap_atomic calls strictly nested, like:
  79. vaddr1 = kmap_atomic(page1);
  80. vaddr2 = kmap_atomic(page2);
  81. memcpy(vaddr1, vaddr2, PAGE_SIZE);
  82. kunmap_atomic(vaddr2);
  83. kunmap_atomic(vaddr1);
  84. ==========================
  85. COST OF TEMPORARY MAPPINGS
  86. ==========================
  87. The cost of creating temporary mappings can be quite high. The arch has to
  88. manipulate the kernel's page tables, the data TLB and/or the MMU's registers.
  89. If CONFIG_HIGHMEM is not set, then the kernel will try and create a mapping
  90. simply with a bit of arithmetic that will convert the page struct address into
  91. a pointer to the page contents rather than juggling mappings about. In such a
  92. case, the unmap operation may be a null operation.
  93. If CONFIG_MMU is not set, then there can be no temporary mappings and no
  94. highmem. In such a case, the arithmetic approach will also be used.
  95. ========
  96. i386 PAE
  97. ========
  98. The i386 arch, under some circumstances, will permit you to stick up to 64GiB
  99. of RAM into your 32-bit machine. This has a number of consequences:
  100. (*) Linux needs a page-frame structure for each page in the system and the
  101. pageframes need to live in the permanent mapping, which means:
  102. (*) you can have 896M/sizeof(struct page) page-frames at most; with struct
  103. page being 32-bytes that would end up being something in the order of 112G
  104. worth of pages; the kernel, however, needs to store more than just
  105. page-frames in that memory...
  106. (*) PAE makes your page tables larger - which slows the system down as more
  107. data has to be accessed to traverse in TLB fills and the like. One
  108. advantage is that PAE has more PTE bits and can provide advanced features
  109. like NX and PAT.
  110. The general recommendation is that you don't use more than 8GiB on a 32-bit
  111. machine - although more might work for you and your workload, you're pretty
  112. much on your own - don't expect kernel developers to really care much if things
  113. come apart.