Improved compatibility for automatically enabling huge pages on Linux systems without NUMA support.
What changed, and why it matters
This commit is a routine compatibility improvement for XMRig's huge-page memory setup on Linux. It makes the miner better at enabling large memory pages on systems that do not expose per-CPU-node (NUMA) huge-page statistics. There is no indication this change introduces a security vulnerability or fixes one; it simply broadens the paths the program reads from and writes to in the Linux /sys filesystem.
No security action required. Treat as normal feature/compatibility commit. If deploying, verify huge-page permissions and sysctl limits as usual for mining workloads.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors LinuxMemory.cpp to support both NUMA-aware paths (/sys/devices/system/node/nodeX/hugepages/…) and non-NUMA fallback paths (/sys/kernel/mm/hugepages/…). It adds helper functions sysfs_write, sysfs_read, sysfs_path overloads, and sysfs_read_hugepages/write_nr_hugepages wrappers that fall back from the per-node sysfs files to the global ones. The public read/write helpers are removed from the header and made static/internal. The change is purely functional: enabling huge pages on single-node/non-NUMA Linux systems.
Changed components
src/crypto/common/LinuxMemory.cppsrc/crypto/common/LinuxMemory.hInspect captured patch +61 / −38
diff --git a/src/crypto/common/LinuxMemory.cpp b/src/crypto/common/LinuxMemory.cpp
index 8a00e1c..a09f5a1 100644
--- a/src/crypto/common/LinuxMemory.cpp
+++ b/src/crypto/common/LinuxMemory.cpp
@@ -1,6 +1,6 @@
/* XMRig
- * Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
- * Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com>
+ * Copyright (c) 2018-2025 SChernykh <https://github.com/SChernykh>
+ * Copyright (c) 2016-2025 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -35,58 +35,84 @@ constexpr size_t twoMiB = 2U * 1024U * 1024U;
constexpr size_t oneGiB = 1024U * 1024U * 1024U;
-static inline std::string sysfs_path(uint32_t node, size_t hugePageSize, bool nr)
+static bool sysfs_write(const std::string &path, uint64_t value)
{
- return fmt::format("/sys/devices/system/node/node{}/hugepages/hugepages-{}kB/{}_hugepages", node, hugePageSize / 1024, nr ? "nr" : "free");
+ std::ofstream file(path, std::ios::out | std::ios::binary | std::ios::trunc);
+ if (!file.is_open()) {
+ return false;
+ }
+
+ file << value;
+ file.flush();
+
+ return true;
}
-static inline bool write_nr_hugepages(uint32_t node, size_t hugePageSize, uint64_t count) { return LinuxMemory::write(sysfs_path(node, hugePageSize, true).c_str(), count); }
-static inline int64_t free_hugepages(uint32_t node, size_t hugePageSize) { return LinuxMemory::read(sysfs_path(node, hugePageSize, false).c_str()); }
-static inline int64_t nr_hugepages(uint32_t node, size_t hugePageSize) { return LinuxMemory::read(sysfs_path(node, hugePageSize, true).c_str()); }
+static int64_t sysfs_read(const std::string &path)
+{
+ std::ifstream file(path);
+ if (!file.is_open()) {
+ return -1;
+ }
+ uint64_t value = 0;
+ file >> value;
-} // namespace xmrig
+ return value;
+}
-bool xmrig::LinuxMemory::reserve(size_t size, uint32_t node, size_t hugePageSize)
+static std::string sysfs_path(uint32_t node, size_t hugePageSize, bool nr)
{
- std::lock_guard<std::mutex> lock(mutex);
+ return fmt::format("/sys/devices/system/node/node{}/hugepages/hugepages-{}kB/{}_hugepages", node, hugePageSize / 1024, nr ? "nr" : "free");
+}
- const size_t required = VirtualMemory::align(size, hugePageSize) / hugePageSize;
- const auto available = free_hugepages(node, hugePageSize);
- if (available < 0 || static_cast<size_t>(available) >= required) {
- return false;
+static std::string sysfs_path(size_t hugePageSize, bool nr)
+{
+ return fmt::format("/sys/kernel/mm/hugepages/hugepages-{}kB/{}_hugepages", hugePageSize / 1024, nr ? "nr" : "free");
+}
+
+
+static bool write_nr_hugepages(uint32_t node, size_t hugePageSize, uint64_t count)
+{
+ if (sysfs_write(sysfs_path(node, hugePageSize, true), count)) {
+ return true;
}
- return write_nr_hugepages(node, hugePageSize, std::max<size_t>(nr_hugepages(node, hugePageSize), 0) + (required - available));
+ return sysfs_write(sysfs_path(hugePageSize, true), count);
}
-bool xmrig::LinuxMemory::write(const char *path, uint64_t value)
+static int64_t sysfs_read_hugepages(uint32_t node, size_t hugePageSize, bool nr)
{
- std::ofstream file(path, std::ios::out | std::ios::binary | std::ios::trunc);
- if (!file.is_open()) {
- return false;
+ const int64_t value = sysfs_read(sysfs_path(node, hugePageSize, nr));
+ if (value >= 0) {
+ return value;
}
- file << value;
- file.flush();
-
- return true;
+ return sysfs_read(sysfs_path(hugePageSize, nr));
}
-int64_t xmrig::LinuxMemory::read(const char *path)
+static inline int64_t free_hugepages(uint32_t node, size_t hugePageSize) { return sysfs_read_hugepages(node, hugePageSize, false); }
+static inline int64_t nr_hugepages(uint32_t node, size_t hugePageSize) { return sysfs_read_hugepages(node, hugePageSize, true); }
+
+
+} // namespace xmrig
+
+
+bool xmrig::LinuxMemory::reserve(size_t size, uint32_t node, size_t hugePageSize)
{
- std::ifstream file(path);
- if (!file.is_open()) {
- return -1;
- }
+ std::lock_guard<std::mutex> lock(mutex);
- uint64_t value = 0;
- file >> value;
+ const size_t required = VirtualMemory::align(size, hugePageSize) / hugePageSize;
- return value;
+ const auto available = free_hugepages(node, hugePageSize);
+ if (available < 0 || static_cast<size_t>(available) >= required) {
+ return false;
+ }
+
+ return write_nr_hugepages(node, hugePageSize, std::max<size_t>(nr_hugepages(node, hugePageSize), 0) + (required - available));
}
diff --git a/src/crypto/common/LinuxMemory.h b/src/crypto/common/LinuxMemory.h
index 0d71af2..c39f96e 100644
--- a/src/crypto/common/LinuxMemory.h
+++ b/src/crypto/common/LinuxMemory.h
@@ -1,6 +1,6 @@
/* XMRig
- * Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
- * Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com>
+ * Copyright (c) 2018-2025 SChernykh <https://github.com/SChernykh>
+ * Copyright (c) 2016-2025 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -31,13 +31,10 @@ class LinuxMemory
{
public:
static bool reserve(size_t size, uint32_t node, size_t hugePageSize);
-
- static bool write(const char *path, uint64_t value);
- static int64_t read(const char *path);
};
-} /* namespace xmrig */
+} // namespace xmrig
-#endif /* XMRIG_LINUXMEMORY_H */
+#endif // XMRIG_LINUXMEMORY_H
Why this scored 17/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.