@@ -339,20 +339,23 @@ __attribute__((__noinline__)) |
339 | 339 | MoveTextRegionToLargePages(const text_region& r) { |
340 | 340 | void* nmem = nullptr; |
341 | 341 | void* tmem = nullptr; |
342 | | - int ret = 0; |
343 | | - |
344 | | - size_t size = r.to - r.from; |
345 | 342 | void* start = r.from; |
| 343 | + size_t size = r.to - r.from; |
| 344 | + |
| 345 | + auto free_mems = OnScopeLeave([&nmem, &tmem, size]() { |
| 346 | + if (nmem != nullptr && nmem != MAP_FAILED && munmap(nmem, size) == -1) |
| 347 | + PrintSystemError(errno); |
| 348 | + if (tmem != nullptr && tmem != MAP_FAILED && munmap(tmem, size) == -1) |
| 349 | + PrintSystemError(errno); |
| 350 | + }); |
346 | 351 |
|
347 | | - // Allocate temporary region preparing for copy. |
| 352 | +#if !defined (__FreeBSD__) |
| 353 | + // Allocate temporary region and back up the code we will re-map. |
348 | 354 | nmem = mmap(nullptr, size, |
349 | 355 | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
350 | | - if (nmem == MAP_FAILED) { |
351 | | - PrintSystemError(errno); |
352 | | - return -1; |
353 | | - } |
354 | | - |
| 356 | + if (nmem == MAP_FAILED) goto fail; |
355 | 357 | memcpy(nmem, r.from, size); |
| 358 | +#endif |
356 | 359 |
|
357 | 360 | #if defined(__linux__) |
358 | 361 | // We already know the original page is r-xp |
@@ -362,32 +365,15 @@ MoveTextRegionToLargePages(const text_region& r) { |
362 | 365 | tmem = mmap(start, size, |
363 | 366 | PROT_READ | PROT_WRITE | PROT_EXEC, |
364 | 367 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1 , 0); |
365 | | - if (tmem == MAP_FAILED) { |
366 | | - PrintSystemError(errno); |
367 | | - return -1; |
368 | | - } |
369 | | - |
370 | | - ret = madvise(tmem, size, 14 /* MADV_HUGEPAGE */); |
371 | | - if (ret == -1) { |
372 | | - PrintSystemError(errno); |
373 | | - ret = munmap(tmem, size); |
374 | | - if (ret == -1) { |
375 | | - PrintSystemError(errno); |
376 | | - } |
377 | | - if (-1 == munmap(nmem, size)) PrintSystemError(errno); |
378 | | - return -1; |
379 | | - } |
| 368 | + if (tmem == MAP_FAILED) goto fail; |
| 369 | + if (madvise(tmem, size, 14 /* MADV_HUGEPAGE */) == -1) goto fail; |
380 | 370 | memcpy(start, nmem, size); |
381 | 371 | #elif defined(__FreeBSD__) |
382 | 372 | tmem = mmap(start, size, |
383 | 373 | PROT_READ | PROT_WRITE | PROT_EXEC, |
384 | 374 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED | |
385 | 375 | MAP_ALIGNED_SUPER, -1 , 0); |
386 | | - if (tmem == MAP_FAILED) { |
387 | | - PrintSystemError(errno); |
388 | | - if (-1 == munmap(nmem, size)) PrintSystemError(errno); |
389 | | - return -1; |
390 | | - } |
| 376 | + if (tmem == MAP_FAILED) goto fail; |
391 | 377 | #elif defined(__APPLE__) |
392 | 378 | // There is not enough room to reserve the mapping close |
393 | 379 | // to the region address so we content to give a hint |
@@ -398,37 +384,20 @@ MoveTextRegionToLargePages(const text_region& r) { |
398 | 384 | PROT_READ | PROT_WRITE | PROT_EXEC, |
399 | 385 | MAP_PRIVATE | MAP_ANONYMOUS, |
400 | 386 | VM_FLAGS_SUPERPAGE_SIZE_2MB, 0); |
401 | | - if (tmem == MAP_FAILED) { |
402 | | - PrintSystemError(errno); |
403 | | - if (-1 == munmap(nmem, size)) PrintSystemError(errno); |
404 | | - return -1; |
405 | | - } |
| 387 | + if (tmem == MAP_FAILED) goto fail; |
406 | 388 | memcpy(tmem, nmem, size); |
407 | | - ret = mprotect(start, size, PROT_READ | PROT_WRITE | PROT_EXEC); |
408 | | - if (ret == -1) { |
409 | | - PrintSystemError(errno); |
410 | | - ret = munmap(tmem, size); |
411 | | - if (ret == -1) { |
412 | | - PrintSystemError(errno); |
413 | | - } |
414 | | - if (-1 == munmap(nmem, size)) PrintSystemError(errno); |
415 | | - return -1; |
416 | | - } |
| 389 | + if (mprotect(start, size, PROT_READ | PROT_WRITE | PROT_EXEC) == -1) |
| 390 | + goto fail; |
417 | 391 | memcpy(start, tmem, size); |
418 | 392 | #endif |
419 | 393 |
|
420 | | - ret = mprotect(start, size, PROT_READ | PROT_EXEC); |
421 | | - if (ret == -1) { |
422 | | - PrintSystemError(errno); |
423 | | - ret = munmap(tmem, size); |
424 | | - if (ret == -1) { |
425 | | - PrintSystemError(errno); |
426 | | - } |
427 | | - if (-1 == munmap(nmem, size)) PrintSystemError(errno); |
428 | | - return -1; |
429 | | - } |
430 | | - if (-1 == munmap(nmem, size)) PrintSystemError(errno); |
431 | | - return ret; |
| 394 | + if (mprotect(start, size, PROT_READ | PROT_EXEC) == -1) goto fail; |
| 395 | + // We need not `munmap(tmem, size)` in the above `OnScopeLeave` on success. |
| 396 | + tmem = nullptr; |
| 397 | + return 0; |
| 398 | +fail: |
| 399 | + PrintSystemError(errno); |
| 400 | + return -1; |
432 | 401 | } |
433 | 402 | #endif // defined(NODE_ENABLE_LARGE_CODE_PAGES) && NODE_ENABLE_LARGE_CODE_PAGES |
434 | 403 |
|
|