From 50c5e23ead7f0be215b8133dfa7ab6a93e60ebf7 Mon Sep 17 00:00:00 2001
From: Lv Ying <lvying6@huawei.com>
Date: Tue, 4 Jun 2024 01:23:09 -0400
Subject: [PATCH 2/2] elf: add some debug info for dynamic library hugepage
 feature

add some debug info for dynamic library hugepage feature, use
page-aligined address space in debug info which is actual mapped

Signed-off-by: Lv Ying <lvying6@huawei.com>
---
 elf/dl-map-segments-hugepage.h | 37 ++++++++++++++++++++--------------
 1 file changed, 22 insertions(+), 15 deletions(-)

diff --git a/elf/dl-map-segments-hugepage.h b/elf/dl-map-segments-hugepage.h
index 3ea52dbd..7410072c 100644
--- a/elf/dl-map-segments-hugepage.h
+++ b/elf/dl-map-segments-hugepage.h
@@ -212,7 +212,7 @@ _mmap_remain_zero_page(ElfW(Addr) zeropage, ElfW(Addr) zeroend, int prot)
     }
 
   if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
-    _dl_debug_printf("\t\t\t=> mmap anonymous hugepage: [%lx-%lx)\n", hp_start, hp_start + len - mod);
+    _dl_debug_printf("\t\t\t=> %s mmap anonymous hugepage: [%lx-%lx)\n", __func__, hp_start, hp_start + len - mod);
   mapat = __mmap((caddr_t) hp_start, len - mod, prot,
                   MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS|MAP_HUGETLB|(SHFIT_2MB << MAP_HUGE_SHIFT),
                   -1, 0);
@@ -221,6 +221,9 @@ _mmap_remain_zero_page(ElfW(Addr) zeropage, ElfW(Addr) zeroend, int prot)
 
   if (mod > 0)
     {
+      if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
+        _dl_debug_printf("\t\t\t=> %s mmap anonymous tail normal page: [%lx-%lx)\n", __func__,
+                        hp_start + len - mod, hp_start + len - mod + ALIGN_UP(mod, GLRO(dl_pagesize)));
       mapat =__mmap((caddr_t)(hp_start + len - mod), mod, prot,
                       MAP_ANON|MAP_PRIVATE|MAP_FIXED, -1, 0);
     if (__glibc_unlikely (mapat == MAP_FAILED))
@@ -301,7 +304,7 @@ _mmap_segment_memsz(struct link_map *l, const struct loadcmd * c,
     {
       *memsz_len = ALIGN_UP(zeroend, GLRO(dl_pagesize)) - zeropage;
       if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
-        _dl_debug_printf("\t\tzero remain page [%lx-%lx)\n", zeropage, zeroend);
+        _dl_debug_printf("\t\tmmap remain anonymous page [%lx-%lx)\n", zeropage, ALIGN_UP(zeroend, GLRO(dl_pagesize)));
       errstring = _mmap_remain_zero_page(zeropage, zeroend, c->prot);
     }
   return errstring;
@@ -475,8 +478,8 @@ __mmap_segment_normalsz(const struct loadcmd *c, ElfW(Addr) mapstart, int fd,
         size_t *mapseglen)
 {
   if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
-    _dl_debug_printf("\tuse normal page mmap segment:[%lx-%lx)\n", mapstart,
-            mapstart + (c->allocend - c->mapstart));
+    _dl_debug_printf("\t\t%s mmap current segment in normal page:[%lx-%lx)\n", __func__, mapstart,
+            mapstart + (ALIGN_UP(c->allocend, GLRO(dl_pagesize)) - c->mapstart));
 
   if (c->mapend > c->mapstart &&
           (__mmap((void *)mapstart, c->mapend - c->mapstart, c->prot,
@@ -515,7 +518,7 @@ __mmap_segment_normalsz(const struct loadcmd *c, ElfW(Addr) mapstart, int fd,
     }
   }
 
-  *mapseglen = c->allocend - c->mapstart;
+  *mapseglen = ALIGN_UP(c->allocend, GLRO(dl_pagesize)) - c->mapstart;
   return NULL;
 }
 
@@ -534,15 +537,18 @@ _mmap_segment(struct link_map *l, const struct loadcmd loadcmds[], size_t nloadc
   const char * errstring = NULL;
   size_t extra_len, memsz_len = 0;
 
-  if (!hp_bitmap[((void *)c - (void *)loadcmds) / sizeof(struct loadcmd)]) {
-    return __mmap_segment_normalsz(c, mapstart, fd, mapseglen);
-  }
-
-  extra_len = _extra_mmap(l, loadcmds, nloadcmds, c, mapstart);
   if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
-    _dl_debug_printf("\t%s(0x%lx): extra_len = 0x%lx\n\t{\n", __func__,
-                    (unsigned long)c, extra_len);
+    _dl_debug_printf("\t%s mmap from %lx: \n\t{\n", __func__, mapstart);
 
+  if (!hp_bitmap[((void *)c - (void *)loadcmds) / sizeof(struct loadcmd)])
+    {
+      errstring = __mmap_segment_normalsz(c, mapstart, fd, mapseglen);
+      if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
+        _dl_debug_printf("\t} => mapseglen = 0x%lx\n", *mapseglen);
+      return errstring;
+    }
+
+  extra_len = _extra_mmap(l, loadcmds, nloadcmds, c, mapstart);
   errstring = _mmap_segment_filesz(l, c, mapstart, extra_len, fd);
   if (__glibc_unlikely (errstring != NULL))
     return errstring;
@@ -552,7 +558,8 @@ _mmap_segment(struct link_map *l, const struct loadcmd loadcmds[], size_t nloadc
 
   *mapseglen = c->mapend - c->mapstart + extra_len + memsz_len;
   if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
-    _dl_debug_printf("\t} => mapseglen = 0x%lx, memsz_len = 0x%lx\n", *mapseglen, memsz_len);
+    _dl_debug_printf("\t} => mapseglen = 0x%lx, extra_len = 0x%lx, memsz_len = 0x%lx\n",
+                     *mapseglen, extra_len, memsz_len);
   return NULL;
 }
 
@@ -652,7 +659,7 @@ _dl_map_segments_largein (struct link_map *l, int fd,
   if (l->l_map_start > (ElfW(Addr))map_area_start)
     {
       if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
-        _dl_debug_printf("__munmap [%lx-%lx)\n",  (ElfW(Addr))map_area_start, l->l_map_start);
+        _dl_debug_printf("__munmap head reserved area [%lx-%lx)\n",  (ElfW(Addr))map_area_start, l->l_map_start);
       __munmap(map_area_start, l->l_map_start - (ElfW(Addr))map_area_start);
     }
 
@@ -663,7 +670,7 @@ _dl_map_segments_largein (struct link_map *l, int fd,
   if ((ElfW(Addr))map_area_start + maparealen > l_map_end)
     {
       if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
-        _dl_debug_printf("__munmap [%lx-%lx)\n", l_map_end, (ElfW(Addr))map_area_start + maparealen);
+        _dl_debug_printf("__munmap tail reserved area [%lx-%lx)\n", l_map_end, (ElfW(Addr))map_area_start + maparealen);
       __munmap((void *)l_map_end, (ElfW(Addr))map_area_start + maparealen - l_map_end);
     }
 
-- 
2.40.0


