PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/release/src-rt/linux/linux-2.6/mm/highmem.c

https://gitlab.com/envieidoc/advancedtomato2
C | 357 lines | 221 code | 57 blank | 79 comment | 23 complexity | fbd7e3fc24966c72edc0a3d95516bc47 MD5 | raw file
  1. /*
  2. * High memory handling common code and variables.
  3. *
  4. * (C) 1999 Andrea Arcangeli, SuSE GmbH, andrea@suse.de
  5. * Gerhard Wichert, Siemens AG, Gerhard.Wichert@pdb.siemens.de
  6. *
  7. *
  8. * Redesigned the x86 32-bit VM architecture to deal with
  9. * 64-bit physical space. With current x86 CPUs this
  10. * means up to 64 Gigabytes physical RAM.
  11. *
  12. * Rewrote high memory support to move the page cache into
  13. * high memory. Implemented permanent (schedulable) kmaps
  14. * based on Linus' idea.
  15. *
  16. * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
  17. */
  18. #include <linux/mm.h>
  19. #include <linux/module.h>
  20. #include <linux/swap.h>
  21. #include <linux/bio.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/mempool.h>
  24. #include <linux/blkdev.h>
  25. #include <linux/init.h>
  26. #include <linux/hash.h>
  27. #include <linux/highmem.h>
  28. #include <linux/blktrace_api.h>
  29. #include <asm/tlbflush.h>
  30. /*
  31. * Virtual_count is not a pure "count".
  32. * 0 means that it is not mapped, and has not been mapped
  33. * since a TLB flush - it is usable.
  34. * 1 means that there are no users, but it has been mapped
  35. * since the last TLB flush - so we can't use it.
  36. * n means that there are (n-1) current users of it.
  37. */
  38. #ifdef CONFIG_HIGHMEM
  39. unsigned long totalhigh_pages __read_mostly;
  40. unsigned int nr_free_highpages (void)
  41. {
  42. pg_data_t *pgdat;
  43. unsigned int pages = 0;
  44. for_each_online_pgdat(pgdat)
  45. pages += zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
  46. NR_FREE_PAGES);
  47. return pages;
  48. }
  49. static int pkmap_count[LAST_PKMAP];
  50. static unsigned int last_pkmap_nr;
  51. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
  52. pte_t * pkmap_page_table;
  53. static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
  54. static void flush_all_zero_pkmaps(void)
  55. {
  56. int i;
  57. flush_cache_kmaps();
  58. for (i = 0; i < LAST_PKMAP; i++) {
  59. struct page *page;
  60. /*
  61. * zero means we don't have anything to do,
  62. * >1 means that it is still in use. Only
  63. * a count of 1 means that it is free but
  64. * needs to be unmapped
  65. */
  66. if (pkmap_count[i] != 1)
  67. continue;
  68. pkmap_count[i] = 0;
  69. /* sanity check */
  70. BUG_ON(pte_none(pkmap_page_table[i]));
  71. /*
  72. * Don't need an atomic fetch-and-clear op here;
  73. * no-one has the page mapped, and cannot get at
  74. * its virtual address (and hence PTE) without first
  75. * getting the kmap_lock (which is held here).
  76. * So no dangers, even with speculative execution.
  77. */
  78. page = pte_page(pkmap_page_table[i]);
  79. pte_clear(&init_mm, (unsigned long)page_address(page),
  80. &pkmap_page_table[i]);
  81. set_page_address(page, NULL);
  82. }
  83. flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
  84. }
  85. /* Flush all unused kmap mappings in order to remove stray
  86. mappings. */
  87. void kmap_flush_unused(void)
  88. {
  89. spin_lock(&kmap_lock);
  90. flush_all_zero_pkmaps();
  91. spin_unlock(&kmap_lock);
  92. }
  93. static inline unsigned long map_new_virtual(struct page *page)
  94. {
  95. unsigned long vaddr;
  96. int count;
  97. start:
  98. count = LAST_PKMAP;
  99. /* Find an empty entry */
  100. for (;;) {
  101. last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
  102. if (!last_pkmap_nr) {
  103. flush_all_zero_pkmaps();
  104. count = LAST_PKMAP;
  105. }
  106. if (!pkmap_count[last_pkmap_nr]) {
  107. if (cpu_has_dc_aliases) {
  108. unsigned int pfn, map_pfn;
  109. /* check page color */
  110. pfn = page_to_pfn(page);
  111. map_pfn = PKMAP_ADDR(last_pkmap_nr) >> PAGE_SHIFT;
  112. /* Avoide possibility of cache Aliasing */
  113. if (!pages_do_alias((map_pfn << PAGE_SHIFT), (pfn << PAGE_SHIFT)))
  114. break; /* Found a usable entry */
  115. } else
  116. break; /* Found a usable entry */
  117. }
  118. if (--count)
  119. continue;
  120. /*
  121. * Sleep for somebody else to unmap their entries
  122. */
  123. {
  124. DECLARE_WAITQUEUE(wait, current);
  125. __set_current_state(TASK_UNINTERRUPTIBLE);
  126. add_wait_queue(&pkmap_map_wait, &wait);
  127. spin_unlock(&kmap_lock);
  128. schedule();
  129. remove_wait_queue(&pkmap_map_wait, &wait);
  130. spin_lock(&kmap_lock);
  131. /* Somebody else might have mapped it while we slept */
  132. if (page_address(page))
  133. return (unsigned long)page_address(page);
  134. /* Re-start */
  135. goto start;
  136. }
  137. }
  138. vaddr = PKMAP_ADDR(last_pkmap_nr);
  139. set_pte_at(&init_mm, vaddr,
  140. &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
  141. pkmap_count[last_pkmap_nr] = 1;
  142. set_page_address(page, (void *)vaddr);
  143. return vaddr;
  144. }
  145. void fastcall *kmap_high(struct page *page)
  146. {
  147. unsigned long vaddr;
  148. /*
  149. * For highmem pages, we can't trust "virtual" until
  150. * after we have the lock.
  151. *
  152. * We cannot call this from interrupts, as it may block
  153. */
  154. spin_lock(&kmap_lock);
  155. vaddr = (unsigned long)page_address(page);
  156. if (!vaddr)
  157. vaddr = map_new_virtual(page);
  158. pkmap_count[PKMAP_NR(vaddr)]++;
  159. BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
  160. spin_unlock(&kmap_lock);
  161. return (void*) vaddr;
  162. }
  163. EXPORT_SYMBOL(kmap_high);
  164. void fastcall kunmap_high(struct page *page)
  165. {
  166. unsigned long vaddr;
  167. unsigned long nr;
  168. int need_wakeup;
  169. spin_lock(&kmap_lock);
  170. vaddr = (unsigned long)page_address(page);
  171. BUG_ON(!vaddr);
  172. nr = PKMAP_NR(vaddr);
  173. /*
  174. * A count must never go down to zero
  175. * without a TLB flush!
  176. */
  177. need_wakeup = 0;
  178. switch (--pkmap_count[nr]) {
  179. case 0:
  180. BUG();
  181. case 1:
  182. /*
  183. * Avoid an unnecessary wake_up() function call.
  184. * The common case is pkmap_count[] == 1, but
  185. * no waiters.
  186. * The tasks queued in the wait-queue are guarded
  187. * by both the lock in the wait-queue-head and by
  188. * the kmap_lock. As the kmap_lock is held here,
  189. * no need for the wait-queue-head's lock. Simply
  190. * test if the queue is empty.
  191. */
  192. need_wakeup = waitqueue_active(&pkmap_map_wait);
  193. }
  194. spin_unlock(&kmap_lock);
  195. /* do wake-up, if needed, race-free outside of the spin lock */
  196. if (need_wakeup)
  197. wake_up(&pkmap_map_wait);
  198. }
  199. EXPORT_SYMBOL(kunmap_high);
  200. #endif
  201. #if defined(HASHED_PAGE_VIRTUAL)
  202. #define PA_HASH_ORDER 7
  203. /*
  204. * Describes one page->virtual association
  205. */
  206. struct page_address_map {
  207. struct page *page;
  208. void *virtual;
  209. struct list_head list;
  210. };
  211. /*
  212. * page_address_map freelist, allocated from page_address_maps.
  213. */
  214. static struct list_head page_address_pool; /* freelist */
  215. static spinlock_t pool_lock; /* protects page_address_pool */
  216. /*
  217. * Hash table bucket
  218. */
  219. static struct page_address_slot {
  220. struct list_head lh; /* List of page_address_maps */
  221. spinlock_t lock; /* Protect this bucket's list */
  222. } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
  223. static struct page_address_slot *page_slot(struct page *page)
  224. {
  225. return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
  226. }
  227. void *page_address(struct page *page)
  228. {
  229. unsigned long flags;
  230. void *ret;
  231. struct page_address_slot *pas;
  232. if (!PageHighMem(page))
  233. return lowmem_page_address(page);
  234. pas = page_slot(page);
  235. ret = NULL;
  236. spin_lock_irqsave(&pas->lock, flags);
  237. if (!list_empty(&pas->lh)) {
  238. struct page_address_map *pam;
  239. list_for_each_entry(pam, &pas->lh, list) {
  240. if (pam->page == page) {
  241. ret = pam->virtual;
  242. goto done;
  243. }
  244. }
  245. }
  246. done:
  247. spin_unlock_irqrestore(&pas->lock, flags);
  248. return ret;
  249. }
  250. EXPORT_SYMBOL(page_address);
  251. void set_page_address(struct page *page, void *virtual)
  252. {
  253. unsigned long flags;
  254. struct page_address_slot *pas;
  255. struct page_address_map *pam;
  256. BUG_ON(!PageHighMem(page));
  257. pas = page_slot(page);
  258. if (virtual) { /* Add */
  259. BUG_ON(list_empty(&page_address_pool));
  260. spin_lock_irqsave(&pool_lock, flags);
  261. pam = list_entry(page_address_pool.next,
  262. struct page_address_map, list);
  263. list_del(&pam->list);
  264. spin_unlock_irqrestore(&pool_lock, flags);
  265. pam->page = page;
  266. pam->virtual = virtual;
  267. spin_lock_irqsave(&pas->lock, flags);
  268. list_add_tail(&pam->list, &pas->lh);
  269. spin_unlock_irqrestore(&pas->lock, flags);
  270. } else { /* Remove */
  271. spin_lock_irqsave(&pas->lock, flags);
  272. list_for_each_entry(pam, &pas->lh, list) {
  273. if (pam->page == page) {
  274. list_del(&pam->list);
  275. spin_unlock_irqrestore(&pas->lock, flags);
  276. spin_lock_irqsave(&pool_lock, flags);
  277. list_add_tail(&pam->list, &page_address_pool);
  278. spin_unlock_irqrestore(&pool_lock, flags);
  279. goto done;
  280. }
  281. }
  282. spin_unlock_irqrestore(&pas->lock, flags);
  283. }
  284. done:
  285. return;
  286. }
  287. static struct page_address_map page_address_maps[LAST_PKMAP];
  288. void __init page_address_init(void)
  289. {
  290. int i;
  291. INIT_LIST_HEAD(&page_address_pool);
  292. for (i = 0; i < ARRAY_SIZE(page_address_maps); i++)
  293. list_add(&page_address_maps[i].list, &page_address_pool);
  294. for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
  295. INIT_LIST_HEAD(&page_address_htable[i].lh);
  296. spin_lock_init(&page_address_htable[i].lock);
  297. }
  298. spin_lock_init(&pool_lock);
  299. }
  300. #endif /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */