ia64/xen-unstable

view xen/common/page_alloc.c @ 15647:cc48264ed647

Merge
author Tim Deegan <Tim.Deegan@xensource.com>
date Tue Jul 24 14:53:06 2007 +0100 (2007-07-24)
parents 3c28bc13a3f8
children cb3e6fcb7f34
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 * Copyright (c) 2006 IBM Ryan Harper <ryanh@us.ibm.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
24 #include <xen/config.h>
25 #include <xen/init.h>
26 #include <xen/types.h>
27 #include <xen/lib.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/domain_page.h>
34 #include <xen/keyhandler.h>
35 #include <xen/perfc.h>
36 #include <xen/numa.h>
37 #include <xen/nodemask.h>
38 #include <asm/page.h>
39 #include <asm/flushtlb.h>
41 /*
42 * Comma-separated list of hexadecimal page numbers containing bad bytes.
43 * e.g. 'badpage=0x3f45,0x8a321'.
44 */
45 static char opt_badpage[100] = "";
46 string_param("badpage", opt_badpage);
48 /*
49 * no-bootscrub -> Free pages are not zeroed during boot.
50 */
51 static int opt_bootscrub __initdata = 1;
52 boolean_param("bootscrub", opt_bootscrub);
54 /*
55 * Bit width of the DMA heap.
56 */
57 static unsigned int dma_bitsize = CONFIG_DMA_BITSIZE;
58 static unsigned long max_dma_mfn = (1UL<<(CONFIG_DMA_BITSIZE-PAGE_SHIFT))-1;
59 static void __init parse_dma_bits(char *s)
60 {
61 unsigned int v = simple_strtol(s, NULL, 0);
62 if ( v >= (BITS_PER_LONG + PAGE_SHIFT) )
63 {
64 dma_bitsize = BITS_PER_LONG + PAGE_SHIFT;
65 max_dma_mfn = ~0UL;
66 }
67 else if ( v > PAGE_SHIFT + 1 )
68 {
69 dma_bitsize = v;
70 max_dma_mfn = (1UL << (dma_bitsize - PAGE_SHIFT)) - 1;
71 }
72 else
73 printk("Invalid dma_bits value of %u ignored.\n", v);
74 }
75 custom_param("dma_bits", parse_dma_bits);
77 /*
78 * Amount of memory to reserve in a low-memory (<4GB) pool for specific
79 * allocation requests. Ordinary requests will not fall back to the
80 * lowmem emergency pool.
81 */
82 static unsigned long dma_emergency_pool_pages;
83 static void __init parse_dma_emergency_pool(char *s)
84 {
85 unsigned long long bytes;
86 bytes = parse_size_and_unit(s, NULL);
87 dma_emergency_pool_pages = bytes >> PAGE_SHIFT;
88 }
89 custom_param("dma_emergency_pool", parse_dma_emergency_pool);
91 #define round_pgdown(_p) ((_p)&PAGE_MASK)
92 #define round_pgup(_p) (((_p)+(PAGE_SIZE-1))&PAGE_MASK)
94 static DEFINE_SPINLOCK(page_scrub_lock);
95 LIST_HEAD(page_scrub_list);
96 static unsigned long scrub_pages;
98 /*********************
99 * ALLOCATION BITMAP
100 * One bit per page of memory. Bit set => page is allocated.
101 */
103 static unsigned long *alloc_bitmap;
104 #define PAGES_PER_MAPWORD (sizeof(unsigned long) * 8)
106 #define allocated_in_map(_pn) \
107 ({ unsigned long ___pn = (_pn); \
108 !!(alloc_bitmap[___pn/PAGES_PER_MAPWORD] & \
109 (1UL<<(___pn&(PAGES_PER_MAPWORD-1)))); })
111 /*
112 * Hint regarding bitwise arithmetic in map_{alloc,free}:
113 * -(1<<n) sets all bits >= n.
114 * (1<<n)-1 sets all bits < n.
115 * Variable names in map_{alloc,free}:
116 * *_idx == Index into `alloc_bitmap' array.
117 * *_off == Bit offset within an element of the `alloc_bitmap' array.
118 */
120 static void map_alloc(unsigned long first_page, unsigned long nr_pages)
121 {
122 unsigned long start_off, end_off, curr_idx, end_idx;
124 #ifndef NDEBUG
125 unsigned long i;
126 /* Check that the block isn't already allocated. */
127 for ( i = 0; i < nr_pages; i++ )
128 ASSERT(!allocated_in_map(first_page + i));
129 #endif
131 curr_idx = first_page / PAGES_PER_MAPWORD;
132 start_off = first_page & (PAGES_PER_MAPWORD-1);
133 end_idx = (first_page + nr_pages) / PAGES_PER_MAPWORD;
134 end_off = (first_page + nr_pages) & (PAGES_PER_MAPWORD-1);
136 if ( curr_idx == end_idx )
137 {
138 alloc_bitmap[curr_idx] |= ((1UL<<end_off)-1) & -(1UL<<start_off);
139 }
140 else
141 {
142 alloc_bitmap[curr_idx] |= -(1UL<<start_off);
143 while ( ++curr_idx < end_idx ) alloc_bitmap[curr_idx] = ~0UL;
144 alloc_bitmap[curr_idx] |= (1UL<<end_off)-1;
145 }
146 }
148 static void map_free(unsigned long first_page, unsigned long nr_pages)
149 {
150 unsigned long start_off, end_off, curr_idx, end_idx;
152 #ifndef NDEBUG
153 unsigned long i;
154 /* Check that the block isn't already freed. */
155 for ( i = 0; i < nr_pages; i++ )
156 ASSERT(allocated_in_map(first_page + i));
157 #endif
159 curr_idx = first_page / PAGES_PER_MAPWORD;
160 start_off = first_page & (PAGES_PER_MAPWORD-1);
161 end_idx = (first_page + nr_pages) / PAGES_PER_MAPWORD;
162 end_off = (first_page + nr_pages) & (PAGES_PER_MAPWORD-1);
164 if ( curr_idx == end_idx )
165 {
166 alloc_bitmap[curr_idx] &= -(1UL<<end_off) | ((1UL<<start_off)-1);
167 }
168 else
169 {
170 alloc_bitmap[curr_idx] &= (1UL<<start_off)-1;
171 while ( ++curr_idx != end_idx ) alloc_bitmap[curr_idx] = 0;
172 alloc_bitmap[curr_idx] &= -(1UL<<end_off);
173 }
174 }
178 /*************************
179 * BOOT-TIME ALLOCATOR
180 */
182 static unsigned long first_valid_mfn = ~0UL;
184 /* Initialise allocator to handle up to @max_page pages. */
185 paddr_t __init init_boot_allocator(paddr_t bitmap_start)
186 {
187 unsigned long bitmap_size;
189 bitmap_start = round_pgup(bitmap_start);
191 /*
192 * Allocate space for the allocation bitmap. Include an extra longword
193 * of padding for possible overrun in map_alloc and map_free.
194 */
195 bitmap_size = max_page / 8;
196 bitmap_size += sizeof(unsigned long);
197 bitmap_size = round_pgup(bitmap_size);
198 alloc_bitmap = (unsigned long *)maddr_to_virt(bitmap_start);
200 /* All allocated by default. */
201 memset(alloc_bitmap, ~0, bitmap_size);
203 return bitmap_start + bitmap_size;
204 }
206 void __init init_boot_pages(paddr_t ps, paddr_t pe)
207 {
208 unsigned long bad_spfn, bad_epfn, i;
209 const char *p;
211 ps = round_pgup(ps);
212 pe = round_pgdown(pe);
213 if ( pe <= ps )
214 return;
216 first_valid_mfn = min_t(unsigned long, ps >> PAGE_SHIFT, first_valid_mfn);
218 map_free(ps >> PAGE_SHIFT, (pe - ps) >> PAGE_SHIFT);
220 /* Check new pages against the bad-page list. */
221 p = opt_badpage;
222 while ( *p != '\0' )
223 {
224 bad_spfn = simple_strtoul(p, &p, 0);
225 bad_epfn = bad_spfn;
227 if ( *p == '-' )
228 {
229 p++;
230 bad_epfn = simple_strtoul(p, &p, 0);
231 if ( bad_epfn < bad_spfn )
232 bad_epfn = bad_spfn;
233 }
235 if ( *p == ',' )
236 p++;
237 else if ( *p != '\0' )
238 break;
240 if ( bad_epfn == bad_spfn )
241 printk("Marking page %lx as bad\n", bad_spfn);
242 else
243 printk("Marking pages %lx through %lx as bad\n",
244 bad_spfn, bad_epfn);
246 for ( i = bad_spfn; i <= bad_epfn; i++ )
247 if ( (i < max_page) && !allocated_in_map(i) )
248 map_alloc(i, 1);
249 }
250 }
252 unsigned long __init alloc_boot_pages(
253 unsigned long nr_pfns, unsigned long pfn_align)
254 {
255 unsigned long pg, i;
257 /* Search backwards to obtain highest available range. */
258 for ( pg = (max_page - nr_pfns) & ~(pfn_align - 1);
259 pg >= first_valid_mfn;
260 pg = (pg + i - nr_pfns) & ~(pfn_align - 1) )
261 {
262 for ( i = 0; i < nr_pfns; i++ )
263 if ( allocated_in_map(pg+i) )
264 break;
265 if ( i == nr_pfns )
266 {
267 map_alloc(pg, nr_pfns);
268 return pg;
269 }
270 }
272 return 0;
273 }
277 /*************************
278 * BINARY BUDDY ALLOCATOR
279 */
281 #define MEMZONE_XEN 0
282 #ifdef PADDR_BITS
283 #define NR_ZONES (PADDR_BITS - PAGE_SHIFT)
284 #else
285 #define NR_ZONES (BITS_PER_LONG - PAGE_SHIFT)
286 #endif
288 #define pfn_dom_zone_type(_pfn) (fls(_pfn) - 1)
290 typedef struct list_head heap_by_zone_and_order_t[NR_ZONES][MAX_ORDER+1];
291 static heap_by_zone_and_order_t *_heap[MAX_NUMNODES];
292 #define heap(node, zone, order) ((*_heap[node])[zone][order])
294 static unsigned long *avail[MAX_NUMNODES];
296 static DEFINE_SPINLOCK(heap_lock);
298 static void init_node_heap(int node)
299 {
300 /* First node to be discovered has its heap metadata statically alloced. */
301 static heap_by_zone_and_order_t _heap_static;
302 static unsigned long avail_static[NR_ZONES];
303 static unsigned long first_node_initialised;
305 int i, j;
307 if ( !test_and_set_bit(0, &first_node_initialised) )
308 {
309 _heap[node] = &_heap_static;
310 avail[node] = avail_static;
311 }
312 else
313 {
314 _heap[node] = xmalloc(heap_by_zone_and_order_t);
315 avail[node] = xmalloc_array(unsigned long, NR_ZONES);
316 BUG_ON(!_heap[node] || !avail[node]);
317 }
319 memset(avail[node], 0, NR_ZONES * sizeof(long));
321 for ( i = 0; i < NR_ZONES; i++ )
322 for ( j = 0; j <= MAX_ORDER; j++ )
323 INIT_LIST_HEAD(&(*_heap[node])[i][j]);
324 }
326 /* Allocate 2^@order contiguous pages. */
327 static struct page_info *alloc_heap_pages(
328 unsigned int zone_lo, unsigned int zone_hi,
329 unsigned int cpu, unsigned int order)
330 {
331 unsigned int i, j, zone;
332 unsigned int node = cpu_to_node(cpu), num_nodes = num_online_nodes();
333 unsigned long request = 1UL << order;
334 cpumask_t extra_cpus_mask, mask;
335 struct page_info *pg;
337 ASSERT(node >= 0);
338 ASSERT(node < num_nodes);
339 ASSERT(zone_lo <= zone_hi);
340 ASSERT(zone_hi < NR_ZONES);
342 if ( unlikely(order > MAX_ORDER) )
343 return NULL;
345 spin_lock(&heap_lock);
347 /*
348 * Start with requested node, but exhaust all node memory in requested
349 * zone before failing, only calc new node value if we fail to find memory
350 * in target node, this avoids needless computation on fast-path.
351 */
352 for ( i = 0; i < num_nodes; i++ )
353 {
354 zone = zone_hi;
355 do {
356 /* Check if target node can support the allocation. */
357 if ( !avail[node] || (avail[node][zone] < request) )
358 continue;
360 /* Find smallest order which can satisfy the request. */
361 for ( j = order; j <= MAX_ORDER; j++ )
362 if ( !list_empty(&heap(node, zone, j)) )
363 goto found;
364 } while ( zone-- > zone_lo ); /* careful: unsigned zone may wrap */
366 /* Pick next node, wrapping around if needed. */
367 if ( ++node == num_nodes )
368 node = 0;
369 }
371 /* No suitable memory blocks. Fail the request. */
372 spin_unlock(&heap_lock);
373 return NULL;
375 found:
376 pg = list_entry(heap(node, zone, j).next, struct page_info, list);
377 list_del(&pg->list);
379 /* We may have to halve the chunk a number of times. */
380 while ( j != order )
381 {
382 PFN_ORDER(pg) = --j;
383 list_add_tail(&pg->list, &heap(node, zone, j));
384 pg += 1 << j;
385 }
387 map_alloc(page_to_mfn(pg), request);
388 ASSERT(avail[node][zone] >= request);
389 avail[node][zone] -= request;
391 spin_unlock(&heap_lock);
393 cpus_clear(mask);
395 for ( i = 0; i < (1 << order); i++ )
396 {
397 /* Reference count must continuously be zero for free pages. */
398 BUG_ON(pg[i].count_info != 0);
400 /* Add in any extra CPUs that need flushing because of this page. */
401 cpus_andnot(extra_cpus_mask, pg[i].u.free.cpumask, mask);
402 tlbflush_filter(extra_cpus_mask, pg[i].tlbflush_timestamp);
403 cpus_or(mask, mask, extra_cpus_mask);
405 /* Initialise fields which have other uses for free pages. */
406 pg[i].u.inuse.type_info = 0;
407 page_set_owner(&pg[i], NULL);
408 }
410 if ( unlikely(!cpus_empty(mask)) )
411 {
412 perfc_incr(need_flush_tlb_flush);
413 flush_tlb_mask(mask);
414 }
416 return pg;
417 }
419 /* Free 2^@order set of pages. */
420 static void free_heap_pages(
421 unsigned int zone, struct page_info *pg, unsigned int order)
422 {
423 unsigned long mask;
424 unsigned int i, node = phys_to_nid(page_to_maddr(pg));
425 struct domain *d;
427 ASSERT(zone < NR_ZONES);
428 ASSERT(order <= MAX_ORDER);
429 ASSERT(node >= 0);
430 ASSERT(node < num_online_nodes());
432 for ( i = 0; i < (1 << order); i++ )
433 {
434 /*
435 * Cannot assume that count_info == 0, as there are some corner cases
436 * where it isn't the case and yet it isn't a bug:
437 * 1. page_get_owner() is NULL
438 * 2. page_get_owner() is a domain that was never accessible by
439 * its domid (e.g., failed to fully construct the domain).
440 * 3. page was never addressable by the guest (e.g., it's an
441 * auto-translate-physmap guest and the page was never included
442 * in its pseudophysical address space).
443 * In all the above cases there can be no guest mappings of this page.
444 */
445 pg[i].count_info = 0;
447 if ( (d = page_get_owner(&pg[i])) != NULL )
448 {
449 pg[i].tlbflush_timestamp = tlbflush_current_time();
450 pg[i].u.free.cpumask = d->domain_dirty_cpumask;
451 }
452 else
453 {
454 cpus_clear(pg[i].u.free.cpumask);
455 }
456 }
458 spin_lock(&heap_lock);
460 map_free(page_to_mfn(pg), 1 << order);
461 avail[node][zone] += 1 << order;
463 /* Merge chunks as far as possible. */
464 while ( order < MAX_ORDER )
465 {
466 mask = 1UL << order;
468 if ( (page_to_mfn(pg) & mask) )
469 {
470 /* Merge with predecessor block? */
471 if ( allocated_in_map(page_to_mfn(pg)-mask) ||
472 (PFN_ORDER(pg-mask) != order) )
473 break;
474 list_del(&(pg-mask)->list);
475 pg -= mask;
476 }
477 else
478 {
479 /* Merge with successor block? */
480 if ( allocated_in_map(page_to_mfn(pg)+mask) ||
481 (PFN_ORDER(pg+mask) != order) )
482 break;
483 list_del(&(pg+mask)->list);
484 }
486 order++;
488 /* After merging, pg should remain in the same node. */
489 ASSERT(phys_to_nid(page_to_maddr(pg)) == node);
490 }
492 PFN_ORDER(pg) = order;
493 list_add_tail(&pg->list, &heap(node, zone, order));
495 spin_unlock(&heap_lock);
496 }
498 /*
499 * Hand the specified arbitrary page range to the specified heap zone
500 * checking the node_id of the previous page. If they differ and the
501 * latter is not on a MAX_ORDER boundary, then we reserve the page by
502 * not freeing it to the buddy allocator.
503 */
504 #define MAX_ORDER_ALIGNED (1UL << (MAX_ORDER))
505 void init_heap_pages(
506 unsigned int zone, struct page_info *pg, unsigned long nr_pages)
507 {
508 unsigned int nid_curr, nid_prev;
509 unsigned long i;
511 ASSERT(zone < NR_ZONES);
513 if ( likely(page_to_mfn(pg) != 0) )
514 nid_prev = phys_to_nid(page_to_maddr(pg-1));
515 else
516 nid_prev = phys_to_nid(page_to_maddr(pg));
518 for ( i = 0; i < nr_pages; i++ )
519 {
520 nid_curr = phys_to_nid(page_to_maddr(pg+i));
522 if ( unlikely(!avail[nid_curr]) )
523 init_node_heap(nid_curr);
525 /*
526 * free pages of the same node, or if they differ, but are on a
527 * MAX_ORDER alignement boundary (which already get reserved)
528 */
529 if ( (nid_curr == nid_prev) || (page_to_maddr(pg+i) &
530 MAX_ORDER_ALIGNED) )
531 free_heap_pages(zone, pg+i, 0);
532 else
533 printk("Reserving non-aligned node boundary @ mfn %lu\n",
534 page_to_mfn(pg+i));
536 nid_prev = nid_curr;
537 }
538 }
540 static unsigned long avail_heap_pages(
541 unsigned int zone_lo, unsigned int zone_hi, unsigned int node)
542 {
543 unsigned int i, zone, num_nodes = num_online_nodes();
544 unsigned long free_pages = 0;
546 if ( zone_hi >= NR_ZONES )
547 zone_hi = NR_ZONES - 1;
549 for ( i = 0; i < num_nodes; i++ )
550 {
551 if ( !avail[i] )
552 continue;
553 for ( zone = zone_lo; zone <= zone_hi; zone++ )
554 if ( (node == -1) || (node == i) )
555 free_pages += avail[i][zone];
556 }
558 return free_pages;
559 }
561 #define avail_for_domheap(mfn) \
562 (!allocated_in_map(mfn) && !is_xen_heap_frame(mfn_to_page(mfn)))
563 void __init end_boot_allocator(void)
564 {
565 unsigned long i;
566 int curr_free, next_free;
568 /* Pages that are free now go to the domain sub-allocator. */
569 if ( (curr_free = next_free = avail_for_domheap(first_valid_mfn)) )
570 map_alloc(first_valid_mfn, 1);
571 for ( i = first_valid_mfn; i < max_page; i++ )
572 {
573 curr_free = next_free;
574 next_free = avail_for_domheap(i+1);
575 if ( next_free )
576 map_alloc(i+1, 1); /* prevent merging in free_heap_pages() */
577 if ( curr_free )
578 init_heap_pages(pfn_dom_zone_type(i), mfn_to_page(i), 1);
579 }
581 printk("Domain heap initialised: DMA width %u bits\n", dma_bitsize);
582 }
583 #undef avail_for_domheap
585 /*
586 * Scrub all unallocated pages in all heap zones. This function is more
587 * convoluted than appears necessary because we do not want to continuously
588 * hold the lock while scrubbing very large memory areas.
589 */
590 void __init scrub_heap_pages(void)
591 {
592 void *p;
593 unsigned long mfn;
595 if ( !opt_bootscrub )
596 return;
598 printk("Scrubbing Free RAM: ");
600 for ( mfn = first_valid_mfn; mfn < max_page; mfn++ )
601 {
602 process_pending_timers();
604 /* Quick lock-free check. */
605 if ( allocated_in_map(mfn) )
606 continue;
608 /* Every 100MB, print a progress dot. */
609 if ( (mfn % ((100*1024*1024)/PAGE_SIZE)) == 0 )
610 printk(".");
612 spin_lock(&heap_lock);
614 /* Re-check page status with lock held. */
615 if ( !allocated_in_map(mfn) )
616 {
617 if ( is_xen_heap_frame(mfn_to_page(mfn)) )
618 {
619 p = page_to_virt(mfn_to_page(mfn));
620 memguard_unguard_range(p, PAGE_SIZE);
621 clear_page(p);
622 memguard_guard_range(p, PAGE_SIZE);
623 }
624 else
625 {
626 p = map_domain_page(mfn);
627 clear_page(p);
628 unmap_domain_page(p);
629 }
630 }
632 spin_unlock(&heap_lock);
633 }
635 printk("done.\n");
636 }
640 /*************************
641 * XEN-HEAP SUB-ALLOCATOR
642 */
644 void init_xenheap_pages(paddr_t ps, paddr_t pe)
645 {
646 ps = round_pgup(ps);
647 pe = round_pgdown(pe);
648 if ( pe <= ps )
649 return;
651 memguard_guard_range(maddr_to_virt(ps), pe - ps);
653 /*
654 * Yuk! Ensure there is a one-page buffer between Xen and Dom zones, to
655 * prevent merging of power-of-two blocks across the zone boundary.
656 */
657 if ( ps && !is_xen_heap_frame(maddr_to_page(ps)-1) )
658 ps += PAGE_SIZE;
659 if ( !is_xen_heap_frame(maddr_to_page(pe)) )
660 pe -= PAGE_SIZE;
662 init_heap_pages(MEMZONE_XEN, maddr_to_page(ps), (pe - ps) >> PAGE_SHIFT);
663 }
666 void *alloc_xenheap_pages(unsigned int order)
667 {
668 struct page_info *pg;
670 ASSERT(!in_irq());
672 pg = alloc_heap_pages(MEMZONE_XEN, MEMZONE_XEN, smp_processor_id(), order);
673 if ( unlikely(pg == NULL) )
674 goto no_memory;
676 memguard_unguard_range(page_to_virt(pg), 1 << (order + PAGE_SHIFT));
678 return page_to_virt(pg);
680 no_memory:
681 printk("Cannot handle page request order %d!\n", order);
682 return NULL;
683 }
686 void free_xenheap_pages(void *v, unsigned int order)
687 {
688 ASSERT(!in_irq());
690 if ( v == NULL )
691 return;
693 memguard_guard_range(v, 1 << (order + PAGE_SHIFT));
695 free_heap_pages(MEMZONE_XEN, virt_to_page(v), order);
696 }
700 /*************************
701 * DOMAIN-HEAP SUB-ALLOCATOR
702 */
704 void init_domheap_pages(paddr_t ps, paddr_t pe)
705 {
706 unsigned long s_tot, e_tot;
707 unsigned int zone;
709 ASSERT(!in_irq());
711 s_tot = round_pgup(ps) >> PAGE_SHIFT;
712 e_tot = round_pgdown(pe) >> PAGE_SHIFT;
714 zone = fls(s_tot);
715 BUG_ON(zone <= MEMZONE_XEN + 1);
716 for ( --zone; s_tot < e_tot; ++zone )
717 {
718 unsigned long end = e_tot;
720 BUILD_BUG_ON(NR_ZONES > BITS_PER_LONG);
721 if ( zone < BITS_PER_LONG - 1 && end > 1UL << (zone + 1) )
722 end = 1UL << (zone + 1);
723 init_heap_pages(zone, mfn_to_page(s_tot), end - s_tot);
724 s_tot = end;
725 }
726 }
729 int assign_pages(
730 struct domain *d,
731 struct page_info *pg,
732 unsigned int order,
733 unsigned int memflags)
734 {
735 unsigned long i;
737 spin_lock(&d->page_alloc_lock);
739 if ( unlikely(d->is_dying) )
740 {
741 gdprintk(XENLOG_INFO, "Cannot assign page to domain%d -- dying.\n",
742 d->domain_id);
743 goto fail;
744 }
746 if ( !(memflags & MEMF_no_refcount) )
747 {
748 if ( unlikely((d->tot_pages + (1 << order)) > d->max_pages) )
749 {
750 gdprintk(XENLOG_INFO, "Over-allocation for domain %u: %u > %u\n",
751 d->domain_id, d->tot_pages + (1 << order), d->max_pages);
752 goto fail;
753 }
755 if ( unlikely(d->tot_pages == 0) )
756 get_knownalive_domain(d);
758 d->tot_pages += 1 << order;
759 }
761 for ( i = 0; i < (1 << order); i++ )
762 {
763 ASSERT(page_get_owner(&pg[i]) == NULL);
764 ASSERT((pg[i].count_info & ~(PGC_allocated | 1)) == 0);
765 page_set_owner(&pg[i], d);
766 wmb(); /* Domain pointer must be visible before updating refcnt. */
767 pg[i].count_info = PGC_allocated | 1;
768 list_add_tail(&pg[i].list, &d->page_list);
769 }
771 spin_unlock(&d->page_alloc_lock);
772 return 0;
774 fail:
775 spin_unlock(&d->page_alloc_lock);
776 return -1;
777 }
780 struct page_info *__alloc_domheap_pages(
781 struct domain *d, unsigned int cpu, unsigned int order,
782 unsigned int memflags)
783 {
784 struct page_info *pg = NULL;
785 unsigned int bits = memflags >> _MEMF_bits, zone_hi = NR_ZONES - 1;
787 ASSERT(!in_irq());
789 if ( bits )
790 {
791 bits = domain_clamp_alloc_bitsize(d, bits);
792 if ( bits <= (PAGE_SHIFT + 1) )
793 return NULL;
794 bits -= PAGE_SHIFT + 1;
795 if ( bits < zone_hi )
796 zone_hi = bits;
797 }
799 if ( (zone_hi + PAGE_SHIFT) >= dma_bitsize )
800 {
801 pg = alloc_heap_pages(dma_bitsize - PAGE_SHIFT, zone_hi, cpu, order);
803 /* Failure? Then check if we can fall back to the DMA pool. */
804 if ( unlikely(pg == NULL) &&
805 ((order > MAX_ORDER) ||
806 (avail_heap_pages(MEMZONE_XEN + 1,
807 dma_bitsize - PAGE_SHIFT - 1,
808 -1) <
809 (dma_emergency_pool_pages + (1UL << order)))) )
810 return NULL;
811 }
813 if ( (pg == NULL) &&
814 ((pg = alloc_heap_pages(MEMZONE_XEN + 1, zone_hi,
815 cpu, order)) == NULL) )
816 return NULL;
818 if ( (d != NULL) && assign_pages(d, pg, order, memflags) )
819 {
820 free_heap_pages(pfn_dom_zone_type(page_to_mfn(pg)), pg, order);
821 return NULL;
822 }
824 return pg;
825 }
827 struct page_info *alloc_domheap_pages(
828 struct domain *d, unsigned int order, unsigned int flags)
829 {
830 return __alloc_domheap_pages(d, smp_processor_id(), order, flags);
831 }
833 void free_domheap_pages(struct page_info *pg, unsigned int order)
834 {
835 int i, drop_dom_ref;
836 struct domain *d = page_get_owner(pg);
838 ASSERT(!in_irq());
840 if ( unlikely(is_xen_heap_frame(pg)) )
841 {
842 /* NB. May recursively lock from relinquish_memory(). */
843 spin_lock_recursive(&d->page_alloc_lock);
845 for ( i = 0; i < (1 << order); i++ )
846 list_del(&pg[i].list);
848 d->xenheap_pages -= 1 << order;
849 drop_dom_ref = (d->xenheap_pages == 0);
851 spin_unlock_recursive(&d->page_alloc_lock);
852 }
853 else if ( likely(d != NULL) )
854 {
855 /* NB. May recursively lock from relinquish_memory(). */
856 spin_lock_recursive(&d->page_alloc_lock);
858 for ( i = 0; i < (1 << order); i++ )
859 {
860 BUG_ON((pg[i].u.inuse.type_info & PGT_count_mask) != 0);
861 list_del(&pg[i].list);
862 }
864 d->tot_pages -= 1 << order;
865 drop_dom_ref = (d->tot_pages == 0);
867 spin_unlock_recursive(&d->page_alloc_lock);
869 if ( likely(!d->is_dying) )
870 {
871 free_heap_pages(pfn_dom_zone_type(page_to_mfn(pg)), pg, order);
872 }
873 else
874 {
875 /*
876 * Normally we expect a domain to clear pages before freeing them,
877 * if it cares about the secrecy of their contents. However, after
878 * a domain has died we assume responsibility for erasure.
879 */
880 for ( i = 0; i < (1 << order); i++ )
881 {
882 page_set_owner(&pg[i], NULL);
883 spin_lock(&page_scrub_lock);
884 list_add(&pg[i].list, &page_scrub_list);
885 scrub_pages++;
886 spin_unlock(&page_scrub_lock);
887 }
888 }
889 }
890 else
891 {
892 /* Freeing anonymous domain-heap pages. */
893 free_heap_pages(pfn_dom_zone_type(page_to_mfn(pg)), pg, order);
894 drop_dom_ref = 0;
895 }
897 if ( drop_dom_ref )
898 put_domain(d);
899 }
901 unsigned long avail_domheap_pages_region(
902 unsigned int node, unsigned int min_width, unsigned int max_width)
903 {
904 int zone_lo, zone_hi;
906 zone_lo = min_width ? (min_width - (PAGE_SHIFT + 1)) : (MEMZONE_XEN + 1);
907 zone_lo = max_t(int, MEMZONE_XEN + 1, zone_lo);
908 zone_lo = min_t(int, NR_ZONES - 1, zone_lo);
910 zone_hi = max_width ? (max_width - (PAGE_SHIFT + 1)) : (NR_ZONES - 1);
911 zone_hi = max_t(int, MEMZONE_XEN + 1, zone_hi);
912 zone_hi = min_t(int, NR_ZONES - 1, zone_hi);
914 return avail_heap_pages(zone_lo, zone_hi, node);
915 }
917 unsigned long avail_domheap_pages(void)
918 {
919 unsigned long avail_nrm, avail_dma;
921 avail_nrm = avail_heap_pages(dma_bitsize - PAGE_SHIFT,
922 NR_ZONES - 1,
923 -1);
925 avail_dma = avail_heap_pages(MEMZONE_XEN + 1,
926 dma_bitsize - PAGE_SHIFT - 1,
927 -1);
929 if ( avail_dma > dma_emergency_pool_pages )
930 avail_dma -= dma_emergency_pool_pages;
931 else
932 avail_dma = 0;
934 return avail_nrm + avail_dma;
935 }
937 static void pagealloc_keyhandler(unsigned char key)
938 {
939 unsigned int zone = MEMZONE_XEN;
940 unsigned long total = 0;
942 printk("Physical memory information:\n");
943 printk(" Xen heap: %lukB free\n",
944 avail_heap_pages(zone, zone, -1) << (PAGE_SHIFT-10));
946 while ( ++zone < NR_ZONES )
947 {
948 unsigned long n;
950 if ( zone == dma_bitsize - PAGE_SHIFT )
951 {
952 printk(" DMA heap: %lukB free\n", total << (PAGE_SHIFT-10));
953 total = 0;
954 }
956 if ( (n = avail_heap_pages(zone, zone, -1)) != 0 )
957 {
958 total += n;
959 printk(" heap[%02u]: %lukB free\n", zone, n << (PAGE_SHIFT-10));
960 }
961 }
963 printk(" Dom heap: %lukB free\n", total << (PAGE_SHIFT-10));
964 }
967 static __init int pagealloc_keyhandler_init(void)
968 {
969 register_keyhandler('m', pagealloc_keyhandler, "memory info");
970 return 0;
971 }
972 __initcall(pagealloc_keyhandler_init);
976 /*************************
977 * PAGE SCRUBBING
978 */
980 static DEFINE_PER_CPU(struct timer, page_scrub_timer);
982 static void page_scrub_softirq(void)
983 {
984 struct list_head *ent;
985 struct page_info *pg;
986 void *p;
987 int i;
988 s_time_t start = NOW();
990 /* Aim to do 1ms of work every 10ms. */
991 do {
992 spin_lock(&page_scrub_lock);
994 if ( unlikely((ent = page_scrub_list.next) == &page_scrub_list) )
995 {
996 spin_unlock(&page_scrub_lock);
997 return;
998 }
1000 /* Peel up to 16 pages from the list. */
1001 for ( i = 0; i < 16; i++ )
1003 if ( ent->next == &page_scrub_list )
1004 break;
1005 ent = ent->next;
1008 /* Remove peeled pages from the list. */
1009 ent->next->prev = &page_scrub_list;
1010 page_scrub_list.next = ent->next;
1011 scrub_pages -= (i+1);
1013 spin_unlock(&page_scrub_lock);
1015 /* Working backwards, scrub each page in turn. */
1016 while ( ent != &page_scrub_list )
1018 pg = list_entry(ent, struct page_info, list);
1019 ent = ent->prev;
1020 p = map_domain_page(page_to_mfn(pg));
1021 clear_page(p);
1022 unmap_domain_page(p);
1023 free_heap_pages(pfn_dom_zone_type(page_to_mfn(pg)), pg, 0);
1025 } while ( (NOW() - start) < MILLISECS(1) );
1027 set_timer(&this_cpu(page_scrub_timer), NOW() + MILLISECS(10));
1030 static void page_scrub_timer_fn(void *unused)
1032 page_scrub_schedule_work();
1035 unsigned long avail_scrub_pages(void)
1037 return scrub_pages;
1040 static void dump_heap(unsigned char key)
1042 s_time_t now = NOW();
1043 int i, j;
1045 printk("'%c' pressed -> dumping heap info (now-0x%X:%08X)\n", key,
1046 (u32)(now>>32), (u32)now);
1048 for ( i = 0; i < MAX_NUMNODES; i++ )
1050 if ( !avail[i] )
1051 continue;
1052 for ( j = 0; j < NR_ZONES; j++ )
1053 printk("heap[node=%d][zone=%d] -> %lu pages\n",
1054 i, j, avail[i][j]);
1058 static __init int register_heap_trigger(void)
1060 register_keyhandler('H', dump_heap, "dump heap info");
1061 return 0;
1063 __initcall(register_heap_trigger);
1066 static __init int page_scrub_init(void)
1068 int cpu;
1069 for_each_cpu ( cpu )
1070 init_timer(&per_cpu(page_scrub_timer, cpu),
1071 page_scrub_timer_fn, NULL, cpu);
1072 open_softirq(PAGE_SCRUB_SOFTIRQ, page_scrub_softirq);
1073 return 0;
1075 __initcall(page_scrub_init);
1077 /*
1078 * Local variables:
1079 * mode: C
1080 * c-set-style: "BSD"
1081 * c-basic-offset: 4
1082 * tab-width: 4
1083 * indent-tabs-mode: nil
1084 * End:
1085 */