Skip to content

Commit c9b1139

Browse files
author
Gabriel Schulhof
committed
src: further simplify large pages code
* Clean up and co-locate explanation for what the code does to the top of the file. * Remove extraneous headers. * Remove the need for `OnScopeLeave` replacing it with a pair of labels `fail:` and `done:`. Re: nodejs#32570 Signed-off-by: Gabriel Schulhof <[email protected]>
1 parent 67d5c90 commit c9b1139

File tree

1 file changed

+48
-47
lines changed

1 file changed

+48
-47
lines changed

src/large_pages/node_large_page.cc

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,56 @@
2020
//
2121
// SPDX-License-Identifier: MIT
2222

23+
// The functions in this file map the .text section of Node.js into 2MB pages.
24+
// They perform the following steps:
25+
//
26+
// 1: Find the Node.js binary's `.text` section in memory. This is done below in
27+
// `FindNodeTextRegion`. It is accomplished in a platform-specific way. On
28+
// Linux and FreeBSD, `dl_iterate_phdr(3)` is used. When the region is found,
29+
// it is "trimmed" as follows:
30+
// * Modify the start to point to the very beginning of node `.text` section
31+
// (from symbol `__node_text_start` declared in node_text_start.S).
32+
// * Possibly modify the end to account for the `lpstub` section which
33+
// contains `MoveTextRegionToLargePages`, the function we do not wish to
34+
// move (see below).
35+
// * Align the address of the start to its nearest higher large page
36+
// boundary.
37+
// * Align the address of the end to its nearest lower large page boundaries.
38+
//
39+
// 2: Move the text region to large pages. This is done below in
40+
// `MoveTextRegionToLargePages`. We need to be very careful:
41+
// a) `MoveTextRegionToLargePages` itself should not be moved.
42+
// We use gcc attributes
43+
// (__section__) to put it outside the `.text` section,
44+
// (__aligned__) to align it at the 2M boundary, and
45+
// (__noline__) to not inline this function.
46+
// b) `MoveTextRegionToLargePages` should not call any function(s) that might
47+
// be moved.
48+
// To move the .text section, perform the following steps:
49+
// * Map a new, temporary area and copy the original code there.
50+
// * Use mmap using the start address with MAP_FIXED so we get exactly the
51+
// same virtual address (except on OSX). On platforms other than Linux,
52+
// use mmap flags to request hugepages.
53+
// * On Linux use madvise with MADV_HUGEPAGE to use anonymous 2MB pages.
54+
// * If successful copy the code to the newly mapped area and protect it to
55+
// be readable and executable.
56+
// * Unmap the temporary area.
57+
2358
#include "node_large_page.h"
2459

2560
#include <cerrno> // NOLINT(build/include)
2661

2762
// Besides returning ENOTSUP at runtime we do nothing if this define is missing.
2863
#if defined(NODE_ENABLE_LARGE_CODE_PAGES) && NODE_ENABLE_LARGE_CODE_PAGES
2964
#include "debug_utils-inl.h"
30-
#include "util.h"
31-
#include "uv.h"
3265

3366
#if defined(__linux__) || defined(__FreeBSD__)
34-
#include <string.h>
3567
#if defined(__linux__)
3668
#ifndef _GNU_SOURCE
3769
#define _GNU_SOURCE
3870
#endif // ifndef _GNU_SOURCE
71+
#elif defined(__FreeBSD__)
72+
#include "uv.h" // uv_exepath
3973
#endif // defined(__linux__)
4074
#include <link.h>
4175
#endif // defined(__linux__) || defined(__FreeBSD__)
@@ -44,38 +78,16 @@
4478
#include <sys/mman.h>
4579
#if defined(__FreeBSD__)
4680
#include <sys/sysctl.h>
47-
#include <sys/user.h>
4881
#elif defined(__APPLE__)
4982
#include <mach/vm_map.h>
5083
#endif
51-
#include <unistd.h> // getpid
5284

