pageattr.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright 2002 Andi Kleen, SuSE Labs.
  3. * Thanks to Ben LaHaise for precious feedback.
  4. */
  5. #include <linux/mm.h>
  6. #include <linux/sched.h>
  7. #include <linux/highmem.h>
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <asm/uaccess.h>
  11. #include <asm/processor.h>
  12. #include <asm/tlbflush.h>
  13. #include <asm/pgalloc.h>
  14. #include <asm/sections.h>
  15. static DEFINE_SPINLOCK(cpa_lock);
  16. static struct list_head df_list = LIST_HEAD_INIT(df_list);
  17. pte_t *lookup_address(unsigned long address)
  18. {
  19. pgd_t *pgd = pgd_offset_k(address);
  20. pud_t *pud;
  21. pmd_t *pmd;
  22. if (pgd_none(*pgd))
  23. return NULL;
  24. pud = pud_offset(pgd, address);
  25. if (pud_none(*pud))
  26. return NULL;
  27. pmd = pmd_offset(pud, address);
  28. if (pmd_none(*pmd))
  29. return NULL;
  30. if (pmd_large(*pmd))
  31. return (pte_t *)pmd;
  32. return pte_offset_kernel(pmd, address);
  33. }
  34. static struct page *split_large_page(unsigned long address, pgprot_t prot,
  35. pgprot_t ref_prot)
  36. {
  37. int i;
  38. unsigned long addr;
  39. struct page *base;
  40. pte_t *pbase;
  41. spin_unlock_irq(&cpa_lock);
  42. base = alloc_pages(GFP_KERNEL, 0);
  43. spin_lock_irq(&cpa_lock);
  44. if (!base)
  45. return NULL;
  46. /*
  47. * page_private is used to track the number of entries in
  48. * the page table page that have non standard attributes.
  49. */
  50. SetPagePrivate(base);
  51. page_private(base) = 0;
  52. address = __pa(address);
  53. addr = address & LARGE_PAGE_MASK;
  54. pbase = (pte_t *)page_address(base);
  55. paravirt_alloc_pt(page_to_pfn(base));
  56. for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE) {
  57. set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT,
  58. addr == address ? prot : ref_prot));
  59. }
  60. return base;
  61. }
  62. static void flush_kernel_map(void *arg)
  63. {
  64. unsigned long adr = (unsigned long)arg;
  65. if (adr && cpu_has_clflush) {
  66. int i;
  67. for (i = 0; i < PAGE_SIZE; i += boot_cpu_data.x86_clflush_size)
  68. asm volatile("clflush (%0)" :: "r" (adr + i));
  69. } else if (boot_cpu_data.x86_model >= 4)
  70. wbinvd();
  71. /* Flush all to work around Errata in early athlons regarding
  72. * large page flushing.
  73. */
  74. __flush_tlb_all();
  75. }
  76. static void set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
  77. {
  78. struct page *page;
  79. unsigned long flags;
  80. set_pte_atomic(kpte, pte); /* change init_mm */
  81. if (SHARED_KERNEL_PMD)
  82. return;
  83. spin_lock_irqsave(&pgd_lock, flags);
  84. for (page = pgd_list; page; page = (struct page *)page->index) {
  85. pgd_t *pgd;
  86. pud_t *pud;
  87. pmd_t *pmd;
  88. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  89. pud = pud_offset(pgd, address);
  90. pmd = pmd_offset(pud, address);
  91. set_pte_atomic((pte_t *)pmd, pte);
  92. }
  93. spin_unlock_irqrestore(&pgd_lock, flags);
  94. }
  95. /*
  96. * No more special protections in this 2/4MB area - revert to a
  97. * large page again.
  98. */
  99. static inline void revert_page(struct page *kpte_page, unsigned long address)
  100. {
  101. pgprot_t ref_prot;
  102. pte_t *linear;
  103. ref_prot =
  104. ((address & LARGE_PAGE_MASK) < (unsigned long)&_etext)
  105. ? PAGE_KERNEL_LARGE_EXEC : PAGE_KERNEL_LARGE;
  106. linear = (pte_t *)
  107. pmd_offset(pud_offset(pgd_offset_k(address), address), address);
  108. set_pmd_pte(linear, address,
  109. pfn_pte((__pa(address) & LARGE_PAGE_MASK) >> PAGE_SHIFT,
  110. ref_prot));
  111. }
  112. static int
  113. __change_page_attr(struct page *page, pgprot_t prot)
  114. {
  115. pte_t *kpte;
  116. unsigned long address;
  117. struct page *kpte_page;
  118. BUG_ON(PageHighMem(page));
  119. address = (unsigned long)page_address(page);
  120. kpte = lookup_address(address);
  121. if (!kpte)
  122. return -EINVAL;
  123. kpte_page = virt_to_page(kpte);
  124. if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL)) {
  125. if (!pte_huge(*kpte)) {
  126. set_pte_atomic(kpte, mk_pte(page, prot));
  127. } else {
  128. pgprot_t ref_prot;
  129. struct page *split;
  130. ref_prot =
  131. ((address & LARGE_PAGE_MASK) < (unsigned long)&_etext)
  132. ? PAGE_KERNEL_EXEC : PAGE_KERNEL;
  133. split = split_large_page(address, prot, ref_prot);
  134. if (!split)
  135. return -ENOMEM;
  136. set_pmd_pte(kpte,address,mk_pte(split, ref_prot));
  137. kpte_page = split;
  138. }
  139. page_private(kpte_page)++;
  140. } else if (!pte_huge(*kpte)) {
  141. set_pte_atomic(kpte, mk_pte(page, PAGE_KERNEL));
  142. BUG_ON(page_private(kpte_page) == 0);
  143. page_private(kpte_page)--;
  144. } else
  145. BUG();
  146. /*
  147. * If the pte was reserved, it means it was created at boot
  148. * time (not via split_large_page) and in turn we must not
  149. * replace it with a largepage.
  150. */
  151. if (!PageReserved(kpte_page)) {
  152. if (cpu_has_pse && (page_private(kpte_page) == 0)) {
  153. ClearPagePrivate(kpte_page);
  154. paravirt_release_pt(page_to_pfn(kpte_page));
  155. list_add(&kpte_page->lru, &df_list);
  156. revert_page(kpte_page, address);
  157. }
  158. }
  159. return 0;
  160. }
  161. static inline void flush_map(void *adr)
  162. {
  163. on_each_cpu(flush_kernel_map, adr, 1, 1);
  164. }
  165. /*
  166. * Change the page attributes of an page in the linear mapping.
  167. *
  168. * This should be used when a page is mapped with a different caching policy
  169. * than write-back somewhere - some CPUs do not like it when mappings with
  170. * different caching policies exist. This changes the page attributes of the
  171. * in kernel linear mapping too.
  172. *
  173. * The caller needs to ensure that there are no conflicting mappings elsewhere.
  174. * This function only deals with the kernel linear map.
  175. *
  176. * Caller must call global_flush_tlb() after this.
  177. */
  178. int change_page_attr(struct page *page, int numpages, pgprot_t prot)
  179. {
  180. int err = 0;
  181. int i;
  182. unsigned long flags;
  183. spin_lock_irqsave(&cpa_lock, flags);
  184. for (i = 0; i < numpages; i++, page++) {
  185. err = __change_page_attr(page, prot);
  186. if (err)
  187. break;
  188. }
  189. spin_unlock_irqrestore(&cpa_lock, flags);
  190. return err;
  191. }
  192. void global_flush_tlb(void)
  193. {
  194. struct list_head l;
  195. struct page *pg, *next;
  196. BUG_ON(irqs_disabled());
  197. spin_lock_irq(&cpa_lock);
  198. list_replace_init(&df_list, &l);
  199. spin_unlock_irq(&cpa_lock);
  200. if (!cpu_has_clflush)
  201. flush_map(NULL);
  202. list_for_each_entry_safe(pg, next, &l, lru) {
  203. if (cpu_has_clflush)
  204. flush_map(page_address(pg));
  205. __free_page(pg);
  206. }
  207. }
  208. #ifdef CONFIG_DEBUG_PAGEALLOC
  209. void kernel_map_pages(struct page *page, int numpages, int enable)
  210. {
  211. if (PageHighMem(page))
  212. return;
  213. if (!enable)
  214. debug_check_no_locks_freed(page_address(page),
  215. numpages * PAGE_SIZE);
  216. /* the return value is ignored - the calls cannot fail,
  217. * large pages are disabled at boot time.
  218. */
  219. change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
  220. /* we should perform an IPI and flush all tlbs,
  221. * but that can deadlock->flush only current cpu.
  222. */
  223. __flush_tlb_all();
  224. }
  225. #endif
  226. EXPORT_SYMBOL(change_page_attr);
  227. EXPORT_SYMBOL(global_flush_tlb);