pageattr.c 5.5 KB

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