2 * High memory handling common code and variables.
4 * (C) 1999 Andrea Arcangeli, SuSE GmbH, andrea@suse.de
5 * Gerhard Wichert, Siemens AG, Gerhard.Wichert@pdb.siemens.de
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.
12 * Rewrote high memory support to move the page cache into
13 * high memory. Implemented permanent (schedulable) kmaps
14 * based on Linus' idea.
16 * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
20 #include <linux/module.h>
21 #include <linux/swap.h>
22 #include <linux/bio.h>
23 #include <linux/pagemap.h>
24 #include <linux/mempool.h>
25 #include <linux/blkdev.h>
26 #include <linux/init.h>
27 #include <linux/hash.h>
28 #include <linux/highmem.h>
29 #include <asm/pgalloc.h>
30 #include <asm/tlbflush.h>
32 static mempool_t *page_pool, *isa_page_pool;
34 static void *page_pool_alloc(int gfp_mask, void *data)
36 int gfp = gfp_mask | (int) (long) data;
38 return alloc_page(gfp);
41 static void page_pool_free(void *page, void *data)
47 * Virtual_count is not a pure "count".
48 * 0 means that it is not mapped, and has not been mapped
49 * since a TLB flush - it is usable.
50 * 1 means that there are no users, but it has been mapped
51 * since the last TLB flush - so we can't use it.
52 * n means that there are (n-1) current users of it.
55 static int pkmap_count[LAST_PKMAP];
56 static unsigned int last_pkmap_nr;
57 static spinlock_t kmap_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
59 pte_t * pkmap_page_table;
61 static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
63 static void flush_all_zero_pkmaps(void)
69 for (i = 0; i < LAST_PKMAP; i++) {
73 * zero means we don't have anything to do,
74 * >1 means that it is still in use. Only
75 * a count of 1 means that it is free but
76 * needs to be unmapped
78 if (pkmap_count[i] != 1)
83 if (pte_none(pkmap_page_table[i]))
87 * Don't need an atomic fetch-and-clear op here;
88 * no-one has the page mapped, and cannot get at
89 * its virtual address (and hence PTE) without first
90 * getting the kmap_lock (which is held here).
91 * So no dangers, even with speculative execution.
93 page = pte_page(pkmap_page_table[i]);
94 pte_clear(&pkmap_page_table[i]);
96 set_page_address(page, NULL);
98 flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
101 static inline unsigned long map_new_virtual(struct page *page)
108 /* Find an empty entry */
110 last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
111 if (!last_pkmap_nr) {
112 flush_all_zero_pkmaps();
115 if (!pkmap_count[last_pkmap_nr])
116 break; /* Found a usable entry */
121 * Sleep for somebody else to unmap their entries
124 DECLARE_WAITQUEUE(wait, current);
126 __set_current_state(TASK_UNINTERRUPTIBLE);
127 add_wait_queue(&pkmap_map_wait, &wait);
128 spin_unlock(&kmap_lock);
130 remove_wait_queue(&pkmap_map_wait, &wait);
131 spin_lock(&kmap_lock);
133 /* Somebody else might have mapped it while we slept */
134 if (page_address(page))
135 return (unsigned long)page_address(page);
141 vaddr = PKMAP_ADDR(last_pkmap_nr);
142 set_pte(&(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
144 pkmap_count[last_pkmap_nr] = 1;
145 set_page_address(page, (void *)vaddr);
150 void fastcall *kmap_high(struct page *page)
155 * For highmem pages, we can't trust "virtual" until
156 * after we have the lock.
158 * We cannot call this from interrupts, as it may block
160 spin_lock(&kmap_lock);
161 vaddr = (unsigned long)page_address(page);
163 vaddr = map_new_virtual(page);
164 pkmap_count[PKMAP_NR(vaddr)]++;
165 if (pkmap_count[PKMAP_NR(vaddr)] < 2)
167 spin_unlock(&kmap_lock);
168 return (void*) vaddr;
171 EXPORT_SYMBOL(kmap_high);
173 void fastcall kunmap_high(struct page *page)
179 spin_lock(&kmap_lock);
180 vaddr = (unsigned long)page_address(page);
183 nr = PKMAP_NR(vaddr);
186 * A count must never go down to zero
187 * without a TLB flush!
190 switch (--pkmap_count[nr]) {
195 * Avoid an unnecessary wake_up() function call.
196 * The common case is pkmap_count[] == 1, but
198 * The tasks queued in the wait-queue are guarded
199 * by both the lock in the wait-queue-head and by
200 * the kmap_lock. As the kmap_lock is held here,
201 * no need for the wait-queue-head's lock. Simply
202 * test if the queue is empty.
204 need_wakeup = waitqueue_active(&pkmap_map_wait);
206 spin_unlock(&kmap_lock);
208 /* do wake-up, if needed, race-free outside of the spin lock */
210 wake_up(&pkmap_map_wait);
213 EXPORT_SYMBOL(kunmap_high);
217 static __init int init_emergency_pool(void)
226 page_pool = mempool_create(POOL_SIZE, page_pool_alloc, page_pool_free, NULL);
229 printk("highmem bounce pool size: %d pages\n", POOL_SIZE);
234 __initcall(init_emergency_pool);
237 * highmem version, map in to vec
239 static void bounce_copy_vec(struct bio_vec *to, unsigned char *vfrom)
244 local_irq_save(flags);
245 vto = kmap_atomic(to->bv_page, KM_BOUNCE_READ);
246 memcpy(vto + to->bv_offset, vfrom, to->bv_len);
247 kunmap_atomic(vto, KM_BOUNCE_READ);
248 local_irq_restore(flags);
251 #else /* CONFIG_HIGHMEM */
253 #define bounce_copy_vec(to, vfrom) \
254 memcpy(page_address((to)->bv_page) + (to)->bv_offset, vfrom, (to)->bv_len)
258 #define ISA_POOL_SIZE 16
261 * gets called "every" time someone init's a queue with BLK_BOUNCE_ISA
262 * as the max address, so check if the pool has already been created.
264 int init_emergency_isa_pool(void)
269 isa_page_pool = mempool_create(ISA_POOL_SIZE, page_pool_alloc, page_pool_free, (void *) __GFP_DMA);
273 printk("isa bounce pool size: %d pages\n", ISA_POOL_SIZE);
278 * Simple bounce buffer support for highmem pages. Depending on the
279 * queue gfp mask set, *to may or may not be a highmem page. kmap it
280 * always, it will do the Right Thing
282 static void copy_to_high_bio_irq(struct bio *to, struct bio *from)
284 unsigned char *vfrom;
285 struct bio_vec *tovec, *fromvec;
288 bio_for_each_segment(tovec, to, i) {
289 fromvec = from->bi_io_vec + i;
294 if (tovec->bv_page == fromvec->bv_page)
298 * fromvec->bv_offset and fromvec->bv_len might have been
299 * modified by the block layer, so use the original copy,
300 * bounce_copy_vec already uses tovec->bv_len
302 vfrom = page_address(fromvec->bv_page) + tovec->bv_offset;
304 bounce_copy_vec(tovec, vfrom);
308 static void bounce_end_io(struct bio *bio, mempool_t *pool)
310 struct bio *bio_orig = bio->bi_private;
311 struct bio_vec *bvec, *org_vec;
314 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
317 set_bit(BIO_UPTODATE, &bio_orig->bi_flags);
320 * free up bounce indirect pages used
322 bio_for_each_segment(bvec, bio, i) {
323 org_vec = bio_orig->bi_io_vec + i;
324 if (bvec->bv_page == org_vec->bv_page)
327 mempool_free(bvec->bv_page, pool);
331 bio_endio(bio_orig, bio_orig->bi_size, 0);
335 static int bounce_end_io_write(struct bio *bio, unsigned int bytes_done,int err)
340 bounce_end_io(bio, page_pool);
344 static int bounce_end_io_write_isa(struct bio *bio, unsigned int bytes_done, int err)
349 bounce_end_io(bio, isa_page_pool);
353 static void __bounce_end_io_read(struct bio *bio, mempool_t *pool)
355 struct bio *bio_orig = bio->bi_private;
357 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
358 copy_to_high_bio_irq(bio_orig, bio);
360 bounce_end_io(bio, pool);
363 static int bounce_end_io_read(struct bio *bio, unsigned int bytes_done, int err)
368 __bounce_end_io_read(bio, page_pool);
372 static int bounce_end_io_read_isa(struct bio *bio, unsigned int bytes_done, int err)
377 __bounce_end_io_read(bio, isa_page_pool);
381 static void __blk_queue_bounce(request_queue_t *q, struct bio **bio_orig,
385 struct bio *bio = NULL;
386 int i, rw = bio_data_dir(*bio_orig);
387 struct bio_vec *to, *from;
389 bio_for_each_segment(from, *bio_orig, i) {
390 page = from->bv_page;
393 * is destination page below bounce pfn?
395 if (page_to_pfn(page) < q->bounce_pfn)
402 bio = bio_alloc(GFP_NOIO, (*bio_orig)->bi_vcnt);
404 to = bio->bi_io_vec + i;
406 to->bv_page = mempool_alloc(pool, q->bounce_gfp);
407 to->bv_len = from->bv_len;
408 to->bv_offset = from->bv_offset;
413 vto = page_address(to->bv_page) + to->bv_offset;
414 vfrom = kmap(from->bv_page) + from->bv_offset;
415 memcpy(vto, vfrom, to->bv_len);
416 kunmap(from->bv_page);
427 * at least one page was bounced, fill in possible non-highmem
430 bio_for_each_segment(from, *bio_orig, i) {
431 to = bio_iovec_idx(bio, i);
433 to->bv_page = from->bv_page;
434 to->bv_len = from->bv_len;
435 to->bv_offset = from->bv_offset;
439 bio->bi_bdev = (*bio_orig)->bi_bdev;
440 bio->bi_flags |= (1 << BIO_BOUNCED);
441 bio->bi_sector = (*bio_orig)->bi_sector;
442 bio->bi_rw = (*bio_orig)->bi_rw;
444 bio->bi_vcnt = (*bio_orig)->bi_vcnt;
445 bio->bi_idx = (*bio_orig)->bi_idx;
446 bio->bi_size = (*bio_orig)->bi_size;
448 if (pool == page_pool) {
449 bio->bi_end_io = bounce_end_io_write;
451 bio->bi_end_io = bounce_end_io_read;
453 bio->bi_end_io = bounce_end_io_write_isa;
455 bio->bi_end_io = bounce_end_io_read_isa;
458 bio->bi_private = *bio_orig;
462 void blk_queue_bounce(request_queue_t *q, struct bio **bio_orig)
467 * for non-isa bounce case, just check if the bounce pfn is equal
468 * to or bigger than the highest pfn in the system -- in that case,
469 * don't waste time iterating over bio segments
471 if (!(q->bounce_gfp & GFP_DMA)) {
472 if (q->bounce_pfn >= blk_max_pfn)
476 BUG_ON(!isa_page_pool);
477 pool = isa_page_pool;
483 __blk_queue_bounce(q, bio_orig, pool);
486 EXPORT_SYMBOL(blk_queue_bounce);
488 #if defined(HASHED_PAGE_VIRTUAL)
490 #define PA_HASH_ORDER 7
493 * Describes one page->virtual association
495 struct page_address_map {
498 struct list_head list;
502 * page_address_map freelist, allocated from page_address_maps.
504 static struct list_head page_address_pool; /* freelist */
505 static spinlock_t pool_lock; /* protects page_address_pool */
510 static struct page_address_slot {
511 struct list_head lh; /* List of page_address_maps */
512 spinlock_t lock; /* Protect this bucket's list */
513 } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
515 static struct page_address_slot *page_slot(struct page *page)
517 return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
520 void *page_address(struct page *page)
524 struct page_address_slot *pas;
526 if (!PageHighMem(page))
527 return lowmem_page_address(page);
529 pas = page_slot(page);
531 spin_lock_irqsave(&pas->lock, flags);
532 if (!list_empty(&pas->lh)) {
533 struct page_address_map *pam;
535 list_for_each_entry(pam, &pas->lh, list) {
536 if (pam->page == page) {
543 spin_unlock_irqrestore(&pas->lock, flags);
547 EXPORT_SYMBOL(page_address);
549 void set_page_address(struct page *page, void *virtual)
552 struct page_address_slot *pas;
553 struct page_address_map *pam;
555 BUG_ON(!PageHighMem(page));
557 pas = page_slot(page);
558 if (virtual) { /* Add */
559 BUG_ON(list_empty(&page_address_pool));
561 spin_lock_irqsave(&pool_lock, flags);
562 pam = list_entry(page_address_pool.next,
563 struct page_address_map, list);
564 list_del(&pam->list);
565 spin_unlock_irqrestore(&pool_lock, flags);
568 pam->virtual = virtual;
570 spin_lock_irqsave(&pas->lock, flags);
571 list_add_tail(&pam->list, &pas->lh);
572 spin_unlock_irqrestore(&pas->lock, flags);
573 } else { /* Remove */
574 spin_lock_irqsave(&pas->lock, flags);
575 list_for_each_entry(pam, &pas->lh, list) {
576 if (pam->page == page) {
577 list_del(&pam->list);
578 spin_unlock_irqrestore(&pas->lock, flags);
579 spin_lock_irqsave(&pool_lock, flags);
580 list_add_tail(&pam->list, &page_address_pool);
581 spin_unlock_irqrestore(&pool_lock, flags);
585 spin_unlock_irqrestore(&pas->lock, flags);
591 static struct page_address_map page_address_maps[LAST_PKMAP];
593 void __init page_address_init(void)
597 INIT_LIST_HEAD(&page_address_pool);
598 for (i = 0; i < ARRAY_SIZE(page_address_maps); i++)
599 list_add(&page_address_maps[i].list, &page_address_pool);
600 for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
601 INIT_LIST_HEAD(&page_address_htable[i].lh);
602 spin_lock_init(&page_address_htable[i].lock);
604 spin_lock_init(&pool_lock);
607 #endif /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */