Merge pull request #40 from cfreehill/master
Check for root access early for functions that require it
[ROCm/amdsmi commit: acc508de60]
This commit is contained in:
@@ -91,7 +91,8 @@ typedef enum {
|
|||||||
//!< version running on the executing
|
//!< version running on the executing
|
||||||
//!< machine
|
//!< machine
|
||||||
RSMI_STATUS_PERMISSION, //!< Permission denied/EACCESS file
|
RSMI_STATUS_PERMISSION, //!< Permission denied/EACCESS file
|
||||||
//!< error
|
//!< error. Many functions require
|
||||||
|
//!< root access to run.
|
||||||
RSMI_STATUS_OUT_OF_RESOURCES, //!< Unable to acquire memory or other
|
RSMI_STATUS_OUT_OF_RESOURCES, //!< Unable to acquire memory or other
|
||||||
//!< resource
|
//!< resource
|
||||||
RSMI_STATUS_INTERNAL_EXCEPTION, //!< An internal exception was caught
|
RSMI_STATUS_INTERNAL_EXCEPTION, //!< An internal exception was caught
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ namespace smi {
|
|||||||
|
|
||||||
class RocmSMI {
|
class RocmSMI {
|
||||||
public:
|
public:
|
||||||
RocmSMI(uint64_t flags);
|
explicit RocmSMI(uint64_t flags);
|
||||||
~RocmSMI(void);
|
~RocmSMI(void);
|
||||||
|
|
||||||
static RocmSMI& getInstance(uint64_t flags = 0);
|
static RocmSMI& getInstance(uint64_t flags = 0);
|
||||||
@@ -82,6 +82,8 @@ class RocmSMI {
|
|||||||
void set_init_options(uint64_t options) {init_options_ = options;}
|
void set_init_options(uint64_t options) {init_options_ = options;}
|
||||||
uint64_t init_options() const {return init_options_;}
|
uint64_t init_options() const {return init_options_;}
|
||||||
|
|
||||||
|
uint32_t euid() const {return euid_;}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::shared_ptr<Device>> devices_;
|
std::vector<std::shared_ptr<Device>> devices_;
|
||||||
std::vector<std::shared_ptr<Monitor>> monitors_;
|
std::vector<std::shared_ptr<Monitor>> monitors_;
|
||||||
@@ -95,6 +97,7 @@ class RocmSMI {
|
|||||||
static std::vector<std::shared_ptr<amd::smi::Device>> s_monitor_devices;
|
static std::vector<std::shared_ptr<amd::smi::Device>> s_monitor_devices;
|
||||||
RocmSMI_env_vars env_vars_;
|
RocmSMI_env_vars env_vars_;
|
||||||
uint64_t init_options_;
|
uint64_t init_options_;
|
||||||
|
uint32_t euid_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace smi
|
} // namespace smi
|
||||||
|
|||||||
@@ -101,6 +101,11 @@ static rsmi_status_t handleException() {
|
|||||||
std::shared_ptr<amd::smi::Device> dev = smi.monitor_devices()[dv_ind]; \
|
std::shared_ptr<amd::smi::Device> dev = smi.monitor_devices()[dv_ind]; \
|
||||||
assert(dev != nullptr);
|
assert(dev != nullptr);
|
||||||
|
|
||||||
|
#define REQUIRE_ROOT_ACCESS \
|
||||||
|
if (amd::smi::RocmSMI::getInstance().euid()) { \
|
||||||
|
return RSMI_STATUS_PERMISSION; \
|
||||||
|
}
|
||||||
|
|
||||||
#define DEVICE_MUTEX \
|
#define DEVICE_MUTEX \
|
||||||
amd::smi::pthread_wrap _pw(*get_mutex(dv_ind)); \
|
amd::smi::pthread_wrap _pw(*get_mutex(dv_ind)); \
|
||||||
amd::smi::ScopedPthread _lock(_pw);
|
amd::smi::ScopedPthread _lock(_pw);
|
||||||
@@ -729,6 +734,8 @@ rsmi_dev_overdrive_level_get(uint32_t dv_ind, uint32_t *od) {
|
|||||||
rsmi_status_t
|
rsmi_status_t
|
||||||
rsmi_dev_overdrive_level_set(int32_t dv_ind, uint32_t od) {
|
rsmi_dev_overdrive_level_set(int32_t dv_ind, uint32_t od) {
|
||||||
TRY
|
TRY
|
||||||
|
REQUIRE_ROOT_ACCESS
|
||||||
|
|
||||||
if (od > kMaxOverdriveLevel) {
|
if (od > kMaxOverdriveLevel) {
|
||||||
return RSMI_STATUS_INVALID_ARGS;
|
return RSMI_STATUS_INVALID_ARGS;
|
||||||
}
|
}
|
||||||
@@ -740,6 +747,8 @@ rsmi_dev_overdrive_level_set(int32_t dv_ind, uint32_t od) {
|
|||||||
rsmi_status_t
|
rsmi_status_t
|
||||||
rsmi_dev_perf_level_set(int32_t dv_ind, rsmi_dev_perf_level_t perf_level) {
|
rsmi_dev_perf_level_set(int32_t dv_ind, rsmi_dev_perf_level_t perf_level) {
|
||||||
TRY
|
TRY
|
||||||
|
REQUIRE_ROOT_ACCESS
|
||||||
|
|
||||||
if (perf_level > RSMI_DEV_PERF_LEVEL_LAST) {
|
if (perf_level > RSMI_DEV_PERF_LEVEL_LAST) {
|
||||||
return RSMI_STATUS_INVALID_ARGS;
|
return RSMI_STATUS_INVALID_ARGS;
|
||||||
}
|
}
|
||||||
@@ -1082,7 +1091,7 @@ rsmi_dev_gpu_clk_freq_set(uint32_t dv_ind,
|
|||||||
rsmi_frequencies_t freqs;
|
rsmi_frequencies_t freqs;
|
||||||
|
|
||||||
TRY
|
TRY
|
||||||
|
REQUIRE_ROOT_ACCESS
|
||||||
DEVICE_MUTEX
|
DEVICE_MUTEX
|
||||||
|
|
||||||
ret = rsmi_dev_gpu_clk_freq_get(dv_ind, clk_type, &freqs);
|
ret = rsmi_dev_gpu_clk_freq_get(dv_ind, clk_type, &freqs);
|
||||||
@@ -1363,7 +1372,7 @@ rsmi_dev_pci_bandwidth_set(uint32_t dv_ind, uint64_t bw_bitmask) {
|
|||||||
rsmi_pcie_bandwidth_t bws;
|
rsmi_pcie_bandwidth_t bws;
|
||||||
|
|
||||||
TRY
|
TRY
|
||||||
|
REQUIRE_ROOT_ACCESS
|
||||||
DEVICE_MUTEX
|
DEVICE_MUTEX
|
||||||
ret = rsmi_dev_pci_bandwidth_get(dv_ind, &bws);
|
ret = rsmi_dev_pci_bandwidth_get(dv_ind, &bws);
|
||||||
|
|
||||||
@@ -1569,6 +1578,7 @@ rsmi_dev_fan_speed_set(uint32_t dv_ind, uint32_t sensor_ind, uint64_t speed) {
|
|||||||
rsmi_status_t ret;
|
rsmi_status_t ret;
|
||||||
uint64_t max_speed;
|
uint64_t max_speed;
|
||||||
|
|
||||||
|
REQUIRE_ROOT_ACCESS
|
||||||
DEVICE_MUTEX
|
DEVICE_MUTEX
|
||||||
|
|
||||||
ret = rsmi_dev_fan_speed_max_get(dv_ind, sensor_ind, &max_speed);
|
ret = rsmi_dev_fan_speed_max_get(dv_ind, sensor_ind, &max_speed);
|
||||||
@@ -1735,6 +1745,7 @@ rsmi_dev_power_cap_set(uint32_t dv_ind, uint32_t sensor_ind, uint64_t cap) {
|
|||||||
rsmi_status_t ret;
|
rsmi_status_t ret;
|
||||||
uint64_t min, max;
|
uint64_t min, max;
|
||||||
|
|
||||||
|
REQUIRE_ROOT_ACCESS
|
||||||
DEVICE_MUTEX
|
DEVICE_MUTEX
|
||||||
|
|
||||||
ret = rsmi_dev_power_cap_range_get(dv_ind, sensor_ind, &max, &min);
|
ret = rsmi_dev_power_cap_range_get(dv_ind, sensor_ind, &max, &min);
|
||||||
@@ -1774,6 +1785,8 @@ rsmi_status_t
|
|||||||
rsmi_dev_power_profile_set(uint32_t dv_ind, uint32_t sensor_ind,
|
rsmi_dev_power_profile_set(uint32_t dv_ind, uint32_t sensor_ind,
|
||||||
rsmi_power_profile_preset_masks_t profile) {
|
rsmi_power_profile_preset_masks_t profile) {
|
||||||
TRY
|
TRY
|
||||||
|
REQUIRE_ROOT_ACCESS
|
||||||
|
|
||||||
++sensor_ind; // power sysfs files have 1-based indices
|
++sensor_ind; // power sysfs files have 1-based indices
|
||||||
|
|
||||||
DEVICE_MUTEX
|
DEVICE_MUTEX
|
||||||
|
|||||||
@@ -241,6 +241,8 @@ RocmSMI::Initialize(uint64_t flags) {
|
|||||||
|
|
||||||
init_options_ = flags;
|
init_options_ = flags;
|
||||||
|
|
||||||
|
euid_ = geteuid();
|
||||||
|
|
||||||
GetEnvVariables();
|
GetEnvVariables();
|
||||||
|
|
||||||
while (std::string(kAMDMonitorTypes[i]) != "") {
|
while (std::string(kAMDMonitorTypes[i]) != "") {
|
||||||
@@ -335,10 +337,9 @@ RocmSMI::AddToDeviceList(std::string dev_name) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const uint32_t kAmdGpuId=0x1002;
|
static const uint32_t kAmdGpuId = 0x1002;
|
||||||
|
|
||||||
static bool isAMDGPU(std::string dev_path) {
|
static bool isAMDGPU(std::string dev_path) {
|
||||||
|
|
||||||
std::string vend_path = dev_path + "/device/vendor";
|
std::string vend_path = dev_path + "/device/vendor";
|
||||||
if (!FileExists(vend_path.c_str())) {
|
if (!FileExists(vend_path.c_str())) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user