r4m0n / ZenStates-Linux

Dynamically edit AMD Ryzen processor P-States
MIT License
522 stars 66 forks source link

Zenstates keeps saying MSR isn't loaded when it is #6

Open smithaa02 opened 5 years ago

smithaa02 commented 5 years ago

I've enabled MSR. I've done this on boot up...

sudo nano -w /etc/modules-load.d/modules.conf msr ctrl-x (y) sudo reboot now

I've also also added this manually:

sudo modprobe msr

When I check to see if my modules are listed with lsmod, I do indeed see MSR:

lsmod | grep msr

But...when I run zenstates...it always says MSR module not loaded.

/home/smithaa02/ryzen/ZenStates-Linux/zenstates.py --c6-disable Traceback (most recent call last): File "/home/smithaa02/ryzen/ZenStates-Linux/zenstates.py", line 112, in <module> writemsr(0xC0010292, readmsr(0xC0010292) & ~(1 << 32)) File "/home/smithaa02/ryzen/ZenStates-Linux/zenstates.py", line 33, in readmsr raise OSError("msr module not loaded (run modprobe msr)") OSError: msr module not loaded (run modprobe msr)

Even the list command shows no MSR:

smithaa02@smithaa02-MS-7B79:/dev/cpu/1$ /home/smithaa02/ryzen/ZenStates-Linux/zenstates.py -l Traceback (most recent call last): File "/home/smithaa02/ryzen/ZenStates-Linux/zenstates.py", line 76, in <module> print('P' + str(p) + " - " + pstate2str(readmsr(pstates[p]))) File "/home/smithaa02/ryzen/ZenStates-Linux/zenstates.py", line 33, in readmsr raise OSError("msr module not loaded (run modprobe msr)") OSError: msr module not loaded (run modprobe msr)

The Pythod code this references is:

try: f = os.open('/dev/cpu/%d/msr' % cpu, os.O_RDONLY) os.lseek(f, msr, os.SEEK_SET) val = struct.unpack('Q', os.read(f, 8))[0] os.close(f) return val except: raise OSError("msr module not loaded (run modprobe msr)")

But this makes no sense. How can the program complain about missing /dev/cpu/*/msr when I see them?

smithaa02@smithaa02-MS-7B79:/dev/cpu$ ls -al /dev/cpu/*/msr crw------- 1 root root 202, 0 Jan 20 16:38 /dev/cpu/0/msr crw------- 1 root root 202, 1 Jan 20 16:38 /dev/cpu/1/msr crw------- 1 root root 202, 2 Jan 20 16:38 /dev/cpu/2/msr crw------- 1 root root 202, 3 Jan 20 16:38 /dev/cpu/3/msr

Any help would be appreciated. My ryzen crashes have been hell...I really think it is the c6 issues and I'm desperate to fix this.

My system is:

OdinVex commented 5 years ago

Due to changes in Linux regarding MSR-permissions (read/write can be used to subvert Kernel, code can be executed) you must (for python 3.6...) (disabling SecureBoot used to work before the changes were made):

sudo setcap cap_sys_rawio=ep /usr/bin/python3.6
sudo /usr/bin/python3.6 ./zenstates.py ...

However, I wouldn't recommend giving python such capabilities like that as a whole. Hopefully a SIGNED kernel module will be available in the future to allow mediating MSR reads/writes for sudoers.

Edit: zenstates.py should be modified to check for a -13 error code. This will differentiate the situation so that you can tell if module msr is loaded or not, instead of reporting (erroneously) that msr is not loaded just because any error occurs.

Edit: To undo any setcap: sudo setcap -r /usr/bin/python3.6

Edit: I would recommend: https://github.com/jfredrickson/disable-c6 Originally posted by @jfredrickson in https://github.com/r4m0n/ZenStates-Linux/issues/4#issuecomment-443007570

smithaa02 commented 5 years ago

Thank you for your suggestion. I followed your instructions and now I don't get an error when I run this command:

sudo /usr/bin/python3.6 /home/smithaa02/ryzen/ZenStates-Linux/zenstates.py --c6-disable

Furthermore I can confirm that C6 is indeed off with this command:

sudo /usr/bin/python3.6 /home/smithaa02/ryzen/ZenStates-Linux/zenstates.py -l

P0 - Enabled - FID = 73 - DID = A - VID = 35 - Ratio = 23.00 - vCore = 1.21875 P1 - Enabled - FID = 8A - DID = C - VID = 5C - Ratio = 23.00 - vCore = 0.97500 P2 - Enabled - FID = 80 - DID = 10 - VID = 6C - Ratio = 16.00 - vCore = 0.87500 P3 - Disabled P4 - Disabled P5 - Disabled P6 - Disabled P7 - Disabled C6 State - Package - Disabled C6 State - Core - Disabled

