direct-io.hg

view linux-2.6-xen-sparse/mm/highmem.c @ 14132:0261e86db3b1

Fix xm vcpu-set command for when wrong number of VCPU is given

Signed-off-by: Masaki Kanno <kanno.masaki@jp.fujitsu.com>
author Ewan Mellor <ewan@xensource.com>
date Mon Feb 26 15:41:35 2007 +0000 (2007-02-26)
parents 3adf00179a63
children
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 <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)
35 {
36 return mempool_alloc_pages(gfp_mask | GFP_DMA, data);
37 }
39 /*
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.
46 */
47 #ifdef CONFIG_HIGHMEM
49 static int pkmap_count[LAST_PKMAP];
50 static unsigned int last_pkmap_nr;
51 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(kmap_lock);
53 pte_t * pkmap_page_table;
55 static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
57 static void flush_all_zero_pkmaps(void)
58 {
59 int i;
61 flush_cache_kmaps();
63 for (i = 0; i < LAST_PKMAP; i++) {
64 struct page *page;
66 /*
67 * zero means we don't have anything to do,
68 * >1 means that it is still in use. Only
69 * a count of 1 means that it is free but
70 * needs to be unmapped
71 */
72 if (pkmap_count[i] != 1)
73 continue;
74 pkmap_count[i] = 0;
76 /* sanity check */
77 BUG_ON(pte_none(pkmap_page_table[i]));
79 /*
80 * Don't need an atomic fetch-and-clear op here;
81 * no-one has the page mapped, and cannot get at
82 * its virtual address (and hence PTE) without first
83 * getting the kmap_lock (which is held here).
84 * So no dangers, even with speculative execution.
85 */
86 page = pte_page(pkmap_page_table[i]);
87 pte_clear(&init_mm, (unsigned long)page_address(page),
88 &pkmap_page_table[i]);
90 set_page_address(page, NULL);
91 }
92 flush_tlb_kernel_range(PKMAP_ADDR(0), PKMAP_ADDR(LAST_PKMAP));
93 }
95 static inline unsigned long map_new_virtual(struct page *page)
96 {
97 unsigned long vaddr;
98 int count;
100 start:
101 count = LAST_PKMAP;
102 /* Find an empty entry */
103 for (;;) {
104 last_pkmap_nr = (last_pkmap_nr + 1) & LAST_PKMAP_MASK;
105 if (!last_pkmap_nr) {
106 flush_all_zero_pkmaps();
107 count = LAST_PKMAP;
108 }
109 if (!pkmap_count[last_pkmap_nr])
110 break; /* Found a usable entry */
111 if (--count)
112 continue;
114 /*
115 * Sleep for somebody else to unmap their entries
116 */
117 {
118 DECLARE_WAITQUEUE(wait, current);
120 __set_current_state(TASK_UNINTERRUPTIBLE);
121 add_wait_queue(&pkmap_map_wait, &wait);
122 spin_unlock(&kmap_lock);
123 schedule();
124 remove_wait_queue(&pkmap_map_wait, &wait);
125 spin_lock(&kmap_lock);
127 /* Somebody else might have mapped it while we slept */
128 if (page_address(page))
129 return (unsigned long)page_address(page);
131 /* Re-start */
132 goto start;
133 }
134 }
135 vaddr = PKMAP_ADDR(last_pkmap_nr);
136 set_pte_at(&init_mm, vaddr,
137 &(pkmap_page_table[last_pkmap_nr]), mk_pte(page, kmap_prot));
139 pkmap_count[last_pkmap_nr] = 1;
140 set_page_address(page, (void *)vaddr);
142 return vaddr;
143 }
145 #ifdef CONFIG_XEN
146 void kmap_flush_unused(void)
147 {
148 spin_lock(&kmap_lock);
149 flush_all_zero_pkmaps();
150 spin_unlock(&kmap_lock);
151 }
153 EXPORT_SYMBOL(kmap_flush_unused);
154 #endif
156 void fastcall *kmap_high(struct page *page)
157 {
158 unsigned long vaddr;
160 /*
161 * For highmem pages, we can't trust "virtual" until
162 * after we have the lock.
163 *
164 * We cannot call this from interrupts, as it may block
165 */
166 spin_lock(&kmap_lock);
167 vaddr = (unsigned long)page_address(page);
168 if (!vaddr)
169 vaddr = map_new_virtual(page);
170 pkmap_count[PKMAP_NR(vaddr)]++;
171 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
172 spin_unlock(&kmap_lock);
173 return (void*) vaddr;
174 }
176 EXPORT_SYMBOL(kmap_high);
178 void fastcall kunmap_high(struct page *page)
179 {
180 unsigned long vaddr;
181 unsigned long nr;
182 int need_wakeup;
184 spin_lock(&kmap_lock);
185 vaddr = (unsigned long)page_address(page);
186 BUG_ON(!vaddr);
187 nr = PKMAP_NR(vaddr);
189 /*
190 * A count must never go down to zero
191 * without a TLB flush!
192 */
193 need_wakeup = 0;
194 switch (--pkmap_count[nr]) {
195 case 0:
196 BUG();
197 case 1:
198 /*
199 * Avoid an unnecessary wake_up() function call.
200 * The common case is pkmap_count[] == 1, but
201 * no waiters.
202 * The tasks queued in the wait-queue are guarded
203 * by both the lock in the wait-queue-head and by
204 * the kmap_lock. As the kmap_lock is held here,
205 * no need for the wait-queue-head's lock. Simply
206 * test if the queue is empty.
207 */
208 need_wakeup = waitqueue_active(&pkmap_map_wait);
209 }
210 spin_unlock(&kmap_lock);
212 /* do wake-up, if needed, race-free outside of the spin lock */
213 if (need_wakeup)
214 wake_up(&pkmap_map_wait);
215 }
217 EXPORT_SYMBOL(kunmap_high);
219 #define POOL_SIZE 64
221 static __init int init_emergency_pool(void)
222 {
223 struct sysinfo i;
224 si_meminfo(&i);
225 si_swapinfo(&i);
227 if (!i.totalhigh)
228 return 0;
230 page_pool = mempool_create_page_pool(POOL_SIZE, 0);
231 BUG_ON(!page_pool);
232 printk("highmem bounce pool size: %d pages\n", POOL_SIZE);
234 return 0;
235 }
237 __initcall(init_emergency_pool);
239 /*
240 * highmem version, map in to vec
241 */
242 static void bounce_copy_vec(struct bio_vec *to, unsigned char *vfrom)
243 {
244 unsigned long flags;
245 unsigned char *vto;
247 local_irq_save(flags);
248 vto = kmap_atomic(to->bv_page, KM_BOUNCE_READ);
249 memcpy(vto + to->bv_offset, vfrom, to->bv_len);
250 kunmap_atomic(vto, KM_BOUNCE_READ);
251 local_irq_restore(flags);
252 }
254 #else /* CONFIG_HIGHMEM */
256 #define bounce_copy_vec(to, vfrom) \
257 memcpy(page_address((to)->bv_page) + (to)->bv_offset, vfrom, (to)->bv_len)
259 #endif
261 #define ISA_POOL_SIZE 16
263 /*
264 * gets called "every" time someone init's a queue with BLK_BOUNCE_ISA
265 * as the max address, so check if the pool has already been created.
266 */
267 int init_emergency_isa_pool(void)
268 {
269 if (isa_page_pool)
270 return 0;
272 isa_page_pool = mempool_create(ISA_POOL_SIZE, mempool_alloc_pages_isa,
273 mempool_free_pages, (void *) 0);
274 BUG_ON(!isa_page_pool);
276 printk("isa bounce pool size: %d pages\n", ISA_POOL_SIZE);
277 return 0;
278 }
280 /*
281 * Simple bounce buffer support for highmem pages. Depending on the
282 * queue gfp mask set, *to may or may not be a highmem page. kmap it
283 * always, it will do the Right Thing
284 */
285 static void copy_to_high_bio_irq(struct bio *to, struct bio *from)
286 {
287 unsigned char *vfrom;
288 struct bio_vec *tovec, *fromvec;
289 int i;
291 __bio_for_each_segment(tovec, to, i, 0) {
292 fromvec = from->bi_io_vec + i;
294 /*
295 * not bounced
296 */
297 if (tovec->bv_page == fromvec->bv_page)
298 continue;
300 /*
301 * fromvec->bv_offset and fromvec->bv_len might have been
302 * modified by the block layer, so use the original copy,
303 * bounce_copy_vec already uses tovec->bv_len
304 */
305 vfrom = page_address(fromvec->bv_page) + tovec->bv_offset;
307 flush_dcache_page(tovec->bv_page);
308 bounce_copy_vec(tovec, vfrom);
309 }
310 }
312 static void bounce_end_io(struct bio *bio, mempool_t *pool, int err)
313 {
314 struct bio *bio_orig = bio->bi_private;
315 struct bio_vec *bvec, *org_vec;
316 int i;
318 if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags))
319 set_bit(BIO_EOPNOTSUPP, &bio_orig->bi_flags);
321 /*
322 * free up bounce indirect pages used
323 */
324 __bio_for_each_segment(bvec, bio, i, 0) {
325 org_vec = bio_orig->bi_io_vec + i;
326 if (bvec->bv_page == org_vec->bv_page)
327 continue;
329 dec_zone_page_state(bvec->bv_page, NR_BOUNCE);
330 mempool_free(bvec->bv_page, pool);
331 }
333 bio_endio(bio_orig, bio_orig->bi_size, err);
334 bio_put(bio);
335 }
337 static int bounce_end_io_write(struct bio *bio, unsigned int bytes_done, int err)
338 {
339 if (bio->bi_size)
340 return 1;
342 bounce_end_io(bio, page_pool, err);
343 return 0;
344 }
346 static int bounce_end_io_write_isa(struct bio *bio, unsigned int bytes_done, int err)
347 {
348 if (bio->bi_size)
349 return 1;
351 bounce_end_io(bio, isa_page_pool, err);
352 return 0;
353 }
355 static void __bounce_end_io_read(struct bio *bio, mempool_t *pool, int err)
356 {
357 struct bio *bio_orig = bio->bi_private;
359 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
360 copy_to_high_bio_irq(bio_orig, bio);
362 bounce_end_io(bio, pool, err);
363 }
365 static int bounce_end_io_read(struct bio *bio, unsigned int bytes_done, int err)
366 {
367 if (bio->bi_size)
368 return 1;
370 __bounce_end_io_read(bio, page_pool, err);
371 return 0;
372 }
374 static int bounce_end_io_read_isa(struct bio *bio, unsigned int bytes_done, int err)
375 {
376 if (bio->bi_size)
377 return 1;
379 __bounce_end_io_read(bio, isa_page_pool, err);
380 return 0;
381 }
383 static void __blk_queue_bounce(request_queue_t *q, struct bio **bio_orig,
384 mempool_t *pool)
385 {
386 struct page *page;
387 struct bio *bio = NULL;
388 int i, rw = bio_data_dir(*bio_orig);
389 struct bio_vec *to, *from;
391 bio_for_each_segment(from, *bio_orig, i) {
392 page = from->bv_page;
394 /*
395 * is destination page below bounce pfn?
396 */
397 if (page_to_pfn(page) < q->bounce_pfn)
398 continue;
400 /*
401 * irk, bounce it
402 */
403 if (!bio)
404 bio = bio_alloc(GFP_NOIO, (*bio_orig)->bi_vcnt);
406 to = bio->bi_io_vec + i;
408 to->bv_page = mempool_alloc(pool, q->bounce_gfp);
409 to->bv_len = from->bv_len;
410 to->bv_offset = from->bv_offset;
411 inc_zone_page_state(to->bv_page, NR_BOUNCE);
413 if (rw == WRITE) {
414 char *vto, *vfrom;
416 flush_dcache_page(from->bv_page);
417 vto = page_address(to->bv_page) + to->bv_offset;
418 vfrom = kmap(from->bv_page) + from->bv_offset;
419 memcpy(vto, vfrom, to->bv_len);
420 kunmap(from->bv_page);
421 }
422 }
424 /*
425 * no pages bounced
426 */
427 if (!bio)
428 return;
430 /*
431 * at least one page was bounced, fill in possible non-highmem
432 * pages
433 */
434 __bio_for_each_segment(from, *bio_orig, i, 0) {
435 to = bio_iovec_idx(bio, i);
436 if (!to->bv_page) {
437 to->bv_page = from->bv_page;
438 to->bv_len = from->bv_len;
439 to->bv_offset = from->bv_offset;
440 }
441 }
443 bio->bi_bdev = (*bio_orig)->bi_bdev;
444 bio->bi_flags |= (1 << BIO_BOUNCED);
445 bio->bi_sector = (*bio_orig)->bi_sector;
446 bio->bi_rw = (*bio_orig)->bi_rw;
448 bio->bi_vcnt = (*bio_orig)->bi_vcnt;
449 bio->bi_idx = (*bio_orig)->bi_idx;
450 bio->bi_size = (*bio_orig)->bi_size;
452 if (pool == page_pool) {
453 bio->bi_end_io = bounce_end_io_write;
454 if (rw == READ)
455 bio->bi_end_io = bounce_end_io_read;
456 } else {
457 bio->bi_end_io = bounce_end_io_write_isa;
458 if (rw == READ)
459 bio->bi_end_io = bounce_end_io_read_isa;
460 }
462 bio->bi_private = *bio_orig;
463 *bio_orig = bio;
464 }
466 void blk_queue_bounce(request_queue_t *q, struct bio **bio_orig)
467 {
468 mempool_t *pool;
470 /*
471 * for non-isa bounce case, just check if the bounce pfn is equal
472 * to or bigger than the highest pfn in the system -- in that case,
473 * don't waste time iterating over bio segments
474 */
475 if (!(q->bounce_gfp & GFP_DMA)) {
476 if (q->bounce_pfn >= blk_max_pfn)
477 return;
478 pool = page_pool;
479 } else {
480 BUG_ON(!isa_page_pool);
481 pool = isa_page_pool;
482 }
484 blk_add_trace_bio(q, *bio_orig, BLK_TA_BOUNCE);
486 /*
487 * slow path
488 */
489 __blk_queue_bounce(q, bio_orig, pool);
490 }
492 EXPORT_SYMBOL(blk_queue_bounce);
494 #if defined(HASHED_PAGE_VIRTUAL)
496 #define PA_HASH_ORDER 7
498 /*
499 * Describes one page->virtual association
500 */
501 struct page_address_map {
502 struct page *page;
503 void *virtual;
504 struct list_head list;
505 };
507 /*
508 * page_address_map freelist, allocated from page_address_maps.
509 */
510 static struct list_head page_address_pool; /* freelist */
511 static spinlock_t pool_lock; /* protects page_address_pool */
513 /*
514 * Hash table bucket
515 */
516 static struct page_address_slot {
517 struct list_head lh; /* List of page_address_maps */
518 spinlock_t lock; /* Protect this bucket's list */
519 } ____cacheline_aligned_in_smp page_address_htable[1<<PA_HASH_ORDER];
521 static struct page_address_slot *page_slot(struct page *page)
522 {
523 return &page_address_htable[hash_ptr(page, PA_HASH_ORDER)];
524 }
526 void *page_address(struct page *page)
527 {
528 unsigned long flags;
529 void *ret;
530 struct page_address_slot *pas;
532 if (!PageHighMem(page))
533 return lowmem_page_address(page);
535 pas = page_slot(page);
536 ret = NULL;
537 spin_lock_irqsave(&pas->lock, flags);
538 if (!list_empty(&pas->lh)) {
539 struct page_address_map *pam;
541 list_for_each_entry(pam, &pas->lh, list) {
542 if (pam->page == page) {
543 ret = pam->virtual;
544 goto done;
545 }
546 }
547 }
548 done:
549 spin_unlock_irqrestore(&pas->lock, flags);
550 return ret;
551 }
553 EXPORT_SYMBOL(page_address);
555 void set_page_address(struct page *page, void *virtual)
556 {
557 unsigned long flags;
558 struct page_address_slot *pas;
559 struct page_address_map *pam;
561 BUG_ON(!PageHighMem(page));
563 pas = page_slot(page);
564 if (virtual) { /* Add */
565 BUG_ON(list_empty(&page_address_pool));
567 spin_lock_irqsave(&pool_lock, flags);
568 pam = list_entry(page_address_pool.next,
569 struct page_address_map, list);
570 list_del(&pam->list);
571 spin_unlock_irqrestore(&pool_lock, flags);
573 pam->page = page;
574 pam->virtual = virtual;
576 spin_lock_irqsave(&pas->lock, flags);
577 list_add_tail(&pam->list, &pas->lh);
578 spin_unlock_irqrestore(&pas->lock, flags);
579 } else { /* Remove */
580 spin_lock_irqsave(&pas->lock, flags);
581 list_for_each_entry(pam, &pas->lh, list) {
582 if (pam->page == page) {
583 list_del(&pam->list);
584 spin_unlock_irqrestore(&pas->lock, flags);
585 spin_lock_irqsave(&pool_lock, flags);
586 list_add_tail(&pam->list, &page_address_pool);
587 spin_unlock_irqrestore(&pool_lock, flags);
588 goto done;
589 }
590 }
591 spin_unlock_irqrestore(&pas->lock, flags);
592 }
593 done:
594 return;
595 }
597 static struct page_address_map page_address_maps[LAST_PKMAP];
599 void __init page_address_init(void)
600 {
601 int i;
603 INIT_LIST_HEAD(&page_address_pool);
604 for (i = 0; i < ARRAY_SIZE(page_address_maps); i++)
605 list_add(&page_address_maps[i].list, &page_address_pool);
606 for (i = 0; i < ARRAY_SIZE(page_address_htable); i++) {
607 INIT_LIST_HEAD(&page_address_htable[i].lh);
608 spin_lock_init(&page_address_htable[i].lock);
609 }
610 spin_lock_init(&pool_lock);
611 }
613 #endif /* defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL) */