Forceful Cooling in Linux

Several weeks ago during a compilation process, I noticed that my laptop became very hot under my palms. At first, I did not pay any attention to this, however, when it became uncomfortable to work I started to worry. My first thought was that the laptop got dusted and cannot remove the heat effectively. But then I noticed that I did not hear the fan noise when the load on the CPU increases, and I decided that my cooler is either broken or blocked. I was almost about to start disassembling my laptop, but luckily I decided to check the temperature using Linux utilities. There I found out that, despite I feel the laptop being hot, the sensor [thinkpad-isa-0000 -> temp1] showed that the CPU temperature was normal (showing all the time the temperature of 45°C). This looked suspicious, and I checked other sensors measurements and found out that the [coretemp-isa-0000] sensors showed more correct temperature values, which in addition reacted on load increase. In this article, I want to describe, how I forced my system to react also on the values from these additional sensors and cooled down my laptop.

Now, several weeks after when I write this article, the temperatures on the default CPU sensor [thinkpad-isa-0000 -> temp1] are back showing correct values. I do not know if it was a software fault or my hardware did not work properly at the time. However, in this article I want to describe how can you configure your system to take into consideration measurements from other temperature sensors in your system and how to change the fan rotation speed depending on the temperature values.
Table of Contents

Preparing to Monitor Temperature

If you run a Linux distro and face with a CPU heat problem, the first thing is to install a utility that will show you the temperature of different hardware components. In the Linux world, this utility is called sensors or lm-sensors (I have found references to both these names). So, if you run Kubuntu 20.04, install it using the following command:

$ sudo apt install lm-sensors

Then, you need to run the command sudo sensors-detect in order to help kernel to understand what modules need to be loaded in order to collect the information from sensors properly. After the inspection, the utility will offer you to add some modules that the kernel should load at boot time. In my case, it offered me to add the following lines to /etc/modules:

# Chip drivers
coretemp

After you have modified this file, reboot your system for the configuration to take effect. After your system boots up, you can execute the sensors command to check your sensors’ measurements. In my case, the output looks the following way (I provide the output of the command now, when the sensor [thinkpad-isa-0000 -> temp1] shows correct values and the fan [thinkpad-isa-0000 -> fan1] rotates. However, when I had the issue the situation was different):

$ sensors
coretemp-isa-0000
Adapter: ISA adapter
Package id 0:  +57.0°C  (high = +84.0°C, crit = +100.0°C)
Core 0:        +52.0°C  (high = +84.0°C, crit = +100.0°C)
Core 1:        +50.0°C  (high = +84.0°C, crit = +100.0°C)
Core 2:        +51.0°C  (high = +84.0°C, crit = +100.0°C)
Core 3:        +52.0°C  (high = +84.0°C, crit = +100.0°C)

BAT0-acpi-0
Adapter: ACPI interface
in0:          12.38 V  

thinkpad-isa-0000
Adapter: ISA adapter
fan1:        2523 RPM
temp1:        +63.0°C  
temp2:         +0.0°C  
temp3:         +0.0°C  
temp4:         +0.0°C  
temp5:         +0.0°C  
temp6:         +0.0°C  
temp7:         +0.0°C  
temp8:         +0.0°C  

nouveau-pci-0100
Adapter: PCI adapter
GPU core:         N/A  (min =  +0.60 V, max =  +1.20 V)
temp1:            N/A  (high = +95.0°C, hyst =  +3.0°C)
                       (crit = +105.0°C, hyst =  +5.0°C)
                       (emerg = +135.0°C, hyst =  +5.0°C)

acpitz-acpi-0
Adapter: ACPI interface
temp1:        +63.0°C  (crit = +200.0°C)

It is better to run this utility periodically and check how the values change. The following command will help you to achieve this (it will run the command sensors every second):

$ watch -n 1 sensors

This approach helped me find out that the CPU fan was not rotating because the temperature remained the same.

Configuring Forceful Cooling

After I have discovered this issue, I started looking for solutions how to configure fan speed using other sensors values as input. Luckily, I am not the first who faced with this issue, and in the Linux world I found references to two utilities: fancontrol and thinkfan. So as I have a Thinkpad laptop, I chose thinkfan. If you run Ubuntu-based distro, you can install this tool with the following command:

$ sudo apt install thinkfan
Note that in Ubuntu 20.04 based repositories, the thinkfan default version is 0.9.1. This utility has been considerably refactored in version 1.0. In this version, the format of the configuration file is changed to YAML, contrary to the simple conf format used in the prior versions. Moreover, in version 1.1 the support of referencing sensor files by name instead of full path is added. So as the path to a sensor file may change during system’s operation, this may cause thinkfan to fail. Therefore, if you have a possibility to install a more recent version I suggest you to do so. However, in this article I will show how to configure thinkfan version default for my system, namely 0.9.1.

