cachetlb.txt 17 KB

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