From 2012ffb231c63bc4693bd2b999e4509b99585878 Mon Sep 17 00:00:00 2001
From: jsonboss <82553966+jsonboss@users.noreply.github.com>
Date: Mon, 19 Apr 2021 10:38:27 +0800
Subject: [PATCH 1/2] support builtin msr

---
 src/hw/msr/Msr_linux.cpp | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/hw/msr/Msr_linux.cpp b/src/hw/msr/Msr_linux.cpp
index 0783ebc18..15e86fc35 100644
--- a/src/hw/msr/Msr_linux.cpp
+++ b/src/hw/msr/Msr_linux.cpp
@@ -57,7 +57,12 @@ public:
 
 xmrig::Msr::Msr() : d_ptr(new MsrPrivate())
 {
-    if (system("/sbin/modprobe msr allow_writes=on > /dev/null 2>&1") != 0) {
+    if(access("/sys/module/msr/parameters/allow_writes", F_OK) == 0) {
+        if(system("echo on > /sys/module/msr/parameters/allow_writes") != 0) {
+            d_ptr->available = false;
+        }
+    }
+    else if (system("/sbin/modprobe msr allow_writes=on > /dev/null 2>&1") != 0) {
         LOG_WARN("%s " YELLOW_BOLD("msr kernel module is not available"), Msr::tag());
 
         d_ptr->available = false;

From e26fbc96e9a343bcf6d9b9d1d7a121ce2b5089d9 Mon Sep 17 00:00:00 2001
From: XMRig <support@xmrig.com>
Date: Sat, 24 Apr 2021 23:22:10 +0700
Subject: [PATCH 2/2] Removed unnecessary system call.

---
 src/hw/msr/Msr_linux.cpp | 36 +++++++++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 11 deletions(-)

diff --git a/src/hw/msr/Msr_linux.cpp b/src/hw/msr/Msr_linux.cpp
index 15e86fc35..29bb8ad1d 100644
--- a/src/hw/msr/Msr_linux.cpp
+++ b/src/hw/msr/Msr_linux.cpp
@@ -29,6 +29,7 @@
 #include <cstdio>
 #include <dirent.h>
 #include <fcntl.h>
+#include <fstream>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
@@ -48,7 +49,27 @@ static int msr_open(int32_t cpu, int flags)
 class MsrPrivate
 {
 public:
-    bool available = true;
+    inline MsrPrivate() : m_available(msr_allow_writes() || msr_modprobe()) {}
+
+    inline bool isAvailable() const { return m_available; }
+
+private:
+    inline bool msr_allow_writes()
+    {
+        std::ofstream file("/sys/module/msr/parameters/allow_writes", std::ios::out | std::ios::binary | std::ios::trunc);
+        if (file.is_open()) {
+            file << "on";
+        }
+
+        return file.good();
+    }
+
+    inline bool msr_modprobe()
+    {
+        return system("/sbin/modprobe msr allow_writes=on > /dev/null 2>&1") == 0;
+    }
+
+    const bool m_available;
 };
 
 
@@ -57,15 +78,8 @@ public:
 
 xmrig::Msr::Msr() : d_ptr(new MsrPrivate())
 {
-    if(access("/sys/module/msr/parameters/allow_writes", F_OK) == 0) {
-        if(system("echo on > /sys/module/msr/parameters/allow_writes") != 0) {
-            d_ptr->available = false;
-        }
-    }
-    else if (system("/sbin/modprobe msr allow_writes=on > /dev/null 2>&1") != 0) {
-        LOG_WARN("%s " YELLOW_BOLD("msr kernel module is not available"), Msr::tag());
-
-        d_ptr->available = false;
+    if (!isAvailable()) {
+        LOG_WARN("%s " YELLOW_BOLD("msr kernel module is not available"), tag());
     }
 }
 
@@ -78,7 +92,7 @@ xmrig::Msr::~Msr()
 
 bool xmrig::Msr::isAvailable() const
 {
-    return d_ptr->available;
+    return d_ptr->isAvailable();
 }