All of lore.kernel.org
 help / color / mirror / Atom feed
From: Roger Pau Monne <roger.pau@citrix.com>
To: xen-devel@lists.xenproject.org
Cc: Ian Campbell <ian.campbell@citrix.com>,
	Andrew Cooper <andrew.cooper3@citrix.com>,
	Tim Deegan <tim@xen.org>,
	Stefano Stabellini <stefano.stabellini@citrix.com>,
	Jan Beulich <jbeulich@suse.com>,
	Roger Pau Monne <roger.pau@citrix.com>
Subject: [PATCH v7 1/4] xen: introduce a helper to allocate non-contiguous memory
Date: Thu, 14 May 2015 17:06:14 +0200	[thread overview]
Message-ID: <1431615977-38284-2-git-send-email-roger.pau@citrix.com> (raw)
In-Reply-To: <1431615977-38284-1-git-send-email-roger.pau@citrix.com>

The allocator uses independent calls to alloc_domheap_pages in order to get
the desired amount of memory and then maps all the independent physical
addresses into a contiguous virtual address space.

Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
Tested-by: Julien Grall <julien.grall@citrix.com> (ARM)
Cc: Stefano Stabellini <stefano.stabellini@citrix.com>
Cc: Ian Campbell <ian.campbell@citrix.com>
Cc: Jan Beulich <jbeulich@suse.com>
Cc: Tim Deegan <tim@xen.org>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>
---
Change since v6:
 - Use clear_page instead of memset in vzalloc.

Changes since v5:
 - Call vunmap before freeing the pages in vfree.
 - Simplify the fail case by reusing i.

Changes since v4:
 - Remove the red-black tree, the same can be achieved by doing a VA -> MFN
   translation.
 - Provide a vzalloc that zeroes the allocated memory area.
 - Allocate domheap annonymous pages.
---
 xen/common/vmap.c          | 67 ++++++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/mm.h   |  2 ++
 xen/include/asm-x86/page.h |  2 ++
 xen/include/xen/vmap.h     |  3 +++
 4 files changed, 74 insertions(+)

diff --git a/xen/common/vmap.c b/xen/common/vmap.c
index 739d468..4519e0c 100644
--- a/xen/common/vmap.c
+++ b/xen/common/vmap.c
@@ -215,4 +215,71 @@ void vunmap(const void *va)
 #endif
     vm_free(va);
 }
+
+void *vmalloc(size_t size)
+{
+    unsigned long *mfn;
+    size_t pages, i;
+    struct page_info *pg;
+    void *va;
+
+    ASSERT( size != 0 );
+
+    pages = PFN_UP(size);
+    mfn = xmalloc_array(unsigned long, pages);
+    if ( mfn == NULL )
+        return NULL;
+
+    for ( i = 0; i < pages; i++ )
+    {
+        pg = alloc_domheap_page(NULL, 0);
+        if ( pg == NULL )
+            goto error;
+        mfn[i] = page_to_mfn(pg);
+    }
+
+    va = vmap(mfn, pages);
+    if ( va == NULL )
+        goto error;
+
+    xfree(mfn);
+    return va;
+
+ error:
+    while ( i-- )
+         free_domheap_page(mfn_to_page(mfn[i]));
+    xfree(mfn);
+    return NULL;
+}
+
+void *vzalloc(size_t size)
+{
+    void *p = vmalloc(size);
+    int i;
+
+    if ( p == NULL )
+        return NULL;
+
+    for ( i = 0; i < size; i += PAGE_SIZE )
+        clear_page(p + i);
+
+    return p;
+}
+
+void vfree(void *va)
+{
+    unsigned int i, pages = vm_size(va);
+    struct page_info *pg;
+    PAGE_LIST_HEAD(pg_list);
+
+    ASSERT( pages != 0 );
+
+    for ( i = 0; i < pages; i++ )
+        page_list_add(vmap_to_page(va + i * PAGE_SIZE), &pg_list);
+
+    vunmap(va);
+
+    while ( (pg = page_list_remove_head(&pg_list)) != NULL )
+        free_domheap_page(pg);
+}
 #endif
