highmem.c 14 KB

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