On Fri, Nov 19, 2010 at 04:33:56PM -0700, Eric B Munson wrote:
> When the kernel supports MAP_HUGETLB use it for requesting
> a huge page backed area instead of creating a file descriptor.
> 
> Signed-off-by: Eric B Munson <emun...@mgebm.net>
> ---
>  alloc.c |   41 +++++++++++++++++++++++++++++++++++------
>  1 files changed, 35 insertions(+), 6 deletions(-)
> 
> diff --git a/alloc.c b/alloc.c
> index a7d37e5..bdee7b3 100644
> --- a/alloc.c
> +++ b/alloc.c
> @@ -77,7 +77,7 @@ static void *fallback_base_pages(size_t len, ghp_t flags)
>  void *get_huge_pages(size_t len, ghp_t flags)
>  {
>       void *buf;
> -     int buf_fd;
> +     int buf_fd = -1;
>       int saved_error;
>       int mmap_reserve = __hugetlb_opts.no_reserve ? MAP_NORESERVE : 0;
>  
> @@ -85,6 +85,27 @@ void *get_huge_pages(size_t len, ghp_t flags)
>       if (flags & GHR_MASK)
>               ERROR("Improper use of GHR_* in get_huge_pages()\n");
>  
> +#ifdef MAP_HUGETLB

The ifdef here should be unnecessary because it needs to exist for
map_hugetlb to be 1, right? That would avoid code duplication.

> +     if (__hugetlb_opts.map_hugetlb &&
> +                     gethugepagesize() == kernel_default_hugepage_size()) {
> +             /* Because we can use MAP_HUGETLB, we simply mmap the region */
> +             buf = mmap(NULL, len, PROT_READ|PROT_WRITE,
> +                     MAP_PRIVATE|MAP_ANONYMOUS|MAP_HUGETLB|mmap_reserve,
> +                     0, 0);
> +     } else {
> +             /* Create a file descriptor for the new region */
> +             buf_fd = hugetlbfs_unlinked_fd();
> +             if (buf_fd < 0) {
> +                     WARNING("Couldn't open hugetlbfs file for %zd-sized 
> buffer\n",
> +                                     len);
> +                     return NULL;
> +             }
> +
> +             /* Map the requested region */
> +             buf = mmap(NULL, len, PROT_READ|PROT_WRITE,
> +                     MAP_PRIVATE|mmap_reserve, buf_fd, 0);
> +     }
> +#else
>       /* Create a file descriptor for the new region */
>       buf_fd = hugetlbfs_unlinked_fd();
>       if (buf_fd < 0) {
> @@ -95,20 +116,28 @@ void *get_huge_pages(size_t len, ghp_t flags)
>  
>       /* Map the requested region */
>       buf = mmap(NULL, len, PROT_READ|PROT_WRITE,
> -              MAP_PRIVATE|mmap_reserve, buf_fd, 0);
> +             MAP_PRIVATE|mmap_reserve, buf_fd, 0);
> +#endif
> +
>       if (buf == MAP_FAILED) {
> -             close(buf_fd);
> +             if (buf_fd >= 0)
> +                     close(buf_fd);
>  
>               WARNING("get_huge_pages: New region mapping failed (flags: 
> 0x%lX): %s\n",
>                       flags, strerror(errno));
>               return NULL;
>       }
>  
> -     /* Fault the region to ensure accesses succeed */
> +     /*
> +      * Fault the region to ensure accesses succeed, buf_fd is passed
> +      * regarless of how we mmap'd because if MAP_HUGETLB was used the value
> +      * in buf_fd is ignored
> +      */
>       if (hugetlbfs_prefault(buf_fd, buf, len) != 0) {
>               saved_error = errno;
>               munmap(buf, len);
> -             close(buf_fd);
> +             if (buf_fd >= 0)
> +                     close(buf_fd);
>  
>               WARNING("get_huge_pages: Prefaulting failed (flags: 0x%lX): 
> %s\n",
>                       flags, strerror(saved_error));
> @@ -116,7 +145,7 @@ void *get_huge_pages(size_t len, ghp_t flags)
>       }
>  
>       /* Close the file so we do not have to track the descriptor */
> -     if (close(buf_fd) != 0) {
> +     if (buf_fd >= 0 && close(buf_fd) != 0) {
>               WARNING("Failed to close new buffer fd: %s\n", strerror(errno));
>               munmap(buf, len);
>               return NULL;

-- 
Mel Gorman
Part-time Phd Student                          Linux Technology Center
University of Limerick                         IBM Dublin Software Lab

------------------------------------------------------------------------------
Increase Visibility of Your 3D Game App & Earn a Chance To Win $500!
Tap into the largest installed PC base & get more eyes on your game by
optimizing for Intel(R) Graphics Technology. Get started today with the
Intel(R) Software Partner Program. Five $500 cash prizes are up for grabs.
http://p.sf.net/sfu/intelisp-dev2dev
_______________________________________________
Libhugetlbfs-devel mailing list
Libhugetlbfs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libhugetlbfs-devel

Reply via email to