cachetlb.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. ==================================
  2. Cache and TLB Flushing Under Linux
  3. ==================================
  4. :Author: David S. Miller <davem@redhat.com>
  5. This document describes the cache/tlb flushing interfaces called
  6. by the Linux VM subsystem. It enumerates over each interface,
  7. describes its intended purpose, and what side effect is expected
  8. after the interface is invoked.
  9. The side effects described below are stated for a uniprocessor
  10. implementation, and what is to happen on that single processor. The
  11. SMP cases are a simple extension, in that you just extend the
  12. definition such that the side effect for a particular interface occurs
  13. on all processors in the system. Don't let this scare you into
  14. thinking SMP cache/tlb flushing must be so inefficient, this is in
  15. fact an area where many optimizations are possible. For example,
  16. if it can be proven that a user address space has never executed
  17. on a cpu (see mm_cpumask()), one need not perform a flush
  18. for this address space on that cpu.
  19. First, the TLB flushing interfaces, since they are the simplest. The
  20. "TLB" is abstracted under Linux as something the cpu uses to cache
  21. virtual-->physical address translations obtained from the software
  22. page tables. Meaning that if the software page tables change, it is
  23. possible for stale translations to exist in this "TLB" cache.
  24. Therefore when software page table changes occur, the kernel will
  25. invoke one of the following flush methods _after_ the page table
  26. changes occur:
  27. 1) ``void flush_tlb_all(void)``
  28. The most severe flush of all. After this interface runs,
  29. any previous page table modification whatsoever will be
  30. visible to the cpu.
  31. This is usually invoked when the kernel page tables are
  32. changed, since such translations are "global" in nature.
  33. 2) ``void flush_tlb_mm(struct mm_struct *mm)``
  34. This interface flushes an entire user address space from
  35. the TLB. After running, this interface must make sure that
  36. any previous page table modifications for the address space
  37. 'mm' will be visible to the cpu. That is, after running,
  38. there will be no entries in the TLB for 'mm'.
  39. This interface is used to handle whole address space
  40. page table operations such as what happens during
  41. fork, and exec.
  42. 3) ``void flush_tlb_range(struct vm_area_struct *vma,
  43. unsigned long start, unsigned long end)``
  44. Here we are flushing a specific range of (user) virtual
  45. address translations from the TLB. After running, this
  46. interface must make sure that any previous page table
  47. modifications for the address space 'vma->vm_mm' in the range
  48. 'start' to 'end-1' will be visible to the cpu. That is, after
  49. running, there will be no entries in the TLB for 'mm' for
  50. virtual addresses in the range 'start' to 'end-1'.
  51. The "vma" is the backing store being used for the region.
  52. Primarily, this is used for munmap() type operations.
  53. The interface is provided in hopes that the port can find
  54. a suitably efficient method for removing multiple page
  55. sized translations from the TLB, instead of having the kernel
  56. call flush_tlb_page (see below) for each entry which may be
  57. modified.
  58. 4) ``void flush_tlb_page(struct vm_area_struct *vma, unsigned long addr)``
  59. This time we need to remove the PAGE_SIZE sized translation
  60. from the TLB. The 'vma' is the backing structure used by
  61. Linux to keep track of mmap'd regions for a process, the
  62. address space is available via vma->vm_mm. Also, one may
  63. test (vma->vm_flags & VM_EXEC) to see if this region is
  64. executable (and thus could be in the 'instruction TLB' in
  65. split-tlb type setups).
  66. After running, this interface must make sure that any previous
  67. page table modification for address space 'vma->vm_mm' for
  68. user virtual address 'addr' will be visible to the cpu. That
  69. is, after running, there will be no entries in the TLB for
  70. 'vma->vm_mm' for virtual address 'addr'.
  71. This is used primarily during fault processing.
  72. 5) ``void update_mmu_cache(struct vm_area_struct *vma,
  73. unsigned long address, pte_t *ptep)``
  74. At the end of every page fault, this routine is invoked to
  75. tell the architecture specific code that a translation
  76. now exists at virtual address "address" for address space
  77. "vma->vm_mm", in the software page tables.
  78. A port may use this information in any way it so chooses.
  79. For example, it could use this event to pre-load TLB
  80. translations for software managed TLB configurations.
  81. The sparc64 port currently does this.
  82. 6) ``void tlb_migrate_finish(struct mm_struct *mm)``
  83. This interface is called at the end of an explicit
  84. process migration. This interface provides a hook
  85. to allow a platform to update TLB or context-specific
  86. information for the address space.
  87. The ia64 sn2 platform is one example of a platform
  88. that uses this interface.
  89. Next, we have the cache flushing interfaces. In general, when Linux
  90. is changing an existing virtual-->physical mapping to a new value,
  91. the sequence will be in one of the following forms::
  92. 1) flush_cache_mm(mm);
  93. change_all_page_tables_of(mm);
  94. flush_tlb_mm(mm);
  95. 2) flush_cache_range(vma, start, end);
  96. change_range_of_page_tables(mm, start, end);
  97. flush_tlb_range(vma, start, end);
  98. 3) flush_cache_page(vma, addr, pfn);
  99. set_pte(pte_pointer, new_pte_val);
  100. flush_tlb_page(vma, addr);
  101. The cache level flush will always be first, because this allows
  102. us to properly handle systems whose caches are strict and require
  103. a virtual-->physical translation to exist for a virtual address
  104. when that virtual address is flushed from the cache. The HyperSparc
  105. cpu is one such cpu with this attribute.
  106. The cache flushing routines below need only deal with cache flushing
  107. to the extent that it is necessary for a particular cpu. Mostly,
  108. these routines must be implemented for cpus which have virtually
  109. indexed caches which must be flushed when virtual-->physical
  110. translations are changed or removed. So, for example, the physically
  111. indexed physically tagged caches of IA32 processors have no need to
  112. implement these interfaces since the caches are fully synchronized
  113. and have no dependency on translation information.
  114. Here are the routines, one by one:
  115. 1) ``void flush_cache_mm(struct mm_struct *mm)``
  116. This interface flushes an entire user address space from
  117. the caches. That is, after running, there will be no cache
  118. lines associated with 'mm'.
  119. This interface is used to handle whole address space
  120. page table operations such as what happens during exit and exec.
  121. 2) ``void flush_cache_dup_mm(struct mm_struct *mm)``
  122. This interface flushes an entire user address space from
  123. the caches. That is, after running, there will be no cache
  124. lines associated with 'mm'.
  125. This interface is used to handle whole address space
  126. page table operations such as what happens during fork.
  127. This option is separate from flush_cache_mm to allow some
  128. optimizations for VIPT caches.
  129. 3) ``void flush_cache_range(struct vm_area_struct *vma,
  130. unsigned long start, unsigned long end)``
  131. Here we are flushing a specific range of (user) virtual
  132. addresses from the cache. After running, there will be no
  133. entries in the cache for 'vma->vm_mm' for virtual addresses in
  134. the range 'start' to 'end-1'.
  135. The "vma" is the backing store being used for the region.
  136. Primarily, this is used for munmap() type operations.
  137. The interface is provided in hopes that the port can find
  138. a suitably efficient method for removing multiple page
  139. sized regions from the cache, instead of having the kernel
  140. call flush_cache_page (see below) for each entry which may be
  141. modified.
  142. 4) ``void flush_cache_page(struct vm_area_struct *vma, unsigned long addr, unsigned long pfn)``
  143. This time we need to remove a PAGE_SIZE sized range
  144. from the cache. The 'vma' is the backing structure used by
  145. Linux to keep track of mmap'd regions for a process, the
  146. address space is available via vma->vm_mm. Also, one may
  147. test (vma->vm_flags & VM_EXEC) to see if this region is
  148. executable (and thus could be in the 'instruction cache' in
  149. "Harvard" type cache layouts).
  150. The 'pfn' indicates the physical page frame (shift this value
  151. left by PAGE_SHIFT to get the physical address) that 'addr'
  152. translates to. It is this mapping which should be removed from
  153. the cache.
  154. After running, there will be no entries in the cache for
  155. 'vma->vm_mm' for virtual address 'addr' which translates
  156. to 'pfn'.
  157. This is used primarily during fault processing.
  158. 5) ``void flush_cache_kmaps(void)``
  159. This routine need only be implemented if the platform utilizes
  160. highmem. It will be called right before all of the kmaps
  161. are invalidated.
  162. After running, there will be no entries in the cache for
  163. the kernel virtual address range PKMAP_ADDR(0) to
  164. PKMAP_ADDR(LAST_PKMAP).
  165. This routing should be implemented in asm/highmem.h
  166. 6) ``void flush_cache_vmap(unsigned long start, unsigned long end)``
  167. ``void flush_cache_vunmap(unsigned long start, unsigned long end)``
  168. Here in these two interfaces we are flushing a specific range
  169. of (kernel) virtual addresses from the cache. After running,
  170. there will be no entries in the cache for the kernel address
  171. space for virtual addresses in the range 'start' to 'end-1'.
  172. The first of these two routines is invoked after map_vm_area()
  173. has installed the page table entries. The second is invoked
  174. before unmap_kernel_range() deletes the page table entries.
  175. There exists another whole class of cpu cache issues which currently
  176. require a whole different set of interfaces to handle properly.
  177. The biggest problem is that of virtual aliasing in the data cache
  178. of a processor.
  179. Is your port susceptible to virtual aliasing in its D-cache?
  180. Well, if your D-cache is virtually indexed, is larger in size than
  181. PAGE_SIZE, and does not prevent multiple cache lines for the same
  182. physical address from existing at once, you have this problem.
  183. If your D-cache has this problem, first define asm/shmparam.h SHMLBA
  184. properly, it should essentially be the size of your virtually
  185. addressed D-cache (or if the size is variable, the largest possible
  186. size). This setting will force the SYSv IPC layer to only allow user
  187. processes to mmap shared memory at address which are a multiple of
  188. this value.
  189. .. note::
  190. This does not fix shared mmaps, check out the sparc64 port for
  191. one way to solve this (in particular SPARC_FLAG_MMAPSHARED).
  192. Next, you have to solve the D-cache aliasing issue for all
  193. other cases. Please keep in mind that fact that, for a given page
  194. mapped into some user address space, there is always at least one more
  195. mapping, that of the kernel in its linear mapping starting at
  196. PAGE_OFFSET. So immediately, once the first user maps a given
  197. physical page into its address space, by implication the D-cache
  198. aliasing problem has the potential to exist since the kernel already
  199. maps this page at its virtual address.
  200. ``void copy_user_page(void *to, void *from, unsigned long addr, struct page *page)``
  201. ``void clear_user_page(void *to, unsigned long addr, struct page *page)``
  202. These two routines store data in user anonymous or COW
  203. pages. It allows a port to efficiently avoid D-cache alias
  204. issues between userspace and the kernel.
  205. For example, a port may temporarily map 'from' and 'to' to
  206. kernel virtual addresses during the copy. The virtual address
  207. for these two pages is chosen in such a way that the kernel
  208. load/store instructions happen to virtual addresses which are
  209. of the same "color" as the user mapping of the page. Sparc64
  210. for example, uses this technique.
  211. The 'addr' parameter tells the virtual address where the
  212. user will ultimately have this page mapped, and the 'page'
  213. parameter gives a pointer to the struct page of the target.
  214. If D-cache aliasing is not an issue, these two routines may
  215. simply call memcpy/memset directly and do nothing more.
  216. ``void flush_dcache_page(struct page *page)``
  217. Any time the kernel writes to a page cache page, _OR_
  218. the kernel is about to read from a page cache page and
  219. user space shared/writable mappings of this page potentially
  220. exist, this routine is called.
  221. .. note::
  222. This routine need only be called for page cache pages
  223. which can potentially ever be mapped into the address
  224. space of a user process. So for example, VFS layer code
  225. handling vfs symlinks in the page cache need not call
  226. this interface at all.
  227. The phrase "kernel writes to a page cache page" means,
  228. specifically, that the kernel executes store instructions
  229. that dirty data in that page at the page->virtual mapping
  230. of that page. It is important to flush here to handle
  231. D-cache aliasing, to make sure these kernel stores are
  232. visible to user space mappings of that page.
  233. The corollary case is just as important, if there are users
  234. which have shared+writable mappings of this file, we must make
  235. sure that kernel reads of these pages will see the most recent
  236. stores done by the user.
  237. If D-cache aliasing is not an issue, this routine may
  238. simply be defined as a nop on that architecture.
  239. There is a bit set aside in page->flags (PG_arch_1) as
  240. "architecture private". The kernel guarantees that,
  241. for pagecache pages, it will clear this bit when such
  242. a page first enters the pagecache.
  243. This allows these interfaces to be implemented much more
  244. efficiently. It allows one to "defer" (perhaps indefinitely)
  245. the actual flush if there are currently no user processes
  246. mapping this page. See sparc64's flush_dcache_page and
  247. update_mmu_cache implementations for an example of how to go
  248. about doing this.
  249. The idea is, first at flush_dcache_page() time, if
  250. page->mapping->i_mmap is an empty tree, just mark the architecture
  251. private page flag bit. Later, in update_mmu_cache(), a check is
  252. made of this flag bit, and if set the flush is done and the flag
  253. bit is cleared.
  254. .. important::
  255. It is often important, if you defer the flush,
  256. that the actual flush occurs on the same CPU
  257. as did the cpu stores into the page to make it
  258. dirty. Again, see sparc64 for examples of how
  259. to deal with this.
  260. ``void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
  261. unsigned long user_vaddr, void *dst, void *src, int len)``
  262. ``void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
  263. unsigned long user_vaddr, void *dst, void *src, int len)``
  264. When the kernel needs to copy arbitrary data in and out
  265. of arbitrary user pages (f.e. for ptrace()) it will use
  266. these two routines.
  267. Any necessary cache flushing or other coherency operations
  268. that need to occur should happen here. If the processor's
  269. instruction cache does not snoop cpu stores, it is very
  270. likely that you will need to flush the instruction cache
  271. for copy_to_user_page().
  272. ``void flush_anon_page(struct vm_area_struct *vma, struct page *page,
  273. unsigned long vmaddr)``
  274. When the kernel needs to access the contents of an anonymous
  275. page, it calls this function (currently only
  276. get_user_pages()). Note: flush_dcache_page() deliberately
  277. doesn't work for an anonymous page. The default
  278. implementation is a nop (and should remain so for all coherent
  279. architectures). For incoherent architectures, it should flush
  280. the cache of the page at vmaddr.
  281. ``void flush_kernel_dcache_page(struct page *page)``
  282. When the kernel needs to modify a user page is has obtained
  283. with kmap, it calls this function after all modifications are
  284. complete (but before kunmapping it) to bring the underlying
  285. page up to date. It is assumed here that the user has no
  286. incoherent cached copies (i.e. the original page was obtained
  287. from a mechanism like get_user_pages()). The default
  288. implementation is a nop and should remain so on all coherent
  289. architectures. On incoherent architectures, this should flush
  290. the kernel cache for page (using page_address(page)).
  291. ``void flush_icache_range(unsigned long start, unsigned long end)``
  292. When the kernel stores into addresses that it will execute
  293. out of (eg when loading modules), this function is called.
  294. If the icache does not snoop stores then this routine will need
  295. to flush it.
  296. ``void flush_icache_page(struct vm_area_struct *vma, struct page *page)``
  297. All the functionality of flush_icache_page can be implemented in
  298. flush_dcache_page and update_mmu_cache. In the future, the hope
  299. is to remove this interface completely.
  300. The final category of APIs is for I/O to deliberately aliased address
  301. ranges inside the kernel. Such aliases are set up by use of the
  302. vmap/vmalloc API. Since kernel I/O goes via physical pages, the I/O
  303. subsystem assumes that the user mapping and kernel offset mapping are
  304. the only aliases. This isn't true for vmap aliases, so anything in
  305. the kernel trying to do I/O to vmap areas must manually manage
  306. coherency. It must do this by flushing the vmap range before doing
  307. I/O and invalidating it after the I/O returns.
  308. ``void flush_kernel_vmap_range(void *vaddr, int size)``
  309. flushes the kernel cache for a given virtual address range in
  310. the vmap area. This is to make sure that any data the kernel
  311. modified in the vmap range is made visible to the physical
  312. page. The design is to make this area safe to perform I/O on.
  313. Note that this API does *not* also flush the offset map alias
  314. of the area.
  315. ``void invalidate_kernel_vmap_range(void *vaddr, int size) invalidates``
  316. the cache for a given virtual address range in the vmap area
  317. which prevents the processor from making the cache stale by
  318. speculatively reading data while the I/O was occurring to the
  319. physical pages. This is only necessary for data reads into the
  320. vmap area.