5385
#include <climits> // PATH_MAX
54-
#include <clocale>
55-
#include <csignal>
5686
#include <cstdlib>
5787
#include <cstdint>
5888
#include <cstring>
5989
#include <string>
6090
#include <fstream>
61-
#include <iostream>
62-
#include <vector>
63-
64-
// The functions in this file map the text segment of node into 2M pages.
65-
// The algorithm is simple
66-
// Find the text region of node binary in memory
67-
// 1: Examine the /proc/self/maps to determine the currently mapped text
68-
// region and obtain the start and end
69-
// Modify the start to point to the very beginning of node text segment
70-
// (from variable nodetext setup in ld.script)
71-
// Align the address of start and end to Large Page Boundaries
72-
//
73-
// 2: Move the text region to large pages
74-
// Map a new area and copy the original code there
75-
// Use mmap using the start address with MAP_FIXED so we get exactly the
76-
// same virtual address
77-
// Use madvise with MADV_HUGEPAGE to use Anonymous 2M Pages
78-
// If successful copy the code there and unmap the original region.
7991

8092
#if defined(__linux__) || defined(__FreeBSD__)
8193
extern "C" {
@@ -284,18 +296,6 @@ bool IsSuperPagesEnabled() {
284296

285297
} // End of anonymous namespace
286298

287-
// Moving the text region to large pages. We need to be very careful.
288-
// 1: This function itself should not be moved.
289-
// We use a gcc attributes
290-
// (__section__) to put it outside the ".text" section
291-
// (__aligned__) to align it at 2M boundary
292-
// (__noline__) to not inline this function
293-
// 2: This function should not call any function(s) that might be moved.
294-
// a. map a new area and copy the original code there
295-
// b. mmap using the start address with MAP_FIXED so we get exactly
296-
// the same virtual address (except on macOS).
297-
// c. madvise with MADV_HUGEPAGE
298-
// d. If successful copy the code there and unmap the original region
299299
int
300300
#if !defined(__APPLE__)
301301
__attribute__((__section__("lpstub")))
@@ -305,18 +305,12 @@ __attribute__((__section__("__TEXT,__lpstub")))
305305
__attribute__((__aligned__(hps)))
306306
__attribute__((__noinline__))
307307
MoveTextRegionToLargePages(const text_region& r) {
308+
int status = 0;
308309
void* nmem = nullptr;
309310
void* tmem = nullptr;
310311
void* start = r.from;
311312
size_t size = r.to - r.from;
312313

313-
auto free_mems = OnScopeLeave([&nmem, &tmem, size]() {
314-
if (nmem != nullptr && nmem != MAP_FAILED && munmap(nmem, size) == -1)
315-
PrintSystemError(errno);
316-
if (tmem != nullptr && tmem != MAP_FAILED && munmap(tmem, size) == -1)
317-
PrintSystemError(errno);
318-
});
319-
320314
// Allocate temporary region and back up the code we will re-map.
321315
nmem = mmap(nullptr, size,
322316
PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
@@ -359,12 +353,19 @@ MoveTextRegionToLargePages(const text_region& r) {
359353
#endif
360354

361355
if (mprotect(start, size, PROT_READ | PROT_EXEC) == -1) goto fail;
362-
// We need not `munmap(tmem, size)` in the above `OnScopeLeave` on success.
356+
357+
// We need not `munmap(tmem, size)` on success.
363358
tmem = nullptr;
364-
return 0;
359+
goto done;
365360
fail:
366361
PrintSystemError(errno);
367-
return -1;
362+
status = -1;
363+
done:
364+
if (nmem != nullptr && nmem != MAP_FAILED && munmap(nmem, size) == -1)
365+
PrintSystemError(errno);
366+
if (tmem != nullptr && tmem != MAP_FAILED && munmap(tmem, size) == -1)
367+
PrintSystemError(errno);
368+
return status;
368369
}
369370
#endif // defined(NODE_ENABLE_LARGE_CODE_PAGES) && NODE_ENABLE_LARGE_CODE_PAGES
370371

0 commit comments

Comments
 (0)