'Cannot insert loadable kernel module in ubuntu 20.04.4

My system information:

$ uname --all
Linux awadh 5.13.0-39-generic #44~20.04.1-Ubuntu SMP Thu Mar 24 16:43:35 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Installed kernel headers with:

$ sudo apt install linux-headers-$(uname -r)

I am trying a simple loadable kernel module. During insertion I get the following output:

$ sudo insmod test.ko 
insmod: ERROR: could not insert module test.ko: Invalid module format

The last entry of dmesg after trying above command:

$ dmesg
[754999.208054] module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 1, loc 00000000995b51d7, val ffffffffc13af000

Code:

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>

static int __init hello_init(void){
    printk(KERN_ALERT "Hello, world\n");
    return 0;
}

static void __exit hello_exit(void){
    printk(KERN_ALERT "Goodbye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("hexagun");
MODULE_DESCRIPTION("Hello world");

Makefile:

obj-m := test.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Any insights on how to resolve the issue?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source