highmem.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. }
  274. bio_endio(bio_orig, bio_orig->bi_size, err);
  275. bio_put(bio);
  276. }
  277. static int bounce_end_io_write(struct bio *bio, unsigned int bytes_done,int err)
  278. {
  279. if (bio->bi_size)
  280. return 1;
  281. bounce_end_io(bio, page_pool, err);
  282. return 0;
  283. }
  284. static int bounce_end_io_write_isa(struct bio *bio, unsigned int bytes_done, int err)
  285. {
  286. if (bio->bi_size)
  287. return 1;
  288. bounce_end_io(bio, isa_page_pool, err);
  289. return 0;
  290. }
  291. static void __bounce_end_io_read(struct bio *bio, mempool_t *pool, int err)
  292. {
  293. struct bio *bio_orig = bio->bi_private;
  294. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  295. copy_to_high_bio_irq(bio_orig, bio);
  296. bounce_end_io(bio, pool, err);
  297. }
  298. static int bounce_end_io_read(struct bio *bio, unsigned int bytes_done, int err)
  299. {
  300. if (bio->bi_size)
  301. return 1;
  302. __bounce_end_io_read(bio, page_pool, err);
  303. return 0;
  304. }
  305. static int bounce_end_io_read_isa(struct bio *bio, unsigned int bytes_done, int err)
  306. {
  307. if (bio->bi_size)
  308. return 1;
  309. __bounce_end_io_read(bio, isa_page_pool, err);
  310. return 0;
  311. }
  312. static void __blk_queue_bounce(request_queue_t *q, struct bio **bio_orig,
  313. mempool_t *pool)
  314. {
  315. struct page *page;
  316. struct bio *bio = NULL;
  317. int i, rw = bio_data_dir(*bio_orig);
  318. struct bio_vec *to, *from;
  319. bio_for_each_segment(from, *bio_orig, i) {
  320. page = from->bv_page;
  321. /*
  322. * is destination page below bounce pfn?
  323. */
  324. if (page_to_pfn(page) < q->bounce_pfn)
  325. continue;
  326. /*
  327. * irk, bounce it
  328. */
  329. if (!bio)
  330. bio = bio_alloc(GFP_NOIO, (*bio_orig)->bi_vcnt);
  331. to = bio->bi_io_vec + i;
  332. to->bv_page = mempool_alloc(pool, q->bounce_gfp);
  333. to->bv_len = from->bv_len;
  334. to->bv_offset = from->bv_offset;
  335. if (rw == WRITE) {
  336. char *vto, *vfrom;
  337. flush_dcache_page(from->bv_page);
  338. vto = page_address(to->bv_page) + to->bv_offset;
  339. vfrom = kmap(from->bv_page) + from->bv_offset;
  340. memcpy(vto, vfrom, to->bv_len);
  341. kunmap(from->bv_page);
  342. }
  343. }
  344. /*
  345. * no pages bounced
  346. */
  347. if (!bio)
  348. return;
  349. /*
  350. * at least one page was bounced, fill in possible non-highmem
  351. * pages
  352. */
  353. __bio_for_each_segment(from, *bio_orig, i, 0) {
  354. to = bio_iovec_idx(bio, i);
  355. if (!to->bv_page) {
  356. to->bv_page = from->bv_page;
  357. to->bv_len = from->bv_len;
  358. to->bv_offset = from->bv_offset;
  359. }
  360. }
  361. bio->bi_bdev = (*bio_orig)->bi_bdev;
  362. bio->bi_flags |= (1 << BIO_BOUNCED);
  363. bio->bi_sector = (*bio_orig)->bi_sector;
  364. bio->bi_rw = (*bio_orig)->bi_rw;
  365. bio->bi_vcnt = (*bio_orig)->bi_vcnt;
  366. bio->bi_idx = (*bio_orig)->bi_idx;
  367. bio->bi_size = (*bio_orig)->bi_size;
  368. if (pool == page_pool) {
  369. bio->bi_end_io = bounce_end_io_write;
  370. if (rw == READ)
  371. bio->bi_end_io = bounce_end_io_read;
  372. } else {
  373. bio->bi_end_io = bounce_end_io_write_isa;
  374. if (rw == READ)
  375. bio->bi_end_io = bounce_end_io_read_isa;
  376. }
  377. bio->bi_private = *bio_orig;
  378. *bio_orig = bio;
  379. }
  380. void blk_queue_bounce(request_queue_t *q, struct bio **bio_orig)
  381. {
  382. mempool_t *pool;
  383. /*
  384. * for non-isa bounce case, just check if the bounce pfn is equal
  385. * to or bigger than the highest pfn in the system -- in that case,
  386. * don't waste time iterating over bio segments
  387. */
  388. if (!(q->bounce_gfp & GFP_DMA)) {
  389. if (q->bounce_pfn >= blk_max_pfn)
  390. return;
  391. pool = page_pool;
  392. } else {
  393. BUG_ON(!isa_page_pool);
  394. pool = isa_page_pool;
  395. }
  396. /*
  397. * slow path
  398. */
  399. __blk_queue_bounce(q, bio_orig, pool);
  400. }
  401. EXPORT_SYMBOL(blk_queue_bounce);
  402. #if defined(HASHED_PAGE_VIRTUAL)
  403. #define PA_HASH_ORDER 7
  404. /*
  405. * Describes one page->virtual association
  406. */
  407. struct page_address_map {
  408. struct page *page;
  409. void *virtual;
  410. struct list_head list;
  411. };
  412. /*
  413. * page_address_map freelist, allocated from page_address_maps.
  414. */
  415. static struct list_head page_address_pool; /* freelist */
  416. static spinlock_t pool_lock; /* protects page_address_pool */
  417. /*
  418. * Hash table bucket
  419. */
  420. static struct page_address_slot {
  421. struct list_head lh; /* List of page_address_maps */
  422. spinlock_t lock; /* Protect this bucket's list */
  423. } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
  424. static struct page_address_slot *page_slot(struct page *page)
  425. {
  426. return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
  427. }
  428. void *page_address(struct page *page)
  429. {
  430. unsigned long flags;
  431. void *ret;
  432. struct page_address_slot *pas;
  433. if (!PageHighMem(page))
  434. return lowmem_page_address(page);
  435. pas = page_slot(page);
  436. ret = NULL;
  437. spin_lock_irqsave(&pas->lock, flags);
  438. if (!list_empty(&pas->lh)) {
  439. struct page_address_map *pam;
  440. list_for_each_entry(pam, &pas->lh, list) {
  441. if (pam->page == page) {
  442. ret = pam->virtual;
  443. goto done;
  444. }
  445. }
  446. }
  447. done:
  448. spin_unlock_irqrestore(&pas->lock, flags);
  449. return ret;
  450. }
  451. EXPORT_SYMBOL(page_address);
  452. void set_page_address(struct page *page, void *virtual)
  453. {
  454. unsigned long flags;
  455. struct page_address_slot *pas;
  456. struct page_address_map *pam;
  457. BUG_ON(!PageHighMem(page));
  458. pas = page_slot(page);
  459. if (virtual) { /* Add */
  460. BUG_ON(list_empty(&page_address_pool));
  461. spin_lock_irqsave(&pool_lock, flags);
  462. pam = list_entry(page_address_pool.next,
  463. struct page_address_map, list);
  464. list_del(&pam->list);
  465. spin_unlock_irqrestore(&pool_lock, flags);
  466. pam->page = page;
  467. pam->virtual = virtual;
  468. spin_lock_irqsave(&pas->lock, flags);
  469. list_add_tail(&pam->list, &pas->lh);
  470. spin_unlock_irqrestore(&pas->lock, flags);
  471. } else { /* Remove */
  472. spin_lock_irqsave(&pas->lock, flags);
  473. list_for_each_entry(pam, &pas->lh, list) {
  474. if (pam->page == page) {
  475. list_del(&pam->list);
  476. spin_unlock_irqrestore(&pas->lock, flags);
  477. spin_lock_irqsave(&pool_lock, flags);
  478. list_add_tail(&pam->list, &page_address_pool);
  479. spin_unlock_irqrestore(&pool_lock, flags);
  480. goto done;
  481. }
  482. }
  483. spin_unlock_irqrestore(&pas->lock, flags);
  484. }
  485. done:
  486. return;
  487. }
  488. static struct page_address_map page_address_maps[LAST_PKMAP];
  489. void __init page_address_init(void)
  490. {
  491. int i;
  492. INIT_LIST_HEAD(&page_address_pool);
  493. for (i = 0; i < ARRAY_SIZE(page_address_maps); i++)
  494. list_add(&page_address_maps[i].list, &page_address_pool);
  495. for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
  496. INIT_LIST_HEAD(&page_address_htable[i].lh);
  497. spin_lock_init(&page_address_htable[i].lock);
  498. }
  499. spin_lock_init(&pool_lock);
  500. }
  501. #endif /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */