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