From 117a913e7b1c380bb99796588b266547de959de9 Mon Sep 17 00:00:00 2001
From: Lv Ying <lvying6@huawei.com>
Date: Thu, 23 Feb 2023 22:15:06 +0800
Subject: [PATCH 3/3] elf/ld.so: remove _mmap_hole when ld.so mmap PT_LOAD
 segment try to use hugepage

When ld.so mmap PT_LOAD segment try to use hugepage, it's unnecessary to
remap hole area, becasue __mmap_reserved_area already mmap reserved area
as PROT_NONE. Besides _mmap_hole may mmap hole area from 4KB page
misaligned addresses which will cause mmap faiure(invalid argument).

Signed-off-by: Lv Ying <lvying6@huawei.com>
---
 elf/dl-load.h                  |  2 --
 elf/dl-map-segments-hugepage.h | 26 --------------------------
 2 files changed, 28 deletions(-)

diff --git a/elf/dl-load.h b/elf/dl-load.h
index fcf91a47..f2428165 100644
--- a/elf/dl-load.h
+++ b/elf/dl-load.h
@@ -136,8 +136,6 @@ static const char *_dl_map_segments (struct link_map *l, int fd,
   N_("cannot map Non shared object file in hugepage")
 #define DL_MAP_SEGMENTS_ERROR_ARRANGE \
   N_("shared object's PT_LOAD segment in wrong arrange")
-#define DL_MAP_SEGMENTS_ERROR_MAP_HOLE_FILL \
-  N_("failed to mmap shared object's hole part of PT_LOAD")
 #define DL_MAP_RESERVED_HUGEPAGE_AREA_ERROR \
   N_("failed to map reserved 2MB contiguous hugepage va space")
 #define DL_FIND_EXEC_SEGMENT_ERROR  \
diff --git a/elf/dl-map-segments-hugepage.h b/elf/dl-map-segments-hugepage.h
index e7202131..a352c903 100644
--- a/elf/dl-map-segments-hugepage.h
+++ b/elf/dl-map-segments-hugepage.h
@@ -486,19 +486,6 @@ _mmap_segment(struct link_map *l, const struct loadcmd loadcmds[], size_t nloadc
   return NULL;
 }
 
-static __always_inline void *
-_mmap_hole(const struct loadcmd *current, const struct loadcmd *next,
-          ElfW(Addr) mapstart, size_t mapseglen, int fd)
-{
-  if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
-    _dl_debug_printf("\tmmap hole area:[%lx-%lx)\n", mapstart + mapseglen,
-                      mapstart + (next->mapstart - current->mapstart));
-  return __mmap((void *)(mapstart + mapseglen),
-                next->mapstart - (current->mapstart + mapseglen),
-                PROT_NONE, MAP_FILE|MAP_PRIVATE|MAP_FIXED,
-                fd, current->mapoff + mapseglen);
-}
-
 static __always_inline const char *
 _dl_map_segments_largein (struct link_map *l, int fd,
                   const ElfW(Ehdr) *header, int type,
@@ -541,13 +528,6 @@ _dl_map_segments_largein (struct link_map *l, int fd,
           goto unmap_reserved_area;
         }
 
-      if (prev->mapstart + mapseglen < c->mapstart &&
-            _mmap_hole(prev, c, map_addr, mapseglen, fd) == MAP_FAILED)
-        {
-          errstring = DL_MAP_SEGMENTS_ERROR_MAP_HOLE_FILL;
-          goto unmap_reserved_area;
-        }
-
       map_addr += c->mapstart - prev->mapstart;
       errstring = _mmap_segment(l, loadcmds, nloadcmds, c, map_addr, fd,
               &mapseglen, hp_bitmap);
@@ -582,12 +562,6 @@ _dl_map_segments_largein (struct link_map *l, int fd,
           goto unmap_reserved_area;
         }
 
-      if (c->mapstart + mapseglen < prev->mapstart &&
-            _mmap_hole(c, prev, map_addr, mapseglen, fd) == MAP_FAILED)
-        {
-          errstring = DL_MAP_SEGMENTS_ERROR_MAP_HOLE_FILL;
-          goto unmap_reserved_area;
-        }
       prev = c;
       --c;
     }
-- 
2.38.1

