highmem.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/highmem.h>
  15. #include <linux/module.h>
  16. #include <linux/pagemap.h>
  17. #include <asm/homecache.h>
  18. #define kmap_get_pte(vaddr) \
  19. pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr), (vaddr)),\
  20. (vaddr)), (vaddr))
  21. void *kmap(struct page *page)
  22. {
  23. void *kva;
  24. unsigned long flags;
  25. pte_t *ptep;
  26. might_sleep();
  27. if (!PageHighMem(page))
  28. return page_address(page);
  29. kva = kmap_high(page);
  30. /*
  31. * Rewrite the PTE under the lock. This ensures that the page
  32. * is not currently migrating.
  33. */
  34. ptep = kmap_get_pte((unsigned long)kva);
  35. flags = homecache_kpte_lock();
  36. set_pte_at(&init_mm, kva, ptep, mk_pte(page, page_to_kpgprot(page)));
  37. homecache_kpte_unlock(flags);
  38. return kva;
  39. }
  40. EXPORT_SYMBOL(kmap);
  41. void kunmap(struct page *page)
  42. {
  43. if (in_interrupt())
  44. BUG();
  45. if (!PageHighMem(page))
  46. return;
  47. kunmap_high(page);
  48. }
  49. EXPORT_SYMBOL(kunmap);
  50. static void debug_kmap_atomic_prot(enum km_type type)
  51. {
  52. #ifdef CONFIG_DEBUG_HIGHMEM
  53. static unsigned warn_count = 10;
  54. if (unlikely(warn_count == 0))
  55. return;
  56. if (unlikely(in_interrupt())) {
  57. if (in_irq()) {
  58. if (type != KM_IRQ0 && type != KM_IRQ1 &&
  59. type != KM_BIO_SRC_IRQ &&
  60. /* type != KM_BIO_DST_IRQ && */
  61. type != KM_BOUNCE_READ) {
  62. WARN_ON(1);
  63. warn_count--;
  64. }
  65. } else if (!irqs_disabled()) { /* softirq */
  66. if (type != KM_IRQ0 && type != KM_IRQ1 &&
  67. type != KM_SOFTIRQ0 && type != KM_SOFTIRQ1 &&
  68. type != KM_SKB_SUNRPC_DATA &&
  69. type != KM_SKB_DATA_SOFTIRQ &&
  70. type != KM_BOUNCE_READ) {
  71. WARN_ON(1);
  72. warn_count--;
  73. }
  74. }
  75. }
  76. if (type == KM_IRQ0 || type == KM_IRQ1 || type == KM_BOUNCE_READ ||
  77. type == KM_BIO_SRC_IRQ /* || type == KM_BIO_DST_IRQ */) {
  78. if (!irqs_disabled()) {
  79. WARN_ON(1);
  80. warn_count--;
  81. }
  82. } else if (type == KM_SOFTIRQ0 || type == KM_SOFTIRQ1) {
  83. if (irq_count() == 0 && !irqs_disabled()) {
  84. WARN_ON(1);
  85. warn_count--;
  86. }
  87. }
  88. #endif
  89. }
  90. /*
  91. * Describe a single atomic mapping of a page on a given cpu at a
  92. * given address, and allow it to be linked into a list.
  93. */
  94. struct atomic_mapped_page {
  95. struct list_head list;
  96. struct page *page;
  97. int cpu;
  98. unsigned long va;
  99. };
  100. static spinlock_t amp_lock = __SPIN_LOCK_UNLOCKED(&amp_lock);
  101. static struct list_head amp_list = LIST_HEAD_INIT(amp_list);
  102. /*
  103. * Combining this structure with a per-cpu declaration lets us give
  104. * each cpu an atomic_mapped_page structure per type.
  105. */
  106. struct kmap_amps {
  107. struct atomic_mapped_page per_type[KM_TYPE_NR];
  108. };
  109. static DEFINE_PER_CPU(struct kmap_amps, amps);
  110. /*
  111. * Add a page and va, on this cpu, to the list of kmap_atomic pages,
  112. * and write the new pte to memory. Writing the new PTE under the
  113. * lock guarantees that it is either on the list before migration starts
  114. * (if we won the race), or set_pte() sets the migrating bit in the PTE
  115. * (if we lost the race). And doing it under the lock guarantees
  116. * that when kmap_atomic_fix_one_pte() comes along, it finds a valid
  117. * PTE in memory, iff the mapping is still on the amp_list.
  118. *
  119. * Finally, doing it under the lock lets us safely examine the page
  120. * to see if it is immutable or not, for the generic kmap_atomic() case.
  121. * If we examine it earlier we are exposed to a race where it looks
  122. * writable earlier, but becomes immutable before we write the PTE.
  123. */
  124. static void kmap_atomic_register(struct page *page, enum km_type type,
  125. unsigned long va, pte_t *ptep, pte_t pteval)
  126. {
  127. unsigned long flags;
  128. struct atomic_mapped_page *amp;
  129. flags = homecache_kpte_lock();
  130. spin_lock(&amp_lock);
  131. /* With interrupts disabled, now fill in the per-cpu info. */
  132. amp = &__get_cpu_var(amps).per_type[type];
  133. amp->page = page;
  134. amp->cpu = smp_processor_id();
  135. amp->va = va;
  136. /* For generic kmap_atomic(), choose the PTE writability now. */
  137. if (!pte_read(pteval))
  138. pteval = mk_pte(page, page_to_kpgprot(page));
  139. list_add(&amp->list, &amp_list);
  140. set_pte(ptep, pteval);
  141. arch_flush_lazy_mmu_mode();
  142. spin_unlock(&amp_lock);
  143. homecache_kpte_unlock(flags);
  144. }
  145. /*
  146. * Remove a page and va, on this cpu, from the list of kmap_atomic pages.
  147. * Linear-time search, but we count on the lists being short.
  148. * We don't need to adjust the PTE under the lock (as opposed to the
  149. * kmap_atomic_register() case), since we're just unconditionally
  150. * zeroing the PTE after it's off the list.
  151. */
  152. static void kmap_atomic_unregister(struct page *page, unsigned long va)
  153. {
  154. unsigned long flags;
  155. struct atomic_mapped_page *amp;
  156. int cpu = smp_processor_id();
  157. spin_lock_irqsave(&amp_lock, flags);
  158. list_for_each_entry(amp, &amp_list, list) {
  159. if (amp->page == page && amp->cpu == cpu && amp->va == va)
  160. break;
  161. }
  162. BUG_ON(&amp->list == &amp_list);
  163. list_del(&amp->list);
  164. spin_unlock_irqrestore(&amp_lock, flags);
  165. }
  166. /* Helper routine for kmap_atomic_fix_kpte(), below. */
  167. static void kmap_atomic_fix_one_kpte(struct atomic_mapped_page *amp,
  168. int finished)
  169. {
  170. pte_t *ptep = kmap_get_pte(amp->va);
  171. if (!finished) {
  172. set_pte(ptep, pte_mkmigrate(*ptep));
  173. flush_remote(0, 0, NULL, amp->va, PAGE_SIZE, PAGE_SIZE,
  174. cpumask_of(amp->cpu), NULL, 0);
  175. } else {
  176. /*
  177. * Rewrite a default kernel PTE for this page.
  178. * We rely on the fact that set_pte() writes the
  179. * present+migrating bits last.
  180. */
  181. pte_t pte = mk_pte(amp->page, page_to_kpgprot(amp->page));
  182. set_pte(ptep, pte);
  183. }
  184. }
  185. /*
  186. * This routine is a helper function for homecache_fix_kpte(); see
  187. * its comments for more information on the "finished" argument here.
  188. *
  189. * Note that we hold the lock while doing the remote flushes, which
  190. * will stall any unrelated cpus trying to do kmap_atomic operations.
  191. * We could just update the PTEs under the lock, and save away copies
  192. * of the structs (or just the va+cpu), then flush them after we
  193. * release the lock, but it seems easier just to do it all under the lock.
  194. */
  195. void kmap_atomic_fix_kpte(struct page *page, int finished)
  196. {
  197. struct atomic_mapped_page *amp;
  198. unsigned long flags;
  199. spin_lock_irqsave(&amp_lock, flags);
  200. list_for_each_entry(amp, &amp_list, list) {
  201. if (amp->page == page)
  202. kmap_atomic_fix_one_kpte(amp, finished);
  203. }
  204. spin_unlock_irqrestore(&amp_lock, flags);
  205. }
  206. /*
  207. * kmap_atomic/kunmap_atomic is significantly faster than kmap/kunmap
  208. * because the kmap code must perform a global TLB invalidation when
  209. * the kmap pool wraps.
  210. *
  211. * Note that they may be slower than on x86 (etc.) because unlike on
  212. * those platforms, we do have to take a global lock to map and unmap
  213. * pages on Tile (see above).
  214. *
  215. * When holding an atomic kmap is is not legal to sleep, so atomic
  216. * kmaps are appropriate for short, tight code paths only.
  217. */
  218. void *kmap_atomic_prot(struct page *page, enum km_type type, pgprot_t prot)
  219. {
  220. enum fixed_addresses idx;
  221. unsigned long vaddr;
  222. pte_t *pte;
  223. /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
  224. pagefault_disable();
  225. /* Avoid icache flushes by disallowing atomic executable mappings. */
  226. BUG_ON(pte_exec(prot));
  227. if (!PageHighMem(page))
  228. return page_address(page);
  229. debug_kmap_atomic_prot(type);
  230. idx = type + KM_TYPE_NR*smp_processor_id();
  231. vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  232. pte = kmap_get_pte(vaddr);
  233. BUG_ON(!pte_none(*pte));
  234. /* Register that this page is mapped atomically on this cpu. */
  235. kmap_atomic_register(page, type, vaddr, pte, mk_pte(page, prot));
  236. return (void *)vaddr;
  237. }
  238. EXPORT_SYMBOL(kmap_atomic_prot);
  239. void *kmap_atomic(struct page *page, enum km_type type)
  240. {
  241. /* PAGE_NONE is a magic value that tells us to check immutability. */
  242. return kmap_atomic_prot(page, type, PAGE_NONE);
  243. }
  244. EXPORT_SYMBOL(kmap_atomic);
  245. void kunmap_atomic_notypecheck(void *kvaddr, enum km_type type)
  246. {
  247. unsigned long vaddr = (unsigned long) kvaddr & PAGE_MASK;
  248. enum fixed_addresses idx = type + KM_TYPE_NR*smp_processor_id();
  249. /*
  250. * Force other mappings to Oops if they try to access this pte without
  251. * first remapping it. Keeping stale mappings around is a bad idea.
  252. */
  253. if (vaddr == __fix_to_virt(FIX_KMAP_BEGIN+idx)) {
  254. pte_t *pte = kmap_get_pte(vaddr);
  255. pte_t pteval = *pte;
  256. BUG_ON(!pte_present(pteval) && !pte_migrating(pteval));
  257. kmap_atomic_unregister(pte_page(pteval), vaddr);
  258. kpte_clear_flush(pte, vaddr);
  259. } else {
  260. /* Must be a lowmem page */
  261. BUG_ON(vaddr < PAGE_OFFSET);
  262. BUG_ON(vaddr >= (unsigned long)high_memory);
  263. }
  264. arch_flush_lazy_mmu_mode();
  265. pagefault_enable();
  266. }
  267. EXPORT_SYMBOL(kunmap_atomic_notypecheck);
  268. /*
  269. * This API is supposed to allow us to map memory without a "struct page".
  270. * Currently we don't support this, though this may change in the future.
  271. */
  272. void *kmap_atomic_pfn(unsigned long pfn, enum km_type type)
  273. {
  274. return kmap_atomic(pfn_to_page(pfn), type);
  275. }
  276. void *kmap_atomic_prot_pfn(unsigned long pfn, enum km_type type, pgprot_t prot)
  277. {
  278. return kmap_atomic_prot(pfn_to_page(pfn), type, prot);
  279. }
  280. struct page *kmap_atomic_to_page(void *ptr)
  281. {
  282. pte_t *pte;
  283. unsigned long vaddr = (unsigned long)ptr;
  284. if (vaddr < FIXADDR_START)
  285. return virt_to_page(ptr);
  286. pte = kmap_get_pte(vaddr);
  287. return pte_page(*pte);
  288. }