Skip to content

Commit b502782

Browse files
Sandboxed API Teamcopybara-github
authored andcommitted
Fix race when calling CreateMemFdWithHugePages concurrently
PiperOrigin-RevId: 875112363 Change-Id: I256cfbbb0b84551eeec05438f325bf5f1838bf89
1 parent 62e72ce commit b502782

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

sandboxed_api/sandbox2/sandbox2.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <fcntl.h>
2020
#include <unistd.h>
2121

22+
#include <atomic>
2223
#include <cerrno>
2324
#include <cstddef>
2425
#include <cstdint>
@@ -74,12 +75,14 @@ class Sandbox2Peer : public internal::SandboxPeer {
7475

7576
absl::StatusOr<sapi::file_util::fileops::FDCloser> CreateMemFdWithHugePages(
7677
const char* name, uintptr_t flags) {
77-
static bool huge_tlb_supported = true;
78+
static std::atomic<bool> huge_tlb_supported = true;
7879

7980
if (huge_tlb_supported) {
8081
auto res =
8182
util::CreateMemFd(name, flags | util::kMfdHugeTLB | util::kMfdHuge2Mb);
82-
huge_tlb_supported = res.ok();
83+
if (!res.ok() && errno == EINVAL) {
84+
huge_tlb_supported = false;
85+
}
8386
return res;
8487
}
8588
return absl::InternalError("No huge page support.");

0 commit comments

Comments
 (0)