Back to home page

OSCL-LXR

 
 

    


0001 ==================================
0002 Cache and TLB Flushing Under Linux
0003 ==================================
0004 
0005 :Author: David S. Miller <davem@redhat.com>
0006 
0007 This document describes the cache/tlb flushing interfaces called
0008 by the Linux VM subsystem.  It enumerates over each interface,
0009 describes its intended purpose, and what side effect is expected
0010 after the interface is invoked.
0011 
0012 The side effects described below are stated for a uniprocessor
0013 implementation, and what is to happen on that single processor.  The
0014 SMP cases are a simple extension, in that you just extend the
0015 definition such that the side effect for a particular interface occurs
0016 on all processors in the system.  Don't let this scare you into
0017 thinking SMP cache/tlb flushing must be so inefficient, this is in
0018 fact an area where many optimizations are possible.  For example,
0019 if it can be proven that a user address space has never executed
0020 on a cpu (see mm_cpumask()), one need not perform a flush
0021 for this address space on that cpu.
0022 
0023 First, the TLB flushing interfaces, since they are the simplest.  The
0024 "TLB" is abstracted under Linux as something the cpu uses to cache
0025 virtual-->physical address translations obtained from the software
0026 page tables.  Meaning that if the software page tables change, it is
0027 possible for stale translations to exist in this "TLB" cache.
0028 Therefore when software page table changes occur, the kernel will
0029 invoke one of the following flush methods _after_ the page table
0030 changes occur:
0031 
0032 1) ``void flush_tlb_all(void)``
0033 
0034         The most severe flush of all.  After this interface runs,
0035         any previous page table modification whatsoever will be
0036         visible to the cpu.
0037 
0038         This is usually invoked when the kernel page tables are
0039         changed, since such translations are "global" in nature.
0040 
0041 2) ``void flush_tlb_mm(struct mm_struct *mm)``
0042 
0043         This interface flushes an entire user address space from
0044         the TLB.  After running, this interface must make sure that
0045         any previous page table modifications for the address space
0046         'mm' will be visible to the cpu.  That is, after running,
0047         there will be no entries in the TLB for 'mm'.
0048 
0049         This interface is used to handle whole address space
0050         page table operations such as what happens during
0051         fork, and exec.
0052 
0053 3) ``void flush_tlb_range(struct vm_area_struct *vma,
0054    unsigned long start, unsigned long end)``
0055 
0056         Here we are flushing a specific range of (user) virtual
0057         address translations from the TLB.  After running, this
0058         interface must make sure that any previous page table
0059         modifications for the address space 'vma->vm_mm' in the range
0060         'start' to 'end-1' will be visible to the cpu.  That is, after
0061         running, there will be no entries in the TLB for 'mm' for
0062         virtual addresses in the range 'start' to 'end-1'.
0063 
0064         The "vma" is the backing store being used for the region.
0065         Primarily, this is used for munmap() type operations.
0066 
0067         The interface is provided in hopes that the port can find
0068         a suitably efficient method for removing multiple page
0069         sized translations from the TLB, instead of having the kernel
0070         call flush_tlb_page (see below) for each entry which may be
0071         modified.
0072 
0073 4) ``void flush_tlb_page(struct vm_area_struct *vma, unsigned long addr)``
0074 
0075         This time we need to remove the PAGE_SIZE sized translation
0076         from the TLB.  The 'vma' is the backing structure used by
0077         Linux to keep track of mmap'd regions for a process, the
0078         address space is available via vma->vm_mm.  Also, one may
0079         test (vma->vm_flags & VM_EXEC) to see if this region is
0080         executable (and thus could be in the 'instruction TLB' in
0081         split-tlb type setups).
0082 
0083         After running, this interface must make sure that any previous
0084         page table modification for address space 'vma->vm_mm' for
0085         user virtual address 'addr' will be visible to the cpu.  That
0086         is, after running, there will be no entries in the TLB for
0087         'vma->vm_mm' for virtual address 'addr'.
0088 
0089         This is used primarily during fault processing.
0090 
0091 5) ``void update_mmu_cache(struct vm_area_struct *vma,
0092    unsigned long address, pte_t *ptep)``
0093 
0094         At the end of every page fault, this routine is invoked to
0095         tell the architecture specific code that a translation
0096         now exists at virtual address "address" for address space
0097         "vma->vm_mm", in the software page tables.
0098 
0099         A port may use this information in any way it so chooses.
0100         For example, it could use this event to pre-load TLB
0101         translations for software managed TLB configurations.
0102         The sparc64 port currently does this.
0103 
0104 Next, we have the cache flushing interfaces.  In general, when Linux
0105 is changing an existing virtual-->physical mapping to a new value,
0106 the sequence will be in one of the following forms::
0107 
0108         1) flush_cache_mm(mm);
0109            change_all_page_tables_of(mm);
0110            flush_tlb_mm(mm);
0111 
0112         2) flush_cache_range(vma, start, end);
0113            change_range_of_page_tables(mm, start, end);
0114            flush_tlb_range(vma, start, end);
0115 
0116         3) flush_cache_page(vma, addr, pfn);
0117            set_pte(pte_pointer, new_pte_val);
0118            flush_tlb_page(vma, addr);
0119 
0120 The cache level flush will always be first, because this allows
0121 us to properly handle systems whose caches are strict and require
0122 a virtual-->physical translation to exist for a virtual address
0123 when that virtual address is flushed from the cache.  The HyperSparc
0124 cpu is one such cpu with this attribute.
0125 
0126 The cache flushing routines below need only deal with cache flushing
0127 to the extent that it is necessary for a particular cpu.  Mostly,
0128 these routines must be implemented for cpus which have virtually
0129 indexed caches which must be flushed when virtual-->physical
0130 translations are changed or removed.  So, for example, the physically
0131 indexed physically tagged caches of IA32 processors have no need to
0132 implement these interfaces since the caches are fully synchronized
0133 and have no dependency on translation information.
0134 
0135 Here are the routines, one by one:
0136 
0137 1) ``void flush_cache_mm(struct mm_struct *mm)``
0138 
0139         This interface flushes an entire user address space from
0140         the caches.  That is, after running, there will be no cache
0141         lines associated with 'mm'.
0142 
0143         This interface is used to handle whole address space
0144         page table operations such as what happens during exit and exec.
0145 
0146 2) ``void flush_cache_dup_mm(struct mm_struct *mm)``
0147 
0148         This interface flushes an entire user address space from
0149         the caches.  That is, after running, there will be no cache
0150         lines associated with 'mm'.
0151 
0152         This interface is used to handle whole address space
0153         page table operations such as what happens during fork.
0154 
0155         This option is separate from flush_cache_mm to allow some
0156         optimizations for VIPT caches.
0157 
0158 3) ``void flush_cache_range(struct vm_area_struct *vma,
0159    unsigned long start, unsigned long end)``
0160 
0161         Here we are flushing a specific range of (user) virtual
0162         addresses from the cache.  After running, there will be no
0163         entries in the cache for 'vma->vm_mm' for virtual addresses in
0164         the range 'start' to 'end-1'.
0165 
0166         The "vma" is the backing store being used for the region.
0167         Primarily, this is used for munmap() type operations.
0168 
0169         The interface is provided in hopes that the port can find
0170         a suitably efficient method for removing multiple page
0171         sized regions from the cache, instead of having the kernel
0172         call flush_cache_page (see below) for each entry which may be
0173         modified.
0174 
0175 4) ``void flush_cache_page(struct vm_area_struct *vma, unsigned long addr, unsigned long pfn)``
0176 
0177         This time we need to remove a PAGE_SIZE sized range
0178         from the cache.  The 'vma' is the backing structure used by
0179         Linux to keep track of mmap'd regions for a process, the
0180         address space is available via vma->vm_mm.  Also, one may
0181         test (vma->vm_flags & VM_EXEC) to see if this region is
0182         executable (and thus could be in the 'instruction cache' in
0183         "Harvard" type cache layouts).
0184 
0185         The 'pfn' indicates the physical page frame (shift this value
0186         left by PAGE_SHIFT to get the physical address) that 'addr'
0187         translates to.  It is this mapping which should be removed from
0188         the cache.
0189 
0190         After running, there will be no entries in the cache for
0191         'vma->vm_mm' for virtual address 'addr' which translates
0192         to 'pfn'.
0193 
0194         This is used primarily during fault processing.
0195 
0196 5) ``void flush_cache_kmaps(void)``
0197 
0198         This routine need only be implemented if the platform utilizes
0199         highmem.  It will be called right before all of the kmaps
0200         are invalidated.
0201 
0202         After running, there will be no entries in the cache for
0203         the kernel virtual address range PKMAP_ADDR(0) to
0204         PKMAP_ADDR(LAST_PKMAP).
0205 
0206         This routing should be implemented in asm/highmem.h
0207 
0208 6) ``void flush_cache_vmap(unsigned long start, unsigned long end)``
0209    ``void flush_cache_vunmap(unsigned long start, unsigned long end)``
0210 
0211         Here in these two interfaces we are flushing a specific range
0212         of (kernel) virtual addresses from the cache.  After running,
0213         there will be no entries in the cache for the kernel address
0214         space for virtual addresses in the range 'start' to 'end-1'.
0215 
0216         The first of these two routines is invoked after vmap_range()
0217         has installed the page table entries.  The second is invoked
0218         before vunmap_range() deletes the page table entries.
0219 
0220 There exists another whole class of cpu cache issues which currently
0221 require a whole different set of interfaces to handle properly.
0222 The biggest problem is that of virtual aliasing in the data cache
0223 of a processor.
0224 
0225 Is your port susceptible to virtual aliasing in its D-cache?
0226 Well, if your D-cache is virtually indexed, is larger in size than
0227 PAGE_SIZE, and does not prevent multiple cache lines for the same
0228 physical address from existing at once, you have this problem.
0229 
0230 If your D-cache has this problem, first define asm/shmparam.h SHMLBA
0231 properly, it should essentially be the size of your virtually
0232 addressed D-cache (or if the size is variable, the largest possible
0233 size).  This setting will force the SYSv IPC layer to only allow user
0234 processes to mmap shared memory at address which are a multiple of
0235 this value.
0236 
0237 .. note::
0238 
0239   This does not fix shared mmaps, check out the sparc64 port for
0240   one way to solve this (in particular SPARC_FLAG_MMAPSHARED).
0241 
0242 Next, you have to solve the D-cache aliasing issue for all
0243 other cases.  Please keep in mind that fact that, for a given page
0244 mapped into some user address space, there is always at least one more
0245 mapping, that of the kernel in its linear mapping starting at
0246 PAGE_OFFSET.  So immediately, once the first user maps a given
0247 physical page into its address space, by implication the D-cache
0248 aliasing problem has the potential to exist since the kernel already
0249 maps this page at its virtual address.
0250 
0251   ``void copy_user_page(void *to, void *from, unsigned long addr, struct page *page)``
0252   ``void clear_user_page(void *to, unsigned long addr, struct page *page)``
0253 
0254         These two routines store data in user anonymous or COW
0255         pages.  It allows a port to efficiently avoid D-cache alias
0256         issues between userspace and the kernel.
0257 
0258         For example, a port may temporarily map 'from' and 'to' to
0259         kernel virtual addresses during the copy.  The virtual address
0260         for these two pages is chosen in such a way that the kernel
0261         load/store instructions happen to virtual addresses which are
0262         of the same "color" as the user mapping of the page.  Sparc64
0263         for example, uses this technique.
0264 
0265         The 'addr' parameter tells the virtual address where the
0266         user will ultimately have this page mapped, and the 'page'
0267         parameter gives a pointer to the struct page of the target.
0268 
0269         If D-cache aliasing is not an issue, these two routines may
0270         simply call memcpy/memset directly and do nothing more.
0271 
0272   ``void flush_dcache_page(struct page *page)``
0273 
0274         This routines must be called when:
0275 
0276           a) the kernel did write to a page that is in the page cache page
0277              and / or in high memory
0278           b) the kernel is about to read from a page cache page and user space
0279              shared/writable mappings of this page potentially exist.  Note
0280              that {get,pin}_user_pages{_fast} already call flush_dcache_page
0281              on any page found in the user address space and thus driver
0282              code rarely needs to take this into account.
0283 
0284         .. note::
0285 
0286               This routine need only be called for page cache pages
0287               which can potentially ever be mapped into the address
0288               space of a user process.  So for example, VFS layer code
0289               handling vfs symlinks in the page cache need not call
0290               this interface at all.
0291 
0292         The phrase "kernel writes to a page cache page" means, specifically,
0293         that the kernel executes store instructions that dirty data in that
0294         page at the page->virtual mapping of that page.  It is important to
0295         flush here to handle D-cache aliasing, to make sure these kernel stores
0296         are visible to user space mappings of that page.
0297 
0298         The corollary case is just as important, if there are users which have
0299         shared+writable mappings of this file, we must make sure that kernel
0300         reads of these pages will see the most recent stores done by the user.
0301 
0302         If D-cache aliasing is not an issue, this routine may simply be defined
0303         as a nop on that architecture.
0304 
0305         There is a bit set aside in page->flags (PG_arch_1) as "architecture
0306         private".  The kernel guarantees that, for pagecache pages, it will
0307         clear this bit when such a page first enters the pagecache.
0308 
0309         This allows these interfaces to be implemented much more efficiently.
0310         It allows one to "defer" (perhaps indefinitely) the actual flush if
0311         there are currently no user processes mapping this page.  See sparc64's
0312         flush_dcache_page and update_mmu_cache implementations for an example
0313         of how to go about doing this.
0314 
0315         The idea is, first at flush_dcache_page() time, if page_file_mapping()
0316         returns a mapping, and mapping_mapped on that mapping returns %false,
0317         just mark the architecture private page flag bit.  Later, in
0318         update_mmu_cache(), a check is made of this flag bit, and if set the
0319         flush is done and the flag bit is cleared.
0320 
0321         .. important::
0322 
0323                         It is often important, if you defer the flush,
0324                         that the actual flush occurs on the same CPU
0325                         as did the cpu stores into the page to make it
0326                         dirty.  Again, see sparc64 for examples of how
0327                         to deal with this.
0328 
0329   ``void flush_dcache_folio(struct folio *folio)``
0330         This function is called under the same circumstances as
0331         flush_dcache_page().  It allows the architecture to
0332         optimise for flushing the entire folio of pages instead
0333         of flushing one page at a time.
0334 
0335   ``void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
0336   unsigned long user_vaddr, void *dst, void *src, int len)``
0337   ``void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
0338   unsigned long user_vaddr, void *dst, void *src, int len)``
0339 
0340         When the kernel needs to copy arbitrary data in and out
0341         of arbitrary user pages (f.e. for ptrace()) it will use
0342         these two routines.
0343 
0344         Any necessary cache flushing or other coherency operations
0345         that need to occur should happen here.  If the processor's
0346         instruction cache does not snoop cpu stores, it is very
0347         likely that you will need to flush the instruction cache
0348         for copy_to_user_page().
0349 
0350   ``void flush_anon_page(struct vm_area_struct *vma, struct page *page,
0351   unsigned long vmaddr)``
0352 
0353         When the kernel needs to access the contents of an anonymous
0354         page, it calls this function (currently only
0355         get_user_pages()).  Note: flush_dcache_page() deliberately
0356         doesn't work for an anonymous page.  The default
0357         implementation is a nop (and should remain so for all coherent
0358         architectures).  For incoherent architectures, it should flush
0359         the cache of the page at vmaddr.
0360 
0361   ``void flush_icache_range(unsigned long start, unsigned long end)``
0362 
0363         When the kernel stores into addresses that it will execute
0364         out of (eg when loading modules), this function is called.
0365 
0366         If the icache does not snoop stores then this routine will need
0367         to flush it.
0368 
0369   ``void flush_icache_page(struct vm_area_struct *vma, struct page *page)``
0370 
0371         All the functionality of flush_icache_page can be implemented in
0372         flush_dcache_page and update_mmu_cache. In the future, the hope
0373         is to remove this interface completely.
0374 
0375 The final category of APIs is for I/O to deliberately aliased address
0376 ranges inside the kernel.  Such aliases are set up by use of the
0377 vmap/vmalloc API.  Since kernel I/O goes via physical pages, the I/O
0378 subsystem assumes that the user mapping and kernel offset mapping are
0379 the only aliases.  This isn't true for vmap aliases, so anything in
0380 the kernel trying to do I/O to vmap areas must manually manage
0381 coherency.  It must do this by flushing the vmap range before doing
0382 I/O and invalidating it after the I/O returns.
0383 
0384   ``void flush_kernel_vmap_range(void *vaddr, int size)``
0385 
0386        flushes the kernel cache for a given virtual address range in
0387        the vmap area.  This is to make sure that any data the kernel
0388        modified in the vmap range is made visible to the physical
0389        page.  The design is to make this area safe to perform I/O on.
0390        Note that this API does *not* also flush the offset map alias
0391        of the area.
0392 
0393   ``void invalidate_kernel_vmap_range(void *vaddr, int size) invalidates``
0394 
0395        the cache for a given virtual address range in the vmap area
0396        which prevents the processor from making the cache stale by
0397        speculatively reading data while the I/O was occurring to the
0398        physical pages.  This is only necessary for data reads into the
0399        vmap area.