pageattr_32.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright 2002 Andi Kleen, SuSE Labs.
  3. * Thanks to Ben LaHaise for precious feedback.
  4. */
  5. #include <linux/highmem.h>
  6. #include <linux/module.h>
  7. #include <linux/sched.h>
  8. #include <linux/slab.h>
  9. #include <linux/mm.h>
  10. #include <asm/processor.h>
  11. #include <asm/tlbflush.h>
  12. #include <asm/sections.h>
  13. #include <asm/uaccess.h>
  14. #include <asm/pgalloc.h>
  15. pte_t *lookup_address(unsigned long address, int *level)
  16. {
  17. pgd_t *pgd = pgd_offset_k(address);
  18. pud_t *pud;
  19. pmd_t *pmd;
  20. if (pgd_none(*pgd))
  21. return NULL;
  22. pud = pud_offset(pgd, address);
  23. if (pud_none(*pud))
  24. return NULL;
  25. pmd = pmd_offset(pud, address);
  26. if (pmd_none(*pmd))
  27. return NULL;
  28. *level = 2;
  29. if (pmd_large(*pmd))
  30. return (pte_t *)pmd;
  31. *level = 3;
  32. return pte_offset_kernel(pmd, address);
  33. }
  34. static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte)
  35. {
  36. struct page *page;
  37. /* change init_mm */
  38. set_pte_atomic(kpte, pte);
  39. if (SHARED_KERNEL_PMD)
  40. return;
  41. for (page = pgd_list; page; page = (struct page *)page->index) {
  42. pgd_t *pgd;
  43. pud_t *pud;
  44. pmd_t *pmd;
  45. pgd = (pgd_t *)page_address(page) + pgd_index(address);
  46. pud = pud_offset(pgd, address);
  47. pmd = pmd_offset(pud, address);
  48. set_pte_atomic((pte_t *)pmd, pte);
  49. }
  50. }
  51. static int split_large_page(pte_t *kpte, unsigned long address)
  52. {
  53. pgprot_t ref_prot = pte_pgprot(pte_clrhuge(*kpte));
  54. gfp_t gfp_flags = GFP_KERNEL;
  55. unsigned long flags;
  56. unsigned long addr;
  57. pte_t *pbase, *tmp;
  58. struct page *base;
  59. int i, level;
  60. #ifdef CONFIG_DEBUG_PAGEALLOC
  61. gfp_flags = GFP_ATOMIC;
  62. #endif
  63. base = alloc_pages(gfp_flags, 0);
  64. if (!base)
  65. return -ENOMEM;
  66. spin_lock_irqsave(&pgd_lock, flags);
  67. /*
  68. * Check for races, another CPU might have split this page
  69. * up for us already:
  70. */
  71. tmp = lookup_address(address, &level);
  72. if (tmp != kpte) {
  73. WARN_ON_ONCE(1);
  74. goto out_unlock;
  75. }
  76. address = __pa(address);
  77. addr = address & LARGE_PAGE_MASK;
  78. pbase = (pte_t *)page_address(base);
  79. paravirt_alloc_pt(&init_mm, page_to_pfn(base));
  80. for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE)
  81. set_pte(&pbase[i], pfn_pte(addr >> PAGE_SHIFT, ref_prot));
  82. /*
  83. * Install the new, split up pagetable:
  84. */
  85. __set_pmd_pte(kpte, address, mk_pte(base, ref_prot));
  86. base = NULL;
  87. out_unlock:
  88. spin_unlock_irqrestore(&pgd_lock, flags);
  89. if (base)
  90. __free_pages(base, 0);
  91. return 0;
  92. }
  93. static int __change_page_attr(struct page *page, pgprot_t prot)
  94. {
  95. struct page *kpte_page;
  96. unsigned long address;
  97. int level, err = 0;
  98. pte_t *kpte;
  99. BUG_ON(PageHighMem(page));
  100. address = (unsigned long)page_address(page);
  101. repeat:
  102. kpte = lookup_address(address, &level);
  103. if (!kpte)
  104. return -EINVAL;
  105. kpte_page = virt_to_page(kpte);
  106. BUG_ON(PageLRU(kpte_page));
  107. BUG_ON(PageCompound(kpte_page));
  108. /*
  109. * Better fail early if someone sets the kernel text to NX.
  110. * Does not cover __inittext
  111. */
  112. BUG_ON(address >= (unsigned long)&_text &&
  113. address < (unsigned long)&_etext &&
  114. (pgprot_val(prot) & _PAGE_NX));
  115. if (level == 3) {
  116. set_pte_atomic(kpte, mk_pte(page, canon_pgprot(prot)));
  117. } else {
  118. err = split_large_page(kpte, address);
  119. if (!err)
  120. goto repeat;
  121. }
  122. return err;
  123. }
  124. /*
  125. * Change the page attributes of an page in the linear mapping.
  126. *
  127. * This should be used when a page is mapped with a different caching policy
  128. * than write-back somewhere - some CPUs do not like it when mappings with
  129. * different caching policies exist. This changes the page attributes of the
  130. * in kernel linear mapping too.
  131. *
  132. * The caller needs to ensure that there are no conflicting mappings elsewhere.
  133. * This function only deals with the kernel linear map.
  134. *
  135. * Caller must call global_flush_tlb() after this.
  136. */
  137. int change_page_attr(struct page *page, int numpages, pgprot_t prot)
  138. {
  139. int err = 0, i;
  140. for (i = 0; i < numpages; i++, page++) {
  141. err = __change_page_attr(page, prot);
  142. if (err)
  143. break;
  144. }
  145. return err;
  146. }
  147. EXPORT_SYMBOL(change_page_attr);
  148. int change_page_attr_addr(unsigned long addr, int numpages, pgprot_t prot)
  149. {
  150. int i;
  151. unsigned long pfn = (__pa(addr) >> PAGE_SHIFT);
  152. for (i = 0; i < numpages; i++) {
  153. if (!pfn_valid(pfn + i)) {
  154. WARN_ON_ONCE(1);
  155. break;
  156. } else {
  157. int level;
  158. pte_t *pte = lookup_address(addr + i*PAGE_SIZE, &level);
  159. BUG_ON(pte && pte_none(*pte));
  160. }
  161. }
  162. return change_page_attr(virt_to_page(addr), i, prot);
  163. }
  164. static void flush_kernel_map(void *arg)
  165. {
  166. /*
  167. * Flush all to work around Errata in early athlons regarding
  168. * large page flushing.
  169. */
  170. __flush_tlb_all();
  171. if (boot_cpu_data.x86_model >= 4)
  172. wbinvd();
  173. }
  174. void global_flush_tlb(void)
  175. {
  176. BUG_ON(irqs_disabled());
  177. on_each_cpu(flush_kernel_map, NULL, 1, 1);
  178. }
  179. EXPORT_SYMBOL(global_flush_tlb);
  180. #ifdef CONFIG_DEBUG_PAGEALLOC
  181. void kernel_map_pages(struct page *page, int numpages, int enable)
  182. {
  183. if (PageHighMem(page))
  184. return;
  185. if (!enable) {
  186. debug_check_no_locks_freed(page_address(page),
  187. numpages * PAGE_SIZE);
  188. }
  189. /*
  190. * If page allocator is not up yet then do not call c_p_a():
  191. */
  192. if (!debug_pagealloc_enabled)
  193. return;
  194. /*
  195. * the return value is ignored - the calls cannot fail,
  196. * large pages are disabled at boot time.
  197. */
  198. change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
  199. /*
  200. * we should perform an IPI and flush all tlbs,
  201. * but that can deadlock->flush only current cpu.
  202. */
  203. __flush_tlb_all();
  204. }
  205. #endif