blob: 0f69a9fec34ddbf3574955312e763b23f7ce45d9 [file] [log] [blame]
Mike Rapoporteeb8a642018-03-21 21:22:21 +02001.. _highmem:
Peter Zijlstrad65bfac2010-10-26 14:21:54 -07002
Mike Rapoporteeb8a642018-03-21 21:22:21 +02003====================
4High Memory Handling
5====================
Peter Zijlstrad65bfac2010-10-26 14:21:54 -07006
7By: Peter Zijlstra <a.p.zijlstra@chello.nl>
8
Mike Rapoporteeb8a642018-03-21 21:22:21 +02009.. contents:: :local:
Peter Zijlstrad65bfac2010-10-26 14:21:54 -070010
Mike Rapoporteeb8a642018-03-21 21:22:21 +020011What Is High Memory?
Peter Zijlstrad65bfac2010-10-26 14:21:54 -070012====================
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
Mike Rapoporteeb8a642018-03-21 21:22:21 +020030userspace and the top 1GiB for kernel space::
Peter Zijlstrad65bfac2010-10-26 14:21:54 -070031
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
Mike Rapoporteeb8a642018-03-21 21:22:21 +020050Temporary Virtual Mappings
Peter Zijlstrad65bfac2010-10-26 14:21:54 -070051==========================
52
53The kernel contains several ways of creating temporary mappings:
54
Mike Rapoporteeb8a642018-03-21 21:22:21 +020055* vmap(). This can be used to make a long duration mapping of multiple
56 physical pages into a contiguous virtual space. It needs global
57 synchronization to unmap.
Peter Zijlstrad65bfac2010-10-26 14:21:54 -070058
Mike Rapoporteeb8a642018-03-21 21:22:21 +020059* kmap(). This permits a short duration mapping of a single page. It needs
60 global synchronization, but is amortized somewhat. It is also prone to
61 deadlocks when using in a nested fashion, and so it is not recommended for
62 new code.
Peter Zijlstrad65bfac2010-10-26 14:21:54 -070063
Mike Rapoporteeb8a642018-03-21 21:22:21 +020064* kmap_atomic(). This permits a very short duration mapping of a single
65 page. Since the mapping is restricted to the CPU that issued it, it
66 performs well, but the issuing task is therefore required to stay on that
67 CPU until it has finished, lest some other task displace its mappings.
Peter Zijlstrad65bfac2010-10-26 14:21:54 -070068
Mike Rapoporteeb8a642018-03-21 21:22:21 +020069 kmap_atomic() may also be used by interrupt contexts, since it is does not
70 sleep and the caller may not sleep until after kunmap_atomic() is called.
Peter Zijlstrad65bfac2010-10-26 14:21:54 -070071
Mike Rapoporteeb8a642018-03-21 21:22:21 +020072 It may be assumed that k[un]map_atomic() won't fail.
Peter Zijlstrad65bfac2010-10-26 14:21:54 -070073
74
Mike Rapoporteeb8a642018-03-21 21:22:21 +020075Using kmap_atomic
Peter Zijlstrad65bfac2010-10-26 14:21:54 -070076=================
77
78When and where to use kmap_atomic() is straightforward. It is used when code
79wants to access the contents of a page that might be allocated from high memory
80(see __GFP_HIGHMEM), for example a page in the pagecache. The API has two
Mike Rapoporteeb8a642018-03-21 21:22:21 +020081functions, and they can be used in a manner similar to the following::
Peter Zijlstrad65bfac2010-10-26 14:21:54 -070082
83 /* Find the page of interest. */
84 struct page *page = find_get_page(mapping, offset);
85
86 /* Gain access to the contents of that page. */
87 void *vaddr = kmap_atomic(page);
88
89 /* Do something to the contents of that page. */
90 memset(vaddr, 0, PAGE_SIZE);
91
92 /* Unmap that page. */
93 kunmap_atomic(vaddr);
94
95Note that the kunmap_atomic() call takes the result of the kmap_atomic() call
96not the argument.
97
98If you need to map two pages because you want to copy from one page to
Mike Rapoporteeb8a642018-03-21 21:22:21 +020099another you need to keep the kmap_atomic calls strictly nested, like::
Peter Zijlstrad65bfac2010-10-26 14:21:54 -0700100
101 vaddr1 = kmap_atomic(page1);
102 vaddr2 = kmap_atomic(page2);
103
104 memcpy(vaddr1, vaddr2, PAGE_SIZE);
105
106 kunmap_atomic(vaddr2);
107 kunmap_atomic(vaddr1);
108
109
Mike Rapoporteeb8a642018-03-21 21:22:21 +0200110Cost of Temporary Mappings
Peter Zijlstrad65bfac2010-10-26 14:21:54 -0700111==========================
112
113The cost of creating temporary mappings can be quite high. The arch has to
114manipulate the kernel's page tables, the data TLB and/or the MMU's registers.
115
116If CONFIG_HIGHMEM is not set, then the kernel will try and create a mapping
117simply with a bit of arithmetic that will convert the page struct address into
118a pointer to the page contents rather than juggling mappings about. In such a
119case, the unmap operation may be a null operation.
120
121If CONFIG_MMU is not set, then there can be no temporary mappings and no
122highmem. In such a case, the arithmetic approach will also be used.
123
124
Peter Zijlstrad65bfac2010-10-26 14:21:54 -0700125i386 PAE
126========
127
128The i386 arch, under some circumstances, will permit you to stick up to 64GiB
129of RAM into your 32-bit machine. This has a number of consequences:
130
Mike Rapoporteeb8a642018-03-21 21:22:21 +0200131* Linux needs a page-frame structure for each page in the system and the
132 pageframes need to live in the permanent mapping, which means:
Peter Zijlstrad65bfac2010-10-26 14:21:54 -0700133
Mike Rapoporteeb8a642018-03-21 21:22:21 +0200134* you can have 896M/sizeof(struct page) page-frames at most; with struct
135 page being 32-bytes that would end up being something in the order of 112G
136 worth of pages; the kernel, however, needs to store more than just
137 page-frames in that memory...
Peter Zijlstrad65bfac2010-10-26 14:21:54 -0700138
Mike Rapoporteeb8a642018-03-21 21:22:21 +0200139* PAE makes your page tables larger - which slows the system down as more
140 data has to be accessed to traverse in TLB fills and the like. One
141 advantage is that PAE has more PTE bits and can provide advanced features
142 like NX and PAT.
Peter Zijlstrad65bfac2010-10-26 14:21:54 -0700143
144The general recommendation is that you don't use more than 8GiB on a 32-bit
145machine - although more might work for you and your workload, you're pretty
146much on your own - don't expect kernel developers to really care much if things
147come apart.