Skip to content

Instantly share code, notes, and snippets.

@dlqqq
Forked from wmealing/C-states.md
Last active February 22, 2024 22:22
Show Gist options
  • Star 51 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save dlqqq/876d74d030f80dc899fc58a244b72df0 to your computer and use it in GitHub Desktop.
Save dlqqq/876d74d030f80dc899fc58a244b72df0 to your computer and use it in GitHub Desktop.
AMD Ryzen "Freezing" Bug on GNU/Linux Systems

Random "Freezing" with AMD Ryzen CPUs

It seems that numerous GNU/Linux users (including myself) have been having issues with the system randomly "freezing" during light usage. From journalctl output and anecdotal accounts, it is speculated that the AMD Ryzen CPUs do not support other C-states for power management very well (at least on GNU/Linux distributions), and the freezing may be resolved by limiting the C-state of the CPU.

Possible Solution

Limiting the C-state of the CPU can be done through the addition of the following kernel boot parameter.

processor.max_cstate=1

It is generally advised to add the kernel boot parameter through /etc/default/grub. The grub.cfg file can then be updated by running

# grub-mkconfig -o /boot/grub/grub.cfg

instead of manually editing the grub.cfg file. See the Arch wiki for more details.

On C-states (forked from wmealing)

To limit a CPU to a certain C-state, you can pass the processor.max_cstate=X option in the kernel line of /boot/grub/grub.conf.

Here we limit the system to only C-State 1:

kernel /vmlinuz-2.6.18-371.1.2.el5 ... processor.max_cstate=1

On some systems, the kernel can override the BIOS setting, and the parameter intel_idle.max_cstate=0 may be required to ensure sleep states are not entered:

kernel /vmlinuz-2.6.32-431.el6.x86_64 ... processor.max_cstate=1 intel_idle.max_cstate=0

You can confirm the maximum allowed CPU C-State with:

# cat /sys/module/intel_idle/parameters/max_cstate
0

In order to save energy when the CPU is idle, the CPU can be commanded to enter a low-power mode. Each CPU has several power modes and they are collectively called “C-states” or “C-modes.”.

The lower-power mode was first introduced with the 486DX4 processor. To the present, more power modes has been introduced and enhancements has been made to each mode for the CPU to consume less power in these low-power modes. The idea of these modes is to cut the clock signal and power from idle units inside the CPU. As many units you stop (by cutting the clock) as you reduce the voltage or even completely shut down to save energy. On the other hand, you have to take into account that more time is required for the CPU to “wake up” and be again 100% operational. These modes are known as C-states. They are usually starting in C0, which is the normal CPU operating mode, i.e., the CPU is 100% turned on. With increasing C number, the CPU sleep mode is deeper, i.e., more circuits and signals are turned off and more time the CPU will require to return to C0 mode, i.e., to wake-up. Each mode is also known by a name and several of them have sub-modes with different power saving – and thus wake-up time – levels.

mode Name What id does CPUs
C1 Operating State CPU fully turned on All CPUs
C1E Halt Stops CPU main internal clocks via software; bus interface unit and APIC are kept running at full speed 486DX4 and above
C1E Enhanced Halt Stops CPU main internal clocks via software and reduces CPU voltage; bus interface unit and APIC are kept running at full speed All socket 775 CPUs
C1E -- Stops all CPU internal clocks Turion 64, 65-nm Athlon X2 and Phenom CPUs
C2 Stop Grant Stops CPU main internal clocks via hardware; bus interface unit and APIC are kept running at full speed 486DX4 and above
C2 Stop Clock Stops CPU internal and external clocks via hardware Only 486DX4, Pentium, Pentium MMX, K5, K6, K6-2, K6-III
C2E Extended Stop Grant Stops CPU main internal clocks via hardware and reduces CPU voltage; bus interface unit and APIC are kept running at full speed Core 2 Duo and above (Intel only)
C3 Sleep Stops all CPU internal clocks Pentium II, Athlon and above, but not on Core 2 Duo E4000 and E6000
C3 Deep Sleep Stops all CPU internal and external clocks Pentium II and above, but not on Core 2 Duo E4000 and E6000; Turion 64
C3 AltVID Stops all CPU internal clocks and reduces CPU voltage AMD Turion 64
C4 Deeper Sleep Reduces CPU voltage Pentium M and above, but not on Core 2 Duo E4000 and E6000 series; AMD Turion 64
C4E/C5 Enhanced Deeper Sleep Reduces CPU voltage even more and turns off the memory cache Core Solo, Core Duo and 45-nm mobile Core 2 Duo only
C6 Deep Power Down Reduces the CPU internal voltage to any value, including 0 V 45-nm mobile Core 2 Duo only
@wmealing
Copy link

Nice write up on the fix.

@dlqqq
Copy link
Author

dlqqq commented Jun 4, 2020

