From 3d68198a71050e5f96df0ac41bb1877e22240717 Mon Sep 17 00:00:00 2001
From: Lv Ying <lvying6@huawei.com>
Date: Tue, 21 Dec 2021 03:27:07 +0800
Subject: [PATCH] use mlock to determine hugepage RLIMIT_MEMLOCK soft resource
 limit is valid

Signed-off-by: Lv Ying <lvying6@huawei.com>
---
 elf/dl-load.c                  |  1 -
 elf/dl-map-segments-hugepage.h | 23 ++++++++++++++++++++++-
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/elf/dl-load.c b/elf/dl-load.c
index 1aa77fa2..2691eefa 100644
--- a/elf/dl-load.c
+++ b/elf/dl-load.c
@@ -1219,7 +1219,6 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
     maplength = loadcmds[nloadcmds - 1].allocend - loadcmds[0].mapstart;
 
 #ifdef HUGEPAGE_SHARED_LIB
-#define ERRSTRING_BUF_LEN 1024
     int hp_errcode = 0;
     char hp_buf[ERRSTRING_BUF_LEN];
     if ((GLRO(dl_debug_mask) & DL_HUGEPAGE_LIB_LARGE_IN_FLAG) ||
diff --git a/elf/dl-map-segments-hugepage.h b/elf/dl-map-segments-hugepage.h
index 37788ef9..ef36f9ad 100644
--- a/elf/dl-map-segments-hugepage.h
+++ b/elf/dl-map-segments-hugepage.h
@@ -22,6 +22,7 @@
 #define SIZE_2MB 0x200000
 #define MASK_2MB 0x1FFFFF
 #define THRESHOLD 16
+#define ERRSTRING_BUF_LEN 1024
 
 /*
  * Find the first PT_LOAD segment with execute permission
@@ -57,12 +58,32 @@ __mmap_reserved_area(const struct loadcmd loadcmds[], size_t nloadcmds,
   /*
    * Get 2MB aligned contiguous va space
    * This va space can not be munmap in case of multi thread dlopen concurrently
+   * the prot can not be PROT_NONE, otherwise mlock always failed
    */
-  void *map_area_start = __mmap(0, *maparealen, PROT_NONE,
+  void *map_area_start = __mmap(0, *maparealen, PROT_READ,
     MAP_PRIVATE|MAP_ANONYMOUS|MAP_HUGETLB|(SHFIT_2MB << MAP_HUGE_SHIFT), -1, 0);
   if (__glibc_unlikely (map_area_start == MAP_FAILED))
     return MAP_FAILED;
 
+  /*
+   * use mlock to see if hugepage is beyond the RLIMIT_MEMLOCK soft resource limit
+   * when hugepage feature is enabled, however cgroup hugetlb.2MB.limit_in_bytes
+   * is set 0, subsequent hugepage mmap will return 0, actual page fault will
+   * send sigbus to this process
+   * the next mmap with MAP_FIXED flag will munlock this area
+   */
+  int mlock_errcode = 0;
+  char mlock_buf[ERRSTRING_BUF_LEN];
+  if (mlock(map_area_start, *maparealen) != 0)
+    {
+      mlock_errcode = errno;
+      if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
+        _dl_debug_printf("mlock failed: [%lx-%lx) :%s\n", (unsigned long)map_area_start,
+            (unsigned long)map_area_start + *maparealen,
+            mlock_errcode ? __strerror_r (mlock_errcode, mlock_buf, sizeof mlock_buf) : "");
+      goto unmap_reserved_area;
+    }
+
   /*
    * Remap 2MB aligned contiguous va space into 4KB contiguous va space
    * to avoid the tedious work of splitting hugepage into 4KB page
-- 
2.27.0

