Back to home page

OSCL-LXR

 
 

    


0001 .. _highmem:
0002 
0003 ====================
0004 High Memory Handling
0005 ====================
0006 
0007 By: Peter Zijlstra <a.p.zijlstra@chello.nl>
0008 
0009 .. contents:: :local:
0010 
0011 What Is High Memory?
0012 ====================
0013 
0014 High memory (highmem) is used when the size of physical memory approaches or
0015 exceeds the maximum size of virtual memory.  At that point it becomes
0016 impossible for the kernel to keep all of the available physical memory mapped
0017 at all times.  This means the kernel needs to start using temporary mappings of
0018 the pieces of physical memory that it wants to access.
0019 
0020 The part of (physical) memory not covered by a permanent mapping is what we
0021 refer to as 'highmem'.  There are various architecture dependent constraints on
0022 where exactly that border lies.
0023 
0024 In the i386 arch, for example, we choose to map the kernel into every process's
0025 VM space so that we don't have to pay the full TLB invalidation costs for
0026 kernel entry/exit.  This means the available virtual memory space (4GiB on
0027 i386) has to be divided between user and kernel space.
0028 
0029 The traditional split for architectures using this approach is 3:1, 3GiB for
0030 userspace and the top 1GiB for kernel space::
0031 
0032                 +--------+ 0xffffffff
0033                 | Kernel |
0034                 +--------+ 0xc0000000
0035                 |        |
0036                 | User   |
0037                 |        |
0038                 +--------+ 0x00000000
0039 
0040 This means that the kernel can at most map 1GiB of physical memory at any one
0041 time, but because we need virtual address space for other things - including
0042 temporary maps to access the rest of the physical memory - the actual direct
0043 map will typically be less (usually around ~896MiB).
0044 
0045 Other architectures that have mm context tagged TLBs can have separate kernel
0046 and user maps.  Some hardware (like some ARMs), however, have limited virtual
0047 space when they use mm context tags.
0048 
0049 
0050 Temporary Virtual Mappings
0051 ==========================
0052 
0053 The kernel contains several ways of creating temporary mappings. The following
0054 list shows them in order of preference of use.
0055 
0056 * kmap_local_page().  This function is used to require short term mappings.
0057   It can be invoked from any context (including interrupts) but the mappings
0058   can only be used in the context which acquired them.
0059 
0060   This function should be preferred, where feasible, over all the others.
0061 
0062   These mappings are thread-local and CPU-local, meaning that the mapping
0063   can only be accessed from within this thread and the thread is bound to the
0064   CPU while the mapping is active. Although preemption is never disabled by
0065   this function, the CPU can not be unplugged from the system via
0066   CPU-hotplug until the mapping is disposed.
0067 
0068   It's valid to take pagefaults in a local kmap region, unless the context
0069   in which the local mapping is acquired does not allow it for other reasons.
0070 
0071   As said, pagefaults and preemption are never disabled. There is no need to
0072   disable preemption because, when context switches to a different task, the
0073   maps of the outgoing task are saved and those of the incoming one are
0074   restored.
0075 
0076   kmap_local_page() always returns a valid virtual address and it is assumed
0077   that kunmap_local() will never fail.
0078 
0079   On CONFIG_HIGHMEM=n kernels and for low memory pages this returns the
0080   virtual address of the direct mapping. Only real highmem pages are
0081   temporarily mapped. Therefore, users may call a plain page_address()
0082   for pages which are known to not come from ZONE_HIGHMEM. However, it is
0083   always safe to use kmap_local_page() / kunmap_local().
0084 
0085   While it is significantly faster than kmap(), for the higmem case it
0086   comes with restrictions about the pointers validity. Contrary to kmap()
0087   mappings, the local mappings are only valid in the context of the caller
0088   and cannot be handed to other contexts. This implies that users must
0089   be absolutely sure to keep the use of the return address local to the
0090   thread which mapped it.
0091 
0092   Most code can be designed to use thread local mappings. User should
0093   therefore try to design their code to avoid the use of kmap() by mapping
0094   pages in the same thread the address will be used and prefer
0095   kmap_local_page().
0096 
0097   Nesting kmap_local_page() and kmap_atomic() mappings is allowed to a certain
0098   extent (up to KMAP_TYPE_NR) but their invocations have to be strictly ordered
0099   because the map implementation is stack based. See kmap_local_page() kdocs
0100   (included in the "Functions" section) for details on how to manage nested
0101   mappings.
0102 
0103 * kmap_atomic().  This permits a very short duration mapping of a single
0104   page.  Since the mapping is restricted to the CPU that issued it, it
0105   performs well, but the issuing task is therefore required to stay on that
0106   CPU until it has finished, lest some other task displace its mappings.
0107 
0108   kmap_atomic() may also be used by interrupt contexts, since it does not
0109   sleep and the callers too may not sleep until after kunmap_atomic() is
0110   called.
0111 
0112   Each call of kmap_atomic() in the kernel creates a non-preemptible section
0113   and disable pagefaults. This could be a source of unwanted latency. Therefore
0114   users should prefer kmap_local_page() instead of kmap_atomic().
0115 
0116   It is assumed that k[un]map_atomic() won't fail.
0117 
0118 * kmap().  This should be used to make short duration mapping of a single
0119   page with no restrictions on preemption or migration. It comes with an
0120   overhead as mapping space is restricted and protected by a global lock
0121   for synchronization. When mapping is no longer needed, the address that
0122   the page was mapped to must be released with kunmap().
0123 
0124   Mapping changes must be propagated across all the CPUs. kmap() also
0125   requires global TLB invalidation when the kmap's pool wraps and it might
0126   block when the mapping space is fully utilized until a slot becomes
0127   available. Therefore, kmap() is only callable from preemptible context.
0128 
0129   All the above work is necessary if a mapping must last for a relatively
0130   long time but the bulk of high-memory mappings in the kernel are
0131   short-lived and only used in one place. This means that the cost of
0132   kmap() is mostly wasted in such cases. kmap() was not intended for long
0133   term mappings but it has morphed in that direction and its use is
0134   strongly discouraged in newer code and the set of the preceding functions
0135   should be preferred.
0136 
0137   On 64-bit systems, calls to kmap_local_page(), kmap_atomic() and kmap() have
0138   no real work to do because a 64-bit address space is more than sufficient to
0139   address all the physical memory whose pages are permanently mapped.
0140 
0141 * vmap().  This can be used to make a long duration mapping of multiple
0142   physical pages into a contiguous virtual space.  It needs global
0143   synchronization to unmap.
0144 
0145 
0146 Cost of Temporary Mappings
0147 ==========================
0148 
0149 The cost of creating temporary mappings can be quite high.  The arch has to
0150 manipulate the kernel's page tables, the data TLB and/or the MMU's registers.
0151 
0152 If CONFIG_HIGHMEM is not set, then the kernel will try and create a mapping
0153 simply with a bit of arithmetic that will convert the page struct address into
0154 a pointer to the page contents rather than juggling mappings about.  In such a
0155 case, the unmap operation may be a null operation.
0156 
0157 If CONFIG_MMU is not set, then there can be no temporary mappings and no
0158 highmem.  In such a case, the arithmetic approach will also be used.
0159 
0160 
0161 i386 PAE
0162 ========
0163 
0164 The i386 arch, under some circumstances, will permit you to stick up to 64GiB
0165 of RAM into your 32-bit machine.  This has a number of consequences:
0166 
0167 * Linux needs a page-frame structure for each page in the system and the
0168   pageframes need to live in the permanent mapping, which means:
0169 
0170 * you can have 896M/sizeof(struct page) page-frames at most; with struct
0171   page being 32-bytes that would end up being something in the order of 112G
0172   worth of pages; the kernel, however, needs to store more than just
0173   page-frames in that memory...
0174 
0175 * PAE makes your page tables larger - which slows the system down as more
0176   data has to be accessed to traverse in TLB fills and the like.  One
0177   advantage is that PAE has more PTE bits and can provide advanced features
0178   like NX and PAT.
0179 
0180 The general recommendation is that you don't use more than 8GiB on a 32-bit
0181 machine - although more might work for you and your workload, you're pretty
0182 much on your own - don't expect kernel developers to really care much if things
0183 come apart.
0184 
0185 
0186 Functions
0187 =========
0188 
0189 .. kernel-doc:: include/linux/highmem.h
0190 .. kernel-doc:: include/linux/highmem-internal.h