highmem.c 14 KB

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