highmem.c 14 KB

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