Question though...how can I automatically load this? "Disable-c6" uses a different permutation of my command, so I assume that wouldn't work?

This site (https://forum.manjaro.org/t/ryzen-freezes-possible-solution-related-to-c6-state/37870) suggests you just need to load the kill command in Systemd...but I'm not super familiar with SystemD...do you know how I would set this up and if it would work with sudo access?

Also, do you know if this kill command must be run continually in a loop on the background, or do you just need to load it once on bootup?

Thanks again for your help.

OdinVex commented 5 years ago

In response, again, I would recommend: https://github.com/jfredrickson/disable-c6. You can use systemd to automatically do this on boot. I modified Zenstates to disable all but P0 and C6 with my own command-line parameter and incorporated that into the systemd stuff that the link mentions. I never had to do anything with kill commands or any such stuff. It seems to keep until shutdown. Not sure if the systemd task/job needs sudo, but I put it in anyway. I did not need to give python any special permission!

smithaa02 commented 5 years ago

If any Ryzen user finds this via Google...I continue to suffer system crashes. I'm going to try to disable P1 and P2 now to see if that helps. Cross my fingers...

sudo /usr/bin/python3.6 /home/smithaa02/ryzen/ZenStates-Linux/zenstates.py --disable -p 1 sudo /usr/bin/python3.6 /home/smithaa02/ryzen/ZenStates-Linux/zenstates.py --disable -p 2 sudo /usr/bin/python3.6 /home/smithaa02/ryzen/ZenStates-Linux/zenstates.py --c6-disable

OdinVex commented 5 years ago

If any Ryzen user finds this via Google...I continue to suffer system crashes. I'm going to try to disable P1 and P2 now to see if that helps. Cross my fingers...

sudo /usr/bin/python3.6 /home/smithaa02/ryzen/ZenStates-Linux/zenstates.py --disable -p 1 sudo /usr/bin/python3.6 /home/smithaa02/ryzen/ZenStates-Linux/zenstates.py --disable -p 2 sudo /usr/bin/python3.6 /home/smithaa02/ryzen/ZenStates-Linux/zenstates.py --c6-disable

I hope that is through systemd. You don't need to give python those caps if you do it through systemd making it much safer...

My modified version of disable-c6 loops through all P-states except 0 and disables them but leaves C6 optionally enabled or disabled. Trivial to modify both projects to add this functionality. I did it to stop ASUS from modifying my rig to throttle with the power unplugged. Helps but not perfect.

smithaa02 commented 5 years ago

What's curious is it seems this script is less effective if executed later. For example if I boot up...load my browser, music program etc...then execute zenstates.py...then I have higher chances of a crash. This appears to be more effective if executed as soon as possible.

Ideally this is executed at bootup. The following are instruction on how to do this for Mint/Ubuntu and SystemD.

1) sudo nano /etc/systemd/system/killryzen.service

2) Wite your file. eg:

[Unit] Description=Kill Ryzen

[Service] ExecStart=/usr/bin/python3.6 /home/smithaa02/ryzen/ZenStates-Linux/zenstates.py --c6-disable

[Install] WantedBy=multi-user.target

3) Save (with nano this is ctrl-x + y)

4) Make the file writeable

sudo chmod u+x /etc/systemd/system/killryzen.service

5) Test this to make sure it works

sudo systemctl start killryzen

6) Add to your startup

sudo systemctl enable killryzen

7) Reboot to verify that this worked

sudo reboot now sudo /usr/bin/python3.6 /home/smithaa02/ryzen/ZenStates-Linux/zenstates.py -l

If C6 is off, then you know this worked. As Yuji mentioned, this is a nice way to do this as you don't need to run systemd commands with sudo.

smithaa02 commented 5 years ago

Another update for wayward googlers that find this. I've tried a LOT of things to fix this stupid Ryzen bug...nothing worked. Except...once I got an external video card ALL problems went away. The card I got was the R7240-2GD3-L which is about $75 on Amazon, and is comparable to the graphics on the 2200.

OdinVex commented 5 years ago

bug

What bug, anyway? Behaves fine on all of my Ryzen systems.

smithaa02 commented 5 years ago

Never worked on my MSI X470 motherboard. Many other users have had similar problem. I suspect this issue might be motherboard specific. Doesn't matter now...an external graphics card fixed all my random lockups.

All3xJ commented 3 years ago

For me the issue was that I wasn't running zenstates as root.