pgtable.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * This file contains common routines for dealing with free of page tables
  3. * Along with common page table handling code
  4. *
  5. * Derived from arch/powerpc/mm/tlb_64.c:
  6. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  7. *
  8. * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
  9. * and Cort Dougan (PReP) (cort@cs.nmt.edu)
  10. * Copyright (C) 1996 Paul Mackerras
  11. *
  12. * Derived from "arch/i386/mm/init.c"
  13. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  14. *
  15. * Dave Engebretsen <engebret@us.ibm.com>
  16. * Rework for PPC64 port.
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License
  20. * as published by the Free Software Foundation; either version
  21. * 2 of the License, or (at your option) any later version.
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/mm.h>
  25. #include <linux/init.h>
  26. #include <linux/percpu.h>
  27. #include <linux/hardirq.h>
  28. #include <asm/pgalloc.h>
  29. #include <asm/tlbflush.h>
  30. #include <asm/tlb.h>
  31. #include "mmu_decl.h"
  32. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  33. #ifdef CONFIG_SMP
  34. /*
  35. * Handle batching of page table freeing on SMP. Page tables are
  36. * queued up and send to be freed later by RCU in order to avoid
  37. * freeing a page table page that is being walked without locks
  38. */
  39. static DEFINE_PER_CPU(struct pte_freelist_batch *, pte_freelist_cur);
  40. static unsigned long pte_freelist_forced_free;
  41. struct pte_freelist_batch
  42. {
  43. struct rcu_head rcu;
  44. unsigned int index;
  45. pgtable_free_t tables[0];
  46. };
  47. #define PTE_FREELIST_SIZE \
  48. ((PAGE_SIZE - sizeof(struct pte_freelist_batch)) \
  49. / sizeof(pgtable_free_t))
  50. static void pte_free_smp_sync(void *arg)
  51. {
  52. /* Do nothing, just ensure we sync with all CPUs */
  53. }
  54. /* This is only called when we are critically out of memory
  55. * (and fail to get a page in pte_free_tlb).
  56. */
  57. static void pgtable_free_now(pgtable_free_t pgf)
  58. {
  59. pte_freelist_forced_free++;
  60. smp_call_function(pte_free_smp_sync, NULL, 1);
  61. pgtable_free(pgf);
  62. }
  63. static void pte_free_rcu_callback(struct rcu_head *head)
  64. {
  65. struct pte_freelist_batch *batch =
  66. container_of(head, struct pte_freelist_batch, rcu);
  67. unsigned int i;
  68. for (i = 0; i < batch->index; i++)
  69. pgtable_free(batch->tables[i]);
  70. free_page((unsigned long)batch);
  71. }
  72. static void pte_free_submit(struct pte_freelist_batch *batch)
  73. {
  74. INIT_RCU_HEAD(&batch->rcu);
  75. call_rcu(&batch->rcu, pte_free_rcu_callback);
  76. }
  77. void pgtable_free_tlb(struct mmu_gather *tlb, pgtable_free_t pgf)
  78. {
  79. /* This is safe since tlb_gather_mmu has disabled preemption */
  80. struct pte_freelist_batch **batchp = &__get_cpu_var(pte_freelist_cur);
  81. if (atomic_read(&tlb->mm->mm_users) < 2 ||
  82. cpumask_equal(mm_cpumask(tlb->mm), cpumask_of(smp_processor_id()))){
  83. pgtable_free(pgf);
  84. return;
  85. }
  86. if (*batchp == NULL) {
  87. *batchp = (struct pte_freelist_batch *)__get_free_page(GFP_ATOMIC);
  88. if (*batchp == NULL) {
  89. pgtable_free_now(pgf);
  90. return;
  91. }
  92. (*batchp)->index = 0;
  93. }
  94. (*batchp)->tables[(*batchp)->index++] = pgf;
  95. if ((*batchp)->index == PTE_FREELIST_SIZE) {
  96. pte_free_submit(*batchp);
  97. *batchp = NULL;
  98. }
  99. }
  100. void pte_free_finish(void)
  101. {
  102. /* This is safe since tlb_gather_mmu has disabled preemption */
  103. struct pte_freelist_batch **batchp = &__get_cpu_var(pte_freelist_cur);
  104. if (*batchp == NULL)
  105. return;
  106. pte_free_submit(*batchp);
  107. *batchp = NULL;
  108. }
  109. #endif /* CONFIG_SMP */
  110. static inline int is_exec_fault(void)
  111. {
  112. return current->thread.regs && TRAP(current->thread.regs) == 0x400;
  113. }
  114. /* We only try to do i/d cache coherency on stuff that looks like
  115. * reasonably "normal" PTEs. We currently require a PTE to be present
  116. * and we avoid _PAGE_SPECIAL and _PAGE_NO_CACHE. We also only do that
  117. * on userspace PTEs
  118. */
  119. static inline int pte_looks_normal(pte_t pte)
  120. {
  121. return (pte_val(pte) &
  122. (_PAGE_PRESENT | _PAGE_SPECIAL | _PAGE_NO_CACHE | _PAGE_USER)) ==
  123. (_PAGE_PRESENT | _PAGE_USER);
  124. }
  125. struct page * maybe_pte_to_page(pte_t pte)
  126. {
  127. unsigned long pfn = pte_pfn(pte);
  128. struct page *page;
  129. if (unlikely(!pfn_valid(pfn)))
  130. return NULL;
  131. page = pfn_to_page(pfn);
  132. if (PageReserved(page))
  133. return NULL;
  134. return page;
  135. }
  136. #if defined(CONFIG_PPC_STD_MMU) || _PAGE_EXEC == 0
  137. /* Server-style MMU handles coherency when hashing if HW exec permission
  138. * is supposed per page (currently 64-bit only). If not, then, we always
  139. * flush the cache for valid PTEs in set_pte. Embedded CPU without HW exec
  140. * support falls into the same category.
  141. */
  142. static pte_t set_pte_filter(pte_t pte, unsigned long addr)
  143. {
  144. pte = __pte(pte_val(pte) & ~_PAGE_HPTEFLAGS);
  145. if (pte_looks_normal(pte) && !(cpu_has_feature(CPU_FTR_COHERENT_ICACHE) ||
  146. cpu_has_feature(CPU_FTR_NOEXECUTE))) {
  147. struct page *pg = maybe_pte_to_page(pte);
  148. if (!pg)
  149. return pte;
  150. if (!test_bit(PG_arch_1, &pg->flags)) {
  151. #ifdef CONFIG_8xx
  152. /* On 8xx, cache control instructions (particularly
  153. * "dcbst" from flush_dcache_icache) fault as write
  154. * operation if there is an unpopulated TLB entry
  155. * for the address in question. To workaround that,
  156. * we invalidate the TLB here, thus avoiding dcbst
  157. * misbehaviour.
  158. */
  159. /* 8xx doesn't care about PID, size or ind args */
  160. _tlbil_va(addr, 0, 0, 0);
  161. #endif /* CONFIG_8xx */
  162. flush_dcache_icache_page(pg);
  163. set_bit(PG_arch_1, &pg->flags);
  164. }
  165. }
  166. return pte;
  167. }
  168. static pte_t set_access_flags_filter(pte_t pte, struct vm_area_struct *vma,
  169. int dirty)
  170. {
  171. return pte;
  172. }
  173. #else /* defined(CONFIG_PPC_STD_MMU) || _PAGE_EXEC == 0 */
  174. /* Embedded type MMU with HW exec support. This is a bit more complicated
  175. * as we don't have two bits to spare for _PAGE_EXEC and _PAGE_HWEXEC so
  176. * instead we "filter out" the exec permission for non clean pages.
  177. */
  178. static pte_t set_pte_filter(pte_t pte, unsigned long addr)
  179. {
  180. struct page *pg;
  181. /* No exec permission in the first place, move on */
  182. if (!(pte_val(pte) & _PAGE_EXEC) || !pte_looks_normal(pte))
  183. return pte;
  184. /* If you set _PAGE_EXEC on weird pages you're on your own */
  185. pg = maybe_pte_to_page(pte);
  186. if (unlikely(!pg))
  187. return pte;
  188. /* If the page clean, we move on */
  189. if (test_bit(PG_arch_1, &pg->flags))
  190. return pte;
  191. /* If it's an exec fault, we flush the cache and make it clean */
  192. if (is_exec_fault()) {
  193. flush_dcache_icache_page(pg);
  194. set_bit(PG_arch_1, &pg->flags);
  195. return pte;
  196. }
  197. /* Else, we filter out _PAGE_EXEC */
  198. return __pte(pte_val(pte) & ~_PAGE_EXEC);
  199. }
  200. static pte_t set_access_flags_filter(pte_t pte, struct vm_area_struct *vma,
  201. int dirty)
  202. {
  203. struct page *pg;
  204. /* So here, we only care about exec faults, as we use them
  205. * to recover lost _PAGE_EXEC and perform I$/D$ coherency
  206. * if necessary. Also if _PAGE_EXEC is already set, same deal,
  207. * we just bail out
  208. */
  209. if (dirty || (pte_val(pte) & _PAGE_EXEC) || !is_exec_fault())
  210. return pte;
  211. #ifdef CONFIG_DEBUG_VM
  212. /* So this is an exec fault, _PAGE_EXEC is not set. If it was
  213. * an error we would have bailed out earlier in do_page_fault()
  214. * but let's make sure of it
  215. */
  216. if (WARN_ON(!(vma->vm_flags & VM_EXEC)))
  217. return pte;
  218. #endif /* CONFIG_DEBUG_VM */
  219. /* If you set _PAGE_EXEC on weird pages you're on your own */
  220. pg = maybe_pte_to_page(pte);
  221. if (unlikely(!pg))
  222. goto bail;
  223. /* If the page is already clean, we move on */
  224. if (test_bit(PG_arch_1, &pg->flags))
  225. goto bail;
  226. /* Clean the page and set PG_arch_1 */
  227. flush_dcache_icache_page(pg);
  228. set_bit(PG_arch_1, &pg->flags);
  229. bail:
  230. return __pte(pte_val(pte) | _PAGE_EXEC);
  231. }
  232. #endif /* !(defined(CONFIG_PPC_STD_MMU) || _PAGE_EXEC == 0) */
  233. /*
  234. * set_pte stores a linux PTE into the linux page table.
  235. */
  236. void set_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep,
  237. pte_t pte)
  238. {
  239. #ifdef CONFIG_DEBUG_VM
  240. WARN_ON(pte_present(*ptep));
  241. #endif
  242. /* Note: mm->context.id might not yet have been assigned as
  243. * this context might not have been activated yet when this
  244. * is called.
  245. */
  246. pte = set_pte_filter(pte, addr);
  247. /* Perform the setting of the PTE */
  248. __set_pte_at(mm, addr, ptep, pte, 0);
  249. }
  250. /*
  251. * This is called when relaxing access to a PTE. It's also called in the page
  252. * fault path when we don't hit any of the major fault cases, ie, a minor
  253. * update of _PAGE_ACCESSED, _PAGE_DIRTY, etc... The generic code will have
  254. * handled those two for us, we additionally deal with missing execute
  255. * permission here on some processors
  256. */
  257. int ptep_set_access_flags(struct vm_area_struct *vma, unsigned long address,
  258. pte_t *ptep, pte_t entry, int dirty)
  259. {
  260. int changed;
  261. entry = set_access_flags_filter(entry, vma, dirty);
  262. changed = !pte_same(*(ptep), entry);
  263. if (changed) {
  264. if (!(vma->vm_flags & VM_HUGETLB))
  265. assert_pte_locked(vma->vm_mm, address);
  266. __ptep_set_access_flags(ptep, entry);
  267. flush_tlb_page_nohash(vma, address);
  268. }
  269. return changed;
  270. }
  271. #ifdef CONFIG_DEBUG_VM
  272. void assert_pte_locked(struct mm_struct *mm, unsigned long addr)
  273. {
  274. pgd_t *pgd;
  275. pud_t *pud;
  276. pmd_t *pmd;
  277. if (mm == &init_mm)
  278. return;
  279. pgd = mm->pgd + pgd_index(addr);
  280. BUG_ON(pgd_none(*pgd));
  281. pud = pud_offset(pgd, addr);
  282. BUG_ON(pud_none(*pud));
  283. pmd = pmd_offset(pud, addr);
  284. BUG_ON(!pmd_present(*pmd));
  285. assert_spin_locked(pte_lockptr(mm, pmd));
  286. }
  287. #endif /* CONFIG_DEBUG_VM */