debuggers.hg

view xen/common/page_alloc.c @ 16586:cd5e1e76d0bc

32-on-64: Fix domain address-size clamping, implement
copy-on-grant-transfer, and eliminate 166GB memory limit for x86/64
Xen.
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Dec 06 13:39:19 2007 +0000 (2007-12-06)
parents 5b8730c78454
children baf90ee3c1da
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 void __init parse_dma_bits(char *s)
59 {
60 unsigned int v = simple_strtol(s, NULL, 0);
61 if ( v >= (BITS_PER_LONG + PAGE_SHIFT) )
62 dma_bitsize = BITS_PER_LONG + PAGE_SHIFT;
63 else if ( v > PAGE_SHIFT + 1 )
64 dma_bitsize = v;
65 else
66 printk("Invalid dma_bits value of %u ignored.\n", v);
67 }
68 custom_param("dma_bits", parse_dma_bits);
70 /*
71 * Amount of memory to reserve in a low-memory (<4GB) pool for specific
72 * allocation requests. Ordinary requests will not fall back to the
73 * lowmem emergency pool.
74 */
75 static unsigned long dma_emergency_pool_pages;
76 static void __init parse_dma_emergency_pool(char *s)
77 {
78 unsigned long long bytes;
79 bytes = parse_size_and_unit(s, NULL);
80 dma_emergency_pool_pages = bytes >> PAGE_SHIFT;
81 }
82 custom_param("dma_emergency_pool", parse_dma_emergency_pool);
84 #define round_pgdown(_p) ((_p)&PAGE_MASK)
85 #define round_pgup(_p) (((_p)+(PAGE_SIZE-1))&PAGE_MASK)
87 #ifndef NDEBUG
88 /* Avoid callers relying on allocations returning zeroed pages. */
89 #define scrub_page(p) memset((p), 0xc2, PAGE_SIZE)
90 #else
91 /* For a production build, clear_page() is the fastest way to scrub. */
92 #define scrub_page(p) clear_page(p)
93 #endif
95 static DEFINE_SPINLOCK(page_scrub_lock);
96 LIST_HEAD(page_scrub_list);
97 static unsigned long scrub_pages;
99 /*********************
100 * ALLOCATION BITMAP
101 * One bit per page of memory. Bit set => page is allocated.
102 */
104 static unsigned long *alloc_bitmap;
105 #define PAGES_PER_MAPWORD (sizeof(unsigned long) * 8)
107 #define allocated_in_map(_pn) \
108 ({ unsigned long ___pn = (_pn); \
109 !!(alloc_bitmap[___pn/PAGES_PER_MAPWORD] & \
110 (1UL<<(___pn&(PAGES_PER_MAPWORD-1)))); })
112 /*
113 * Hint regarding bitwise arithmetic in map_{alloc,free}:
114 * -(1<<n) sets all bits >= n.
115 * (1<<n)-1 sets all bits < n.
116 * Variable names in map_{alloc,free}:
117 * *_idx == Index into `alloc_bitmap' array.
118 * *_off == Bit offset within an element of the `alloc_bitmap' array.
119 */
121 static void map_alloc(unsigned long first_page, unsigned long nr_pages)
122 {
123 unsigned long start_off, end_off, curr_idx, end_idx;
125 #ifndef NDEBUG
126 unsigned long i;
127 /* Check that the block isn't already allocated. */
128 for ( i = 0; i < nr_pages; i++ )
129 ASSERT(!allocated_in_map(first_page + i));
130 #endif
132 curr_idx = first_page / PAGES_PER_MAPWORD;
133 start_off = first_page & (PAGES_PER_MAPWORD-1);
134 end_idx = (first_page + nr_pages) / PAGES_PER_MAPWORD;
135 end_off = (first_page + nr_pages) & (PAGES_PER_MAPWORD-1);
137 if ( curr_idx == end_idx )
138 {
139 alloc_bitmap[curr_idx] |= ((1UL<<end_off)-1) & -(1UL<<start_off);
140 }
141 else
142 {
143 alloc_bitmap[curr_idx] |= -(1UL<<start_off);
144 while ( ++curr_idx < end_idx ) alloc_bitmap[curr_idx] = ~0UL;
145 alloc_bitmap[curr_idx] |= (1UL<<end_off)-1;
146 }
147 }
149 static void map_free(unsigned long first_page, unsigned long nr_pages)
150 {
151 unsigned long start_off, end_off, curr_idx, end_idx;
153 #ifndef NDEBUG
154 unsigned long i;
155 /* Check that the block isn't already freed. */
156 for ( i = 0; i < nr_pages; i++ )
157 ASSERT(allocated_in_map(first_page + i));
158 #endif
160 curr_idx = first_page / PAGES_PER_MAPWORD;
161 start_off = first_page & (PAGES_PER_MAPWORD-1);
162 end_idx = (first_page + nr_pages) / PAGES_PER_MAPWORD;
163 end_off = (first_page + nr_pages) & (PAGES_PER_MAPWORD-1);
165 if ( curr_idx == end_idx )
166 {
167 alloc_bitmap[curr_idx] &= -(1UL<<end_off) | ((1UL<<start_off)-1);
168 }
169 else
170 {
171 alloc_bitmap[curr_idx] &= (1UL<<start_off)-1;
172 while ( ++curr_idx != end_idx ) alloc_bitmap[curr_idx] = 0;
173 alloc_bitmap[curr_idx] &= -(1UL<<end_off);
174 }
175 }
179 /*************************
180 * BOOT-TIME ALLOCATOR
181 */
183 static unsigned long first_valid_mfn = ~0UL;
185 /* Initialise allocator to handle up to @max_page pages. */
186 paddr_t __init init_boot_allocator(paddr_t bitmap_start)
187 {
188 unsigned long bitmap_size;
190 bitmap_start = round_pgup(bitmap_start);
192 /*
193 * Allocate space for the allocation bitmap. Include an extra longword
194 * of padding for possible overrun in map_alloc and map_free.
195 */
196 bitmap_size = max_page / 8;
197 bitmap_size += sizeof(unsigned long);
198 bitmap_size = round_pgup(bitmap_size);
199 alloc_bitmap = (unsigned long *)maddr_to_virt(bitmap_start);
201 /* All allocated by default. */
202 memset(alloc_bitmap, ~0, bitmap_size);
204 return bitmap_start + bitmap_size;
205 }
207 void __init init_boot_pages(paddr_t ps, paddr_t pe)
208 {
209 unsigned long bad_spfn, bad_epfn, i;
210 const char *p;
212 ps = round_pgup(ps);
213 pe = round_pgdown(pe);
214 if ( pe <= ps )
215 return;
217 first_valid_mfn = min_t(unsigned long, ps >> PAGE_SHIFT, first_valid_mfn);
219 map_free(ps >> PAGE_SHIFT, (pe - ps) >> PAGE_SHIFT);
221 /* Check new pages against the bad-page list. */
222 p = opt_badpage;
223 while ( *p != '\0' )
224 {
225 bad_spfn = simple_strtoul(p, &p, 0);
226 bad_epfn = bad_spfn;
228 if ( *p == '-' )
229 {
230 p++;
231 bad_epfn = simple_strtoul(p, &p, 0);
232 if ( bad_epfn < bad_spfn )
233 bad_epfn = bad_spfn;
234 }
236 if ( *p == ',' )
237 p++;
238 else if ( *p != '\0' )
239 break;
241 if ( bad_epfn == bad_spfn )
242 printk("Marking page %lx as bad\n", bad_spfn);
243 else
244 printk("Marking pages %lx through %lx as bad\n",
245 bad_spfn, bad_epfn);
247 for ( i = bad_spfn; i <= bad_epfn; i++ )
248 if ( (i < max_page) && !allocated_in_map(i) )
249 map_alloc(i, 1);
250 }
251 }
253 unsigned long __init alloc_boot_pages(
254 unsigned long nr_pfns, unsigned long pfn_align)
255 {
256 unsigned long pg, i;
258 /* Search backwards to obtain highest available range. */
259 for ( pg = (max_page - nr_pfns) & ~(pfn_align - 1);
260 pg >= first_valid_mfn;
261 pg = (pg + i - nr_pfns) & ~(pfn_align - 1) )
262 {
263 for ( i = 0; i < nr_pfns; i++ )
264 if ( allocated_in_map(pg+i) )
265 break;
266 if ( i == nr_pfns )
267 {
268 map_alloc(pg, nr_pfns);
269 return pg;
270 }
271 }
273 return 0;
274 }
278 /*************************
279 * BINARY BUDDY ALLOCATOR
280 */
282 #define MEMZONE_XEN 0
283 #ifdef PADDR_BITS
284 #define NR_ZONES (PADDR_BITS - PAGE_SHIFT)
285 #else
286 #define NR_ZONES (BITS_PER_LONG - PAGE_SHIFT)
287 #endif
289 #define pfn_dom_zone_type(_pfn) (fls(_pfn) - 1)
291 typedef struct list_head heap_by_zone_and_order_t[NR_ZONES][MAX_ORDER+1];
292 static heap_by_zone_and_order_t *_heap[MAX_NUMNODES];
293 #define heap(node, zone, order) ((*_heap[node])[zone][order])
295 static unsigned long *avail[MAX_NUMNODES];
297 static DEFINE_SPINLOCK(heap_lock);
299 static void init_node_heap(int node)
300 {
301 /* First node to be discovered has its heap metadata statically alloced. */
302 static heap_by_zone_and_order_t _heap_static;
303 static unsigned long avail_static[NR_ZONES];
304 static unsigned long first_node_initialised;
306 int i, j;
308 if ( !test_and_set_bit(0, &first_node_initialised) )
309 {
310 _heap[node] = &_heap_static;
311 avail[node] = avail_static;
312 }
313 else
314 {
315 _heap[node] = xmalloc(heap_by_zone_and_order_t);
316 avail[node] = xmalloc_array(unsigned long, NR_ZONES);
317 BUG_ON(!_heap[node] || !avail[node]);
318 }
320 memset(avail[node], 0, NR_ZONES * sizeof(long));
322 for ( i = 0; i < NR_ZONES; i++ )
323 for ( j = 0; j <= MAX_ORDER; j++ )
324 INIT_LIST_HEAD(&(*_heap[node])[i][j]);
325 }
327 /* Allocate 2^@order contiguous pages. */
328 static struct page_info *alloc_heap_pages(
329 unsigned int zone_lo, unsigned int zone_hi,
330 unsigned int cpu, unsigned int order)
331 {
332 unsigned int i, j, zone;
333 unsigned int node = cpu_to_node(cpu), num_nodes = num_online_nodes();
334 unsigned long request = 1UL << order;
335 cpumask_t extra_cpus_mask, mask;
336 struct page_info *pg;
338 ASSERT(node >= 0);
339 ASSERT(node < num_nodes);
340 ASSERT(zone_lo <= zone_hi);
341 ASSERT(zone_hi < NR_ZONES);
343 if ( unlikely(order > MAX_ORDER) )
344 return NULL;
346 spin_lock(&heap_lock);
348 /*
349 * Start with requested node, but exhaust all node memory in requested
350 * zone before failing, only calc new node value if we fail to find memory
351 * in target node, this avoids needless computation on fast-path.
352 */
353 for ( i = 0; i < num_nodes; i++ )
354 {
355 zone = zone_hi;
356 do {
357 /* Check if target node can support the allocation. */
358 if ( !avail[node] || (avail[node][zone] < request) )
359 continue;
361 /* Find smallest order which can satisfy the request. */
362 for ( j = order; j <= MAX_ORDER; j++ )
363 if ( !list_empty(&heap(node, zone, j)) )
364 goto found;
365 } while ( zone-- > zone_lo ); /* careful: unsigned zone may wrap */
367 /* Pick next node, wrapping around if needed. */
368 if ( ++node == num_nodes )
369 node = 0;
370 }
372 /* No suitable memory blocks. Fail the request. */
373 spin_unlock(&heap_lock);
374 return NULL;
376 found:
377 pg = list_entry(heap(node, zone, j).next, struct page_info, list);
378 list_del(&pg->list);
380 /* We may have to halve the chunk a number of times. */
381 while ( j != order )
382 {
383 PFN_ORDER(pg) = --j;
384 list_add_tail(&pg->list, &heap(node, zone, j));
385 pg += 1 << j;
386 }
388 map_alloc(page_to_mfn(pg), request);
389 ASSERT(avail[node][zone] >= request);
390 avail[node][zone] -= request;
392 spin_unlock(&heap_lock);
394 cpus_clear(mask);
396 for ( i = 0; i < (1 << order); i++ )
397 {
398 /* Reference count must continuously be zero for free pages. */
399 BUG_ON(pg[i].count_info != 0);
401 /* Add in any extra CPUs that need flushing because of this page. */
402 cpus_andnot(extra_cpus_mask, pg[i].u.free.cpumask, mask);
403 tlbflush_filter(extra_cpus_mask, pg[i].tlbflush_timestamp);
404 cpus_or(mask, mask, extra_cpus_mask);
406 /* Initialise fields which have other uses for free pages. */
407 pg[i].u.inuse.type_info = 0;
408 page_set_owner(&pg[i], NULL);
409 }
411 if ( unlikely(!cpus_empty(mask)) )
412 {
413 perfc_incr(need_flush_tlb_flush);
414 flush_tlb_mask(mask);
415 }
417 return pg;
418 }
420 /* Free 2^@order set of pages. */
421 static void free_heap_pages(
422 unsigned int zone, struct page_info *pg, unsigned int order)
423 {
424 unsigned long mask;
425 unsigned int i, node = phys_to_nid(page_to_maddr(pg));
426 struct domain *d;
428 ASSERT(zone < NR_ZONES);
429 ASSERT(order <= MAX_ORDER);
430 ASSERT(node >= 0);
431 ASSERT(node < num_online_nodes());
433 for ( i = 0; i < (1 << order); i++ )
434 {
435 /*
436 * Cannot assume that count_info == 0, as there are some corner cases
437 * where it isn't the case and yet it isn't a bug:
438 * 1. page_get_owner() is NULL
439 * 2. page_get_owner() is a domain that was never accessible by
440 * its domid (e.g., failed to fully construct the domain).
441 * 3. page was never addressable by the guest (e.g., it's an
442 * auto-translate-physmap guest and the page was never included
443 * in its pseudophysical address space).
444 * In all the above cases there can be no guest mappings of this page.
445 */
446 pg[i].count_info = 0;
448 if ( (d = page_get_owner(&pg[i])) != NULL )
449 {
450 pg[i].tlbflush_timestamp = tlbflush_current_time();
451 pg[i].u.free.cpumask = d->domain_dirty_cpumask;
452 }
453 else
454 {
455 cpus_clear(pg[i].u.free.cpumask);
456 }
457 }
459 spin_lock(&heap_lock);
461 map_free(page_to_mfn(pg), 1 << order);
462 avail[node][zone] += 1 << order;
464 /* Merge chunks as far as possible. */
465 while ( order < MAX_ORDER )
466 {
467 mask = 1UL << order;
469 if ( (page_to_mfn(pg) & mask) )
470 {
471 /* Merge with predecessor block? */
472 if ( allocated_in_map(page_to_mfn(pg)-mask) ||
473 (PFN_ORDER(pg-mask) != order) )
474 break;
475 list_del(&(pg-mask)->list);
476 pg -= mask;
477 }
478 else
479 {
480 /* Merge with successor block? */
481 if ( allocated_in_map(page_to_mfn(pg)+mask) ||
482 (PFN_ORDER(pg+mask) != order) )
483 break;
484 list_del(&(pg+mask)->list);
485 }
487 order++;
489 /* After merging, pg should remain in the same node. */
490 ASSERT(phys_to_nid(page_to_maddr(pg)) == node);
491 }
493 PFN_ORDER(pg) = order;
494 list_add_tail(&pg->list, &heap(node, zone, order));
496 spin_unlock(&heap_lock);
497 }
499 /*
500 * Hand the specified arbitrary page range to the specified heap zone
501 * checking the node_id of the previous page. If they differ and the
502 * latter is not on a MAX_ORDER boundary, then we reserve the page by
503 * not freeing it to the buddy allocator.
504 */
505 #define MAX_ORDER_ALIGNED (1UL << (MAX_ORDER))
506 void init_heap_pages(
507 unsigned int zone, struct page_info *pg, unsigned long nr_pages)
508 {
509 unsigned int nid_curr, nid_prev;
510 unsigned long i;
512 ASSERT(zone < NR_ZONES);
514 if ( likely(page_to_mfn(pg) != 0) )
515 nid_prev = phys_to_nid(page_to_maddr(pg-1));
516 else
517 nid_prev = phys_to_nid(page_to_maddr(pg));
519 for ( i = 0; i < nr_pages; i++ )
520 {
521 nid_curr = phys_to_nid(page_to_maddr(pg+i));
523 if ( unlikely(!avail[nid_curr]) )
524 init_node_heap(nid_curr);
526 /*
527 * free pages of the same node, or if they differ, but are on a
528 * MAX_ORDER alignement boundary (which already get reserved)
529 */
530 if ( (nid_curr == nid_prev) || (page_to_maddr(pg+i) &
531 MAX_ORDER_ALIGNED) )
532 free_heap_pages(zone, pg+i, 0);
533 else
534 printk("Reserving non-aligned node boundary @ mfn %lu\n",
535 page_to_mfn(pg+i));
537 nid_prev = nid_curr;
538 }
539 }
541 static unsigned long avail_heap_pages(
542 unsigned int zone_lo, unsigned int zone_hi, unsigned int node)
543 {
544 unsigned int i, zone, num_nodes = num_online_nodes();
545 unsigned long free_pages = 0;
547 if ( zone_hi >= NR_ZONES )
548 zone_hi = NR_ZONES - 1;
550 for ( i = 0; i < num_nodes; i++ )
551 {
552 if ( !avail[i] )
553 continue;
554 for ( zone = zone_lo; zone <= zone_hi; zone++ )
555 if ( (node == -1) || (node == i) )
556 free_pages += avail[i][zone];
557 }
559 return free_pages;
560 }
562 #define avail_for_domheap(mfn) !(allocated_in_map(mfn) || is_xen_heap_mfn(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_mfn(mfn) )
618 {
619 p = page_to_virt(mfn_to_page(mfn));
620 memguard_unguard_range(p, PAGE_SIZE);
621 scrub_page(p);
622 memguard_guard_range(p, PAGE_SIZE);
623 }
624 else
625 {
626 p = map_domain_page(mfn);
627 scrub_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_mfn(paddr_to_pfn(ps)-1) )
658 ps += PAGE_SIZE;
659 if ( !is_xen_heap_mfn(paddr_to_pfn(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 bits = domain_clamp_alloc_bitsize(d, bits ? : BITS_PER_LONG);
790 if ( bits <= (PAGE_SHIFT + 1) )
791 return NULL;
793 bits -= PAGE_SHIFT + 1;
794 if ( bits < zone_hi )
795 zone_hi = bits;
797 if ( (zone_hi + PAGE_SHIFT) >= dma_bitsize )
798 {
799 pg = alloc_heap_pages(dma_bitsize - PAGE_SHIFT, zone_hi, cpu, order);
801 /* Failure? Then check if we can fall back to the DMA pool. */
802 if ( unlikely(pg == NULL) &&
803 ((order > MAX_ORDER) ||
804 (avail_heap_pages(MEMZONE_XEN + 1,
805 dma_bitsize - PAGE_SHIFT - 1,
806 -1) <
807 (dma_emergency_pool_pages + (1UL << order)))) )
808 return NULL;
809 }
811 if ( (pg == NULL) &&
812 ((pg = alloc_heap_pages(MEMZONE_XEN + 1, zone_hi,
813 cpu, order)) == NULL) )
814 return NULL;
816 if ( (d != NULL) && assign_pages(d, pg, order, memflags) )
817 {
818 free_heap_pages(pfn_dom_zone_type(page_to_mfn(pg)), pg, order);
819 return NULL;
820 }
822 return pg;
823 }
825 struct page_info *alloc_domheap_pages(
826 struct domain *d, unsigned int order, unsigned int flags)
827 {
828 return __alloc_domheap_pages(d, smp_processor_id(), order, flags);
829 }
831 void free_domheap_pages(struct page_info *pg, unsigned int order)
832 {
833 int i, drop_dom_ref;
834 struct domain *d = page_get_owner(pg);
836 ASSERT(!in_irq());
838 if ( unlikely(is_xen_heap_page(pg)) )
839 {
840 /* NB. May recursively lock from relinquish_memory(). */
841 spin_lock_recursive(&d->page_alloc_lock);
843 for ( i = 0; i < (1 << order); i++ )
844 list_del(&pg[i].list);
846 d->xenheap_pages -= 1 << order;
847 drop_dom_ref = (d->xenheap_pages == 0);
849 spin_unlock_recursive(&d->page_alloc_lock);
850 }
851 else if ( likely(d != NULL) )
852 {
853 /* NB. May recursively lock from relinquish_memory(). */
854 spin_lock_recursive(&d->page_alloc_lock);
856 for ( i = 0; i < (1 << order); i++ )
857 {
858 BUG_ON((pg[i].u.inuse.type_info & PGT_count_mask) != 0);
859 list_del(&pg[i].list);
860 }
862 d->tot_pages -= 1 << order;
863 drop_dom_ref = (d->tot_pages == 0);
865 spin_unlock_recursive(&d->page_alloc_lock);
867 if ( likely(!d->is_dying) )
868 {
869 free_heap_pages(pfn_dom_zone_type(page_to_mfn(pg)), pg, order);
870 }
871 else
872 {
873 /*
874 * Normally we expect a domain to clear pages before freeing them,
875 * if it cares about the secrecy of their contents. However, after
876 * a domain has died we assume responsibility for erasure.
877 */
878 for ( i = 0; i < (1 << order); i++ )
879 {
880 page_set_owner(&pg[i], NULL);
881 spin_lock(&page_scrub_lock);
882 list_add(&pg[i].list, &page_scrub_list);
883 scrub_pages++;
884 spin_unlock(&page_scrub_lock);
885 }
886 }
887 }
888 else
889 {
890 /* Freeing anonymous domain-heap pages. */
891 free_heap_pages(pfn_dom_zone_type(page_to_mfn(pg)), pg, order);
892 drop_dom_ref = 0;
893 }
895 if ( drop_dom_ref )
896 put_domain(d);
897 }
899 unsigned long avail_domheap_pages_region(
900 unsigned int node, unsigned int min_width, unsigned int max_width)
901 {
902 int zone_lo, zone_hi;
904 zone_lo = min_width ? (min_width - (PAGE_SHIFT + 1)) : (MEMZONE_XEN + 1);
905 zone_lo = max_t(int, MEMZONE_XEN + 1, zone_lo);
906 zone_lo = min_t(int, NR_ZONES - 1, zone_lo);
908 zone_hi = max_width ? (max_width - (PAGE_SHIFT + 1)) : (NR_ZONES - 1);
909 zone_hi = max_t(int, MEMZONE_XEN + 1, zone_hi);
910 zone_hi = min_t(int, NR_ZONES - 1, zone_hi);
912 return avail_heap_pages(zone_lo, zone_hi, node);
913 }
915 unsigned long avail_domheap_pages(void)
916 {
917 unsigned long avail_nrm, avail_dma;
919 avail_nrm = avail_heap_pages(dma_bitsize - PAGE_SHIFT,
920 NR_ZONES - 1,
921 -1);
923 avail_dma = avail_heap_pages(MEMZONE_XEN + 1,
924 dma_bitsize - PAGE_SHIFT - 1,
925 -1);
927 if ( avail_dma > dma_emergency_pool_pages )
928 avail_dma -= dma_emergency_pool_pages;
929 else
930 avail_dma = 0;
932 return avail_nrm + avail_dma;
933 }
935 static void pagealloc_keyhandler(unsigned char key)
936 {
937 unsigned int zone = MEMZONE_XEN;
938 unsigned long total = 0;
940 printk("Physical memory information:\n");
941 printk(" Xen heap: %lukB free\n",
942 avail_heap_pages(zone, zone, -1) << (PAGE_SHIFT-10));
944 while ( ++zone < NR_ZONES )
945 {
946 unsigned long n;
948 if ( zone == dma_bitsize - PAGE_SHIFT )
949 {
950 printk(" DMA heap: %lukB free\n", total << (PAGE_SHIFT-10));
951 total = 0;
952 }
954 if ( (n = avail_heap_pages(zone, zone, -1)) != 0 )
955 {
956 total += n;
957 printk(" heap[%02u]: %lukB free\n", zone, n << (PAGE_SHIFT-10));
958 }
959 }
961 printk(" Dom heap: %lukB free\n", total << (PAGE_SHIFT-10));
962 }
965 static __init int pagealloc_keyhandler_init(void)
966 {
967 register_keyhandler('m', pagealloc_keyhandler, "memory info");
968 return 0;
969 }
970 __initcall(pagealloc_keyhandler_init);
974 /*************************
975 * PAGE SCRUBBING
976 */
978 static DEFINE_PER_CPU(struct timer, page_scrub_timer);
980 static void page_scrub_softirq(void)
981 {
982 struct list_head *ent;
983 struct page_info *pg;
984 void *p;
985 int i;
986 s_time_t start = NOW();
988 /* Aim to do 1ms of work every 10ms. */
989 do {
990 spin_lock(&page_scrub_lock);
992 if ( unlikely((ent = page_scrub_list.next) == &page_scrub_list) )
993 {
994 spin_unlock(&page_scrub_lock);
995 return;
996 }
998 /* Peel up to 16 pages from the list. */
999 for ( i = 0; i < 16; i++ )
1001 if ( ent->next == &page_scrub_list )
1002 break;
1003 ent = ent->next;
1006 /* Remove peeled pages from the list. */
1007 ent->next->prev = &page_scrub_list;
1008 page_scrub_list.next = ent->next;
1009 scrub_pages -= (i+1);
1011 spin_unlock(&page_scrub_lock);
1013 /* Working backwards, scrub each page in turn. */
1014 while ( ent != &page_scrub_list )
1016 pg = list_entry(ent, struct page_info, list);
1017 ent = ent->prev;
1018 p = map_domain_page(page_to_mfn(pg));
1019 scrub_page(p);
1020 unmap_domain_page(p);
1021 free_heap_pages(pfn_dom_zone_type(page_to_mfn(pg)), pg, 0);
1023 } while ( (NOW() - start) < MILLISECS(1) );
1025 set_timer(&this_cpu(page_scrub_timer), NOW() + MILLISECS(10));
1028 static void page_scrub_timer_fn(void *unused)
1030 page_scrub_schedule_work();
1033 unsigned long avail_scrub_pages(void)
1035 return scrub_pages;
1038 static void dump_heap(unsigned char key)
1040 s_time_t now = NOW();
1041 int i, j;
1043 printk("'%c' pressed -> dumping heap info (now-0x%X:%08X)\n", key,
1044 (u32)(now>>32), (u32)now);
1046 for ( i = 0; i < MAX_NUMNODES; i++ )
1048 if ( !avail[i] )
1049 continue;
1050 for ( j = 0; j < NR_ZONES; j++ )
1051 printk("heap[node=%d][zone=%d] -> %lu pages\n",
1052 i, j, avail[i][j]);
1056 static __init int register_heap_trigger(void)
1058 register_keyhandler('H', dump_heap, "dump heap info");
1059 return 0;
1061 __initcall(register_heap_trigger);
1064 static __init int page_scrub_init(void)
1066 int cpu;
1067 for_each_cpu ( cpu )
1068 init_timer(&per_cpu(page_scrub_timer, cpu),
1069 page_scrub_timer_fn, NULL, cpu);
1070 open_softirq(PAGE_SCRUB_SOFTIRQ, page_scrub_softirq);
1071 return 0;
1073 __initcall(page_scrub_init);
1075 /*
1076 * Local variables:
1077 * mode: C
1078 * c-set-style: "BSD"
1079 * c-basic-offset: 4
1080 * tab-width: 4
1081 * indent-tabs-mode: nil
1082 * End:
1083 */