From 36642542219053746651f897c2c6e733677af1b7 Mon Sep 17 00:00:00 2001 From: Chen Ziyang Date: Sat, 25 Mar 2023 16:16:30 +0800 Subject: [PATCH] elf/ld.so: fix ld.so mmap shared object use hugepage bugfix (cherry picked from commit a7c72dd6ca9aa03f9c7c5e6cbe1f3e32883d12d5) --- ...E-flag-for-the-first-mmap-2MB-contig.patch | 33 ++++++++++++ ...e-i-options-and-do-not-allow-i-speci.patch | 54 +++++++++++++++++++ glibc.spec | 9 +++- 3 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 elf-ld.so-add-MAP_NORESERVE-flag-for-the-first-mmap-2MB-contig.patch create mode 100644 elf-ld.so-prohibit-multiple-i-options-and-do-not-allow-i-speci.patch diff --git a/elf-ld.so-add-MAP_NORESERVE-flag-for-the-first-mmap-2MB-contig.patch b/elf-ld.so-add-MAP_NORESERVE-flag-for-the-first-mmap-2MB-contig.patch new file mode 100644 index 0000000..b2b62ac --- /dev/null +++ b/elf-ld.so-add-MAP_NORESERVE-flag-for-the-first-mmap-2MB-contig.patch @@ -0,0 +1,33 @@ + +From 9145e7cc47b0a43620273ec6f3d1bf016618eb62 Mon Sep 17 00:00:00 2001 +From: chenziyang +Date: Sat, 4 Mar 2023 17:13:57 +0800 +Subject: [PATCH 1/2] elf/ld.so: add MAP_NORESERVE flag for the first mmap 2MB contiguous va + +If environment only has 50MB hugepage resources, RX PT_LOAD segment +requires 25MB and RW PT_LOAD segment requires 100MB memory. Before this +patch, we would exit mmap hugepage because we will try to mmap 125MB +hugepage. After this change, ld.so will allow RX PT_LOAD to map into 2MB +hugepage + +Signed-off-by: Chen Ziyang +--- + elf/dl-map-segments-hugepage.h | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/elf/dl-map-segments-hugepage.h b/elf/dl-map-segments-hugepage.h +index 5ea89a36..6ab5f939 100644 +--- a/elf/dl-map-segments-hugepage.h ++++ b/elf/dl-map-segments-hugepage.h +@@ -59,7 +59,7 @@ __mmap_reserved_area(const struct loadcmd loadcmds[], size_t nloadcmds, + * This va space can not be munmap in case of multi thread dlopen concurrently + */ + void *map_area_start = __mmap(0, *maparealen, PROT_NONE, +- MAP_PRIVATE|MAP_ANONYMOUS|MAP_HUGETLB|(SHFIT_2MB << MAP_HUGE_SHIFT), -1, 0); ++ MAP_PRIVATE|MAP_ANONYMOUS|MAP_HUGETLB|MAP_NORESERVE|(SHFIT_2MB << MAP_HUGE_SHIFT), -1, 0); + if (__glibc_unlikely (map_area_start == MAP_FAILED)) + return MAP_FAILED; + +-- +2.33.0 + diff --git a/elf-ld.so-prohibit-multiple-i-options-and-do-not-allow-i-speci.patch b/elf-ld.so-prohibit-multiple-i-options-and-do-not-allow-i-speci.patch new file mode 100644 index 0000000..0422a5c --- /dev/null +++ b/elf-ld.so-prohibit-multiple-i-options-and-do-not-allow-i-speci.patch @@ -0,0 +1,54 @@ + +From f46625e444f2bbab110e217a34abd0898a242c11 Mon Sep 17 00:00:00 2001 +From: chenziyang +Date: Sun, 5 Mar 2023 17:04:09 +0800 +Subject: [PATCH 2/2] prohibit multiple -i options and do not allow -i specify non + PT_LOAD segment + +Signed-off-by: Chen Ziyang + +--- + elf/hugepageedit.c | 13 ++++++++++++- + 1 file changed, 12 insertions(+), 1 deletion(-) + +diff --git a/elf/hugepageedit.c b/elf/hugepageedit.c +index 0a44ece6..38b4db70 100644 +--- a/elf/hugepageedit.c ++++ b/elf/hugepageedit.c +@@ -75,7 +75,7 @@ int main(int argc, char *argv[]) + size_t length; + int exit_status = -1; + int i, opt, delete = 0, exec_only = 0, index_set = 0; +- long index = -1; ++ long index = -1, index_count = 0; + while ((opt = getopt(argc, argv, "dxi:")) != -1) + { + switch (opt) +@@ -91,6 +91,11 @@ int main(int argc, char *argv[]) + index_set = 1; + if (index < 0) + return -1; ++ index_count++; ++ if (index_count > 1) { ++ fprintf(stderr, "too many -i options in command line\n"); ++ return -1; ++ } + break; + default: + print_usage(); +@@ -150,6 +155,12 @@ int main(int argc, char *argv[]) + continue; + phdr[i].p_flags |= PF_HUGEPAGE; + } ++ } else { ++ if (index_set && index == i) { ++ fprintf(stderr, "Index %ld in -i %s option is not a PT_LOAD segment\n", ++ index, argv[optind]); ++ goto unmap; ++ } + } + } + exit_status = 0; +-- +2.33.0 + diff --git a/glibc.spec b/glibc.spec index 54a9dd6..0e59804 100644 --- a/glibc.spec +++ b/glibc.spec @@ -66,7 +66,7 @@ ############################################################################## Name: glibc Version: 2.34 -Release: 113 +Release: 114 Summary: The GNU libc libraries License: %{all_license} URL: http://www.gnu.org/software/glibc/ @@ -286,6 +286,8 @@ Patch9032: add-pthread_cond_clockwait-GLIBC_2_28.patch Patch9033: 0001-ld.so-support-ld.so-mmap-hugetlb-hugepage-according-.patch Patch9034: 0002-elf-ld.so-keep-compatible-with-the-original-policy-o.patch Patch9035: 0003-elf-ld.so-remove-_mmap_hole-when-ld.so-mmap-PT_LOAD-.patch +Patch9036: elf-ld.so-add-MAP_NORESERVE-flag-for-the-first-mmap-2MB-contig.patch +Patch9037: elf-ld.so-prohibit-multiple-i-options-and-do-not-allow-i-speci.patch Provides: ldconfig rtld(GNU_HASH) bundled(gnulib) @@ -1451,6 +1453,11 @@ fi %endif %changelog +* Sat Mar 25 2023 Chen Ziyang - 2.34-114 +- elf/ld.so: fix 2 bugs in ld.so mmap shared object use hugepage + - bugfix: ld.so mmap now first mmap 2MB continuous memory by MAP_NORESERVE flag because we do not want to revert to 4KB when 2MB resources is smaller then entire so. We want to check resources happend in later _mmap_segment_filesz function + - bugfix: fix hugepageedit tool range check logic, prohibit multiple -i options and do not allow -i specify non PT_LOAD segment index + * Tue Mar 14 2023 Qingqing Li - 2.34-113 - malloc: Fix transposed arguments in sysmalloc_mmap_fallback call -- Gitee