diff --git a/xen/include/asm-arm/mm.h b/xen/include/asm-arm/mm.h
index d25e485..c0afcec 100644
--- a/xen/include/asm-arm/mm.h
+++ b/xen/include/asm-arm/mm.h
@@ -208,6 +208,8 @@ static inline void __iomem *ioremap_wc(paddr_t start, size_t len)
 #define pfn_to_paddr(pfn) ((paddr_t)(pfn) << PAGE_SHIFT)
 #define paddr_to_pfn(pa)  ((unsigned long)((pa) >> PAGE_SHIFT))
 #define paddr_to_pdx(pa)    pfn_to_pdx(paddr_to_pfn(pa))
+#define vmap_to_mfn(va)     paddr_to_pfn(virt_to_maddr((vaddr_t)va))
+#define vmap_to_page(va)    mfn_to_page(vmap_to_mfn(va))
 
 /* Page-align address and convert to frame number format */
 #define paddr_to_pfn_aligned(paddr)    paddr_to_pfn(PAGE_ALIGN(paddr))
diff --git a/xen/include/asm-x86/page.h b/xen/include/asm-x86/page.h
index a8bc999..8977a76 100644
--- a/xen/include/asm-x86/page.h
+++ b/xen/include/asm-x86/page.h
@@ -262,6 +262,8 @@ void copy_page_sse2(void *, const void *);
 #define pfn_to_paddr(pfn)   __pfn_to_paddr(pfn)
 #define paddr_to_pfn(pa)    __paddr_to_pfn(pa)
 #define paddr_to_pdx(pa)    pfn_to_pdx(paddr_to_pfn(pa))
+#define vmap_to_mfn(va)     l1e_get_pfn(*virt_to_xen_l1e((unsigned long)(va)))
+#define vmap_to_page(va)    mfn_to_page(vmap_to_mfn(va))
 
 #endif /* !defined(__ASSEMBLY__) */
 
diff --git a/xen/include/xen/vmap.h b/xen/include/xen/vmap.h
index b1923dd..a13591d 100644
--- a/xen/include/xen/vmap.h
+++ b/xen/include/xen/vmap.h
@@ -11,6 +11,9 @@ void *__vmap(const unsigned long *mfn, unsigned int granularity,
              unsigned int nr, unsigned int align, unsigned int flags);
 void *vmap(const unsigned long *mfn, unsigned int nr);
 void vunmap(const void *);
+void *vmalloc(size_t size);
+void *vzalloc(size_t size);
+void vfree(void *va);
 
 void __iomem *ioremap(paddr_t, size_t);
 
-- 
1.9.5 (Apple Git-50.3)


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

  reply	other threads:[~2015-05-14 15:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-14 15:06 [PATCH v7 0/4] xen/pvh: enable migration on PVH Dom0 Roger Pau Monne
2015-05-14 15:06 ` Roger Pau Monne [this message]
2015-05-14 15:06 ` [PATCH v7 2/4] xen/shadow: fix shadow_track_dirty_vram to work on hvm guests Roger Pau Monne
2015-05-22  7:59   ` Jan Beulich
2015-05-14 15:06 ` [PATCH v7 3/4] xen/hap: make hap_track_dirty_vram use non-contiguous memory for temporary map Roger Pau Monne
2015-05-14 15:06 ` [PATCH v7 4/4] xen: rework paging_log_dirty_op to work with hvm guests Roger Pau Monne
2015-05-14 15:21 ` [PATCH v7 0/4] xen/pvh: enable migration on PVH Dom0 Tim Deegan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1431615977-38284-2-git-send-email-roger.pau@citrix.com \
    --to=roger.pau@citrix.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=ian.campbell@citrix.com \
    --cc=jbeulich@suse.com \
    --cc=stefano.stabellini@citrix.com \
    --cc=tim@xen.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.