Oh, something else worth mentioning: If the changing the C-state doesn't work, make sure you have AMD microcode updates installed (amd-ucode package). That's actually the first thing you should do, but I just assumed most people had already done so during the installation (since it's written clearly on the Arch wiki).

@lcreed-ibm
Copy link

Oh, something else worth mentioning: If the changing the C-state doesn't work, make sure you have AMD microcode updates installed (amd-ucode package). That's actually the first thing you should do, but I just assumed most people had already done so during the installation (since it's written clearly on the Arch wiki).

Where can I find the AMD microcode? None of the links I"m finding work. I'm having freezes on every distribution I've installed and am currently on Fedora.

@dlqqq
Copy link
Author

dlqqq commented Jan 15, 2021

@lcreed-ibm
Here's the Arch Linux package. You can try following the upstream URL link for more instructions on how to port it to your distribution.
https://archlinux.org/packages/core/any/amd-ucode/

@thisisanurag
Copy link

Can't see any kernel line (kernel /vmlinuz-2.6.32-431.el6.x86_64) in my grub conf, and adding processor.max_cstate=1 plainly gives me this error : - "processor.max_cstate=1: not found".

@thisisanurag
Copy link

thisisanurag commented Jun 24, 2021

Can't see any kernel line (kernel /vmlinuz-2.6.32-431.el6.x86_64) in my grub conf, and adding processor.max_cstate=1 plainly gives me this error : - "processor.max_cstate=1: not found".

So i found out you can pass parameters to load up during boot time in "GRUB_CMDLINE_LINUX_DEFAULT" which is located in "/etc/default/grub" file. It might look something like this

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash intel_idle.max_cstate=0"

so you can give parameters related to CPU C state over here, and after editing don't forget to do a "sudo update-grub".

@amfern
Copy link

amfern commented May 26, 2023

Limiting the c-state to 1 has solved my issue of freezing while idle. And allowed me to use negative curve optimizer without freezing during boot.

@moadennagi
Copy link

This worked for me, could someone point me (newbie) to more resources on this ?

@mjamroz
Copy link

mjamroz commented Aug 13, 2023

For me overclocking ram (volts) fixed the issue

@AndrewSpec
Copy link

This helped me fix my problem on my Proxmox node with an Ryzen 7 PRO 4750G and True NAS inside running on a Deskmeet X300.

@philippulus
Copy link

@dlqqq @wmealing @moadennagi @AndrewSpec
Can you please explain step by step how to update grub.cfg with the code line
processor.max_cstate=1
I have tried but it does not work at my end.
By the way the grub.cfg file is in /boot/grub, not in /etc/default/grub

@philippulus
Copy link

@mjamroz , how do you do overclocking RAM?

@mjamroz
Copy link

mjamroz commented Nov 13, 2023

@philippulus i was able to do it by BIOS, but I've dell (alienware) box.

@philippulus
Copy link

@mjamroz
My system bios does not appear to avail this option.
It is a MinisForum Mini PC.
So, I have to try to edit the grub file but don't understand how to do this.

@philippulus
Copy link

@dlqqq

Limiting the C-state of the CPU can be done through the addition of the following kernel boot parameter.

processor.max_cstate=1

It is generally advised to add the kernel boot parameter through /etc/default/grub. The grub.cfg file can then be updated by running

Where and how does adding this line of code concretely work? I can't find out.
Thanks for your help.

@wmealing
Copy link

Consult your distros documentation. There are too many options. It might be possible that it also doesn't fix your problem.

@philippulus
Copy link

@wmealing

This is my distro:
Ubuntu 23.10 Kernel 6.5.0-10
and my system is an AMD Ryzen 9 7940HS w/ RADEON 780M Graphics

I can do a
$ sudo nano /boot/grub/grub.cfg
where do I go from there?
I there a prefix to ad, or do i write the line
processor.max_cstate=1
anywhere in the file?
and after that do I still need to update /etc/default/grub even if did not do any modification in there?
Thank you.

@philippulus
Copy link

philippulus commented Nov 14, 2023

For now I did had no crash today since I changed the Power Settings in Power Saver mode, but it is obviously not the objective of purchasing this higher end Ryzen system. I would need high performance when running multiple and demanding applications.

@dlqqq
Copy link
Author

dlqqq commented Nov 14, 2023

@philippulus Please stop pinging everybody directly in a 4 year old fork of a 10 year old document. If you need help on this, it's best to consult the Arch Linux forums.

(I don't even use Arch Linux very much anymore)

@SalimF
Copy link

SalimF commented Nov 20, 2023

Where is AMD CPUs on this table? I guess I can assume it supported if not mentioned otherwise?
Anyway disabling C state will cause other problems with power saving on Linux you will no be able to put you pc on sleep or Hibernate mode
I got serious problem after enabling Hibernate my Laptop won't able to boot (wake) and
i stock in infinite restart loop, only solve it by re installing my entire boot loader using Arch instillation disk.

@philippulus
Copy link

philippulus commented Nov 21, 2023

Well, I edited
Sudo Nano /etc/default/grub
Changed the following line by adding 'processor.max_cstate=1':
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash processor.max_cstate=1"
CTRL + O, Return, CTRL+X
Changed Power state to balance, and have not experienced any freeze, and can suspend the PC.
Don't know if it is a direct effect of this change but it is working well so far.

@SalimF
Copy link

SalimF commented Nov 22, 2023

Well, I edited

BTW you need to update your config file with
sudo grub-mkconfig -o /boot/grub/grub.cfg

@yantodotid
Copy link

Well, I edited

BTW you need to update your config file with sudo grub-mkconfig -o /boot/grub/grub.cfg

Hi, this command is same as sudo update-grub right? Because recently got same issue with AMD Processor and randomly freezes on Ubuntu 22.04. I also edited my grub config in line GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" same as above and add the processor.max_cstate=1 with space after quiet splash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment