Provide locking around all adl calls to prevent races.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
diff --git a/adl.c b/adl.c
index cbbacd5..0e23b20 100644
--- a/adl.c
+++ b/adl.c
@@ -23,6 +23,7 @@ bool adl_active;
int opt_hysteresis = 3;
int opt_targettemp = 75;
int opt_overheattemp = 85;
+static pthread_mutex_t adl_lock;
// Memory allocation function
static void * __stdcall ADL_Main_Memory_Alloc(int iSize)
@@ -79,6 +80,16 @@ static LPAdapterInfo lpInfo = NULL;
static int set_fanspeed(int gpu, int iFanSpeed);
+static inline void lock_adl(void)
+{
+ mutex_lock(&adl_lock);
+}
+
+static inline void unlock_adl(void)
+{
+ mutex_unlock(&adl_lock);
+}
+
void init_adl(int nDevs)
{
int i, devices = 0, last_adapter = -1, gpu = 0, dummy = 0;
@@ -97,6 +108,11 @@ void init_adl(int nDevs)
return;
}
+ if (unlikely(pthread_mutex_init(&adl_lock, NULL))) {
+ applog(LOG_ERR, "Failed to init adl_lock in init_adl");
+ return;
+ }
+
ADL_Main_Control_Create = (ADL_MAIN_CONTROL_CREATE) GetProcAddress(hDLL,"ADL_Main_Control_Create");
ADL_Main_Control_Destroy = (ADL_MAIN_CONTROL_DESTROY) GetProcAddress(hDLL,"ADL_Main_Control_Destroy");
ADL_Adapter_NumberOfAdapters_Get = (ADL_ADAPTER_NUMBEROFADAPTERS_GET) GetProcAddress(hDLL,"ADL_Adapter_NumberOfAdapters_Get");
@@ -318,12 +334,16 @@ static inline float __gpu_temp(struct gpu_adl *ga)
float gpu_temp(int gpu)
{
struct gpu_adl *ga;
+ float ret = 0;
if (!gpus[gpu].has_adl || !adl_active)
- return 0;
+ return ret;
ga = &gpus[gpu].adl;
- return __gpu_temp(ga);
+ lock_adl();
+ ret = __gpu_temp(ga);
+ unlock_adl();
+ return ret;
}
static inline int __gpu_engineclock(struct gpu_adl *ga)
@@ -334,14 +354,19 @@ static inline int __gpu_engineclock(struct gpu_adl *ga)
int gpu_engineclock(int gpu)
{
struct gpu_adl *ga;
+ int ret = 0;
if (!gpus[gpu].has_adl || !adl_active)
- return 0;
+ return ret;
ga = &gpus[gpu].adl;
+ lock_adl();
if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
- return 0;
- return __gpu_engineclock(ga);
+ goto out;
+ ret = __gpu_engineclock(ga);
+out:
+ unlock_adl();
+ return ret;
}
static inline int __gpu_memclock(struct gpu_adl *ga)
@@ -352,14 +377,19 @@ static inline int __gpu_memclock(struct gpu_adl *ga)
int gpu_memclock(int gpu)
{
struct gpu_adl *ga;
+ int ret = 0;
if (!gpus[gpu].has_adl || !adl_active)
- return 0;
+ return ret;
ga = &gpus[gpu].adl;
+ lock_adl();
if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
- return 0;
- return __gpu_memclock(ga);
+ goto out;
+ ret = __gpu_memclock(ga);
+out:
+ unlock_adl();
+ return ret;
}
static inline float __gpu_vddc(struct gpu_adl *ga)
@@ -370,14 +400,19 @@ static inline float __gpu_vddc(struct gpu_adl *ga)
float gpu_vddc(int gpu)
{
struct gpu_adl *ga;
+ float ret = 0;
if (!gpus[gpu].has_adl || !adl_active)
- return 0;
+ return ret;
ga = &gpus[gpu].adl;
+ lock_adl();
if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
- return 0;
- return __gpu_vddc(ga);
+ goto out;
+ ret = __gpu_vddc(ga);
+out:
+ unlock_adl();
+ return ret;
}
static inline int __gpu_activity(struct gpu_adl *ga)
@@ -390,14 +425,20 @@ static inline int __gpu_activity(struct gpu_adl *ga)
int gpu_activity(int gpu)
{
struct gpu_adl *ga;
+ int ret;
if (!gpus[gpu].has_adl || !adl_active)
return 0;
ga = &gpus[gpu].adl;
- if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK)
+ lock_adl();
+ ret = ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity);
+ unlock_adl();
+ if (ret != ADL_OK)
return 0;
- return __gpu_activity(ga);
+ if (!ga->lpOdParameters.iActivityReportingSupported)
+ return 0;
+ return ga->lpActivity.iActivityPercent;
}
static inline int __gpu_fanspeed(struct gpu_adl *ga)
@@ -413,12 +454,16 @@ static inline int __gpu_fanspeed(struct gpu_adl *ga)
int gpu_fanspeed(int gpu)
{
struct gpu_adl *ga;
+ int ret = 0;
if (!gpus[gpu].has_adl || !adl_active)
- return 0;
+ return ret;
ga = &gpus[gpu].adl;
- return __gpu_fanspeed(ga);
+ lock_adl();
+ ret = __gpu_fanspeed(ga);
+ unlock_adl();
+ return ret;
}
static inline int __gpu_fanpercent(struct gpu_adl *ga)
@@ -434,12 +479,16 @@ static inline int __gpu_fanpercent(struct gpu_adl *ga)
int gpu_fanpercent(int gpu)
{
struct gpu_adl *ga;
+ int ret = 0;
if (!gpus[gpu].has_adl || !adl_active)
return -1;
ga = &gpus[gpu].adl;
- return __gpu_fanpercent(ga);
+ lock_adl();
+ ret = __gpu_fanpercent(ga);
+ unlock_adl();
+ return ret;
}
static inline int __gpu_powertune(struct gpu_adl *ga)
@@ -454,12 +503,16 @@ static inline int __gpu_powertune(struct gpu_adl *ga)
int gpu_powertune(int gpu)
{
struct gpu_adl *ga;
+ int ret = -1;
if (!gpus[gpu].has_adl || !adl_active)
- return -1;
+ return ret;
ga = &gpus[gpu].adl;
- return __gpu_powertune(ga);
+ lock_adl();
+ ret = __gpu_powertune(ga);
+ unlock_adl();
+ return ret;
}
bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc,
@@ -471,6 +524,8 @@ bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vdd
return false;
ga = &gpus[gpu].adl;
+
+ lock_adl();
*temp = __gpu_temp(ga);
if (ADL_Overdrive5_CurrentActivity_Get(ga->iAdapterIndex, &ga->lpActivity) != ADL_OK) {
*engineclock = 0;
@@ -486,6 +541,7 @@ bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vdd
*fanspeed = __gpu_fanspeed(ga);
*fanpercent = __gpu_fanpercent(ga);
*powertune = __gpu_powertune(ga);
+ unlock_adl();
return true;
}
@@ -507,11 +563,11 @@ static int set_engineclock(int gpu, int iEngineClock)
{
ADLODPerformanceLevels *lpOdPerformanceLevels;
struct gpu_adl *ga;
- int lev;
+ int lev, ret = 1;
if (!gpus[gpu].has_adl || !adl_active) {
wlogprint("Set engineclock not supported\n");
- return 1;
+ return ret;
}
iEngineClock *= 100;
@@ -520,11 +576,13 @@ static int set_engineclock(int gpu, int iEngineClock)
lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
+
+ lock_adl();
if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
- return 1;
+ goto out;
lpOdPerformanceLevels->aLevels[lev].iEngineClock = iEngineClock;
if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
- return 1;
+ goto out;
ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
/* Reset to old value if it fails! */
if (lpOdPerformanceLevels->aLevels[lev].iEngineClock != iEngineClock) {
@@ -534,7 +592,7 @@ static int set_engineclock(int gpu, int iEngineClock)
lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
- return 1;
+ goto out;
}
ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
if (ga->iEngineClock > ga->maxspeed)
@@ -543,7 +601,10 @@ static int set_engineclock(int gpu, int iEngineClock)
ga->minspeed = ga->iEngineClock;
ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
- return 0;
+ ret = 0;
+out:
+ unlock_adl();
+ return ret;
}
static void get_memoryrange(int gpu, int *imin, int *imax)
@@ -563,11 +624,11 @@ static int set_memoryclock(int gpu, int iMemoryClock)
{
ADLODPerformanceLevels *lpOdPerformanceLevels;
struct gpu_adl *ga;
- int lev;
+ int lev, ret = 1;
if (!gpus[gpu].has_adl || !adl_active) {
wlogprint("Set memoryclock not supported\n");
- return 1;
+ return ret;
}
iMemoryClock *= 100;
@@ -576,11 +637,13 @@ static int set_memoryclock(int gpu, int iMemoryClock)
lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
+
+ lock_adl();
if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
- return 1;
+ goto out;
lpOdPerformanceLevels->aLevels[lev].iMemoryClock = iMemoryClock;
if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
- return 1;
+ goto out;
ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
/* Reset to old value if it fails! */
if (lpOdPerformanceLevels->aLevels[lev].iMemoryClock != iMemoryClock) {
@@ -590,12 +653,15 @@ static int set_memoryclock(int gpu, int iMemoryClock)
lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
- return 1;
+ goto out;
}
ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
- return 0;
+ ret = 0;
+out:
+ unlock_adl();
+ return ret;
}
static void get_vddcrange(int gpu, float *imin, float *imax)
@@ -625,12 +691,12 @@ static float curses_float(const char *query)
static int set_vddc(int gpu, float fVddc)
{
ADLODPerformanceLevels *lpOdPerformanceLevels;
+ int iVddc, lev, ret = 1;
struct gpu_adl *ga;
- int iVddc, lev;
if (!gpus[gpu].has_adl || !adl_active) {
wlogprint("Set vddc not supported\n");
- return 1;
+ return ret;
}
iVddc = 1000 * fVddc;
@@ -639,11 +705,13 @@ static int set_vddc(int gpu, float fVddc)
lev = ga->lpOdParameters.iNumberOfPerformanceLevels - 1;
lpOdPerformanceLevels = alloca(sizeof(ADLODPerformanceLevels) + (lev * sizeof(ADLODPerformanceLevel)));
lpOdPerformanceLevels->iSize = sizeof(ADLODPerformanceLevels) + sizeof(ADLODPerformanceLevel) * lev;
+
+ lock_adl();
if (ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK)
- return 1;
+ goto out;
lpOdPerformanceLevels->aLevels[lev].iVddc = iVddc;
if (ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels) != ADL_OK)
- return 1;
+ goto out;
ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
/* Reset to old value if it fails! */
if (lpOdPerformanceLevels->aLevels[lev].iVddc != iVddc) {
@@ -653,12 +721,15 @@ static int set_vddc(int gpu, float fVddc)
lpOdPerformanceLevels->aLevels[lev].iVddc = ga->iVddc;
ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, lpOdPerformanceLevels);
ADL_Overdrive5_ODPerformanceLevels_Get(ga->iAdapterIndex, 0, lpOdPerformanceLevels);
- return 1;
+ goto out;
}
ga->iEngineClock = lpOdPerformanceLevels->aLevels[lev].iEngineClock;
ga->iMemoryClock = lpOdPerformanceLevels->aLevels[lev].iMemoryClock;
ga->iVddc = lpOdPerformanceLevels->aLevels[lev].iVddc;
- return 0;
+ ret = 0;
+out:
+ unlock_adl();
+ return ret;
}
static void get_fanrange(int gpu, int *imin, int *imax)
@@ -677,17 +748,20 @@ static void get_fanrange(int gpu, int *imin, int *imax)
static int set_fanspeed(int gpu, int iFanSpeed)
{
struct gpu_adl *ga;
+ int ret = 1;
if (!gpus[gpu].has_adl || !adl_active) {
wlogprint("Set fanspeed not supported\n");
- return 1;
+ return ret;
}
ga = &gpus[gpu].adl;
if (!(ga->lpFanSpeedInfo.iFlags & (ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE | ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE )))
- return 1;
+ return ret;
+
+ lock_adl();
if (ADL_Overdrive5_FanSpeed_Get(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
- return 1;
+ goto out;
ga->lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
if ((ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_RPM_WRITE) &&
!(ga->lpFanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE)) {
@@ -698,28 +772,37 @@ static int set_fanspeed(int gpu, int iFanSpeed)
ga->lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
ga->lpFanSpeedValue.iFanSpeed = iFanSpeed;
if (ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->lpFanSpeedValue) != ADL_OK)
- return 1;
- return 0;
+ goto out;
+ ret = 0;
+out:
+ unlock_adl();
+ return ret;
}
static int set_powertune(int gpu, int iPercentage)
{
int oldPercentage, dummy;
struct gpu_adl *ga;
+ int ret = 1;
if (!gpus[gpu].has_adl || !adl_active) {
wlogprint("Set powertune not supported\n");
- return 1;
+ return ret;
}
ga = &gpus[gpu].adl;
oldPercentage = ga->iPercentage;
+
+ lock_adl();
if (ADL_Overdrive5_PowerControl_Set(ga->iAdapterIndex, iPercentage) != ADL_OK) {
ADL_Overdrive5_PowerControl_Set(ga->iAdapterIndex, ga->iPercentage);
- return 1;
+ goto out;
}
ADL_Overdrive5_PowerControl_Get(ga->iAdapterIndex, &ga->iPercentage, &dummy);
- return 0;
+ ret = 0;
+out:
+ unlock_adl();
+ return ret;
}
void gpu_autotune(int gpu)
@@ -732,8 +815,12 @@ void gpu_autotune(int gpu)
return;
ga = &gpus[gpu].adl;
+
+ lock_adl();
temp = __gpu_temp(ga);
newpercent = fanpercent = __gpu_fanpercent(ga);
+ unlock_adl();
+
newengine = engine = gpu_engineclock(gpu) * 100;
if (temp && fanpercent >= 0 && ga->autofan) {
@@ -796,7 +883,9 @@ void set_defaultfan(int gpu)
return;
ga = &gpus[gpu].adl;
+ lock_adl();
ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
+ unlock_adl();
}
void set_defaultengine(int gpu)
@@ -806,7 +895,9 @@ void set_defaultengine(int gpu)
return;
ga = &gpus[gpu].adl;
+ lock_adl();
ADL_Overdrive5_ODPerformanceLevels_Set(ga->iAdapterIndex, ga->DefPerfLev);
+ unlock_adl();
}
void change_autosettings(int gpu)
@@ -961,6 +1052,7 @@ void clear_adl(nDevs)
if (!adl_active)
return;
+ lock_adl();
/* Try to reset values to their defaults */
for (i = 0; i < nDevs; i++) {
ga = &gpus[i].adl;
@@ -974,6 +1066,7 @@ void clear_adl(nDevs)
ADL_Main_Memory_Free ( (void **)&lpInfo );
ADL_Main_Control_Destroy ();
+ unlock_adl();
#if defined (LINUX)
dlclose(hDLL);