Now, we need to configure our thinkfan utility. At first, we need to discover the full paths to the sensors files to read the temperature values from. You can list all the temperature file sensors using the following command:

$ find /sys/devices -type f -name "temp*_input"
/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon5/temp6_input
/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon5/temp3_input
/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon5/temp7_input
/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon5/temp4_input
/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon5/temp8_input
/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon5/temp1_input
/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon5/temp5_input
/sys/devices/platform/thinkpad_hwmon/hwmon/hwmon5/temp2_input
/sys/devices/platform/coretemp.0/hwmon/hwmon4/temp3_input
/sys/devices/platform/coretemp.0/hwmon/hwmon4/temp4_input
/sys/devices/platform/coretemp.0/hwmon/hwmon4/temp1_input
/sys/devices/platform/coretemp.0/hwmon/hwmon4/temp5_input
/sys/devices/platform/coretemp.0/hwmon/hwmon4/temp2_input
/sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0/hwmon/hwmon3/temp1_input
/sys/devices/virtual/thermal/thermal_zone0/hwmon1/temp1_input

Compare this list here with the output of the sensors command and select the ones that are relevant for your platform. In my case, I chose the following sensor files:

hwmon /sys/devices/platform/thinkpad_hwmon/hwmon/hwmon5/temp1_input
hwmon /sys/devices/platform/coretemp.0/hwmon/hwmon4/temp1_input
hwmon /sys/devices/platform/coretemp.0/hwmon/hwmon4/temp3_input
hwmon /sys/devices/platform/coretemp.0/hwmon/hwmon4/temp4_input
hwmon /sys/devices/platform/coretemp.0/hwmon/hwmon4/temp5_input
hwmon /sys/devices/platform/coretemp.0/hwmon/hwmon4/temp2_input

Once we found the full paths to the sensor files, we can start editing thinkfan configuration file. The default configuration is stored in the /etc/thinkfan.conf file. Open this file (sudo nano /etc/thinkfan.conf) and add the paths to your selected sensors files.

After the lines with the file paths, you have to add a set of tuples. The first element in a tuple reflects the relative fan speed value (it changes from 0 to 7, where 0 means that the fan does not rotate and 7 forces fan to rotate with its maximum speed). The second tuple element shows the minimum temperature when the fan should switch to the previous speed. The third element, correspondingly, shows the maximum temperature the fan should switch to the next speed value. The values of the temperatures you should select on your own, based on the normal working and max temperatures recommended by your processor manufacturer (search the Internet for these values).

Thus, the file in my case looks in the following way:

hwmon /sys/devices/platform/thinkpad_hwmon/hwmon/hwmon5/temp1_input
hwmon /sys/devices/platform/coretemp.0/hwmon/hwmon4/temp1_input
hwmon /sys/devices/platform/coretemp.0/hwmon/hwmon4/temp3_input
hwmon /sys/devices/platform/coretemp.0/hwmon/hwmon4/temp4_input
hwmon /sys/devices/platform/coretemp.0/hwmon/hwmon4/temp5_input
hwmon /sys/devices/platform/coretemp.0/hwmon/hwmon4/temp2_input
(0,     0,      55)
(1,     48,     60)
(2,     50,     61)
(3,     52,     63)
(4,     56,     65)
(5,     59,     66)
(7,     63,     32767)
Note that the last temperature should be equal to 32767 – the maximum possible value.

After the configuration is done, you need to start the thinkfan utility. In Ubuntu 20.04 during the installation, thinkfan creates a systemd service entry. So you can run it by starting this service: sudo systemctl start thinkfan. Now, check that it runs correctly and does not produce errors. You can check the service status and the logs by executing the command:

$ sudo systemctl status thinkfan

If everything is fine, then enable your service so that it starts during the boot:

$ sudo systemctl enable thinkfan

I also suggest checking the status of the utility after you reboot your computer. As I have previously mentioned the paths to the sensors files may change, and this may cause the utility to fail, and your system would not work properly.

Note that in the list of temperature sensors there could also be the temperature sensors of your hard drives. The working temperatures for them are usually much lower than for CPU. Thus, make sure that you use CPU sensors in your configuration, otherwise you may burn your system.

Conclusion

I hope that the advices from this article won’t come in handy because your monitoring system works well, and the fan draws heat off efficiently. However, if you face with the same issue as myself or want to experiment with fan noise levels, you are given with the necessary knowledge.

Related