highmem.c 14 KB

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