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