direct-io.hg

view linux-2.6-xen-sparse/mm/highmem.c @ 11509:2e6c10dc7c0b

[POWERPC][XEN] make sure put_domain() is called in case of allocate_rma() failuer

Signed-off-by: Jimi Xenidis <jimix@watson.ibm.com>
Signed-off-by: Hollis Blanchard <hollisb@us.ibm.com>
author Jimi Xenidis <jimix@watson.ibm.com>
date Tue Sep 12 10:53:46 2006 -0400 (2006-09-12)
parents 5a63f675107c
children 4fad820a2233
line source
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 */
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 <asm/tlbflush.h>
31 static mempool_t *page_pool, *isa_page_pool;
33 static void *page_pool_alloc_isa(gfp_t gfp_mask, void *data)
34 {
35 return alloc_page(gfp_mask | GFP_DMA);
36 }
38 static void page_pool_free(void *page, void *data)
39 {
40 __free_page(page);
41 }
43 /*
44 * Virtual_count is not a pure "count".
45 * 0 means that it is not mapped, and has not been mapped
46 * since a TLB flush - it is usable.
47 * 1 means that there are no users, but it has been mapped
48 * since the last TLB flush - so we can't use it.
49 * n means that there are (n-1) current users of it.
50 */
51 #ifdef CONFIG_HIGHMEM
53 static void *page_pool_alloc(gfp_t gfp_mask, void *data)
54 {
55 return alloc_page(gfp_mask);
56 }
58 static int pkmap_count[LAST_PKMAP];
59 static unsigned int last_pkmap_nr;
60 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
62 pte_t * pkmap_page_table;
64 static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
66 static void flush_all_zero_pkmaps(void)
67 {
68 int i;
70 flush_cache_kmaps();
72 for (i = 0; i < LAST_PKMAP; i++) {
73 struct page *page;
75 /*
76 * zero means we don't have anything to do,
77 * >1 means that it is still in use. Only
78 * a count of 1 means that it is free but
79 * needs to be unmapped
80 */
81 if (pkmap_count[i] != 1)
82 continue;
83 pkmap_count[i] = 0;
85 /* sanity check */
86 if (pte_none(pkmap_page_table[i]))
87 BUG();
89 /*
90 * Don't need an atomic fetch-and-clear op here;
91 * no-one has the page mapped, and cannot get at
92 * its virtual address (and hence PTE) without first
93 * getting the kmap_lock (which is held here).
94 * So no dangers, even with speculative execution.
95 */
96 page = pte_page(pkmap_page_table[i]);
97 pte_clear(&init_mm, (unsigned long)page_address(page),
98 &pkmap_page_table[i]);
100 set_page_address(page, NULL);
101 }
102 flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
103 }
105 static inline unsigned long map_new_virtual(struct page *page)
106 {
107 unsigned long vaddr;
108 int count;
110 start:
111 count = LAST_PKMAP;
112 /* Find an empty entry */
113 for (;;) {
114 last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
115 if (!last_pkmap_nr) {
116 flush_all_zero_pkmaps();
117 count = LAST_PKMAP;
118 }
119 if (!pkmap_count[last_pkmap_nr])
120 break; /* Found a usable entry */
121 if (--count)
122 continue;
124 /*
125 * Sleep for somebody else to unmap their entries
126 */
127 {
128 DECLARE_WAITQUEUE(wait, current);
130 __set_current_state(TASK_UNINTERRUPTIBLE);
131 add_wait_queue(&pkmap_map_wait, &wait);
132 spin_unlock(&kmap_lock);
133 schedule();
134 remove_wait_queue(&pkmap_map_wait, &wait);
135 spin_lock(&kmap_lock);
137 /* Somebody else might have mapped it while we slept */
138 if (page_address(page))
139 return (unsigned long)page_address(page);
141 /* Re-start */
142 goto start;
143 }
144 }
145 vaddr = PKMAP_ADDR(last_pkmap_nr);
146 set_pte_at(&init_mm, vaddr,
147 &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
149 pkmap_count[last_pkmap_nr] = 1;
150 set_page_address(page, (void *)vaddr);
152 return vaddr;
153 }
155 #ifdef CONFIG_XEN
156 void kmap_flush_unused(void)
157 {
158 spin_lock(&kmap_lock);
159 flush_all_zero_pkmaps();
160 spin_unlock(&kmap_lock);
161 }
163 EXPORT_SYMBOL(kmap_flush_unused);
164 #endif
166 void fastcall *kmap_high(struct page *page)
167 {
168 unsigned long vaddr;
170 /*
171 * For highmem pages, we can't trust "virtual" until
172 * after we have the lock.
173 *
174 * We cannot call this from interrupts, as it may block
175 */
176 spin_lock(&kmap_lock);
177 vaddr = (unsigned long)page_address(page);
178 if (!vaddr)
179 vaddr = map_new_virtual(page);
180 pkmap_count[PKMAP_NR(vaddr)]++;
181 if (pkmap_count[PKMAP_NR(vaddr)] < 2)
182 BUG();
183 spin_unlock(&kmap_lock);
184 return (void*) vaddr;
185 }
187 EXPORT_SYMBOL(kmap_high);
189 void fastcall kunmap_high(struct page *page)
190 {
191 unsigned long vaddr;
192 unsigned long nr;
193 int need_wakeup;
195 spin_lock(&kmap_lock);
196 vaddr = (unsigned long)page_address(page);
197 if (!vaddr)
198 BUG();
199 nr = PKMAP_NR(vaddr);
201 /*
202 * A count must never go down to zero
203 * without a TLB flush!
204 */
205 need_wakeup = 0;
206 switch (--pkmap_count[nr]) {
207 case 0:
208 BUG();
209 case 1:
210 /*
211 * Avoid an unnecessary wake_up() function call.
212 * The common case is pkmap_count[] == 1, but
213 * no waiters.
214 * The tasks queued in the wait-queue are guarded
215 * by both the lock in the wait-queue-head and by
216 * the kmap_lock. As the kmap_lock is held here,
217 * no need for the wait-queue-head's lock. Simply
218 * test if the queue is empty.
219 */
220 need_wakeup = waitqueue_active(&pkmap_map_wait);
221 }
222 spin_unlock(&kmap_lock);
224 /* do wake-up, if needed, race-free outside of the spin lock */
225 if (need_wakeup)
226 wake_up(&pkmap_map_wait);
227 }
229 EXPORT_SYMBOL(kunmap_high);
231 #define POOL_SIZE 64
233 static __init int init_emergency_pool(void)
234 {
235 struct sysinfo i;
236 si_meminfo(&i);
237 si_swapinfo(&i);
239 if (!i.totalhigh)
240 return 0;
242 page_pool = mempool_create(POOL_SIZE, page_pool_alloc, page_pool_free, NULL);
243 if (!page_pool)
244 BUG();
245 printk("highmem bounce pool size: %d pages\n", POOL_SIZE);
247 return 0;
248 }
250 __initcall(init_emergency_pool);
252 /*
253 * highmem version, map in to vec
254 */
255 static void bounce_copy_vec(struct bio_vec *to, unsigned char *vfrom)
256 {
257 unsigned long flags;
258 unsigned char *vto;
260 local_irq_save(flags);
261 vto = kmap_atomic(to->bv_page, KM_BOUNCE_READ);
262 memcpy(vto + to->bv_offset, vfrom, to->bv_len);
263 kunmap_atomic(vto, KM_BOUNCE_READ);
264 local_irq_restore(flags);
265 }
267 #else /* CONFIG_HIGHMEM */
269 #define bounce_copy_vec(to, vfrom) \
270 memcpy(page_address((to)->bv_page) + (to)->bv_offset, vfrom, (to)->bv_len)
272 #endif
274 #define ISA_POOL_SIZE 16
276 /*
277 * gets called "every" time someone init's a queue with BLK_BOUNCE_ISA
278 * as the max address, so check if the pool has already been created.
279 */
280 int init_emergency_isa_pool(void)
281 {
282 if (isa_page_pool)
283 return 0;
285 isa_page_pool = mempool_create(ISA_POOL_SIZE, page_pool_alloc_isa, page_pool_free, NULL);
286 if (!isa_page_pool)
287 BUG();
289 printk("isa bounce pool size: %d pages\n", ISA_POOL_SIZE);
290 return 0;
291 }
293 /*
294 * Simple bounce buffer support for highmem pages. Depending on the
295 * queue gfp mask set, *to may or may not be a highmem page. kmap it
296 * always, it will do the Right Thing
297 */
298 static void copy_to_high_bio_irq(struct bio *to, struct bio *from)
299 {
300 unsigned char *vfrom;
301 struct bio_vec *tovec, *fromvec;
302 int i;
304 __bio_for_each_segment(tovec, to, i, 0) {
305 fromvec = from->bi_io_vec + i;
307 /*
308 * not bounced
309 */
310 if (tovec->bv_page == fromvec->bv_page)
311 continue;
313 /*
314 * fromvec->bv_offset and fromvec->bv_len might have been
315 * modified by the block layer, so use the original copy,
316 * bounce_copy_vec already uses tovec->bv_len
317 */
318 vfrom = page_address(fromvec->bv_page) + tovec->bv_offset;
320 flush_dcache_page(tovec->bv_page);
321 bounce_copy_vec(tovec, vfrom);
322 }
323 }
325 static void bounce_end_io(struct bio *bio, mempool_t *pool, int err)
326 {
327 struct bio *bio_orig = bio->bi_private;
328 struct bio_vec *bvec, *org_vec;
329 int i;
331 if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags))
332 set_bit(BIO_EOPNOTSUPP, &bio_orig->bi_flags);
334 /*
335 * free up bounce indirect pages used
336 */
337 __bio_for_each_segment(bvec, bio, i, 0) {
338 org_vec = bio_orig->bi_io_vec + i;
339 if (bvec->bv_page == org_vec->bv_page)
340 continue;
342 mempool_free(bvec->bv_page, pool);
343 dec_page_state(nr_bounce);
344 }
346 bio_endio(bio_orig, bio_orig->bi_size, err);
347 bio_put(bio);
348 }
350 static int bounce_end_io_write(struct bio *bio, unsigned int bytes_done,int err)
351 {
352 if (bio->bi_size)
353 return 1;
355 bounce_end_io(bio, page_pool, err);
356 return 0;
357 }
359 static int bounce_end_io_write_isa(struct bio *bio, unsigned int bytes_done, int err)
360 {
361 if (bio->bi_size)
362 return 1;
364 bounce_end_io(bio, isa_page_pool, err);
365 return 0;
366 }
368 static void __bounce_end_io_read(struct bio *bio, mempool_t *pool, int err)
369 {
370 struct bio *bio_orig = bio->bi_private;
372 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
373 copy_to_high_bio_irq(bio_orig, bio);
375 bounce_end_io(bio, pool, err);
376 }
378 static int bounce_end_io_read(struct bio *bio, unsigned int bytes_done, int err)
379 {
380 if (bio->bi_size)
381 return 1;
383 __bounce_end_io_read(bio, page_pool, err);
384 return 0;
385 }
387 static int bounce_end_io_read_isa(struct bio *bio, unsigned int bytes_done, int err)
388 {
389 if (bio->bi_size)
390 return 1;
392 __bounce_end_io_read(bio, isa_page_pool, err);
393 return 0;
394 }
396 static void __blk_queue_bounce(request_queue_t *q, struct bio **bio_orig,
397 mempool_t *pool)
398 {
399 struct page *page;
400 struct bio *bio = NULL;
401 int i, rw = bio_data_dir(*bio_orig);
402 struct bio_vec *to, *from;
404 bio_for_each_segment(from, *bio_orig, i) {
405 page = from->bv_page;
407 /*
408 * is destination page below bounce pfn?
409 */
410 if (page_to_pfn(page) < q->bounce_pfn)
411 continue;
413 /*
414 * irk, bounce it
415 */
416 if (!bio)
417 bio = bio_alloc(GFP_NOIO, (*bio_orig)->bi_vcnt);
419 to = bio->bi_io_vec + i;
421 to->bv_page = mempool_alloc(pool, q->bounce_gfp);
422 to->bv_len = from->bv_len;
423 to->bv_offset = from->bv_offset;
424 inc_page_state(nr_bounce);
426 if (rw == WRITE) {
427 char *vto, *vfrom;
429 flush_dcache_page(from->bv_page);
430 vto = page_address(to->bv_page) + to->bv_offset;
431 vfrom = kmap(from->bv_page) + from->bv_offset;
432 memcpy(vto, vfrom, to->bv_len);
433 kunmap(from->bv_page);
434 }
435 }
437 /*
438 * no pages bounced
439 */
440 if (!bio)
441 return;
443 /*
444 * at least one page was bounced, fill in possible non-highmem
445 * pages
446 */
447 __bio_for_each_segment(from, *bio_orig, i, 0) {
448 to = bio_iovec_idx(bio, i);
449 if (!to->bv_page) {
450 to->bv_page = from->bv_page;
451 to->bv_len = from->bv_len;
452 to->bv_offset = from->bv_offset;
453 }
454 }
456 bio->bi_bdev = (*bio_orig)->bi_bdev;
457 bio->bi_flags |= (1 << BIO_BOUNCED);
458 bio->bi_sector = (*bio_orig)->bi_sector;
459 bio->bi_rw = (*bio_orig)->bi_rw;
461 bio->bi_vcnt = (*bio_orig)->bi_vcnt;
462 bio->bi_idx = (*bio_orig)->bi_idx;
463 bio->bi_size = (*bio_orig)->bi_size;
465 if (pool == page_pool) {
466 bio->bi_end_io = bounce_end_io_write;
467 if (rw == READ)
468 bio->bi_end_io = bounce_end_io_read;
469 } else {
470 bio->bi_end_io = bounce_end_io_write_isa;
471 if (rw == READ)
472 bio->bi_end_io = bounce_end_io_read_isa;
473 }
475 bio->bi_private = *bio_orig;
476 *bio_orig = bio;
477 }
479 void blk_queue_bounce(request_queue_t *q, struct bio **bio_orig)
480 {
481 mempool_t *pool;
483 /*
484 * for non-isa bounce case, just check if the bounce pfn is equal
485 * to or bigger than the highest pfn in the system -- in that case,
486 * don't waste time iterating over bio segments
487 */
488 if (!(q->bounce_gfp & GFP_DMA)) {
489 if (q->bounce_pfn >= blk_max_pfn)
490 return;
491 pool = page_pool;
492 } else {
493 BUG_ON(!isa_page_pool);
494 pool = isa_page_pool;
495 }
497 /*
498 * slow path
499 */
500 __blk_queue_bounce(q, bio_orig, pool);
501 }
503 EXPORT_SYMBOL(blk_queue_bounce);
505 #if defined(HASHED_PAGE_VIRTUAL)
507 #define PA_HASH_ORDER 7
509 /*
510 * Describes one page->virtual association
511 */
512 struct page_address_map {
513 struct page *page;
514 void *virtual;
515 struct list_head list;
516 };
518 /*
519 * page_address_map freelist, allocated from page_address_maps.
520 */
521 static struct list_head page_address_pool; /* freelist */
522 static spinlock_t pool_lock; /* protects page_address_pool */
524 /*
525 * Hash table bucket
526 */
527 static struct page_address_slot {
528 struct list_head lh; /* List of page_address_maps */
529 spinlock_t lock; /* Protect this bucket's list */
530 } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
532 static struct page_address_slot *page_slot(struct page *page)
533 {
534 return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
535 }
537 void *page_address(struct page *page)
538 {
539 unsigned long flags;
540 void *ret;
541 struct page_address_slot *pas;
543 if (!PageHighMem(page))
544 return lowmem_page_address(page);
546 pas = page_slot(page);
547 ret = NULL;
548 spin_lock_irqsave(&pas->lock, flags);
549 if (!list_empty(&pas->lh)) {
550 struct page_address_map *pam;
552 list_for_each_entry(pam, &pas->lh, list) {
553 if (pam->page == page) {
554 ret = pam->virtual;
555 goto done;
556 }
557 }
558 }
559 done:
560 spin_unlock_irqrestore(&pas->lock, flags);
561 return ret;
562 }
564 EXPORT_SYMBOL(page_address);
566 void set_page_address(struct page *page, void *virtual)
567 {
568 unsigned long flags;
569 struct page_address_slot *pas;
570 struct page_address_map *pam;
572 BUG_ON(!PageHighMem(page));
574 pas = page_slot(page);
575 if (virtual) { /* Add */
576 BUG_ON(list_empty(&page_address_pool));
578 spin_lock_irqsave(&pool_lock, flags);
579 pam = list_entry(page_address_pool.next,
580 struct page_address_map, list);
581 list_del(&pam->list);
582 spin_unlock_irqrestore(&pool_lock, flags);
584 pam->page = page;
585 pam->virtual = virtual;
587 spin_lock_irqsave(&pas->lock, flags);
588 list_add_tail(&pam->list, &pas->lh);
589 spin_unlock_irqrestore(&pas->lock, flags);
590 } else { /* Remove */
591 spin_lock_irqsave(&pas->lock, flags);
592 list_for_each_entry(pam, &pas->lh, list) {
593 if (pam->page == page) {
594 list_del(&pam->list);
595 spin_unlock_irqrestore(&pas->lock, flags);
596 spin_lock_irqsave(&pool_lock, flags);
597 list_add_tail(&pam->list, &page_address_pool);
598 spin_unlock_irqrestore(&pool_lock, flags);
599 goto done;
600 }
601 }
602 spin_unlock_irqrestore(&pas->lock, flags);
603 }
604 done:
605 return;
606 }
608 static struct page_address_map page_address_maps[LAST_PKMAP];
610 void __init page_address_init(void)
611 {
612 int i;
614 INIT_LIST_HEAD(&page_address_pool);
615 for (i = 0; i < ARRAY_SIZE(page_address_maps); i++)
616 list_add(&page_address_maps[i].list, &page_address_pool);
617 for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
618 INIT_LIST_HEAD(&page_address_htable[i].lh);
619 spin_lock_init(&page_address_htable[i].lock);
620 }
621 spin_lock_init(&pool_lock);
622 }
624 #endif /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */