pageattr.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 cache_flush_page(struct page *p)
  63. {
  64. unsigned long adr = (unsigned long)page_address(p);
  65. int i;
  66. for (i = 0; i < PAGE_SIZE; i += boot_cpu_data.x86_clflush_size)
  67. asm volatile("clflush (%0)" :: "r" (adr + i));
  68. }
  69. static void flush_kernel_map(void *arg)
  70. {
  71. struct list_head *lh = (struct list_head *)arg;
  72. struct page *p;
  73. /* High level code is not ready for clflush yet */
  74. if (0 && cpu_has_clflush) {
  75. list_for_each_entry (p, lh, lru)
  76. cache_flush_page(p);
  77. } else if (boot_cpu_data.x86_model >= 4)
  78. wbinvd();
  79. /* Flush all to work around Errata in early athlons regarding
  80. * large page flushing.
  81. */
  82. __flush_tlb_all();
  83. }
  84. static void set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
  85. {
  86. struct page *page;
  87. unsigned long flags;
  88. set_pte_atomic(kpte, pte); /* change init_mm */
  89. if (SHARED_KERNEL_PMD)
  90. return;
  91. spin_lock_irqsave(&pgd_lock, flags);
  92. for (page = pgd_list; page; page = (struct page *)page->index) {
  93. pgd_t *pgd;
  94. pud_t *pud;
  95. pmd_t *pmd;
  96. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  97. pud = pud_offset(pgd, address);
  98. pmd = pmd_offset(pud, address);
  99. set_pte_atomic((pte_t *)pmd, pte);
  100. }
  101. spin_unlock_irqrestore(&pgd_lock, flags);
  102. }
  103. /*
  104. * No more special protections in this 2/4MB area - revert to a
  105. * large page again.
  106. */
  107. static inline void revert_page(struct page *kpte_page, unsigned long address)
  108. {
  109. pgprot_t ref_prot;
  110. pte_t *linear;
  111. ref_prot =
  112. ((address & LARGE_PAGE_MASK) < (unsigned long)&_etext)
  113. ? PAGE_KERNEL_LARGE_EXEC : PAGE_KERNEL_LARGE;
  114. linear = (pte_t *)
  115. pmd_offset(pud_offset(pgd_offset_k(address), address), address);
  116. set_pmd_pte(linear, address,
  117. pfn_pte((__pa(address) & LARGE_PAGE_MASK) >> PAGE_SHIFT,
  118. ref_prot));
  119. }
  120. static int
  121. __change_page_attr(struct page *page, pgprot_t prot)
  122. {
  123. pte_t *kpte;
  124. unsigned long address;
  125. struct page *kpte_page;
  126. BUG_ON(PageHighMem(page));
  127. address = (unsigned long)page_address(page);
  128. kpte = lookup_address(address);
  129. if (!kpte)
  130. return -EINVAL;
  131. kpte_page = virt_to_page(kpte);
  132. if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL)) {
  133. if (!pte_huge(*kpte)) {
  134. set_pte_atomic(kpte, mk_pte(page, prot));
  135. } else {
  136. pgprot_t ref_prot;
  137. struct page *split;
  138. ref_prot =
  139. ((address & LARGE_PAGE_MASK) < (unsigned long)&_etext)
  140. ? PAGE_KERNEL_EXEC : PAGE_KERNEL;
  141. split = split_large_page(address, prot, ref_prot);
  142. if (!split)
  143. return -ENOMEM;
  144. set_pmd_pte(kpte,address,mk_pte(split, ref_prot));
  145. kpte_page = split;
  146. }
  147. page_private(kpte_page)++;
  148. } else if (!pte_huge(*kpte)) {
  149. set_pte_atomic(kpte, mk_pte(page, PAGE_KERNEL));
  150. BUG_ON(page_private(kpte_page) == 0);
  151. page_private(kpte_page)--;
  152. } else
  153. BUG();
  154. /*
  155. * If the pte was reserved, it means it was created at boot
  156. * time (not via split_large_page) and in turn we must not
  157. * replace it with a largepage.
  158. */
  159. if (!PageReserved(kpte_page)) {
  160. if (cpu_has_pse && (page_private(kpte_page) == 0)) {
  161. ClearPagePrivate(kpte_page);
  162. paravirt_release_pt(page_to_pfn(kpte_page));
  163. list_add(&kpte_page->lru, &df_list);
  164. revert_page(kpte_page, address);
  165. }
  166. }
  167. return 0;
  168. }
  169. static inline void flush_map(struct list_head *l)
  170. {
  171. on_each_cpu(flush_kernel_map, l, 1, 1);
  172. }
  173. /*
  174. * Change the page attributes of an page in the linear mapping.
  175. *
  176. * This should be used when a page is mapped with a different caching policy
  177. * than write-back somewhere - some CPUs do not like it when mappings with
  178. * different caching policies exist. This changes the page attributes of the
  179. * in kernel linear mapping too.
  180. *
  181. * The caller needs to ensure that there are no conflicting mappings elsewhere.
  182. * This function only deals with the kernel linear map.
  183. *
  184. * Caller must call global_flush_tlb() after this.
  185. */
  186. int change_page_attr(struct page *page, int numpages, pgprot_t prot)
  187. {
  188. int err = 0;
  189. int i;
  190. unsigned long flags;
  191. spin_lock_irqsave(&cpa_lock, flags);
  192. for (i = 0; i < numpages; i++, page++) {
  193. err = __change_page_attr(page, prot);
  194. if (err)
  195. break;
  196. }
  197. spin_unlock_irqrestore(&cpa_lock, flags);
  198. return err;
  199. }
  200. void global_flush_tlb(void)
  201. {
  202. struct list_head l;
  203. struct page *pg, *next;
  204. BUG_ON(irqs_disabled());
  205. spin_lock_irq(&cpa_lock);
  206. list_replace_init(&df_list, &l);
  207. spin_unlock_irq(&cpa_lock);
  208. flush_map(&l);
  209. list_for_each_entry_safe(pg, next, &l, lru) {
  210. __free_page(pg);
  211. }
  212. }
  213. #ifdef CONFIG_DEBUG_PAGEALLOC
  214. void kernel_map_pages(struct page *page, int numpages, int enable)
  215. {
  216. if (PageHighMem(page))
  217. return;
  218. if (!enable)
  219. debug_check_no_locks_freed(page_address(page),
  220. numpages * PAGE_SIZE);
  221. /* the return value is ignored - the calls cannot fail,
  222. * large pages are disabled at boot time.
  223. */
  224. change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
  225. /* we should perform an IPI and flush all tlbs,
  226. * but that can deadlock->flush only current cpu.
  227. */
  228. __flush_tlb_all();
  229. }
  230. #endif
  231. EXPORT_SYMBOL(change_page_attr);
  232. EXPORT_SYMBOL(global_flush_tlb);