highmem.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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 <asm/tlbflush.h>
  29. static mempool_t *page_pool, *isa_page_pool;
  30. static void *page_pool_alloc_isa(gfp_t gfp_mask, void *data)
  31. {
  32. return alloc_page(gfp_mask | GFP_DMA);
  33. }
  34. static void page_pool_free(void *page, void *data)
  35. {
  36. __free_page(page);
  37. }
  38. /*
  39. * Virtual_count is not a pure "count".
  40. * 0 means that it is not mapped, and has not been mapped
  41. * since a TLB flush - it is usable.
  42. * 1 means that there are no users, but it has been mapped
  43. * since the last TLB flush - so we can't use it.
  44. * n means that there are (n-1) current users of it.
  45. */
  46. #ifdef CONFIG_HIGHMEM
  47. static void *page_pool_alloc(gfp_t gfp_mask, void *data)
  48. {
  49. return alloc_page(gfp_mask);
  50. }
  51. static int pkmap_count[LAST_PKMAP];
  52. static unsigned int last_pkmap_nr;
  53. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
  54. pte_t * pkmap_page_table;
  55. static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
  56. static void flush_all_zero_pkmaps(void)
  57. {
  58. int i;
  59. flush_cache_kmaps();
  60. for (i = 0; i < LAST_PKMAP; i++) {
  61. struct page *page;
  62. /*
  63. * zero means we don't have anything to do,
  64. * >1 means that it is still in use. Only
  65. * a count of 1 means that it is free but
  66. * needs to be unmapped
  67. */
  68. if (pkmap_count[i] != 1)
  69. continue;
  70. pkmap_count[i] = 0;
  71. /* sanity check */
  72. if (pte_none(pkmap_page_table[i]))
  73. BUG();
  74. /*
  75. * Don't need an atomic fetch-and-clear op here;
  76. * no-one has the page mapped, and cannot get at
  77. * its virtual address (and hence PTE) without first
  78. * getting the kmap_lock (which is held here).
  79. * So no dangers, even with speculative execution.
  80. */
  81. page = pte_page(pkmap_page_table[i]);
  82. pte_clear(&init_mm, (unsigned long)page_address(page),
  83. &pkmap_page_table[i]);
  84. set_page_address(page, NULL);
  85. }
  86. flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
  87. }
  88. static inline unsigned long map_new_virtual(struct page *page)
  89. {
  90. unsigned long vaddr;
  91. int count;
  92. start:
  93. count = LAST_PKMAP;
  94. /* Find an empty entry */
  95. for (;;) {
  96. last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
  97. if (!last_pkmap_nr) {
  98. flush_all_zero_pkmaps();
  99. count = LAST_PKMAP;
  100. }
  101. if (!pkmap_count[last_pkmap_nr])
  102. break; /* Found a usable entry */
  103. if (--count)
  104. continue;
  105. /*
  106. * Sleep for somebody else to unmap their entries
  107. */
  108. {
  109. DECLARE_WAITQUEUE(wait, current);
  110. __set_current_state(TASK_UNINTERRUPTIBLE);
  111. add_wait_queue(&pkmap_map_wait, &wait);
  112. spin_unlock(&kmap_lock);
  113. schedule();
  114. remove_wait_queue(&pkmap_map_wait, &wait);
  115. spin_lock(&kmap_lock);
  116. /* Somebody else might have mapped it while we slept */
  117. if (page_address(page))
  118. return (unsigned long)page_address(page);
  119. /* Re-start */
  120. goto start;
  121. }
  122. }
  123. vaddr = PKMAP_ADDR(last_pkmap_nr);
  124. set_pte_at(&init_mm, vaddr,
  125. &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
  126. pkmap_count[last_pkmap_nr] = 1;
  127. set_page_address(page, (void *)vaddr);
  128. return vaddr;
  129. }
  130. void fastcall *kmap_high(struct page *page)
  131. {
  132. unsigned long vaddr;
  133. /*
  134. * For highmem pages, we can't trust "virtual" until
  135. * after we have the lock.
  136. *
  137. * We cannot call this from interrupts, as it may block
  138. */
  139. spin_lock(&kmap_lock);
  140. vaddr = (unsigned long)page_address(page);
  141. if (!vaddr)
  142. vaddr = map_new_virtual(page);
  143. pkmap_count[PKMAP_NR(vaddr)]++;
  144. if (pkmap_count[PKMAP_NR(vaddr)] < 2)
  145. BUG();
  146. spin_unlock(&kmap_lock);
  147. return (void*) vaddr;
  148. }
  149. EXPORT_SYMBOL(kmap_high);
  150. void fastcall kunmap_high(struct page *page)
  151. {
  152. unsigned long vaddr;
  153. unsigned long nr;
  154. int need_wakeup;
  155. spin_lock(&kmap_lock);
  156. vaddr = (unsigned long)page_address(page);
  157. if (!vaddr)
  158. BUG();
  159. nr = PKMAP_NR(vaddr);
  160. /*
  161. * A count must never go down to zero
  162. * without a TLB flush!
  163. */
  164. need_wakeup = 0;
  165. switch (--pkmap_count[nr]) {
  166. case 0:
  167. BUG();
  168. case 1:
  169. /*
  170. * Avoid an unnecessary wake_up() function call.
  171. * The common case is pkmap_count[] == 1, but
  172. * no waiters.
  173. * The tasks queued in the wait-queue are guarded
  174. * by both the lock in the wait-queue-head and by
  175. * the kmap_lock. As the kmap_lock is held here,
  176. * no need for the wait-queue-head's lock. Simply
  177. * test if the queue is empty.
  178. */
  179. need_wakeup = waitqueue_active(&pkmap_map_wait);
  180. }
  181. spin_unlock(&kmap_lock);
  182. /* do wake-up, if needed, race-free outside of the spin lock */
  183. if (need_wakeup)
  184. wake_up(&pkmap_map_wait);
  185. }
  186. EXPORT_SYMBOL(kunmap_high);
  187. #define POOL_SIZE 64
  188. static __init int init_emergency_pool(void)
  189. {
  190. struct sysinfo i;
  191. si_meminfo(&i);
  192. si_swapinfo(&i);
  193. if (!i.totalhigh)
  194. return 0;
  195. page_pool = mempool_create(POOL_SIZE, page_pool_alloc, page_pool_free, NULL);
  196. if (!page_pool)
  197. BUG();
  198. printk("highmem bounce pool size: %d pages\n", POOL_SIZE);
  199. return 0;
  200. }
  201. __initcall(init_emergency_pool);
  202. /*
  203. * highmem version, map in to vec
  204. */
  205. static void bounce_copy_vec(struct bio_vec *to, unsigned char *vfrom)
  206. {
  207. unsigned long flags;
  208. unsigned char *vto;
  209. local_irq_save(flags);
  210. vto = kmap_atomic(to->bv_page, KM_BOUNCE_READ);
  211. memcpy(vto + to->bv_offset, vfrom, to->bv_len);
  212. kunmap_atomic(vto, KM_BOUNCE_READ);
  213. local_irq_restore(flags);
  214. }
  215. #else /* CONFIG_HIGHMEM */
  216. #define bounce_copy_vec(to, vfrom) \
  217. memcpy(page_address((to)->bv_page) + (to)->bv_offset, vfrom, (to)->bv_len)
  218. #endif
  219. #define ISA_POOL_SIZE 16
  220. /*
  221. * gets called "every" time someone init's a queue with BLK_BOUNCE_ISA
  222. * as the max address, so check if the pool has already been created.
  223. */
  224. int init_emergency_isa_pool(void)
  225. {
  226. if (isa_page_pool)
  227. return 0;
  228. isa_page_pool = mempool_create(ISA_POOL_SIZE, page_pool_alloc_isa, page_pool_free, NULL);
  229. if (!isa_page_pool)
  230. BUG();
  231. printk("isa bounce pool size: %d pages\n", ISA_POOL_SIZE);
  232. return 0;
  233. }
  234. /*
  235. * Simple bounce buffer support for highmem pages. Depending on the
  236. * queue gfp mask set, *to may or may not be a highmem page. kmap it
  237. * always, it will do the Right Thing
  238. */
  239. static void copy_to_high_bio_irq(struct bio *to, struct bio *from)
  240. {
  241. unsigned char *vfrom;
  242. struct bio_vec *tovec, *fromvec;
  243. int i;
  244. __bio_for_each_segment(tovec, to, i, 0) {
  245. fromvec = from->bi_io_vec + i;
  246. /*
  247. * not bounced
  248. */
  249. if (tovec->bv_page == fromvec->bv_page)
  250. continue;
  251. /*
  252. * fromvec->bv_offset and fromvec->bv_len might have been
  253. * modified by the block layer, so use the original copy,
  254. * bounce_copy_vec already uses tovec->bv_len
  255. */
  256. vfrom = page_address(fromvec->bv_page) + tovec->bv_offset;
  257. flush_dcache_page(tovec->bv_page);
  258. bounce_copy_vec(tovec, vfrom);
  259. }
  260. }
  261. static void bounce_end_io(struct bio *bio, mempool_t *pool, int err)
  262. {
  263. struct bio *bio_orig = bio->bi_private;
  264. struct bio_vec *bvec, *org_vec;
  265. int i;
  266. if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags))
  267. set_bit(BIO_EOPNOTSUPP, &bio_orig->bi_flags);
  268. /*
  269. * free up bounce indirect pages used
  270. */
  271. __bio_for_each_segment(bvec, bio, i, 0) {
  272. org_vec = bio_orig->bi_io_vec + i;
  273. if (bvec->bv_page == org_vec->bv_page)
  274. continue;
  275. mempool_free(bvec->bv_page, pool);
  276. dec_page_state(nr_bounce);
  277. }
  278. bio_endio(bio_orig, bio_orig->bi_size, err);
  279. bio_put(bio);
  280. }
  281. static int bounce_end_io_write(struct bio *bio, unsigned int bytes_done,int err)
  282. {
  283. if (bio->bi_size)
  284. return 1;
  285. bounce_end_io(bio, page_pool, err);
  286. return 0;
  287. }
  288. static int bounce_end_io_write_isa(struct bio *bio, unsigned int bytes_done, int err)
  289. {
  290. if (bio->bi_size)
  291. return 1;
  292. bounce_end_io(bio, isa_page_pool, err);
  293. return 0;
  294. }
  295. static void __bounce_end_io_read(struct bio *bio, mempool_t *pool, int err)
  296. {
  297. struct bio *bio_orig = bio->bi_private;
  298. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  299. copy_to_high_bio_irq(bio_orig, bio);
  300. bounce_end_io(bio, pool, err);
  301. }
  302. static int bounce_end_io_read(struct bio *bio, unsigned int bytes_done, int err)
  303. {
  304. if (bio->bi_size)
  305. return 1;
  306. __bounce_end_io_read(bio, page_pool, err);
  307. return 0;
  308. }
  309. static int bounce_end_io_read_isa(struct bio *bio, unsigned int bytes_done, int err)
  310. {
  311. if (bio->bi_size)
  312. return 1;
  313. __bounce_end_io_read(bio, isa_page_pool, err);
  314. return 0;
  315. }
  316. static void __blk_queue_bounce(request_queue_t *q, struct bio **bio_orig,
  317. mempool_t *pool)
  318. {
  319. struct page *page;
  320. struct bio *bio = NULL;
  321. int i, rw = bio_data_dir(*bio_orig);
  322. struct bio_vec *to, *from;
  323. bio_for_each_segment(from, *bio_orig, i) {
  324. page = from->bv_page;
  325. /*
  326. * is destination page below bounce pfn?
  327. */
  328. if (page_to_pfn(page) < q->bounce_pfn)
  329. continue;
  330. /*
  331. * irk, bounce it
  332. */
  333. if (!bio)
  334. bio = bio_alloc(GFP_NOIO, (*bio_orig)->bi_vcnt);
  335. to = bio->bi_io_vec + i;
  336. to->bv_page = mempool_alloc(pool, q->bounce_gfp);
  337. to->bv_len = from->bv_len;
  338. to->bv_offset = from->bv_offset;
  339. inc_page_state(nr_bounce);
  340. if (rw == WRITE) {
  341. char *vto, *vfrom;
  342. flush_dcache_page(from->bv_page);
  343. vto = page_address(to->bv_page) + to->bv_offset;
  344. vfrom = kmap(from->bv_page) + from->bv_offset;
  345. memcpy(vto, vfrom, to->bv_len);
  346. kunmap(from->bv_page);
  347. }
  348. }
  349. /*
  350. * no pages bounced
  351. */
  352. if (!bio)
  353. return;
  354. /*
  355. * at least one page was bounced, fill in possible non-highmem
  356. * pages
  357. */
  358. __bio_for_each_segment(from, *bio_orig, i, 0) {
  359. to = bio_iovec_idx(bio, i);
  360. if (!to->bv_page) {
  361. to->bv_page = from->bv_page;
  362. to->bv_len = from->bv_len;
  363. to->bv_offset = from->bv_offset;
  364. }
  365. }
  366. bio->bi_bdev = (*bio_orig)->bi_bdev;
  367. bio->bi_flags |= (1 << BIO_BOUNCED);
  368. bio->bi_sector = (*bio_orig)->bi_sector;
  369. bio->bi_rw = (*bio_orig)->bi_rw;
  370. bio->bi_vcnt = (*bio_orig)->bi_vcnt;
  371. bio->bi_idx = (*bio_orig)->bi_idx;
  372. bio->bi_size = (*bio_orig)->bi_size;
  373. if (pool == page_pool) {
  374. bio->bi_end_io = bounce_end_io_write;
  375. if (rw == READ)
  376. bio->bi_end_io = bounce_end_io_read;
  377. } else {
  378. bio->bi_end_io = bounce_end_io_write_isa;
  379. if (rw == READ)
  380. bio->bi_end_io = bounce_end_io_read_isa;
  381. }
  382. bio->bi_private = *bio_orig;
  383. *bio_orig = bio;
  384. }
  385. void blk_queue_bounce(request_queue_t *q, struct bio **bio_orig)
  386. {
  387. mempool_t *pool;
  388. /*
  389. * for non-isa bounce case, just check if the bounce pfn is equal
  390. * to or bigger than the highest pfn in the system -- in that case,
  391. * don't waste time iterating over bio segments
  392. */
  393. if (!(q->bounce_gfp & GFP_DMA)) {
  394. if (q->bounce_pfn >= blk_max_pfn)
  395. return;
  396. pool = page_pool;
  397. } else {
  398. BUG_ON(!isa_page_pool);
  399. pool = isa_page_pool;
  400. }
  401. /*
  402. * slow path
  403. */
  404. __blk_queue_bounce(q, bio_orig, pool);
  405. }
  406. EXPORT_SYMBOL(blk_queue_bounce);
  407. #if defined(HASHED_PAGE_VIRTUAL)
  408. #define PA_HASH_ORDER 7
  409. /*
  410. * Describes one page->virtual association
  411. */
  412. struct page_address_map {
  413. struct page *page;
  414. void *virtual;
  415. struct list_head list;
  416. };
  417. /*
  418. * page_address_map freelist, allocated from page_address_maps.
  419. */
  420. static struct list_head page_address_pool; /* freelist */
  421. static spinlock_t pool_lock; /* protects page_address_pool */
  422. /*
  423. * Hash table bucket
  424. */
  425. static struct page_address_slot {
  426. struct list_head lh; /* List of page_address_maps */
  427. spinlock_t lock; /* Protect this bucket's list */
  428. } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
  429. static struct page_address_slot *page_slot(struct page *page)
  430. {
  431. return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
  432. }
  433. void *page_address(struct page *page)
  434. {
  435. unsigned long flags;
  436. void *ret;
  437. struct page_address_slot *pas;
  438. if (!PageHighMem(page))
  439. return lowmem_page_address(page);
  440. pas = page_slot(page);
  441. ret = NULL;
  442. spin_lock_irqsave(&pas->lock, flags);
  443. if (!list_empty(&pas->lh)) {
  444. struct page_address_map *pam;
  445. list_for_each_entry(pam, &pas->lh, list) {
  446. if (pam->page == page) {
  447. ret = pam->virtual;
  448. goto done;
  449. }
  450. }
  451. }
  452. done:
  453. spin_unlock_irqrestore(&pas->lock, flags);
  454. return ret;
  455. }
  456. EXPORT_SYMBOL(page_address);
  457. void set_page_address(struct page *page, void *virtual)
  458. {
  459. unsigned long flags;
  460. struct page_address_slot *pas;
  461. struct page_address_map *pam;
  462. BUG_ON(!PageHighMem(page));
  463. pas = page_slot(page);
  464. if (virtual) { /* Add */
  465. BUG_ON(list_empty(&page_address_pool));
  466. spin_lock_irqsave(&pool_lock, flags);
  467. pam = list_entry(page_address_pool.next,
  468. struct page_address_map, list);
  469. list_del(&pam->list);
  470. spin_unlock_irqrestore(&pool_lock, flags);
  471. pam->page = page;
  472. pam->virtual = virtual;
  473. spin_lock_irqsave(&pas->lock, flags);
  474. list_add_tail(&pam->list, &pas->lh);
  475. spin_unlock_irqrestore(&pas->lock, flags);
  476. } else { /* Remove */
  477. spin_lock_irqsave(&pas->lock, flags);
  478. list_for_each_entry(pam, &pas->lh, list) {
  479. if (pam->page == page) {
  480. list_del(&pam->list);
  481. spin_unlock_irqrestore(&pas->lock, flags);
  482. spin_lock_irqsave(&pool_lock, flags);
  483. list_add_tail(&pam->list, &page_address_pool);
  484. spin_unlock_irqrestore(&pool_lock, flags);
  485. goto done;
  486. }
  487. }
  488. spin_unlock_irqrestore(&pas->lock, flags);
  489. }
  490. done:
  491. return;
  492. }
  493. static struct page_address_map page_address_maps[LAST_PKMAP];
  494. void __init page_address_init(void)
  495. {
  496. int i;
  497. INIT_LIST_HEAD(&page_address_pool);
  498. for (i = 0; i < ARRAY_SIZE(page_address_maps); i++)
  499. list_add(&page_address_maps[i].list, &page_address_pool);
  500. for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
  501. INIT_LIST_HEAD(&page_address_htable[i].lh);
  502. spin_lock_init(&page_address_htable[i].lock);
  503. }
  504. spin_lock_init(&pool_lock);
  505. }
  506. #endif /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */