How to check if a kernel feature is supported in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a kernel feature is supported in Linux. This involves exploring different methods to identify CPU features and verify kernel configurations.

First, you'll use the cat /proc/cpuinfo command to examine detailed information about your CPU, including its model, speed, and supported features, which are listed as flags. Next, you'll learn how to verify the kernel configuration using zcat /proc/config.gz. Finally, you'll inspect feature logs in dmesg.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/TextProcessingGroup(["Text Processing"]) linux(("Linux")) -.-> linux/CompressionandArchivingGroup(["Compression and Archiving"]) linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/TextProcessingGroup -.-> linux/grep("Pattern Searching") linux/CompressionandArchivingGroup -.-> linux/gzip("Gzip") subgraph Lab Skills linux/cat -.-> lab-558863{{"How to check if a kernel feature is supported in Linux"}} linux/grep -.-> lab-558863{{"How to check if a kernel feature is supported in Linux"}} linux/gzip -.-> lab-558863{{"How to check if a kernel feature is supported in Linux"}} end

Check CPU features with cat /proc/cpuinfo

In this step, we'll explore how to check your CPU's features using the cat /proc/cpuinfo command. This command displays detailed information about each CPU core in your system, including its model, speed, and supported features.

The /proc directory is a virtual file system that provides information about the system's processes and hardware. The /proc/cpuinfo file contains information about the CPU.

To view the CPU information, open your terminal. You can find the Xfce Terminal icon on the left side of your desktop.

Now, type the following command and press Enter:

cat /proc/cpuinfo

This command will output a lot of information. Let's break down some of the key fields:

  • processor: This is the logical processor number. In a multi-core system, each core will have its own processor number.
  • vendor_id: This identifies the CPU manufacturer (e.g., GenuineIntel or AuthenticAMD).
  • cpu family: This indicates the CPU family.
  • model name: This provides a more detailed description of the CPU model. For example: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz.
  • cpu MHz: This shows the CPU's clock speed in megahertz (MHz).
  • cache size: This indicates the size of the CPU cache.
  • flags: This is a list of CPU features, such as fpu, vme, de, sse2, avx2, and many others. These flags indicate the instruction sets and capabilities supported by the CPU.

The output will look something like this (the exact details will vary depending on your CPU):

processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 158
model name      : Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
stepping        : 10
microcode       : 0xca
cpu MHz         : 3696.062
cache size      : 12288 KB
physical id     : 0
siblings        : 1
core id         : 0
cpu cores       : 1
apicid          : 0
initial apicid  : 0
fpu             : yes
fpu_exception   : yes
cpuid level     : 22
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm_syscall invpcid_single pti ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts md_clear flush_l1d
bugs            : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs taa
bogomips        : 7392.12
clflush size    : 64
cache_alignment : 64
address sizes   : 46 bits physical, 48 bits virtual

You can scroll through the output to see all the details.

This information can be useful for troubleshooting performance issues, identifying hardware limitations, or simply understanding your system's capabilities.

Verify kernel config with zcat /proc/config.gz

In this step, we'll learn how to verify the kernel configuration using the zcat /proc/config.gz command. The kernel configuration determines which features and modules are built into the kernel.

The /proc/config.gz file contains the kernel configuration, but it's usually compressed using gzip. We'll use zcat to decompress and view the contents of this file.

To view the kernel configuration, open your terminal.

Now, type the following command and press Enter:

zcat /proc/config.gz

This command will output the kernel configuration. It's a long list of options, each starting with CONFIG_.

The output will look something like this (the exact details will vary depending on your kernel configuration):

#
## Automatically generated file; DO NOT EDIT.
## Linux/x86_64 5.15.0-76-generic Kernel Configuration
#
CONFIG_64=y
CONFIG_X86_64=y
CONFIG_X86=y
CONFIG_MMU=y
CONFIG_FIRMWARE_MEMMAP=y
CONFIG_PPC_MM_NATIVE=n
CONFIG_ARCH_HAS_FORTIFY_SOURCE=y
CONFIG_ARCH_FORTIFY_SOURCE_STRIPPED=y
CONFIG_HAVE_SETUP_PER_CPU_AREA=y
CONFIG_HAVE_DYNAMIC_FTRACE=y
CONFIG_HAVE_FUNCTION_GRAPH_TRACER=y
CONFIG_HAVE_FUNCTION_TRACER=y
CONFIG_HAVE_FTRACE_MCOUNT_RECORD=y
CONFIG_HAVE_ARCH_SPINLOCK=y
CONFIG_HAVE_ARCH_SPINLOCK_UNLOCKED=y
CONFIG_HAVE_ARCH_RWLOCK=y
CONFIG_HAVE_ARCH_SECCOMP_FILTER=y
CONFIG_HAVE_ARCH_EFFICIENT_UNALIGNED=y
CONFIG_HAVE_ARCH_MMAP_RND_BITS=y
CONFIG_HAVE_ARCH_USERFAULTFD_WP=y
CONFIG_HAVE_ARCH_USERFAULTFD_MINOR=y
CONFIG_HAVE_ARCH_SELECT_MEMORY_MODEL=y
CONFIG_HAVE_MIN_ALIGNMENT=y
CONFIG_CC_IS_Clang=n
CONFIG_IRQ_WORK=y
CONFIG_BUILDTIME_TABLE_SORT=y
CONFIG_THREAD_INFO_IN_TASK=y

