ia64/xen-unstable

view xen/common/page_alloc.c @ 18594:5e4e234d58be

x86: Define __per_cpu_shift label to help kdump/crashdump.
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Oct 08 13:11:06 2008 +0100 (2008-10-08)
parents 32aa43364f5d
children 845aa241e163
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/numa.h>
40 #include <asm/flushtlb.h>
42 /*
43 * Comma-separated list of hexadecimal page numbers containing bad bytes.
44 * e.g. 'badpage=0x3f45,0x8a321'.
45 */
46 static char opt_badpage[100] = "";
47 string_param("badpage", opt_badpage);
49 /*
50 * no-bootscrub -> Free pages are not zeroed during boot.
51 */
52 static int opt_bootscrub __initdata = 1;
53 boolean_param("bootscrub", opt_bootscrub);
55 /*
56 * Bit width of the DMA heap -- used to override NUMA-node-first.
57 * allocation strategy, which can otherwise exhaust low memory.
58 */
59 static unsigned int dma_bitsize;
60 integer_param("dma_bits", dma_bitsize);
62 #define round_pgdown(_p) ((_p)&PAGE_MASK)
63 #define round_pgup(_p) (((_p)+(PAGE_SIZE-1))&PAGE_MASK)
65 #ifndef NDEBUG
66 /* Avoid callers relying on allocations returning zeroed pages. */
67 #define scrub_page(p) memset((p), 0xc2, PAGE_SIZE)
68 #else
69 /* For a production build, clear_page() is the fastest way to scrub. */
70 #define scrub_page(p) clear_page(p)
71 #endif
73 static DEFINE_SPINLOCK(page_scrub_lock);
74 LIST_HEAD(page_scrub_list);
75 static unsigned long scrub_pages;
77 /*********************
78 * ALLOCATION BITMAP
79 * One bit per page of memory. Bit set => page is allocated.
80 */
82 unsigned long *alloc_bitmap;
83 #define PAGES_PER_MAPWORD (sizeof(unsigned long) * 8)
85 #define allocated_in_map(_pn) \
86 ({ unsigned long ___pn = (_pn); \
87 !!(alloc_bitmap[___pn/PAGES_PER_MAPWORD] & \
88 (1UL<<(___pn&(PAGES_PER_MAPWORD-1)))); })
90 /*
91 * Hint regarding bitwise arithmetic in map_{alloc,free}:
92 * -(1<<n) sets all bits >= n.
93 * (1<<n)-1 sets all bits < n.
94 * Variable names in map_{alloc,free}:
95 * *_idx == Index into `alloc_bitmap' array.
96 * *_off == Bit offset within an element of the `alloc_bitmap' array.
97 */
99 static void map_alloc(unsigned long first_page, unsigned long nr_pages)
100 {
101 unsigned long start_off, end_off, curr_idx, end_idx;
103 #ifndef NDEBUG
104 unsigned long i;
105 /* Check that the block isn't already allocated. */
106 for ( i = 0; i < nr_pages; i++ )
107 ASSERT(!allocated_in_map(first_page + i));
108 #endif
110 curr_idx = first_page / PAGES_PER_MAPWORD;
111 start_off = first_page & (PAGES_PER_MAPWORD-1);
112 end_idx = (first_page + nr_pages) / PAGES_PER_MAPWORD;
113 end_off = (first_page + nr_pages) & (PAGES_PER_MAPWORD-1);
115 if ( curr_idx == end_idx )
116 {
117 alloc_bitmap[curr_idx] |= ((1UL<<end_off)-1) & -(1UL<<start_off);
118 }
119 else
120 {
121 alloc_bitmap[curr_idx] |= -(1UL<<start_off);
122 while ( ++curr_idx < end_idx ) alloc_bitmap[curr_idx] = ~0UL;
123 alloc_bitmap[curr_idx] |= (1UL<<end_off)-1;
124 }
125 }
127 static void map_free(unsigned long first_page, unsigned long nr_pages)
128 {
129 unsigned long start_off, end_off, curr_idx, end_idx;
131 #ifndef NDEBUG
132 unsigned long i;
133 /* Check that the block isn't already freed. */
134 for ( i = 0; i < nr_pages; i++ )
135 ASSERT(allocated_in_map(first_page + i));
136 #endif
138 curr_idx = first_page / PAGES_PER_MAPWORD;
139 start_off = first_page & (PAGES_PER_MAPWORD-1);
140 end_idx = (first_page + nr_pages) / PAGES_PER_MAPWORD;
141 end_off = (first_page + nr_pages) & (PAGES_PER_MAPWORD-1);
143 if ( curr_idx == end_idx )
144 {
145 alloc_bitmap[curr_idx] &= -(1UL<<end_off) | ((1UL<<start_off)-1);
146 }
147 else
148 {
149 alloc_bitmap[curr_idx] &= (1UL<<start_off)-1;
150 while ( ++curr_idx != end_idx ) alloc_bitmap[curr_idx] = 0;
151 alloc_bitmap[curr_idx] &= -(1UL<<end_off);
152 }
153 }
157 /*************************
158 * BOOT-TIME ALLOCATOR
159 */
161 static unsigned long first_valid_mfn = ~0UL;
163 /* Initialise allocator to handle up to @max_page pages. */
164 paddr_t __init init_boot_allocator(paddr_t bitmap_start)
165 {
166 unsigned long bitmap_size;
168 bitmap_start = round_pgup(bitmap_start);
170 /*
171 * Allocate space for the allocation bitmap. Include an extra longword
172 * of padding for possible overrun in map_alloc and map_free.
173 */
174 bitmap_size = max_page / 8;
175 bitmap_size += sizeof(unsigned long);
176 bitmap_size = round_pgup(bitmap_size);
177 alloc_bitmap = (unsigned long *)maddr_to_virt(bitmap_start);
179 /* All allocated by default. */
180 memset(alloc_bitmap, ~0, bitmap_size);
182 return bitmap_start + bitmap_size;
183 }
185 void __init init_boot_pages(paddr_t ps, paddr_t pe)
186 {
187 unsigned long bad_spfn, bad_epfn, i;
188 const char *p;
190 ps = round_pgup(ps);
191 pe = round_pgdown(pe);
192 if ( pe <= ps )
193 return;
195 first_valid_mfn = min_t(unsigned long, ps >> PAGE_SHIFT, first_valid_mfn);
197 map_free(ps >> PAGE_SHIFT, (pe - ps) >> PAGE_SHIFT);
199 /* Check new pages against the bad-page list. */
200 p = opt_badpage;
201 while ( *p != '\0' )
202 {
203 bad_spfn = simple_strtoul(p, &p, 0);
204 bad_epfn = bad_spfn;
206 if ( *p == '-' )
207 {
208 p++;
209 bad_epfn = simple_strtoul(p, &p, 0);
210 if ( bad_epfn < bad_spfn )
211 bad_epfn = bad_spfn;
212 }
214 if ( *p == ',' )
215 p++;
216 else if ( *p != '\0' )
217 break;
219 if ( bad_epfn == bad_spfn )
220 printk("Marking page %lx as bad\n", bad_spfn);
221 else
222 printk("Marking pages %lx through %lx as bad\n",
223 bad_spfn, bad_epfn);
225 for ( i = bad_spfn; i <= bad_epfn; i++ )
226 if ( (i < max_page) && !allocated_in_map(i) )
227 map_alloc(i, 1);
228 }
229 }
231 unsigned long __init alloc_boot_pages(
232 unsigned long nr_pfns, unsigned long pfn_align)
233 {
234 unsigned long pg, i;
236 /* Search backwards to obtain highest available range. */
237 for ( pg = (max_page - nr_pfns) & ~(pfn_align - 1);
238 pg >= first_valid_mfn;
239 pg = (pg + i - nr_pfns) & ~(pfn_align - 1) )
240 {
241 for ( i = 0; i < nr_pfns; i++ )
242 if ( allocated_in_map(pg+i) )
243 break;
244 if ( i == nr_pfns )
245 {
246 map_alloc(pg, nr_pfns);
247 return pg;
248 }
249 }
251 return 0;
252 }
256 /*************************
257 * BINARY BUDDY ALLOCATOR
258 */
260 #define MEMZONE_XEN 0
261 #define NR_ZONES (PADDR_BITS - PAGE_SHIFT)
263 #define pfn_dom_zone_type(_pfn) (fls(_pfn) - 1)
265 typedef struct list_head heap_by_zone_and_order_t[NR_ZONES][MAX_ORDER+1];
266 static heap_by_zone_and_order_t *_heap[MAX_NUMNODES];
267 #define heap(node, zone, order) ((*_heap[node])[zone][order])
269 static unsigned long *avail[MAX_NUMNODES];
271 static DEFINE_SPINLOCK(heap_lock);
273 static void init_node_heap(int node)
274 {
275 /* First node to be discovered has its heap metadata statically alloced. */
276 static heap_by_zone_and_order_t _heap_static;
277 static unsigned long avail_static[NR_ZONES];
278 static int first_node_initialised;
280 int i, j;
282 if ( !first_node_initialised )
283 {
284 _heap[node] = &_heap_static;
285 avail[node] = avail_static;
286 first_node_initialised = 1;
287 }
288 else
289 {
290 _heap[node] = xmalloc(heap_by_zone_and_order_t);
291 avail[node] = xmalloc_array(unsigned long, NR_ZONES);
292 BUG_ON(!_heap[node] || !avail[node]);
293 }
295 memset(avail[node], 0, NR_ZONES * sizeof(long));
297 for ( i = 0; i < NR_ZONES; i++ )
298 for ( j = 0; j <= MAX_ORDER; j++ )
299 INIT_LIST_HEAD(&(*_heap[node])[i][j]);
300 }
302 /* Allocate 2^@order contiguous pages. */
303 static struct page_info *alloc_heap_pages(
304 unsigned int zone_lo, unsigned int zone_hi,
305 unsigned int node, unsigned int order)
306 {
307 unsigned int i, j, zone;
308 unsigned int num_nodes = num_online_nodes();
309 unsigned long request = 1UL << order;
310 cpumask_t extra_cpus_mask, mask;
311 struct page_info *pg;
313 if ( node == NUMA_NO_NODE )
314 node = cpu_to_node(smp_processor_id());
316 ASSERT(node >= 0);
317 ASSERT(node < num_nodes);
318 ASSERT(zone_lo <= zone_hi);
319 ASSERT(zone_hi < NR_ZONES);
321 if ( unlikely(order > MAX_ORDER) )
322 return NULL;
324 spin_lock(&heap_lock);
326 /*
327 * Start with requested node, but exhaust all node memory in requested
328 * zone before failing, only calc new node value if we fail to find memory
329 * in target node, this avoids needless computation on fast-path.
330 */
331 for ( i = 0; i < num_nodes; i++ )
332 {
333 zone = zone_hi;
334 do {
335 /* Check if target node can support the allocation. */
336 if ( !avail[node] || (avail[node][zone] < request) )
337 continue;
339 /* Find smallest order which can satisfy the request. */
340 for ( j = order; j <= MAX_ORDER; j++ )
341 if ( !list_empty(&heap(node, zone, j)) )
342 goto found;
343 } while ( zone-- > zone_lo ); /* careful: unsigned zone may wrap */
345 /* Pick next node, wrapping around if needed. */
346 if ( ++node == num_nodes )
347 node = 0;
348 }
350 /* No suitable memory blocks. Fail the request. */
351 spin_unlock(&heap_lock);
352 return NULL;
354 found:
355 pg = list_entry(heap(node, zone, j).next, struct page_info, list);
356 list_del(&pg->list);
358 /* We may have to halve the chunk a number of times. */
359 while ( j != order )
360 {
361 PFN_ORDER(pg) = --j;
362 list_add_tail(&pg->list, &heap(node, zone, j));
363 pg += 1 << j;
364 }
366 map_alloc(page_to_mfn(pg), request);
367 ASSERT(avail[node][zone] >= request);
368 avail[node][zone] -= request;
370 spin_unlock(&heap_lock);
372 cpus_clear(mask);
374 for ( i = 0; i < (1 << order); i++ )
375 {
376 /* Reference count must continuously be zero for free pages. */
377 BUG_ON(pg[i].count_info != 0);
379 /* Add in any extra CPUs that need flushing because of this page. */
380 cpus_andnot(extra_cpus_mask, pg[i].u.free.cpumask, mask);
381 tlbflush_filter(extra_cpus_mask, pg[i].tlbflush_timestamp);
382 cpus_or(mask, mask, extra_cpus_mask);
384 /* Initialise fields which have other uses for free pages. */
385 pg[i].u.inuse.type_info = 0;
386 page_set_owner(&pg[i], NULL);
387 }
389 if ( unlikely(!cpus_empty(mask)) )
390 {
391 perfc_incr(need_flush_tlb_flush);
392 flush_tlb_mask(mask);
393 }
395 return pg;
396 }
398 /* Free 2^@order set of pages. */
399 static void free_heap_pages(
400 unsigned int zone, struct page_info *pg, unsigned int order)
401 {
402 unsigned long mask;
403 unsigned int i, node = phys_to_nid(page_to_maddr(pg));
404 struct domain *d;
406 ASSERT(zone < NR_ZONES);
407 ASSERT(order <= MAX_ORDER);
408 ASSERT(node >= 0);
409 ASSERT(node < num_online_nodes());
411 for ( i = 0; i < (1 << order); i++ )
412 {
413 /*
414 * Cannot assume that count_info == 0, as there are some corner cases
415 * where it isn't the case and yet it isn't a bug:
416 * 1. page_get_owner() is NULL
417 * 2. page_get_owner() is a domain that was never accessible by
418 * its domid (e.g., failed to fully construct the domain).
419 * 3. page was never addressable by the guest (e.g., it's an
420 * auto-translate-physmap guest and the page was never included
421 * in its pseudophysical address space).
422 * In all the above cases there can be no guest mappings of this page.
423 */
424 pg[i].count_info = 0;
426 if ( (d = page_get_owner(&pg[i])) != NULL )
427 {
428 pg[i].tlbflush_timestamp = tlbflush_current_time();
429 pg[i].u.free.cpumask = d->domain_dirty_cpumask;
430 }
431 else
432 {
433 cpus_clear(pg[i].u.free.cpumask);
434 }
435 }
437 spin_lock(&heap_lock);
439 map_free(page_to_mfn(pg), 1 << order);
440 avail[node][zone] += 1 << order;
442 /* Merge chunks as far as possible. */
443 while ( order < MAX_ORDER )
444 {
445 mask = 1UL << order;
447 if ( (page_to_mfn(pg) & mask) )
448 {
449 /* Merge with predecessor block? */
450 if ( allocated_in_map(page_to_mfn(pg)-mask) ||
451 (PFN_ORDER(pg-mask) != order) )
452 break;
453 list_del(&(pg-mask)->list);
454 pg -= mask;
455 }
456 else
457 {
458 /* Merge with successor block? */
459 if ( allocated_in_map(page_to_mfn(pg)+mask) ||
460 (PFN_ORDER(pg+mask) != order) )
461 break;
462 list_del(&(pg+mask)->list);
463 }
465 order++;
467 /* After merging, pg should remain in the same node. */
468 ASSERT(phys_to_nid(page_to_maddr(pg)) == node);
469 }
471 PFN_ORDER(pg) = order;
472 list_add_tail(&pg->list, &heap(node, zone, order));
474 spin_unlock(&heap_lock);
475 }
477 /*
478 * Hand the specified arbitrary page range to the specified heap zone
479 * checking the node_id of the previous page. If they differ and the
480 * latter is not on a MAX_ORDER boundary, then we reserve the page by
481 * not freeing it to the buddy allocator.
482 */
483 #define MAX_ORDER_ALIGNED (1UL << (MAX_ORDER))
484 static void init_heap_pages(
485 unsigned int zone, struct page_info *pg, unsigned long nr_pages)
486 {
487 unsigned int nid_curr, nid_prev;
488 unsigned long i;
490 ASSERT(zone < NR_ZONES);
492 if ( likely(page_to_mfn(pg) != 0) )
493 nid_prev = phys_to_nid(page_to_maddr(pg-1));
494 else
495 nid_prev = phys_to_nid(page_to_maddr(pg));
497 for ( i = 0; i < nr_pages; i++ )
498 {
499 nid_curr = phys_to_nid(page_to_maddr(pg+i));
501 if ( unlikely(!avail[nid_curr]) )
502 init_node_heap(nid_curr);
504 /*
505 * free pages of the same node, or if they differ, but are on a
506 * MAX_ORDER alignement boundary (which already get reserved)
507 */
508 if ( (nid_curr == nid_prev) || (page_to_maddr(pg+i) &
509 MAX_ORDER_ALIGNED) )
510 free_heap_pages(zone, pg+i, 0);
511 else
512 printk("Reserving non-aligned node boundary @ mfn %lu\n",
513 page_to_mfn(pg+i));
515 nid_prev = nid_curr;
516 }
517 }
519 static unsigned long avail_heap_pages(
520 unsigned int zone_lo, unsigned int zone_hi, unsigned int node)
521 {
522 unsigned int i, zone, num_nodes = num_online_nodes();
523 unsigned long free_pages = 0;
525 if ( zone_hi >= NR_ZONES )
526 zone_hi = NR_ZONES - 1;
528 for ( i = 0; i < num_nodes; i++ )
529 {
530 if ( !avail[i] )
531 continue;
532 for ( zone = zone_lo; zone <= zone_hi; zone++ )
533 if ( (node == -1) || (node == i) )
534 free_pages += avail[i][zone];
535 }
537 return free_pages;
538 }
540 #define avail_for_domheap(mfn) !(allocated_in_map(mfn) || is_xen_heap_mfn(mfn))
541 void __init end_boot_allocator(void)
542 {
543 unsigned long i;
544 int curr_free, next_free;
546 /* Pages that are free now go to the domain sub-allocator. */
547 if ( (curr_free = next_free = avail_for_domheap(first_valid_mfn)) )
548 map_alloc(first_valid_mfn, 1);
549 for ( i = first_valid_mfn; i < max_page; i++ )
550 {
551 curr_free = next_free;
552 next_free = avail_for_domheap(i+1);
553 if ( next_free )
554 map_alloc(i+1, 1); /* prevent merging in free_heap_pages() */
555 if ( curr_free )
556 init_heap_pages(pfn_dom_zone_type(i), mfn_to_page(i), 1);
557 }
559 if ( !dma_bitsize && (num_online_nodes() > 1) )
560 {
561 #ifdef CONFIG_X86
562 dma_bitsize = min_t(unsigned int,
563 fls(NODE_DATA(0)->node_spanned_pages) - 1
564 + PAGE_SHIFT - 2,
565 32);
566 #else
567 dma_bitsize = 32;
568 #endif
569 }
571 printk("Domain heap initialised");
572 if ( dma_bitsize )
573 printk(" DMA width %u bits", dma_bitsize);
574 printk("\n");
575 }
576 #undef avail_for_domheap
578 /*
579 * Scrub all unallocated pages in all heap zones. This function is more
580 * convoluted than appears necessary because we do not want to continuously
581 * hold the lock while scrubbing very large memory areas.
582 */
583 void __init scrub_heap_pages(void)
584 {
585 void *p;
586 unsigned long mfn;
588 if ( !opt_bootscrub )
589 return;
591 printk("Scrubbing Free RAM: ");
593 for ( mfn = first_valid_mfn; mfn < max_page; mfn++ )
594 {
595 process_pending_timers();
597 /* Quick lock-free check. */
598 if ( allocated_in_map(mfn) )
599 continue;
601 /* Every 100MB, print a progress dot. */
602 if ( (mfn % ((100*1024*1024)/PAGE_SIZE)) == 0 )
603 printk(".");
605 spin_lock(&heap_lock);
607 /* Re-check page status with lock held. */
608 if ( !allocated_in_map(mfn) )
609 {
610 if ( is_xen_heap_mfn(mfn) )
611 {
612 p = page_to_virt(mfn_to_page(mfn));
613 memguard_unguard_range(p, PAGE_SIZE);
614 scrub_page(p);
615 memguard_guard_range(p, PAGE_SIZE);
616 }
617 else
618 {
619 p = map_domain_page(mfn);
620 scrub_page(p);
621 unmap_domain_page(p);
622 }
623 }
625 spin_unlock(&heap_lock);
626 }
628 printk("done.\n");
629 }
633 /*************************
634 * XEN-HEAP SUB-ALLOCATOR
635 */
637 void init_xenheap_pages(paddr_t ps, paddr_t pe)
638 {
639 ps = round_pgup(ps);
640 pe = round_pgdown(pe);
641 if ( pe <= ps )
642 return;
644 memguard_guard_range(maddr_to_virt(ps), pe - ps);
646 /*
647 * Yuk! Ensure there is a one-page buffer between Xen and Dom zones, to
648 * prevent merging of power-of-two blocks across the zone boundary.
649 */
650 if ( ps && !is_xen_heap_mfn(paddr_to_pfn(ps)-1) )
651 ps += PAGE_SIZE;
652 if ( !is_xen_heap_mfn(paddr_to_pfn(pe)) )
653 pe -= PAGE_SIZE;
655 init_heap_pages(MEMZONE_XEN, maddr_to_page(ps), (pe - ps) >> PAGE_SHIFT);
656 }
659 void *alloc_xenheap_pages(unsigned int order)
660 {
661 struct page_info *pg;
663 ASSERT(!in_irq());
665 pg = alloc_heap_pages(
666 MEMZONE_XEN, MEMZONE_XEN, cpu_to_node(smp_processor_id()), order);
667 if ( unlikely(pg == NULL) )
668 goto no_memory;
670 memguard_unguard_range(page_to_virt(pg), 1 << (order + PAGE_SHIFT));
672 return page_to_virt(pg);
674 no_memory:
675 printk("Cannot handle page request order %d!\n", order);
676 return NULL;
677 }
680 void free_xenheap_pages(void *v, unsigned int order)
681 {
682 ASSERT(!in_irq());
684 if ( v == NULL )
685 return;
687 memguard_guard_range(v, 1 << (order + PAGE_SHIFT));
689 free_heap_pages(MEMZONE_XEN, virt_to_page(v), order);
690 }
694 /*************************
695 * DOMAIN-HEAP SUB-ALLOCATOR
696 */
698 void init_domheap_pages(paddr_t ps, paddr_t pe)
699 {
700 unsigned long s_tot, e_tot;
701 unsigned int zone;
703 ASSERT(!in_irq());
705 s_tot = round_pgup(ps) >> PAGE_SHIFT;
706 e_tot = round_pgdown(pe) >> PAGE_SHIFT;
708 zone = fls(s_tot);
709 BUG_ON(zone <= MEMZONE_XEN + 1);
710 for ( --zone; s_tot < e_tot; ++zone )
711 {
712 unsigned long end = e_tot;
714 BUILD_BUG_ON(NR_ZONES > BITS_PER_LONG);
715 if ( zone < BITS_PER_LONG - 1 && end > 1UL << (zone + 1) )
716 end = 1UL << (zone + 1);
717 init_heap_pages(zone, mfn_to_page(s_tot), end - s_tot);
718 s_tot = end;
719 }
720 }
723 int assign_pages(
724 struct domain *d,
725 struct page_info *pg,
726 unsigned int order,
727 unsigned int memflags)
728 {
729 unsigned long i;
731 spin_lock(&d->page_alloc_lock);
733 if ( unlikely(d->is_dying) )
734 {
735 gdprintk(XENLOG_INFO, "Cannot assign page to domain%d -- dying.\n",
736 d->domain_id);
737 goto fail;
738 }
740 if ( !(memflags & MEMF_no_refcount) )
741 {
742 if ( unlikely((d->tot_pages + (1 << order)) > d->max_pages) )
743 {
744 gdprintk(XENLOG_INFO, "Over-allocation for domain %u: %u > %u\n",
745 d->domain_id, d->tot_pages + (1 << order), d->max_pages);
746 goto fail;
747 }
749 if ( unlikely(d->tot_pages == 0) )
750 get_knownalive_domain(d);
752 d->tot_pages += 1 << order;
753 }
755 for ( i = 0; i < (1 << order); i++ )
756 {
757 ASSERT(page_get_owner(&pg[i]) == NULL);
758 ASSERT((pg[i].count_info & ~(PGC_allocated | 1)) == 0);
759 page_set_owner(&pg[i], d);
760 wmb(); /* Domain pointer must be visible before updating refcnt. */
761 pg[i].count_info = PGC_allocated | 1;
762 list_add_tail(&pg[i].list, &d->page_list);
763 }
765 spin_unlock(&d->page_alloc_lock);
766 return 0;
768 fail:
769 spin_unlock(&d->page_alloc_lock);
770 return -1;
771 }
774 struct page_info *alloc_domheap_pages(
775 struct domain *d, unsigned int order, unsigned int memflags)
776 {
777 struct page_info *pg = NULL;
778 unsigned int bits = memflags >> _MEMF_bits, zone_hi = NR_ZONES - 1;
779 unsigned int node = (uint8_t)((memflags >> _MEMF_node) - 1);
781 ASSERT(!in_irq());
783 if ( (node == NUMA_NO_NODE) && (d != NULL) )
784 node = domain_to_node(d);
786 bits = domain_clamp_alloc_bitsize(d, bits ? : (BITS_PER_LONG+PAGE_SHIFT));
787 if ( bits <= (PAGE_SHIFT + 1) )
788 return NULL;
790 bits -= PAGE_SHIFT + 1;
791 if ( bits < zone_hi )
792 zone_hi = bits;
794 if ( (dma_bitsize > PAGE_SHIFT) &&
795 ((zone_hi + PAGE_SHIFT) >= dma_bitsize) )
796 pg = alloc_heap_pages(dma_bitsize - PAGE_SHIFT, zone_hi, node, order);
798 if ( (pg == NULL) &&
799 ((pg = alloc_heap_pages(MEMZONE_XEN + 1, zone_hi,
800 node, order)) == NULL) )
801 return NULL;
803 if ( (d != NULL) && assign_pages(d, pg, order, memflags) )
804 {
805 free_heap_pages(pfn_dom_zone_type(page_to_mfn(pg)), pg, order);
806 return NULL;
807 }
809 return pg;
810 }
812 void free_domheap_pages(struct page_info *pg, unsigned int order)
813 {
814 int i, drop_dom_ref;
815 struct domain *d = page_get_owner(pg);
817 ASSERT(!in_irq());
819 if ( unlikely(is_xen_heap_page(pg)) )
820 {
821 /* NB. May recursively lock from relinquish_memory(). */
822 spin_lock_recursive(&d->page_alloc_lock);
824 for ( i = 0; i < (1 << order); i++ )
825 list_del(&pg[i].list);
827 d->xenheap_pages -= 1 << order;
828 drop_dom_ref = (d->xenheap_pages == 0);
830 spin_unlock_recursive(&d->page_alloc_lock);
831 }
832 else if ( likely(d != NULL) )
833 {
834 /* NB. May recursively lock from relinquish_memory(). */
835 spin_lock_recursive(&d->page_alloc_lock);
837 for ( i = 0; i < (1 << order); i++ )
838 {
839 BUG_ON((pg[i].u.inuse.type_info & PGT_count_mask) != 0);
840 list_del(&pg[i].list);
841 }
843 d->tot_pages -= 1 << order;
844 drop_dom_ref = (d->tot_pages == 0);
846 spin_unlock_recursive(&d->page_alloc_lock);
848 if ( likely(!d->is_dying) )
849 {
850 free_heap_pages(pfn_dom_zone_type(page_to_mfn(pg)), pg, order);
851 }
852 else
853 {
854 /*
855 * Normally we expect a domain to clear pages before freeing them,
856 * if it cares about the secrecy of their contents. However, after
857 * a domain has died we assume responsibility for erasure.
858 */
859 for ( i = 0; i < (1 << order); i++ )
860 {
861 page_set_owner(&pg[i], NULL);
862 spin_lock(&page_scrub_lock);
863 list_add(&pg[i].list, &page_scrub_list);
864 scrub_pages++;
865 spin_unlock(&page_scrub_lock);
866 }
867 }
868 }
869 else
870 {
871 /* Freeing anonymous domain-heap pages. */
872 free_heap_pages(pfn_dom_zone_type(page_to_mfn(pg)), pg, order);
873 drop_dom_ref = 0;
874 }
876 if ( drop_dom_ref )
877 put_domain(d);
878 }
880 unsigned long avail_domheap_pages_region(
881 unsigned int node, unsigned int min_width, unsigned int max_width)
882 {
883 int zone_lo, zone_hi;
885 zone_lo = min_width ? (min_width - (PAGE_SHIFT + 1)) : (MEMZONE_XEN + 1);
886 zone_lo = max_t(int, MEMZONE_XEN + 1, zone_lo);
887 zone_lo = min_t(int, NR_ZONES - 1, zone_lo);
889 zone_hi = max_width ? (max_width - (PAGE_SHIFT + 1)) : (NR_ZONES - 1);
890 zone_hi = max_t(int, MEMZONE_XEN + 1, zone_hi);
891 zone_hi = min_t(int, NR_ZONES - 1, zone_hi);
893 return avail_heap_pages(zone_lo, zone_hi, node);
894 }
896 unsigned long avail_domheap_pages(void)
897 {
898 return avail_heap_pages(MEMZONE_XEN + 1,
899 NR_ZONES - 1,
900 -1);
901 }
903 static void pagealloc_keyhandler(unsigned char key)
904 {
905 unsigned int zone = MEMZONE_XEN;
906 unsigned long n, total = 0;
908 printk("Physical memory information:\n");
909 printk(" Xen heap: %lukB free\n",
910 avail_heap_pages(zone, zone, -1) << (PAGE_SHIFT-10));
912 while ( ++zone < NR_ZONES )
913 {
914 if ( (zone + PAGE_SHIFT) == dma_bitsize )
915 {
916 printk(" DMA heap: %lukB free\n", total << (PAGE_SHIFT-10));
917 total = 0;
918 }
920 if ( (n = avail_heap_pages(zone, zone, -1)) != 0 )
921 {
922 total += n;
923 printk(" heap[%02u]: %lukB free\n", zone, n << (PAGE_SHIFT-10));
924 }
925 }
927 printk(" Dom heap: %lukB free\n", total << (PAGE_SHIFT-10));
928 }
931 static __init int pagealloc_keyhandler_init(void)
932 {
933 register_keyhandler('m', pagealloc_keyhandler, "memory info");
934 return 0;
935 }
936 __initcall(pagealloc_keyhandler_init);
940 /*************************
941 * PAGE SCRUBBING
942 */
944 static DEFINE_PER_CPU(struct timer, page_scrub_timer);
946 static void page_scrub_softirq(void)
947 {
948 struct list_head *ent;
949 struct page_info *pg;
950 void *p;
951 int i;
952 s_time_t start = NOW();
953 static spinlock_t serialise_lock = SPIN_LOCK_UNLOCKED;
955 /* free_heap_pages() does not parallelise well. Serialise this function. */
956 if ( !spin_trylock(&serialise_lock) )
957 {
958 set_timer(&this_cpu(page_scrub_timer), NOW() + MILLISECS(1));
959 return;
960 }
962 /* Aim to do 1ms of work every 10ms. */
963 do {
964 spin_lock(&page_scrub_lock);
966 if ( unlikely((ent = page_scrub_list.next) == &page_scrub_list) )
967 {
968 spin_unlock(&page_scrub_lock);
969 goto out;
970 }
972 /* Peel up to 16 pages from the list. */
973 for ( i = 0; i < 16; i++ )
974 {
975 if ( ent->next == &page_scrub_list )
976 break;
977 ent = ent->next;
978 }
980 /* Remove peeled pages from the list. */
981 ent->next->prev = &page_scrub_list;
982 page_scrub_list.next = ent->next;
983 scrub_pages -= (i+1);
985 spin_unlock(&page_scrub_lock);
987 /* Working backwards, scrub each page in turn. */
988 while ( ent != &page_scrub_list )
989 {
990 pg = list_entry(ent, struct page_info, list);
991 ent = ent->prev;
992 p = map_domain_page(page_to_mfn(pg));
993 scrub_page(p);
994 unmap_domain_page(p);
995 free_heap_pages(pfn_dom_zone_type(page_to_mfn(pg)), pg, 0);
996 }
997 } while ( (NOW() - start) < MILLISECS(1) );
999 set_timer(&this_cpu(page_scrub_timer), NOW() + MILLISECS(10));
1001 out:
1002 spin_unlock(&serialise_lock);
1005 static void page_scrub_timer_fn(void *unused)
1007 page_scrub_schedule_work();
1010 unsigned long avail_scrub_pages(void)
1012 return scrub_pages;
1015 static void dump_heap(unsigned char key)
1017 s_time_t now = NOW();
1018 int i, j;
1020 printk("'%c' pressed -> dumping heap info (now-0x%X:%08X)\n", key,
1021 (u32)(now>>32), (u32)now);
1023 for ( i = 0; i < MAX_NUMNODES; i++ )
1025 if ( !avail[i] )
1026 continue;
1027 for ( j = 0; j < NR_ZONES; j++ )
1028 printk("heap[node=%d][zone=%d] -> %lu pages\n",
1029 i, j, avail[i][j]);
1033 static __init int register_heap_trigger(void)
1035 register_keyhandler('H', dump_heap, "dump heap info");
1036 return 0;
1038 __initcall(register_heap_trigger);
1041 static __init int page_scrub_init(void)
1043 int cpu;
1044 for_each_cpu ( cpu )
1045 init_timer(&per_cpu(page_scrub_timer, cpu),
1046 page_scrub_timer_fn, NULL, cpu);
1047 open_softirq(PAGE_SCRUB_SOFTIRQ, page_scrub_softirq);
1048 return 0;
1050 __initcall(page_scrub_init);
1052 /*
1053 * Local variables:
1054 * mode: C
1055 * c-set-style: "BSD"
1056 * c-basic-offset: 4
1057 * tab-width: 4
1058 * indent-tabs-mode: nil
1059 * End:
1060 */