debuggers.hg

view xen/common/memory.c @ 21959:581ebaa7e2da

numa: Attempt more efficient NUMA allocation in hypervisor by default.

1. Try to allocate from nodes containing CPUs which a guest can be
scheduled on.
2. Remember which node we allocated from last, and round-robin
allocations among above-mentioned nodes.

Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Aug 04 15:35:28 2010 +0100 (2010-08-04)
parents 9d965ac1b0db
children e7afe98afd43
line source
1 /******************************************************************************
2 * memory.c
3 *
4 * Code to handle memory-related requests.
5 *
6 * Copyright (c) 2003-2004, B Dragovic
7 * Copyright (c) 2003-2005, K A Fraser
8 */
10 #include <xen/config.h>
11 #include <xen/types.h>
12 #include <xen/lib.h>
13 #include <xen/mm.h>
14 #include <xen/perfc.h>
15 #include <xen/sched.h>
16 #include <xen/event.h>
17 #include <xen/paging.h>
18 #include <xen/iocap.h>
19 #include <xen/guest_access.h>
20 #include <xen/hypercall.h>
21 #include <xen/errno.h>
22 #include <xen/tmem.h>
23 #include <asm/current.h>
24 #include <asm/hardirq.h>
25 #ifdef CONFIG_X86
26 # include <asm/p2m.h>
27 #endif
28 #include <xen/numa.h>
29 #include <public/memory.h>
30 #include <xsm/xsm.h>
31 #include <xen/trace.h>
33 struct memop_args {
34 /* INPUT */
35 struct domain *domain; /* Domain to be affected. */
36 XEN_GUEST_HANDLE(xen_pfn_t) extent_list; /* List of extent base addrs. */
37 unsigned int nr_extents; /* Number of extents to allocate or free. */
38 unsigned int extent_order; /* Size of each extent. */
39 unsigned int memflags; /* Allocation flags. */
41 /* INPUT/OUTPUT */
42 unsigned int nr_done; /* Number of extents processed so far. */
43 int preempted; /* Was the hypercall preempted? */
44 };
46 static void increase_reservation(struct memop_args *a)
47 {
48 struct page_info *page;
49 unsigned long i;
50 xen_pfn_t mfn;
51 struct domain *d = a->domain;
53 if ( !guest_handle_is_null(a->extent_list) &&
54 !guest_handle_subrange_okay(a->extent_list, a->nr_done,
55 a->nr_extents-1) )
56 return;
58 if ( !multipage_allocation_permitted(current->domain, a->extent_order) )
59 return;
61 for ( i = a->nr_done; i < a->nr_extents; i++ )
62 {
63 if ( hypercall_preempt_check() )
64 {
65 a->preempted = 1;
66 goto out;
67 }
69 page = alloc_domheap_pages(d, a->extent_order, a->memflags);
70 if ( unlikely(page == NULL) )
71 {
72 gdprintk(XENLOG_INFO, "Could not allocate order=%d extent: "
73 "id=%d memflags=%x (%ld of %d)\n",
74 a->extent_order, d->domain_id, a->memflags,
75 i, a->nr_extents);
76 goto out;
77 }
79 /* Inform the domain of the new page's machine address. */
80 if ( !guest_handle_is_null(a->extent_list) )
81 {
82 mfn = page_to_mfn(page);
83 if ( unlikely(__copy_to_guest_offset(a->extent_list, i, &mfn, 1)) )
84 goto out;
85 }
86 }
88 out:
89 a->nr_done = i;
90 }
92 static void populate_physmap(struct memop_args *a)
93 {
94 struct page_info *page;
95 unsigned long i, j;
96 xen_pfn_t gpfn, mfn;
97 struct domain *d = a->domain;
99 if ( !guest_handle_subrange_okay(a->extent_list, a->nr_done,
100 a->nr_extents-1) )
101 return;
103 if ( !multipage_allocation_permitted(current->domain, a->extent_order) )
104 return;
106 for ( i = a->nr_done; i < a->nr_extents; i++ )
107 {
108 if ( hypercall_preempt_check() )
109 {
110 a->preempted = 1;
111 goto out;
112 }
114 if ( unlikely(__copy_from_guest_offset(&gpfn, a->extent_list, i, 1)) )
115 goto out;
117 if ( a->memflags & MEMF_populate_on_demand )
118 {
119 if ( guest_physmap_mark_populate_on_demand(d, gpfn,
120 a->extent_order) < 0 )
121 goto out;
122 }
123 else
124 {
125 page = alloc_domheap_pages(d, a->extent_order, a->memflags);
126 if ( unlikely(page == NULL) )
127 {
128 if ( !opt_tmem || (a->extent_order != 0) )
129 gdprintk(XENLOG_INFO, "Could not allocate order=%d extent:"
130 " id=%d memflags=%x (%ld of %d)\n",
131 a->extent_order, d->domain_id, a->memflags,
132 i, a->nr_extents);
133 goto out;
134 }
136 mfn = page_to_mfn(page);
137 guest_physmap_add_page(d, gpfn, mfn, a->extent_order);
139 if ( !paging_mode_translate(d) )
140 {
141 for ( j = 0; j < (1 << a->extent_order); j++ )
142 set_gpfn_from_mfn(mfn + j, gpfn + j);
144 /* Inform the domain of the new page's machine address. */
145 if ( unlikely(__copy_to_guest_offset(a->extent_list, i, &mfn, 1)) )
146 goto out;
147 }
148 }
149 }
151 out:
152 a->nr_done = i;
153 }
155 int guest_remove_page(struct domain *d, unsigned long gmfn)
156 {
157 struct page_info *page;
158 #ifdef CONFIG_X86
159 p2m_type_t p2mt;
160 #endif
161 unsigned long mfn;
163 #ifdef CONFIG_X86
164 mfn = mfn_x(gfn_to_mfn(d, gmfn, &p2mt));
165 #else
166 mfn = gmfn_to_mfn(d, gmfn);
167 #endif
168 if ( unlikely(!mfn_valid(mfn)) )
169 {
170 gdprintk(XENLOG_INFO, "Domain %u page number %lx invalid\n",
171 d->domain_id, gmfn);
172 return 0;
173 }
175 page = mfn_to_page(mfn);
176 #ifdef CONFIG_X86
177 /* If gmfn is shared, just drop the guest reference (which may or may not
178 * free the page) */
179 if(p2m_is_shared(p2mt))
180 {
181 put_page_and_type(page);
182 guest_physmap_remove_page(d, gmfn, mfn, 0);
183 return 1;
184 }
186 #endif /* CONFIG_X86 */
187 if ( unlikely(!get_page(page, d)) )
188 {
189 gdprintk(XENLOG_INFO, "Bad page free for domain %u\n", d->domain_id);
190 return 0;
191 }
193 if ( test_and_clear_bit(_PGT_pinned, &page->u.inuse.type_info) )
194 put_page_and_type(page);
196 if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
197 put_page(page);
199 guest_physmap_remove_page(d, gmfn, mfn, 0);
201 put_page(page);
203 return 1;
204 }
206 static void decrease_reservation(struct memop_args *a)
207 {
208 unsigned long i, j;
209 xen_pfn_t gmfn;
211 if ( !guest_handle_subrange_okay(a->extent_list, a->nr_done,
212 a->nr_extents-1) )
213 return;
215 for ( i = a->nr_done; i < a->nr_extents; i++ )
216 {
217 if ( hypercall_preempt_check() )
218 {
219 a->preempted = 1;
220 goto out;
221 }
223 if ( unlikely(__copy_from_guest_offset(&gmfn, a->extent_list, i, 1)) )
224 goto out;
226 if ( tb_init_done )
227 {
228 struct {
229 u64 gfn;
230 int d:16,order:16;
231 } t;
233 t.gfn = gmfn;
234 t.d = a->domain->domain_id;
235 t.order = a->extent_order;
237 __trace_var(TRC_MEM_DECREASE_RESERVATION, 0, sizeof(t), (unsigned char *)&t);
238 }
240 /* See if populate-on-demand wants to handle this */
241 if ( is_hvm_domain(a->domain)
242 && p2m_pod_decrease_reservation(a->domain, gmfn, a->extent_order) )
243 continue;
245 for ( j = 0; j < (1 << a->extent_order); j++ )
246 if ( !guest_remove_page(a->domain, gmfn + j) )
247 goto out;
248 }
250 out:
251 a->nr_done = i;
252 }
254 static long memory_exchange(XEN_GUEST_HANDLE(xen_memory_exchange_t) arg)
255 {
256 struct xen_memory_exchange exch;
257 PAGE_LIST_HEAD(in_chunk_list);
258 PAGE_LIST_HEAD(out_chunk_list);
259 unsigned long in_chunk_order, out_chunk_order;
260 xen_pfn_t gpfn, gmfn, mfn;
261 unsigned long i, j, k;
262 unsigned int memflags = 0;
263 long rc = 0;
264 struct domain *d;
265 struct page_info *page;
267 if ( copy_from_guest(&exch, arg, 1) )
268 return -EFAULT;
270 /* Various sanity checks. */
271 if ( (exch.nr_exchanged > exch.in.nr_extents) ||
272 /* Input and output domain identifiers match? */
273 (exch.in.domid != exch.out.domid) ||
274 /* Sizes of input and output lists do not overflow a long? */
275 ((~0UL >> exch.in.extent_order) < exch.in.nr_extents) ||
276 ((~0UL >> exch.out.extent_order) < exch.out.nr_extents) ||
277 /* Sizes of input and output lists match? */
278 ((exch.in.nr_extents << exch.in.extent_order) !=
279 (exch.out.nr_extents << exch.out.extent_order)) )
280 {
281 rc = -EINVAL;
282 goto fail_early;
283 }
285 /* Only privileged guests can allocate multi-page contiguous extents. */
286 if ( !multipage_allocation_permitted(current->domain,
287 exch.in.extent_order) ||
288 !multipage_allocation_permitted(current->domain,
289 exch.out.extent_order) )
290 {
291 rc = -EPERM;
292 goto fail_early;
293 }
295 if ( exch.in.extent_order <= exch.out.extent_order )
296 {
297 in_chunk_order = exch.out.extent_order - exch.in.extent_order;
298 out_chunk_order = 0;
299 }
300 else
301 {
302 in_chunk_order = 0;
303 out_chunk_order = exch.in.extent_order - exch.out.extent_order;
304 }
306 if ( likely(exch.in.domid == DOMID_SELF) )
307 {
308 d = rcu_lock_current_domain();
309 }
310 else
311 {
312 if ( (d = rcu_lock_domain_by_id(exch.in.domid)) == NULL )
313 goto fail_early;
315 if ( !IS_PRIV_FOR(current->domain, d) )
316 {
317 rcu_unlock_domain(d);
318 rc = -EPERM;
319 goto fail_early;
320 }
321 }
323 memflags |= MEMF_bits(domain_clamp_alloc_bitsize(
324 d,
325 XENMEMF_get_address_bits(exch.out.mem_flags) ? :
326 (BITS_PER_LONG+PAGE_SHIFT)));
327 memflags |= MEMF_node(XENMEMF_get_node(exch.out.mem_flags));
329 for ( i = (exch.nr_exchanged >> in_chunk_order);
330 i < (exch.in.nr_extents >> in_chunk_order);
331 i++ )
332 {
333 if ( hypercall_preempt_check() )
334 {
335 exch.nr_exchanged = i << in_chunk_order;
336 rcu_unlock_domain(d);
337 if ( copy_field_to_guest(arg, &exch, nr_exchanged) )
338 return -EFAULT;
339 return hypercall_create_continuation(
340 __HYPERVISOR_memory_op, "lh", XENMEM_exchange, arg);
341 }
343 /* Steal a chunk's worth of input pages from the domain. */
344 for ( j = 0; j < (1UL << in_chunk_order); j++ )
345 {
346 if ( unlikely(__copy_from_guest_offset(
347 &gmfn, exch.in.extent_start, (i<<in_chunk_order)+j, 1)) )
348 {
349 rc = -EFAULT;
350 goto fail;
351 }
353 for ( k = 0; k < (1UL << exch.in.extent_order); k++ )
354 {
355 #ifdef CONFIG_X86
356 p2m_type_t p2mt;
358 /* Shared pages cannot be exchanged */
359 mfn = mfn_x(gfn_to_mfn_unshare(d, gmfn + k, &p2mt, 0));
360 if ( p2m_is_shared(p2mt) )
361 {
362 rc = -ENOMEM;
363 goto fail;
364 }
365 #else /* !CONFIG_X86 */
366 mfn = gmfn_to_mfn(d, gmfn + k);
367 #endif
368 if ( unlikely(!mfn_valid(mfn)) )
369 {
370 rc = -EINVAL;
371 goto fail;
372 }
374 page = mfn_to_page(mfn);
376 if ( unlikely(steal_page(d, page, MEMF_no_refcount)) )
377 {
378 rc = -EINVAL;
379 goto fail;
380 }
382 page_list_add(page, &in_chunk_list);
383 }
384 }
386 /* Allocate a chunk's worth of anonymous output pages. */
387 for ( j = 0; j < (1UL << out_chunk_order); j++ )
388 {
389 page = alloc_domheap_pages(NULL, exch.out.extent_order, memflags);
390 if ( unlikely(page == NULL) )
391 {
392 rc = -ENOMEM;
393 goto fail;
394 }
396 page_list_add(page, &out_chunk_list);
397 }
399 /*
400 * Success! Beyond this point we cannot fail for this chunk.
401 */
403 /* Destroy final reference to each input page. */
404 while ( (page = page_list_remove_head(&in_chunk_list)) )
405 {
406 unsigned long gfn;
408 if ( !test_and_clear_bit(_PGC_allocated, &page->count_info) )
409 BUG();
410 mfn = page_to_mfn(page);
411 gfn = mfn_to_gmfn(d, mfn);
412 /* Pages were unshared above */
413 BUG_ON(SHARED_M2P(gfn));
414 guest_physmap_remove_page(d, gfn, mfn, 0);
415 put_page(page);
416 }
418 /* Assign each output page to the domain. */
419 j = 0;
420 while ( (page = page_list_remove_head(&out_chunk_list)) )
421 {
422 if ( assign_pages(d, page, exch.out.extent_order,
423 MEMF_no_refcount) )
424 {
425 unsigned long dec_count;
426 bool_t drop_dom_ref;
428 /*
429 * Pages in in_chunk_list is stolen without
430 * decreasing the tot_pages. If the domain is dying when
431 * assign pages, we need decrease the count. For those pages
432 * that has been assigned, it should be covered by
433 * domain_relinquish_resources().
434 */
435 dec_count = (((1UL << exch.in.extent_order) *
436 (1UL << in_chunk_order)) -
437 (j * (1UL << exch.out.extent_order)));
439 spin_lock(&d->page_alloc_lock);
440 d->tot_pages -= dec_count;
441 drop_dom_ref = (dec_count && !d->tot_pages);
442 spin_unlock(&d->page_alloc_lock);
444 if ( drop_dom_ref )
445 put_domain(d);
447 free_domheap_pages(page, exch.out.extent_order);
448 goto dying;
449 }
451 /* Note that we ignore errors accessing the output extent list. */
452 (void)__copy_from_guest_offset(
453 &gpfn, exch.out.extent_start, (i<<out_chunk_order)+j, 1);
455 mfn = page_to_mfn(page);
456 guest_physmap_add_page(d, gpfn, mfn, exch.out.extent_order);
458 if ( !paging_mode_translate(d) )
459 {
460 for ( k = 0; k < (1UL << exch.out.extent_order); k++ )
461 set_gpfn_from_mfn(mfn + k, gpfn + k);
462 (void)__copy_to_guest_offset(
463 exch.out.extent_start, (i<<out_chunk_order)+j, &mfn, 1);
464 }
465 j++;
466 }
467 BUG_ON( !(d->is_dying) && (j != (1UL << out_chunk_order)) );
468 }
470 exch.nr_exchanged = exch.in.nr_extents;
471 if ( copy_field_to_guest(arg, &exch, nr_exchanged) )
472 rc = -EFAULT;
473 rcu_unlock_domain(d);
474 return rc;
476 /*
477 * Failed a chunk! Free any partial chunk work. Tell caller how many
478 * chunks succeeded.
479 */
480 fail:
481 /* Reassign any input pages we managed to steal. */
482 while ( (page = page_list_remove_head(&in_chunk_list)) )
483 if ( assign_pages(d, page, 0, MEMF_no_refcount) )
484 BUG();
485 dying:
486 rcu_unlock_domain(d);
487 /* Free any output pages we managed to allocate. */
488 while ( (page = page_list_remove_head(&out_chunk_list)) )
489 free_domheap_pages(page, exch.out.extent_order);
491 exch.nr_exchanged = i << in_chunk_order;
493 fail_early:
494 if ( copy_field_to_guest(arg, &exch, nr_exchanged) )
495 rc = -EFAULT;
496 return rc;
497 }
499 long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE(void) arg)
500 {
501 struct domain *d;
502 int rc, op;
503 unsigned int address_bits;
504 unsigned long start_extent;
505 struct xen_memory_reservation reservation;
506 struct memop_args args;
507 domid_t domid;
509 op = cmd & MEMOP_CMD_MASK;
511 switch ( op )
512 {
513 case XENMEM_increase_reservation:
514 case XENMEM_decrease_reservation:
515 case XENMEM_populate_physmap:
516 start_extent = cmd >> MEMOP_EXTENT_SHIFT;
518 if ( copy_from_guest(&reservation, arg, 1) )
519 return start_extent;
521 /* Is size too large for us to encode a continuation? */
522 if ( reservation.nr_extents > (ULONG_MAX >> MEMOP_EXTENT_SHIFT) )
523 return start_extent;
525 if ( unlikely(start_extent >= reservation.nr_extents) )
526 return start_extent;
528 args.extent_list = reservation.extent_start;
529 args.nr_extents = reservation.nr_extents;
530 args.extent_order = reservation.extent_order;
531 args.nr_done = start_extent;
532 args.preempted = 0;
533 args.memflags = 0;
535 address_bits = XENMEMF_get_address_bits(reservation.mem_flags);
536 if ( (address_bits != 0) &&
537 (address_bits < (get_order_from_pages(max_page) + PAGE_SHIFT)) )
538 {
539 if ( address_bits <= PAGE_SHIFT )
540 return start_extent;
541 args.memflags = MEMF_bits(address_bits);
542 }
544 args.memflags |= MEMF_node(XENMEMF_get_node(reservation.mem_flags));
545 if ( reservation.mem_flags & XENMEMF_exact_node_request )
546 args.memflags |= MEMF_exact_node;
548 if ( op == XENMEM_populate_physmap
549 && (reservation.mem_flags & XENMEMF_populate_on_demand) )
550 args.memflags |= MEMF_populate_on_demand;
552 if ( likely(reservation.domid == DOMID_SELF) )
553 {
554 d = rcu_lock_current_domain();
555 }
556 else
557 {
558 if ( (d = rcu_lock_domain_by_id(reservation.domid)) == NULL )
559 return start_extent;
560 if ( !IS_PRIV_FOR(current->domain, d) )
561 {
562 rcu_unlock_domain(d);
563 return start_extent;
564 }
565 }
566 args.domain = d;
568 rc = xsm_memory_adjust_reservation(current->domain, d);
569 if ( rc )
570 {
571 rcu_unlock_domain(d);
572 return rc;
573 }
575 switch ( op )
576 {
577 case XENMEM_increase_reservation:
578 increase_reservation(&args);
579 break;
580 case XENMEM_decrease_reservation:
581 decrease_reservation(&args);
582 break;
583 default: /* XENMEM_populate_physmap */
584 populate_physmap(&args);
585 break;
586 }
588 rcu_unlock_domain(d);
590 rc = args.nr_done;
592 if ( args.preempted )
593 return hypercall_create_continuation(
594 __HYPERVISOR_memory_op, "lh",
595 op | (rc << MEMOP_EXTENT_SHIFT), arg);
597 break;
599 case XENMEM_exchange:
600 rc = memory_exchange(guest_handle_cast(arg, xen_memory_exchange_t));
601 break;
603 case XENMEM_maximum_ram_page:
604 rc = max_page;
605 break;
607 case XENMEM_current_reservation:
608 case XENMEM_maximum_reservation:
609 case XENMEM_maximum_gpfn:
610 if ( copy_from_guest(&domid, arg, 1) )
611 return -EFAULT;
613 rc = rcu_lock_target_domain_by_id(domid, &d);
614 if ( rc )
615 return rc;
617 rc = xsm_memory_stat_reservation(current->domain, d);
618 if ( rc )
619 {
620 rcu_unlock_domain(d);
621 return rc;
622 }
624 switch ( op )
625 {
626 case XENMEM_current_reservation:
627 rc = d->tot_pages;
628 break;
629 case XENMEM_maximum_reservation:
630 rc = d->max_pages;
631 break;
632 default:
633 ASSERT(op == XENMEM_maximum_gpfn);
634 rc = domain_get_maximum_gpfn(d);
635 break;
636 }
638 rcu_unlock_domain(d);
640 break;
642 default:
643 rc = arch_memory_op(op, arg);
644 break;
645 }
647 return rc;
648 }
650 /*
651 * Local variables:
652 * mode: C
653 * c-set-style: "BSD"
654 * c-basic-offset: 4
655 * tab-width: 4
656 * indent-tabs-mode: nil
657 * End:
658 */