[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] Re: [PATCH] libxc: osdep: convert hypercall buffer allocation



On Tue, 2011-03-08 at 11:16 +0000, Ian Campbell wrote:
> # HG changeset patch
> # User Ian Campbell <ian.campbell@xxxxxxxxxx>
> # Date 1299488485 0
> # Node ID 72cbc9120370ae9d04ad93a9bf11577a070d8dc2
> # Parent  f2212579853a585a0cfdb3533a7136b5c2069ea7
> libxc: osdep: convert hypercall buffer allocation

I should have said something like:

This will allow us to use OS specific interfaces to ensure that the
allocated memory is safe for use as a hypercall buffer in the future.

Ian.

> 
> Signed-off-by: Ian Campbell <ian.campbell@xxxxxxxxxx>
> 
> diff -r f2212579853a -r 72cbc9120370 tools/libxc/xc_hcall_buf.c
> --- a/tools/libxc/xc_hcall_buf.c      Mon Mar 07 09:01:25 2011 +0000
> +++ b/tools/libxc/xc_hcall_buf.c      Mon Mar 07 09:01:25 2011 +0000
> @@ -17,7 +17,7 @@
>   */
>  
>  #include <stdlib.h>
> -#include <malloc.h>
> +#include <string.h>
>  #include <pthread.h>
>  
>  #include "xc_private.h"
> @@ -95,15 +95,6 @@ static int hypercall_buffer_cache_free(x
>      return rc;
>  }
>  
> -static void do_hypercall_buffer_free_pages(void *ptr, int nr_pages)
> -{
> -#ifndef __sun__
> -    (void) munlock(ptr, nr_pages * PAGE_SIZE);
> -#endif
> -
> -    free(ptr);
> -}
> -
>  void xc__hypercall_buffer_cache_release(xc_interface *xch)
>  {
>      void *p;
> @@ -126,7 +117,7 @@ void xc__hypercall_buffer_cache_release(
>      while ( xch->hypercall_buffer_cache_nr > 0 )
>      {
>          p = xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr];
> -        do_hypercall_buffer_free_pages(p, 1);
> +        xch->ops->u.privcmd.free_hypercall_buffer(xch, xch->ops_handle, p, 
> 1);
>      }
>  
>      hypercall_buffer_cache_unlock(xch);
> @@ -134,36 +125,18 @@ void xc__hypercall_buffer_cache_release(
>  
>  void *xc__hypercall_buffer_alloc_pages(xc_interface *xch, 
> xc_hypercall_buffer_t *b, int nr_pages)
>  {
> -    size_t size = nr_pages * PAGE_SIZE;
>      void *p = hypercall_buffer_cache_alloc(xch, nr_pages);
>  
> -    if ( !p ) {
> -#if defined(_POSIX_C_SOURCE) && !defined(__sun__)
> -        int ret;
> -        ret = posix_memalign(&p, PAGE_SIZE, size);
> -        if (ret != 0)
> -            return NULL;
> -#elif defined(__NetBSD__) || defined(__OpenBSD__)
> -        p = valloc(size);
> -#else
> -        p = memalign(PAGE_SIZE, size);
> -#endif
> +    if ( !p )
> +        p = xch->ops->u.privcmd.alloc_hypercall_buffer(xch, xch->ops_handle, 
> nr_pages);
>  
> -        if (!p)
> -            return NULL;
> -
> -#ifndef __sun__
> -        if ( mlock(p, size) < 0 )
> -        {
> -            free(p);
> -            return NULL;
> -        }
> -#endif
> -    }
> +    if (!p)
> +        return NULL;
>  
>      b->hbuf = p;
>  
> -    memset(p, 0, size);
> +    memset(p, 0, nr_pages * PAGE_SIZE);
> +
>      return b->hbuf;
>  }
>  
> @@ -173,7 +146,7 @@ void xc__hypercall_buffer_free_pages(xc_
>          return;
>  
>      if ( !hypercall_buffer_cache_free(xch, b->hbuf, nr_pages) )
> -        do_hypercall_buffer_free_pages(b->hbuf, nr_pages);
> +        xch->ops->u.privcmd.free_hypercall_buffer(xch, xch->ops_handle, 
> b->hbuf, nr_pages);
>  }
>  
>  struct allocation_header {
> diff -r f2212579853a -r 72cbc9120370 tools/libxc/xc_linux_osdep.c
> --- a/tools/libxc/xc_linux_osdep.c    Mon Mar 07 09:01:25 2011 +0000
> +++ b/tools/libxc/xc_linux_osdep.c    Mon Mar 07 09:01:25 2011 +0000
> @@ -84,6 +84,30 @@ static int linux_privcmd_close(xc_interf
>  {
>      int fd = (int)h;
>      return close(fd);
> +}
> +
> +static void *linux_privcmd_alloc_hypercall_buffer(xc_interface *xch, 
> xc_osdep_handle h, int npages)
> +{
> +    size_t size = npages * XC_PAGE_SIZE;
> +    void *p;
> +    int ret;
> +
> +    ret = posix_memalign(&p, XC_PAGE_SIZE, size);
> +    if (ret != 0 || !p)
> +        return NULL;
> +
> +    if ( mlock(p, size) < 0 )
> +    {
> +        free(p);
> +        return NULL;
> +    }
> +    return p;
> +}
> +
> +static void linux_privcmd_free_hypercall_buffer(xc_interface *xch, 
> xc_osdep_handle h, void *ptr, int npages)
> +{
> +    (void) munlock(ptr, npages * XC_PAGE_SIZE);
> +    free(ptr);
>  }
>  
>  static int linux_privcmd_hypercall(xc_interface *xch, xc_osdep_handle h, 
> privcmd_hypercall_t *hypercall)
> @@ -344,6 +368,9 @@ static struct xc_osdep_ops linux_privcmd
>      .close = &linux_privcmd_close,
>  
>      .u.privcmd = {
> +        .alloc_hypercall_buffer = &linux_privcmd_alloc_hypercall_buffer,
> +        .free_hypercall_buffer = &linux_privcmd_free_hypercall_buffer,
> +
>          .hypercall = &linux_privcmd_hypercall,
>  
>          .map_foreign_batch = &linux_privcmd_map_foreign_batch,
> diff -r f2212579853a -r 72cbc9120370 tools/libxc/xc_minios.c
> --- a/tools/libxc/xc_minios.c Mon Mar 07 09:01:25 2011 +0000
> +++ b/tools/libxc/xc_minios.c Mon Mar 07 09:01:25 2011 +0000
> @@ -37,6 +37,7 @@
>  #include <assert.h>
>  #include <stdint.h>
>  #include <inttypes.h>
> +#include <malloc.h>
>  
>  #include "xc_private.h"
>  
> @@ -68,6 +69,16 @@ void minios_interface_close_fd(int fd)
>  void minios_interface_close_fd(int fd)
>  {
>      files[fd].type = FTYPE_NONE;
> +}
> +
> +static void *minios_privcmd_alloc_hypercall_buffer(xc_interface *xch, 
> xc_osdep_handle h, int npages)
> +{
> +    return memalign(PAGE_SIZE, npages * PAGE_SIZE);
> +}
> +
> +static void minios_privcmd_free_hypercall_buffer(xc_interface *xch, 
> xc_osdep_handle h, void *ptr, int npages)
> +{
> +    free(ptr);
>  }
>  
>  static int minios_privcmd_hypercall(xc_interface *xch, xc_osdep_handle h, 
> privcmd_hypercall_t *hypercall)
> @@ -187,6 +198,9 @@ static struct xc_osdep_ops minios_privcm
>      .close = &minios_privcmd_close,
>  
>      .u.privcmd = {
> +        .alloc_hypercall_buffer = &minios_privcmd_alloc_hypercall_buffer,
> +        .free_hypercall_buffer = &minios_privcmd_free_hypercall_buffer,
> +
>          .hypercall = &minios_privcmd_hypercall,
>  
>          .map_foreign_batch = &minios_privcmd_map_foreign_batch,
> diff -r f2212579853a -r 72cbc9120370 tools/libxc/xc_netbsd.c
> --- a/tools/libxc/xc_netbsd.c Mon Mar 07 09:01:25 2011 +0000
> +++ b/tools/libxc/xc_netbsd.c Mon Mar 07 09:01:25 2011 +0000
> @@ -23,6 +23,8 @@
>  #include <xen/sys/evtchn.h>
>  #include <unistd.h>
>  #include <fcntl.h>
> +#include <malloc.h>
> +#include <sys/mman.h>
>  
>  static xc_osdep_handle netbsd_privcmd_open(xc_interface *xch)
>  {
> @@ -64,6 +66,28 @@ static int netbsd_privcmd_close(xc_inter
>  {
>      int fd = (int)h;
>      return close(fd);
> +}
> +
> +static void *netbsd_privcmd_alloc_hypercall_buffer(xc_interface *xch, 
> xc_osdep_handle h, int npages)
> +{
> +    size_t size = npages * XC_PAGE_SIZE;
> +    void *p = valloc(size);
> +
> +    if (!p)
> +        return NULL;
> +
> +    if ( mlock(p, size) < 0 )
> +    {
> +        free(p);
> +        return NULL;
> +    }
> +    return p;
> +}
> +
> +static void netbsd_privcmd_free_hypercall_buffer(xc_interface *xch, 
> xc_osdep_handle h, void *ptr, int npages)
> +{
> +    (void) munlock(ptr, npages * XC_PAGE_SIZE);
> +    free(ptr);
>  }
>  
>  static int netbsd_privcmd_hypercall(xc_interface *xch, xc_osdep_handle h, 
> privcmd_hypercall_t *hypercall)
> @@ -181,6 +205,9 @@ static struct xc_osdep_ops netbsd_privcm
>      .close = &netbsd_privcmd_close,
>  
>      .u.privcmd = {
> +        .alloc_hypercall_buffer = &netbsd_privcmd_alloc_hypercall_buffer,
> +        .free_hypercall_buffer = &netbsd_privcmd_free_hypercall_buffer,
> +
>          .hypercall = &netbsd_privcmd_hypercall,
>  
>          .map_foreign_batch = &netbsd_privcmd_map_foreign_batch,
> diff -r f2212579853a -r 72cbc9120370 tools/libxc/xc_solaris.c
> --- a/tools/libxc/xc_solaris.c        Mon Mar 07 09:01:25 2011 +0000
> +++ b/tools/libxc/xc_solaris.c        Mon Mar 07 09:01:25 2011 +0000
> @@ -24,6 +24,7 @@
>  #include <xen/sys/evtchn.h>
>  #include <unistd.h>
>  #include <fcntl.h>
> +#include <malloc.h>
>  
>  static xc_osdep_handle solaris_privcmd_open(xc_interface *xch)
>  {
> @@ -65,6 +66,16 @@ static int solaris_privcmd_close(xc_inte
>  {
>      int fd = (int)h;
>      return close(fd);
> +}
> +
> +static void *solaris_privcmd_alloc_hypercall_buffer(xc_interface *xch, 
> xc_osdep_handle h, int npages)
> +{
> +    return memalign(XC_PAGE_SIZE, npages * XC_PAGE_SIZE);
> +}
> +
> +static void solaris_privcmd_free_hypercall_buffer(xc_interface *xch, 
> xc_osdep_handle h, void *ptr, int npages)
> +{
> +    free(ptr);
>  }
>  
>  static int solaris_privcmd_hypercall(xc_interface *xch, xc_osdep_handle h, 
> privcmd_hypercall_t *hypercall)
> @@ -172,6 +183,9 @@ static struct xc_osdep_ops solaris_privc
>      .close = &solaris_privcmd_close,
>  
>      .u.privcmd = {
> +        .alloc_hypercall_buffer = &solaris_privcmd_alloc_hypercall_buffer,
> +        .free_hypercall_buffer = &solaris_privcmd_free_hypercall_buffer,
> +
>          .hypercall = &solaris_privcmd_hypercall;
>  
>          .map_foreign_batch = &solaris_privcmd_map_foreign_batch,
> diff -r f2212579853a -r 72cbc9120370 tools/libxc/xenctrlosdep.h
> --- a/tools/libxc/xenctrlosdep.h      Mon Mar 07 09:01:25 2011 +0000
> +++ b/tools/libxc/xenctrlosdep.h      Mon Mar 07 09:01:25 2011 +0000
> @@ -74,6 +74,9 @@ struct xc_osdep_ops
>  
>      union {
>          struct {
> +            void *(*alloc_hypercall_buffer)(xc_interface *xch, 
> xc_osdep_handle h, int npages);
> +            void (*free_hypercall_buffer)(xc_interface *xch, xc_osdep_handle 
> h, void *ptr, int npages);
> +
>              int (*hypercall)(xc_interface *xch, xc_osdep_handle h, 
> privcmd_hypercall_t *hypercall);
>  
>              void *(*map_foreign_batch)(xc_interface *xch, xc_osdep_handle h, 
> uint32_t dom, int prot,



_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.