When HUGETLB_SHARE=1, read-only text segments are copied to a file on
a hugetlbfs mount and shared between multiple processes.  However with
kernel 2.6.27 and later, the expectation is that pages will be reserved for
MAP_PRIVATE mappings as well as MAP_SHARED. This will render HUGETLB_SHARE
ineffective as huge pages will be reserved that already exist in the page
cache.  This patch disables reservations for read-only mappings when sharing
is enabled on the assumption mprotect() will never be called for remapped
read-only segments.

Signed-off-by: Mel Gorman <[EMAIL PROTECTED]>
--- 
diff --git a/elflink.c b/elflink.c
index 2816f8f..fdb5698 100644
--- a/elflink.c
+++ b/elflink.c
@@ -1035,6 +1035,7 @@ static void remap_segments(struct seg_info *seg, int num)
        unsigned long start, offset, mapsize;
        long page_size = getpagesize();
        long hpage_size = gethugepagesize();
+       int mmap_flags;
 
        /*
         * XXX: The bogus call to mmap below forces ld.so to resolve the
@@ -1064,9 +1065,19 @@ static void remap_segments(struct seg_info *seg, int num)
                start = ALIGN_DOWN((unsigned long)seg[i].vaddr, hpage_size);
                offset = (unsigned long)(seg[i].vaddr - start);
                mapsize = ALIGN(offset + seg[i].memsz, hpage_size);
+               mmap_flags = MAP_PRIVATE|MAP_FIXED;
+
+               /*
+                * If HUGETLB_SHARE is enabled and this is a read-only
+                * segment, then use MAP_NORESERVE. The assumption is that
+                * the pages already exist in the hugetlbfs file and that
+                * mprotect() will not be called requiring a COW
+                */
+               if (sharing && !(seg[i].prot & PROT_WRITE))
+                       mmap_flags |= MAP_NORESERVE;
 
                p = mmap((void *) start, mapsize, seg[i].prot,
-                        MAP_PRIVATE|MAP_FIXED, seg[i].fd, 0);
+                        mmap_flags, seg[i].fd, 0);
                if (p == MAP_FAILED)
                        unmapped_abort("Failed to map hugepage segment %u: "
                                        "%p-%p (errno=%u)\n", i, start,

-------------------------------------------------------------------------
Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW!
Studies have shown that voting for your favorite open source project,
along with a healthy diet, reduces your potential for chronic lameness
and boredom. Vote Now at http://www.sourceforge.net/community/cca08
_______________________________________________
Libhugetlbfs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libhugetlbfs-devel

Reply via email to