[IPV6]: bh_lock_sock_nested on tcp_v6_rcv
[hh.org.git] / mm / highmem.c
blobee5519b176ee9a2fc704e9af51f4f26836b53df1
1 /*
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>
19 #include <linux/mm.h>
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 <linux/blktrace_api.h>
30 #include <asm/tlbflush.h>
32 static mempool_t *page_pool, *isa_page_pool;
34 static void *mempool_alloc_pages_isa(gfp_t gfp_mask, void *data)
36 return mempool_alloc_pages(gfp_mask | GFP_DMA, data);
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.
47 #ifdef CONFIG_HIGHMEM
49 unsigned long totalhigh_pages __read_mostly;
51 unsigned int nr_free_highpages (void)
53 pg_data_t *pgdat;
54 unsigned int pages = 0;
56 for_each_online_pgdat(pgdat)
57 pages += pgdat->node_zones[ZONE_HIGHMEM].free_pages;
59 return pages;
62 static int pkmap_count[LAST_PKMAP];
63 static unsigned int last_pkmap_nr;
64 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
66 pte_t * pkmap_page_table;
68 static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
70 static void flush_all_zero_pkmaps(void)
72 int i;
74 flush_cache_kmaps();
76 for (i = 0; i < LAST_PKMAP; i++) {
77 struct page *page;
80 * zero means we don't have anything to do,
81 * >1 means that it is still in use. Only
82 * a count of 1 means that it is free but
83 * needs to be unmapped
85 if (pkmap_count[i] != 1)
86 continue;
87 pkmap_count[i] = 0;
89 /* sanity check */
90 BUG_ON(pte_none(pkmap_page_table[i]));
93 * Don't need an atomic fetch-and-clear op here;
94 * no-one has the page mapped, and cannot get at
95 * its virtual address (and hence PTE) without first
96 * getting the kmap_lock (which is held here).
97 * So no dangers, even with speculative execution.
99 page = pte_page(pkmap_page_table[i]);
100 pte_clear(&init_mm, (unsigned long)page_address(page),
101 &pkmap_page_table[i]);
103 set_page_address(page, NULL);
105 flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
108 static inline unsigned long map_new_virtual(struct page *page)
110 unsigned long vaddr;
111 int count;
113 start:
114 count = LAST_PKMAP;
115 /* Find an empty entry */
116 for (;;) {
117 last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
118 if (!last_pkmap_nr) {
119 flush_all_zero_pkmaps();
120 count = LAST_PKMAP;
122 if (!pkmap_count[last_pkmap_nr])
123 break; /* Found a usable entry */
124 if (--count)
125 continue;
128 * Sleep for somebody else to unmap their entries
131 DECLARE_WAITQUEUE(wait, current);
133 __set_current_state(TASK_UNINTERRUPTIBLE);
134 add_wait_queue(&pkmap_map_wait, &wait);
135 spin_unlock(&kmap_lock);
136 schedule();
137 remove_wait_queue(&pkmap_map_wait, &wait);
138 spin_lock(&kmap_lock);
140 /* Somebody else might have mapped it while we slept */
141 if (page_address(page))
142 return (unsigned long)page_address(page);
144 /* Re-start */
145 goto start;
148 vaddr = PKMAP_ADDR(last_pkmap_nr);
149 set_pte_at(&init_mm, vaddr,
150 &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
152 pkmap_count[last_pkmap_nr] = 1;
153 set_page_address(page, (void *)vaddr);
155 return vaddr;
158 void fastcall *kmap_high(struct page *page)
160 unsigned long vaddr;
163 * For highmem pages, we can't trust "virtual" until
164 * after we have the lock.
166 * We cannot call this from interrupts, as it may block
168 spin_lock(&kmap_lock);
169 vaddr = (unsigned long)page_address(page);
170 if (!vaddr)
171 vaddr = map_new_virtual(page);
172 pkmap_count[PKMAP_NR(vaddr)]++;
173 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
174 spin_unlock(&kmap_lock);
175 return (void*) vaddr;
178 EXPORT_SYMBOL(kmap_high);
180 void fastcall kunmap_high(struct page *page)
182 unsigned long vaddr;
183 unsigned long nr;
184 int need_wakeup;
186 spin_lock(&kmap_lock);
187 vaddr = (unsigned long)page_address(page);
188 BUG_ON(!vaddr);
189 nr = PKMAP_NR(vaddr);
192 * A count must never go down to zero
193 * without a TLB flush!
195 need_wakeup = 0;
196 switch (--pkmap_count[nr]) {
197 case 0:
198 BUG();
199 case 1:
201 * Avoid an unnecessary wake_up() function call.
202 * The common case is pkmap_count[] == 1, but
203 * no waiters.
204 * The tasks queued in the wait-queue are guarded
205 * by both the lock in the wait-queue-head and by
206 * the kmap_lock. As the kmap_lock is held here,
207 * no need for the wait-queue-head's lock. Simply
208 * test if the queue is empty.
210 need_wakeup = waitqueue_active(&pkmap_map_wait);
212 spin_unlock(&kmap_lock);
214 /* do wake-up, if needed, race-free outside of the spin lock */
215 if (need_wakeup)
216 wake_up(&pkmap_map_wait);
219 EXPORT_SYMBOL(kunmap_high);
221 #define POOL_SIZE 64
223 static __init int init_emergency_pool(void)
225 struct sysinfo i;
226 si_meminfo(&i);
227 si_swapinfo(&i);
229 if (!i.totalhigh)
230 return 0;
232 page_pool = mempool_create_page_pool(POOL_SIZE, 0);
233 BUG_ON(!page_pool);
234 printk("highmem bounce pool size: %d pages\n", POOL_SIZE);
236 return 0;
239 __initcall(init_emergency_pool);
242 * highmem version, map in to vec
244 static void bounce_copy_vec(struct bio_vec *to, unsigned char *vfrom)
246 unsigned long flags;
247 unsigned char *vto;
249 local_irq_save(flags);
250 vto = kmap_atomic(to->bv_page, KM_BOUNCE_READ);
251 memcpy(vto + to->bv_offset, vfrom, to->bv_len);
252 kunmap_atomic(vto, KM_BOUNCE_READ);
253 local_irq_restore(flags);
256 #else /* CONFIG_HIGHMEM */
258 #define bounce_copy_vec(to, vfrom) \
259 memcpy(page_address((to)->bv_page) + (to)->bv_offset, vfrom, (to)->bv_len)
261 #endif
263 #define ISA_POOL_SIZE 16
266 * gets called "every" time someone init's a queue with BLK_BOUNCE_ISA
267 * as the max address, so check if the pool has already been created.
269 int init_emergency_isa_pool(void)
271 if (isa_page_pool)
272 return 0;
274 isa_page_pool = mempool_create(ISA_POOL_SIZE, mempool_alloc_pages_isa,
275 mempool_free_pages, (void *) 0);
276 BUG_ON(!isa_page_pool);
278 printk("isa bounce pool size: %d pages\n", ISA_POOL_SIZE);
279 return 0;
283 * Simple bounce buffer support for highmem pages. Depending on the
284 * queue gfp mask set, *to may or may not be a highmem page. kmap it
285 * always, it will do the Right Thing
287 static void copy_to_high_bio_irq(struct bio *to, struct bio *from)
289 unsigned char *vfrom;
290 struct bio_vec *tovec, *fromvec;
291 int i;
293 __bio_for_each_segment(tovec, to, i, 0) {
294 fromvec = from->bi_io_vec + i;
297 * not bounced
299 if (tovec->bv_page == fromvec->bv_page)
300 continue;
303 * fromvec->bv_offset and fromvec->bv_len might have been
304 * modified by the block layer, so use the original copy,
305 * bounce_copy_vec already uses tovec->bv_len
307 vfrom = page_address(fromvec->bv_page) + tovec->bv_offset;
309 flush_dcache_page(tovec->bv_page);
310 bounce_copy_vec(tovec, vfrom);
314 static void bounce_end_io(struct bio *bio, mempool_t *pool, int err)
316 struct bio *bio_orig = bio->bi_private;
317 struct bio_vec *bvec, *org_vec;
318 int i;
320 if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags))
321 set_bit(BIO_EOPNOTSUPP, &bio_orig->bi_flags);
324 * free up bounce indirect pages used
326 __bio_for_each_segment(bvec, bio, i, 0) {
327 org_vec = bio_orig->bi_io_vec + i;
328 if (bvec->bv_page == org_vec->bv_page)
329 continue;
331 dec_zone_page_state(bvec->bv_page, NR_BOUNCE);
332 mempool_free(bvec->bv_page, pool);
335 bio_endio(bio_orig, bio_orig->bi_size, err);
336 bio_put(bio);
339 static int bounce_end_io_write(struct bio *bio, unsigned int bytes_done, int err)
341 if (bio->bi_size)
342 return 1;
344 bounce_end_io(bio, page_pool, err);
345 return 0;
348 static int bounce_end_io_write_isa(struct bio *bio, unsigned int bytes_done, int err)
350 if (bio->bi_size)
351 return 1;
353 bounce_end_io(bio, isa_page_pool, err);
354 return 0;
357 static void __bounce_end_io_read(struct bio *bio, mempool_t *pool, int err)
359 struct bio *bio_orig = bio->bi_private;
361 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
362 copy_to_high_bio_irq(bio_orig, bio);
364 bounce_end_io(bio, pool, err);
367 static int bounce_end_io_read(struct bio *bio, unsigned int bytes_done, int err)
369 if (bio->bi_size)
370 return 1;
372 __bounce_end_io_read(bio, page_pool, err);
373 return 0;
376 static int bounce_end_io_read_isa(struct bio *bio, unsigned int bytes_done, int err)
378 if (bio->bi_size)
379 return 1;
381 __bounce_end_io_read(bio, isa_page_pool, err);
382 return 0;
385 static void __blk_queue_bounce(request_queue_t *q, struct bio **bio_orig,
386 mempool_t *pool)
388 struct page *page;
389 struct bio *bio = NULL;
390 int i, rw = bio_data_dir(*bio_orig);
391 struct bio_vec *to, *from;
393 bio_for_each_segment(from, *bio_orig, i) {
394 page = from->bv_page;
397 * is destination page below bounce pfn?
399 if (page_to_pfn(page) < q->bounce_pfn)
400 continue;
403 * irk, bounce it
405 if (!bio)
406 bio = bio_alloc(GFP_NOIO, (*bio_orig)->bi_vcnt);
408 to = bio->bi_io_vec + i;
410 to->bv_page = mempool_alloc(pool, q->bounce_gfp);
411 to->bv_len = from->bv_len;
412 to->bv_offset = from->bv_offset;
413 inc_zone_page_state(to->bv_page, NR_BOUNCE);
415 if (rw == WRITE) {
416 char *vto, *vfrom;
418 flush_dcache_page(from->bv_page);
419 vto = page_address(to->bv_page) + to->bv_offset;
420 vfrom = kmap(from->bv_page) + from->bv_offset;
421 memcpy(vto, vfrom, to->bv_len);
422 kunmap(from->bv_page);
427 * no pages bounced
429 if (!bio)
430 return;
433 * at least one page was bounced, fill in possible non-highmem
434 * pages
436 __bio_for_each_segment(from, *bio_orig, i, 0) {
437 to = bio_iovec_idx(bio, i);
438 if (!to->bv_page) {
439 to->bv_page = from->bv_page;
440 to->bv_len = from->bv_len;
441 to->bv_offset = from->bv_offset;
445 bio->bi_bdev = (*bio_orig)->bi_bdev;
446 bio->bi_flags |= (1 << BIO_BOUNCED);
447 bio->bi_sector = (*bio_orig)->bi_sector;
448 bio->bi_rw = (*bio_orig)->bi_rw;
450 bio->bi_vcnt = (*bio_orig)->bi_vcnt;
451 bio->bi_idx = (*bio_orig)->bi_idx;
452 bio->bi_size = (*bio_orig)->bi_size;
454 if (pool == page_pool) {
455 bio->bi_end_io = bounce_end_io_write;
456 if (rw == READ)
457 bio->bi_end_io = bounce_end_io_read;
458 } else {
459 bio->bi_end_io = bounce_end_io_write_isa;
460 if (rw == READ)
461 bio->bi_end_io = bounce_end_io_read_isa;
464 bio->bi_private = *bio_orig;
465 *bio_orig = bio;
468 void blk_queue_bounce(request_queue_t *q, struct bio **bio_orig)
470 mempool_t *pool;
473 * for non-isa bounce case, just check if the bounce pfn is equal
474 * to or bigger than the highest pfn in the system -- in that case,
475 * don't waste time iterating over bio segments
477 if (!(q->bounce_gfp & GFP_DMA)) {
478 if (q->bounce_pfn >= blk_max_pfn)
479 return;
480 pool = page_pool;
481 } else {
482 BUG_ON(!isa_page_pool);
483 pool = isa_page_pool;
486 blk_add_trace_bio(q, *bio_orig, BLK_TA_BOUNCE);
489 * slow path
491 __blk_queue_bounce(q, bio_orig, pool);
494 EXPORT_SYMBOL(blk_queue_bounce);
496 #if defined(HASHED_PAGE_VIRTUAL)
498 #define PA_HASH_ORDER 7
501 * Describes one page->virtual association
503 struct page_address_map {
504 struct page *page;
505 void *virtual;
506 struct list_head list;
510 * page_address_map freelist, allocated from page_address_maps.
512 static struct list_head page_address_pool; /* freelist */
513 static spinlock_t pool_lock; /* protects page_address_pool */
516 * Hash table bucket
518 static struct page_address_slot {
519 struct list_head lh; /* List of page_address_maps */
520 spinlock_t lock; /* Protect this bucket's list */
521 } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
523 static struct page_address_slot *page_slot(struct page *page)
525 return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
528 void *page_address(struct page *page)
530 unsigned long flags;
531 void *ret;
532 struct page_address_slot *pas;
534 if (!PageHighMem(page))
535 return lowmem_page_address(page);
537 pas = page_slot(page);
538 ret = NULL;
539 spin_lock_irqsave(&pas->lock, flags);
540 if (!list_empty(&pas->lh)) {
541 struct page_address_map *pam;
543 list_for_each_entry(pam, &pas->lh, list) {
544 if (pam->page == page) {
545 ret = pam->virtual;
546 goto done;
550 done:
551 spin_unlock_irqrestore(&pas->lock, flags);
552 return ret;
555 EXPORT_SYMBOL(page_address);
557 void set_page_address(struct page *page, void *virtual)
559 unsigned long flags;
560 struct page_address_slot *pas;
561 struct page_address_map *pam;
563 BUG_ON(!PageHighMem(page));
565 pas = page_slot(page);
566 if (virtual) { /* Add */
567 BUG_ON(list_empty(&page_address_pool));
569 spin_lock_irqsave(&pool_lock, flags);
570 pam = list_entry(page_address_pool.next,
571 struct page_address_map, list);
572 list_del(&pam->list);
573 spin_unlock_irqrestore(&pool_lock, flags);
575 pam->page = page;
576 pam->virtual = virtual;
578 spin_lock_irqsave(&pas->lock, flags);
579 list_add_tail(&pam->list, &pas->lh);
580 spin_unlock_irqrestore(&pas->lock, flags);
581 } else { /* Remove */
582 spin_lock_irqsave(&pas->lock, flags);
583 list_for_each_entry(pam, &pas->lh, list) {
584 if (pam->page == page) {
585 list_del(&pam->list);
586 spin_unlock_irqrestore(&pas->lock, flags);
587 spin_lock_irqsave(&pool_lock, flags);
588 list_add_tail(&pam->list, &page_address_pool);
589 spin_unlock_irqrestore(&pool_lock, flags);
590 goto done;
593 spin_unlock_irqrestore(&pas->lock, flags);
595 done:
596 return;
599 static struct page_address_map page_address_maps[LAST_PKMAP];
601 void __init page_address_init(void)
603 int i;
605 INIT_LIST_HEAD(&page_address_pool);
606 for (i = 0; i < ARRAY_SIZE(page_address_maps); i++)
607 list_add(&page_address_maps[i].list, &page_address_pool);
608 for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
609 INIT_LIST_HEAD(&page_address_htable[i].lh);
610 spin_lock_init(&page_address_htable[i].lock);
612 spin_lock_init(&pool_lock);
615 #endif /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */