From c417722b9109e856f2e228d7e58be5f69765c311 Mon Sep 17 00:00:00 2001 From: Konrad Rzeszutek Wilk Date: Mon, 4 Mar 2013 10:34:03 -0500 Subject: [PATCH] xen/pvh: Use ballooning to allocate grant table pages [v2] This patch fixes a fixme in Linux to use alloc_xenballooned_pages() to allocate pfns for grant table pages instead of kmalloc. This also simplifies add to physmap on the xen side a bit. Signed-off-by: Mukesh Rathor Signed-off-by: Konrad Rzeszutek Wilk [v1: Rebase on xen/grant-table: correctly initialize grant table version 1] --- drivers/xen/grant-table.c | 67 +++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 14 deletions(-) diff --git a/drivers/xen/grant-table.c b/drivers/xen/grant-table.c index c5c2eb3bce5c..f4e067b05303 100644 --- a/drivers/xen/grant-table.c +++ b/drivers/xen/grant-table.c @@ -47,6 +47,7 @@ #include #include #include +#include #include #include @@ -1026,10 +1027,22 @@ static void gnttab_unmap_frames_v2(void) arch_gnttab_unmap(grstatus, nr_status_frames(nr_grant_frames)); } +static xen_pfn_t pvh_get_grant_pfn(int grant_idx) +{ + unsigned long vaddr; + unsigned int level; + pte_t *pte; + + vaddr = (unsigned long)(gnttab_shared.addr) + grant_idx * PAGE_SIZE; + pte = lookup_address(vaddr, &level); + BUG_ON(pte == NULL); + return pte_mfn(*pte); +} + static int gnttab_map(unsigned int start_idx, unsigned int end_idx) { struct gnttab_setup_table setup; - unsigned long start_gpfn; + unsigned long start_gpfn = 0; xen_pfn_t *frames; unsigned int nr_gframes = end_idx + 1; int rc; @@ -1041,8 +1054,6 @@ static int gnttab_map(unsigned int start_idx, unsigned int end_idx) if (xen_hvm_domain()) start_gpfn = xen_hvm_resume_frames >> PAGE_SHIFT; - else - start_gpfn = virt_to_pfn(gnttab_shared.addr); /* * Loop backwards, so that the first hypercall has the largest * index, ensuring that the table will grow only once. @@ -1051,7 +1062,11 @@ static int gnttab_map(unsigned int start_idx, unsigned int end_idx) xatp.domid = DOMID_SELF; xatp.idx = i; xatp.space = XENMAPSPACE_grant_table; - xatp.gpfn = start_gpfn + i; + if (xen_hvm_domain()) + xatp.gpfn = start_gpfn + i; + else + xatp.gpfn = pvh_get_grant_pfn(i); + rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp); if (rc != 0) { printk(KERN_WARNING @@ -1141,26 +1156,50 @@ static void gnttab_request_version(void) grant_table_version); } +/* + * PVH: we need three things: virtual address, pfns, and mfns. The pfns + * are allocated via ballooning, then we call arch_gnttab_map_shared to + * allocate the VA and put pfn's in the pte's for the VA. The mfn's are + * finally allocated in gnttab_map() by xen which also populates the P2M. + */ +static int xlated_setup_gnttab_pages(unsigned long numpages, void **addr) +{ + int i, rc; + unsigned long pfns[numpages]; + struct page *pages[numpages]; + + rc = alloc_xenballooned_pages(numpages, pages, 0); + if (rc != 0) { + pr_warn("%s Couldn't balloon alloc %ld pfns rc:%d\n", __func__, + numpages, rc); + return rc; + } + for (i = 0; i < numpages; i++) + pfns[i] = page_to_pfn(pages[i]); + + rc = arch_gnttab_map_shared(pfns, numpages, numpages, addr); + if (rc != 0) + free_xenballooned_pages(numpages, pages); + + return rc; +} + static int gnttab_setup(void) { + int rc; unsigned int max_nr_gframes; - char *kmsg = "Failed to kmalloc pages for pv in hvm grant frames\n"; max_nr_gframes = gnttab_max_grant_frames(); if (max_nr_gframes < nr_grant_frames) return -ENOSYS; - /* PVH note: xen will free existing kmalloc'd mfn in - * XENMEM_add_to_physmap. TBD/FIXME: use xen ballooning instead of - * kmalloc(). */ if (xen_pv_domain() && xen_feature(XENFEAT_auto_translated_physmap) && !gnttab_shared.addr) { - gnttab_shared.addr = - kmalloc(max_nr_gframes * PAGE_SIZE, GFP_KERNEL); - if (!gnttab_shared.addr) { - pr_warn("%s", kmsg); - return -ENOMEM; - } + + rc = xlated_setup_gnttab_pages((unsigned long)max_nr_gframes, + &gnttab_shared.addr); + if (rc != 0) + return rc; } if (xen_pv_domain()) return gnttab_map(0, nr_grant_frames - 1); -- 2.39.5