... (more configuration options) ...

You can search for specific options using grep. For example, to check if the CONFIG_EXT4_FS option is enabled, you can use the following command:

zcat /proc/config.gz | grep CONFIG_EXT4_FS

If the option is enabled, you'll see a line like this:

CONFIG_EXT4_FS=y

If the option is disabled, you might see a line like this:

## CONFIG_EXT4_FS is not set

This information can be useful for understanding how your kernel is configured and for troubleshooting issues related to specific kernel features.

Inspect feature logs in dmesg

In this step, we'll learn how to inspect feature logs using the dmesg command. dmesg displays the kernel's message buffer, which contains information about hardware detection, driver initialization, and other system events. It's a valuable tool for troubleshooting hardware-related issues.

To view the kernel message buffer, open your terminal.

Now, type the following command and press Enter:

dmesg

This command will output a lot of information. The output is a chronological log of kernel messages.

The output will look something like this (the exact details will vary depending on your system):

[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Initializing cgroup subsys cpuacct
[    0.000000] Linux version 5.15.0-76-generic (buildd@lcy02-amd64-078) (gcc (Ubuntu 9.4.0-1ubuntu1) 9.4.0, GNU ld (GNU Binutils for Ubuntu) 2.34) #83-Ubuntu SMP Thu Jun 15 19:16:42 UTC 2023
[    0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-5.15.0-76-generic root=UUID=... ro
[    0.000000] KERNEL supported cpus:
[    0.000000]   Intel GenuineIntel
[    0.000000]   AMD AuthenticAMD
[    0.000000]   Hygon HygonGenuine
[    0.000000]   Centaur CentaurHauls
[    0.000000] x86/fpu: Supporting XSAVE feature 0x1: 'x87 floating point registers'
[    0.000000] x86/fpu: Supporting XSAVE feature 0x2: 'SSE registers'
[    0.000000] x86/fpu: Supporting XSAVE feature 0x4: 'AVX registers'
[    0.000000] x86/fpu: Supporting XSAVE feature 0xd: 'AVX-512 opmask, upper bytes of ZMM0-ZMM15, EVEX encoded data ZMM0-ZMM15'
[    0.000000] x86/fpu: Supporting XSAVE feature 0xe: 'AVX-512 hi256 zmm, opmask registers ZMM16-ZMM31'
[    0.000000] x86/fpu: Supporting XSAVE feature 0x12: 'Tile registers'
[    0.000000] x86/fpu: xstate_offset[2]:  576, xstate_sizes[2]:  832
[    0.000000] x86/fpu: xstate_offset[d]: 1408, xstate_sizes[d]:  512
[    0.000000] x86/fpu: xstate_offset[e]: 1920, xstate_sizes[e]:  512
[    0.000000] x86/fpu: xstate_offset[12]: 2432, xstate_sizes[12]: 1664
[    0.000000] x86/fpu: Enabled xstate features 0x13, context size is 4096 bytes, using 'compacted' format.
[    0.000000] signal: max sigframe size: 9216
[    0.000000] BIOS-provided physical RAM map:
[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009d7ff] usable
[    0.000000] BIOS-e820: [mem 0x000000000009d800-0x000000000009ffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000000e0000-0x00000000000fffff] reserved
...

You can filter the output using grep to search for specific keywords. For example, to search for messages related to USB devices, you can use the following command:

dmesg | grep USB

This will show you only the lines that contain the word "USB".

You can also use dmesg to check for error messages. For example, to search for messages containing the word "error", you can use the following command:

dmesg | grep error

By inspecting the dmesg output, you can gain valuable insights into your system's hardware and driver behavior.

Summary

In this lab, we explored how to check CPU features using the cat /proc/cpuinfo command. This command accesses the /proc/cpuinfo file, a virtual file within the /proc directory, to display detailed information about each CPU core, including its vendor, model name, clock speed, cache size, and supported features.

By examining the output of cat /proc/cpuinfo, specifically the flags field, we can identify the instruction sets and capabilities supported by the CPU, such as fpu, vme, de, sse2, and avx2. This allows us to determine if a specific CPU feature is present and enabled.