pageattr.c 5.9 KB

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