Rate This Document
Findability
Accuracy
Completeness
Readability

Linux Kernel-Mode CRC32 Algorithm Optimization

The CRC32 algorithm is used to verify data accuracy. For example, in network communication, when a network packet is received, the CRC32 algorithm is used to calculate its CRC32 value and compare the value with the received CRC value to determine the data integrity. The Linux kernel contains a set of CRC32 verification code implemented by the C language, but the execution efficiency is low. When a large amount of network throughput occurs in kernel mode, CRC32 verification may become a performance bottleneck.

In addition, in the kernel of a later version (Kernel 5.x), the CRC32 verification mode through the CRC instruction has been implemented. This mode is more efficient and can greatly reduce the time consumed by the CRC32 algorithm. The following describes how to integrate the implementation into kernel 4.x.

  1. Copy the crc32.S file (https://github.com/torvalds/linux/blob/master/arch/arm64/lib/crc32.S) to the linux-4.14.105/arch/arm64/lib/ directory.

    Take kernel 4.14.105 as an example. Copy the compilation implementation of CRC32 to the linux-4.14.105/arch/arm64/lib/ directory.

    1
    cp crc32.S linux-4.14.105/arch/arm64/lib/
    
  2. Modify Makefile.

    Add a line at the end of linux-4.14.105/arch/arm64/lib/Makefile to ensure that the crc32.S file can be compiled to the kernel.

    1
    obj-$(CONFIG_CRC32) += crc32.o
    

  3. Modify the lib/crc32.c source code.

    Add the __weak flag to the CRC32 verification interface of the C code. When the flag specifies the crc32_le interface, the C interface is not called at the first priority. In this case, if the interface has assembly implementation, the assembly implementation is preferentially called. At the same time, the flag points to the interface implemented in the assembly.

    Figure 1 Before
    Figure 2 After
  4. Recompile the kernel.