Setting the Linux CPU scaling governor through udev rules

❝How to configure udev to set up a specific CPU scaling governor on boot.❞

Introduction

Linux allows you to select your own preferred scaling governor, which is the algorithm that is used control processor performance. On heavy load, the processor will work on full speed, while - during idle times - the processor is tuned back into a low-speed operating mode that is power-efficient.

Typically, a linux distribution comes with a particular scaling governor pre-selected. There are many instructions on how to manually configure/select a scaling governor. However, that is not helpful if you want to set such a scaling governor up as the default. The options that I have found on the internet for this are:

  1. Compile your own kernel and configure it to have a certain scaling governor as the default.
  2. Install cpufreq or whatever-arbitrary-scaling-tool and then call this command with this scaling governor to set it. Sometimes, if you’re lucky they offer a systemd init script so you can configure it on boot.

Both options, however are unsatisfactory in certain situations. For example, in case of using Fedora Silverblue you are left with little options in making changes to the root filesystem. This is done on purpose, to keep things manageable. However, one should carefully consider any application installed directly on top of the base image. The cpufreq package was not available, and installing your own kernel is certainly not recommended in such a carefully constructed immutable OS-image.

schedutil scaling governor

I have found no reference of it, but it turns out that it is possible to configure the scaling governor using the following udev rule. In my case I prefer schedutil as the scaling governor.

# Ensure `intel_pstate=passive`
KERNEL=="cpu[0-9]", SUBSYSTEM=="cpu", ATTR{cpufreq/scaling_governor}="schedutil"

The last part, ATTR{cpufreq/scaling_governor}="schedutil", is the directive that assigns the new scaling governor schedutil to the system’s processors. This could be a different scaling governor, such as powersave, ondemand or performance.

The schedutil scaling governor can only work if, in case of Intel processors, intel_pstate scaling driver is configured as passive. If you do not configure intel_pstate as passive, then schedutil will not be a valid scaling governor option. You can query available scaling governors through cat /sys/devices/system/cpu/cpufreq/policy0/scaling_available_governors.

To configure the intel_pstate kernel boot parameter on Fedora Silverblue:

 rpm-ostree kargs --append=intel_pstate=passive

Finally, once everything is set up, it is mandatory to reboot the system. After the reboot, one can check the active scaling governors through:

 cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

It will list the active scaling governor for each processor. Upon successful configuration, it should list all schedutil scaling governors.