ia64/xen-unstable

view xen/common/page_alloc.c @ 8700:1712b52e0074

Move MAX_DMADOM_PFN to asm/config.h.

Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Mon Jan 30 11:23:32 2006 +0100 (2006-01-30)
parents 82eafda1c710
children 0c94043f5c5b
line source
1 /******************************************************************************
2 * page_alloc.c
3 *
4 * Simple buddy heap allocator for Xen.
5 *
6 * Copyright (c) 2002-2004 K A Fraser
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 #include <xen/config.h>
24 #include <xen/init.h>
25 #include <xen/types.h>
26 #include <xen/lib.h>
27 #include <xen/perfc.h>
28 #include <xen/sched.h>
29 #include <xen/spinlock.h>
30 #include <xen/mm.h>
31 #include <xen/irq.h>
32 #include <xen/softirq.h>
33 #include <xen/shadow.h>
34 #include <xen/domain_page.h>
35 #include <asm/page.h>
37 /*
38 * Comma-separated list of hexadecimal page numbers containing bad bytes.
39 * e.g. 'badpage=0x3f45,0x8a321'.
40 */
41 static char opt_badpage[100] = "";
42 string_param("badpage", opt_badpage);
44 #define round_pgdown(_p) ((_p)&PAGE_MASK)
45 #define round_pgup(_p) (((_p)+(PAGE_SIZE-1))&PAGE_MASK)
47 static spinlock_t page_scrub_lock = SPIN_LOCK_UNLOCKED;
48 LIST_HEAD(page_scrub_list);
50 /*********************
51 * ALLOCATION BITMAP
52 * One bit per page of memory. Bit set => page is allocated.
53 */
55 static unsigned long *alloc_bitmap;
56 #define PAGES_PER_MAPWORD (sizeof(unsigned long) * 8)
58 #define allocated_in_map(_pn) \
59 ( !! (alloc_bitmap[(_pn)/PAGES_PER_MAPWORD] & \
60 (1UL<<((_pn)&(PAGES_PER_MAPWORD-1)))) )
62 /*
63 * Hint regarding bitwise arithmetic in map_{alloc,free}:
64 * -(1<<n) sets all bits >= n.
65 * (1<<n)-1 sets all bits < n.
66 * Variable names in map_{alloc,free}:
67 * *_idx == Index into `alloc_bitmap' array.
68 * *_off == Bit offset within an element of the `alloc_bitmap' array.
69 */
71 static void map_alloc(unsigned long first_page, unsigned long nr_pages)
72 {
73 unsigned long start_off, end_off, curr_idx, end_idx;
75 #ifndef NDEBUG
76 unsigned long i;
77 /* Check that the block isn't already allocated. */
78 for ( i = 0; i < nr_pages; i++ )
79 ASSERT(!allocated_in_map(first_page + i));
80 #endif
82 curr_idx = first_page / PAGES_PER_MAPWORD;
83 start_off = first_page & (PAGES_PER_MAPWORD-1);
84 end_idx = (first_page + nr_pages) / PAGES_PER_MAPWORD;
85 end_off = (first_page + nr_pages) & (PAGES_PER_MAPWORD-1);
87 if ( curr_idx == end_idx )
88 {
89 alloc_bitmap[curr_idx] |= ((1UL<<end_off)-1) & -(1UL<<start_off);
90 }
91 else
92 {
93 alloc_bitmap[curr_idx] |= -(1UL<<start_off);
94 while ( ++curr_idx < end_idx ) alloc_bitmap[curr_idx] = ~0UL;
95 alloc_bitmap[curr_idx] |= (1UL<<end_off)-1;
96 }
97 }
100 static void map_free(unsigned long first_page, unsigned long nr_pages)
101 {
102 unsigned long start_off, end_off, curr_idx, end_idx;
104 #ifndef NDEBUG
105 unsigned long i;
106 /* Check that the block isn't already freed. */
107 for ( i = 0; i < nr_pages; i++ )
108 ASSERT(allocated_in_map(first_page + i));
109 #endif
111 curr_idx = first_page / PAGES_PER_MAPWORD;
112 start_off = first_page & (PAGES_PER_MAPWORD-1);
113 end_idx = (first_page + nr_pages) / PAGES_PER_MAPWORD;
114 end_off = (first_page + nr_pages) & (PAGES_PER_MAPWORD-1);
116 if ( curr_idx == end_idx )
117 {
118 alloc_bitmap[curr_idx] &= -(1UL<<end_off) | ((1UL<<start_off)-1);
119 }
120 else
121 {
122 alloc_bitmap[curr_idx] &= (1UL<<start_off)-1;
123 while ( ++curr_idx != end_idx ) alloc_bitmap[curr_idx] = 0;
124 alloc_bitmap[curr_idx] &= -(1UL<<end_off);
125 }
126 }
130 /*************************
131 * BOOT-TIME ALLOCATOR
132 */
134 /* Initialise allocator to handle up to @max_page pages. */
135 physaddr_t init_boot_allocator(physaddr_t bitmap_start)
136 {
137 unsigned long bitmap_size;
139 bitmap_start = round_pgup(bitmap_start);
141 /*
142 * Allocate space for the allocation bitmap. Include an extra longword
143 * of padding for possible overrun in map_alloc and map_free.
144 */
145 bitmap_size = max_page / 8;
146 bitmap_size += sizeof(unsigned long);
147 bitmap_size = round_pgup(bitmap_size);
148 alloc_bitmap = (unsigned long *)phys_to_virt(bitmap_start);
150 /* All allocated by default. */
151 memset(alloc_bitmap, ~0, bitmap_size);
153 return bitmap_start + bitmap_size;
154 }
156 void init_boot_pages(physaddr_t ps, physaddr_t pe)
157 {
158 unsigned long bad_pfn;
159 char *p;
161 ps = round_pgup(ps);
162 pe = round_pgdown(pe);
163 if ( pe <= ps )
164 return;
166 map_free(ps >> PAGE_SHIFT, (pe - ps) >> PAGE_SHIFT);
168 /* Check new pages against the bad-page list. */
169 p = opt_badpage;
170 while ( *p != '\0' )
171 {
172 bad_pfn = simple_strtoul(p, &p, 0);
174 if ( *p == ',' )
175 p++;
176 else if ( *p != '\0' )
177 break;
179 if ( (bad_pfn < max_page) && !allocated_in_map(bad_pfn) )
180 {
181 printk("Marking page %lx as bad\n", bad_pfn);
182 map_alloc(bad_pfn, 1);
183 }
184 }
185 }
187 unsigned long alloc_boot_pages(unsigned long nr_pfns, unsigned long pfn_align)
188 {
189 unsigned long pg, i;
191 for ( pg = 0; (pg + nr_pfns) < max_page; pg += pfn_align )
192 {
193 for ( i = 0; i < nr_pfns; i++ )
194 if ( allocated_in_map(pg + i) )
195 break;
197 if ( i == nr_pfns )
198 {
199 map_alloc(pg, nr_pfns);
200 return pg;
201 }
202 }
204 return 0;
205 }
209 /*************************
210 * BINARY BUDDY ALLOCATOR
211 */
213 #define MEMZONE_XEN 0
214 #define MEMZONE_DOM 1
215 #define MEMZONE_DMADOM 2
216 #define NR_ZONES 3
218 #define pfn_dom_zone_type(_pfn) \
219 (((_pfn) <= MAX_DMADOM_PFN) ? MEMZONE_DMADOM : MEMZONE_DOM)
221 /* Up to 2^20 pages can be allocated at once. */
222 #define MAX_ORDER 20
223 static struct list_head heap[NR_ZONES][MAX_ORDER+1];
225 static unsigned long avail[NR_ZONES];
227 static spinlock_t heap_lock = SPIN_LOCK_UNLOCKED;
229 void end_boot_allocator(void)
230 {
231 unsigned long i, j;
232 int curr_free = 0, next_free = 0;
234 memset(avail, 0, sizeof(avail));
236 for ( i = 0; i < NR_ZONES; i++ )
237 for ( j = 0; j <= MAX_ORDER; j++ )
238 INIT_LIST_HEAD(&heap[i][j]);
240 /* Pages that are free now go to the domain sub-allocator. */
241 for ( i = 0; i < max_page; i++ )
242 {
243 curr_free = next_free;
244 next_free = !allocated_in_map(i+1);
245 if ( next_free )
246 map_alloc(i+1, 1); /* prevent merging in free_heap_pages() */
247 if ( curr_free )
248 free_heap_pages(pfn_dom_zone_type(i), pfn_to_page(i), 0);
249 }
250 }
252 /* Hand the specified arbitrary page range to the specified heap zone. */
253 void init_heap_pages(
254 unsigned int zone, struct pfn_info *pg, unsigned long nr_pages)
255 {
256 unsigned long i;
258 ASSERT(zone < NR_ZONES);
260 for ( i = 0; i < nr_pages; i++ )
261 free_heap_pages(zone, pg+i, 0);
262 }
265 /* Allocate 2^@order contiguous pages. */
266 struct pfn_info *alloc_heap_pages(unsigned int zone, unsigned int order)
267 {
268 int i;
269 struct pfn_info *pg;
271 ASSERT(zone < NR_ZONES);
273 if ( unlikely(order > MAX_ORDER) )
274 return NULL;
276 spin_lock(&heap_lock);
278 /* Find smallest order which can satisfy the request. */
279 for ( i = order; i <= MAX_ORDER; i++ )
280 if ( !list_empty(&heap[zone][i]) )
281 goto found;
283 /* No suitable memory blocks. Fail the request. */
284 spin_unlock(&heap_lock);
285 return NULL;
287 found:
288 pg = list_entry(heap[zone][i].next, struct pfn_info, list);
289 list_del(&pg->list);
291 /* We may have to halve the chunk a number of times. */
292 while ( i != order )
293 {
294 PFN_ORDER(pg) = --i;
295 list_add_tail(&pg->list, &heap[zone][i]);
296 pg += 1 << i;
297 }
299 map_alloc(page_to_pfn(pg), 1 << order);
300 avail[zone] -= 1 << order;
302 spin_unlock(&heap_lock);
304 return pg;
305 }
308 /* Free 2^@order set of pages. */
309 void free_heap_pages(
310 unsigned int zone, struct pfn_info *pg, unsigned int order)
311 {
312 unsigned long mask;
314 ASSERT(zone < NR_ZONES);
315 ASSERT(order <= MAX_ORDER);
317 spin_lock(&heap_lock);
319 map_free(page_to_pfn(pg), 1 << order);
320 avail[zone] += 1 << order;
322 /* Merge chunks as far as possible. */
323 while ( order < MAX_ORDER )
324 {
325 mask = 1 << order;
327 if ( (page_to_pfn(pg) & mask) )
328 {
329 /* Merge with predecessor block? */
330 if ( allocated_in_map(page_to_pfn(pg)-mask) ||
331 (PFN_ORDER(pg-mask) != order) )
332 break;
333 list_del(&(pg-mask)->list);
334 pg -= mask;
335 }
336 else
337 {
338 /* Merge with successor block? */
339 if ( allocated_in_map(page_to_pfn(pg)+mask) ||
340 (PFN_ORDER(pg+mask) != order) )
341 break;
342 list_del(&(pg+mask)->list);
343 }
345 order++;
346 }
348 PFN_ORDER(pg) = order;
349 list_add_tail(&pg->list, &heap[zone][order]);
351 spin_unlock(&heap_lock);
352 }
355 /*
356 * Scrub all unallocated pages in all heap zones. This function is more
357 * convoluted than appears necessary because we do not want to continuously
358 * hold the lock or disable interrupts while scrubbing very large memory areas.
359 */
360 void scrub_heap_pages(void)
361 {
362 void *p;
363 unsigned long pfn;
364 int cpu = smp_processor_id();
366 printk("Scrubbing Free RAM: ");
368 for ( pfn = 0; pfn < max_page; pfn++ )
369 {
370 /* Every 100MB, print a progress dot. */
371 if ( (pfn % ((100*1024*1024)/PAGE_SIZE)) == 0 )
372 printk(".");
374 if ( unlikely(softirq_pending(cpu)) )
375 do_softirq();
377 /* Quick lock-free check. */
378 if ( allocated_in_map(pfn) )
379 continue;
381 spin_lock_irq(&heap_lock);
383 /* Re-check page status with lock held. */
384 if ( !allocated_in_map(pfn) )
385 {
386 if ( IS_XEN_HEAP_FRAME(pfn_to_page(pfn)) )
387 {
388 p = page_to_virt(pfn_to_page(pfn));
389 memguard_unguard_range(p, PAGE_SIZE);
390 clear_page(p);
391 memguard_guard_range(p, PAGE_SIZE);
392 }
393 else
394 {
395 p = map_domain_page(pfn);
396 clear_page(p);
397 unmap_domain_page(p);
398 }
399 }
401 spin_unlock_irq(&heap_lock);
402 }
404 printk("done.\n");
405 }
409 /*************************
410 * XEN-HEAP SUB-ALLOCATOR
411 */
413 void init_xenheap_pages(physaddr_t ps, physaddr_t pe)
414 {
415 unsigned long flags;
417 ps = round_pgup(ps);
418 pe = round_pgdown(pe);
419 if ( pe <= ps )
420 return;
422 memguard_guard_range(phys_to_virt(ps), pe - ps);
424 /*
425 * Yuk! Ensure there is a one-page buffer between Xen and Dom zones, to
426 * prevent merging of power-of-two blocks across the zone boundary.
427 */
428 if ( !IS_XEN_HEAP_FRAME(phys_to_page(pe)) )
429 pe -= PAGE_SIZE;
431 local_irq_save(flags);
432 init_heap_pages(MEMZONE_XEN, phys_to_page(ps), (pe - ps) >> PAGE_SHIFT);
433 local_irq_restore(flags);
434 }
437 void *alloc_xenheap_pages(unsigned int order)
438 {
439 unsigned long flags;
440 struct pfn_info *pg;
441 int i;
443 local_irq_save(flags);
444 pg = alloc_heap_pages(MEMZONE_XEN, order);
445 local_irq_restore(flags);
447 if ( unlikely(pg == NULL) )
448 goto no_memory;
450 memguard_unguard_range(page_to_virt(pg), 1 << (order + PAGE_SHIFT));
452 for ( i = 0; i < (1 << order); i++ )
453 {
454 pg[i].count_info = 0;
455 pg[i].u.inuse._domain = 0;
456 pg[i].u.inuse.type_info = 0;
457 }
459 return page_to_virt(pg);
461 no_memory:
462 printk("Cannot handle page request order %d!\n", order);
463 return NULL;
464 }
467 void free_xenheap_pages(void *v, unsigned int order)
468 {
469 unsigned long flags;
471 if ( v == NULL )
472 return;
474 memguard_guard_range(v, 1 << (order + PAGE_SHIFT));
476 local_irq_save(flags);
477 free_heap_pages(MEMZONE_XEN, virt_to_page(v), order);
478 local_irq_restore(flags);
479 }
483 /*************************
484 * DOMAIN-HEAP SUB-ALLOCATOR
485 */
487 void init_domheap_pages(physaddr_t ps, physaddr_t pe)
488 {
489 unsigned long s_tot, e_tot, s_dma, e_dma, s_nrm, e_nrm;
491 ASSERT(!in_irq());
493 s_tot = round_pgup(ps) >> PAGE_SHIFT;
494 e_tot = round_pgdown(pe) >> PAGE_SHIFT;
496 s_dma = min(s_tot, MAX_DMADOM_PFN + 1);
497 e_dma = min(e_tot, MAX_DMADOM_PFN + 1);
498 if ( s_dma < e_dma )
499 init_heap_pages(MEMZONE_DMADOM, pfn_to_page(s_dma), e_dma - s_dma);
501 s_nrm = max(s_tot, MAX_DMADOM_PFN + 1);
502 e_nrm = max(e_tot, MAX_DMADOM_PFN + 1);
503 if ( s_nrm < e_nrm )
504 init_heap_pages(MEMZONE_DOM, pfn_to_page(s_nrm), e_nrm - s_nrm);
505 }
508 struct pfn_info *alloc_domheap_pages(
509 struct domain *d, unsigned int order, unsigned int flags)
510 {
511 struct pfn_info *pg = NULL;
512 cpumask_t mask;
513 int i;
515 ASSERT(!in_irq());
517 if ( !(flags & ALLOC_DOM_DMA) )
518 pg = alloc_heap_pages(MEMZONE_DOM, order);
520 if ( pg == NULL )
521 if ( (pg = alloc_heap_pages(MEMZONE_DMADOM, order)) == NULL )
522 return NULL;
524 mask = pg->u.free.cpumask;
525 tlbflush_filter(mask, pg->tlbflush_timestamp);
527 pg->count_info = 0;
528 pg->u.inuse._domain = 0;
529 pg->u.inuse.type_info = 0;
531 for ( i = 1; i < (1 << order); i++ )
532 {
533 /* Add in any extra CPUs that need flushing because of this page. */
534 cpumask_t extra_cpus_mask;
535 cpus_andnot(extra_cpus_mask, pg[i].u.free.cpumask, mask);
536 tlbflush_filter(extra_cpus_mask, pg[i].tlbflush_timestamp);
537 cpus_or(mask, mask, extra_cpus_mask);
539 pg[i].count_info = 0;
540 pg[i].u.inuse._domain = 0;
541 pg[i].u.inuse.type_info = 0;
542 }
544 if ( unlikely(!cpus_empty(mask)) )
545 {
546 perfc_incrc(need_flush_tlb_flush);
547 flush_tlb_mask(mask);
548 }
550 if ( d == NULL )
551 return pg;
553 spin_lock(&d->page_alloc_lock);
555 if ( unlikely(test_bit(_DOMF_dying, &d->domain_flags)) ||
556 unlikely((d->tot_pages + (1 << order)) > d->max_pages) )
557 {
558 DPRINTK("Over-allocation for domain %u: %u > %u\n",
559 d->domain_id, d->tot_pages + (1 << order), d->max_pages);
560 DPRINTK("...or the domain is dying (%d)\n",
561 !!test_bit(_DOMF_dying, &d->domain_flags));
562 spin_unlock(&d->page_alloc_lock);
563 free_heap_pages(pfn_dom_zone_type(page_to_pfn(pg)), pg, order);
564 return NULL;
565 }
567 if ( unlikely(d->tot_pages == 0) )
568 get_knownalive_domain(d);
570 d->tot_pages += 1 << order;
572 for ( i = 0; i < (1 << order); i++ )
573 {
574 page_set_owner(&pg[i], d);
575 wmb(); /* Domain pointer must be visible before updating refcnt. */
576 pg[i].count_info |= PGC_allocated | 1;
577 list_add_tail(&pg[i].list, &d->page_list);
578 }
580 spin_unlock(&d->page_alloc_lock);
582 return pg;
583 }
586 void free_domheap_pages(struct pfn_info *pg, unsigned int order)
587 {
588 int i, drop_dom_ref;
589 struct domain *d = page_get_owner(pg);
591 ASSERT(!in_irq());
593 if ( unlikely(IS_XEN_HEAP_FRAME(pg)) )
594 {
595 /* NB. May recursively lock from relinquish_memory(). */
596 spin_lock_recursive(&d->page_alloc_lock);
598 for ( i = 0; i < (1 << order); i++ )
599 list_del(&pg[i].list);
601 d->xenheap_pages -= 1 << order;
602 drop_dom_ref = (d->xenheap_pages == 0);
604 spin_unlock_recursive(&d->page_alloc_lock);
605 }
606 else if ( likely(d != NULL) )
607 {
608 /* NB. May recursively lock from relinquish_memory(). */
609 spin_lock_recursive(&d->page_alloc_lock);
611 for ( i = 0; i < (1 << order); i++ )
612 {
613 shadow_drop_references(d, &pg[i]);
614 ASSERT((pg[i].u.inuse.type_info & PGT_count_mask) == 0);
615 pg[i].tlbflush_timestamp = tlbflush_current_time();
616 pg[i].u.free.cpumask = d->domain_dirty_cpumask;
617 list_del(&pg[i].list);
618 }
620 d->tot_pages -= 1 << order;
621 drop_dom_ref = (d->tot_pages == 0);
623 spin_unlock_recursive(&d->page_alloc_lock);
625 if ( likely(!test_bit(_DOMF_dying, &d->domain_flags)) )
626 {
627 free_heap_pages(pfn_dom_zone_type(page_to_pfn(pg)), pg, order);
628 }
629 else
630 {
631 /*
632 * Normally we expect a domain to clear pages before freeing them,
633 * if it cares about the secrecy of their contents. However, after
634 * a domain has died we assume responsibility for erasure.
635 */
636 for ( i = 0; i < (1 << order); i++ )
637 {
638 spin_lock(&page_scrub_lock);
639 list_add(&pg[i].list, &page_scrub_list);
640 spin_unlock(&page_scrub_lock);
641 }
642 }
643 }
644 else
645 {
646 /* Freeing anonymous domain-heap pages. */
647 for ( i = 0; i < (1 << order); i++ )
648 pg[i].u.free.cpumask = CPU_MASK_NONE;
649 free_heap_pages(pfn_dom_zone_type(page_to_pfn(pg)), pg, order);
650 drop_dom_ref = 0;
651 }
653 if ( drop_dom_ref )
654 put_domain(d);
655 }
658 unsigned long avail_domheap_pages(void)
659 {
660 return avail[MEMZONE_DOM] + avail[MEMZONE_DMADOM];
661 }
665 /*************************
666 * PAGE SCRUBBING
667 */
669 static void page_scrub_softirq(void)
670 {
671 struct list_head *ent;
672 struct pfn_info *pg;
673 void *p;
674 int i;
675 s_time_t start = NOW();
677 /* Aim to do 1ms of work (ten percent of a 10ms jiffy). */
678 do {
679 spin_lock(&page_scrub_lock);
681 if ( unlikely((ent = page_scrub_list.next) == &page_scrub_list) )
682 {
683 spin_unlock(&page_scrub_lock);
684 return;
685 }
687 /* Peel up to 16 pages from the list. */
688 for ( i = 0; i < 16; i++ )
689 {
690 if ( ent->next == &page_scrub_list )
691 break;
692 ent = ent->next;
693 }
695 /* Remove peeled pages from the list. */
696 ent->next->prev = &page_scrub_list;
697 page_scrub_list.next = ent->next;
699 spin_unlock(&page_scrub_lock);
701 /* Working backwards, scrub each page in turn. */
702 while ( ent != &page_scrub_list )
703 {
704 pg = list_entry(ent, struct pfn_info, list);
705 ent = ent->prev;
706 p = map_domain_page(page_to_pfn(pg));
707 clear_page(p);
708 unmap_domain_page(p);
709 free_heap_pages(pfn_dom_zone_type(page_to_pfn(pg)), pg, 0);
710 }
711 } while ( (NOW() - start) < MILLISECS(1) );
712 }
714 static __init int page_scrub_init(void)
715 {
716 open_softirq(PAGE_SCRUB_SOFTIRQ, page_scrub_softirq);
717 return 0;
718 }
719 __initcall(page_scrub_init);
721 /*
722 * Local variables:
723 * mode: C
724 * c-set-style: "BSD"
725 * c-basic-offset: 4
726 * tab-width: 4
727 * indent-tabs-mode: nil
728 * End:
729 */