kernel-moduleinsmod

insmod not throwing error for a positive return


I am writing my first kernel module and it is a simple Hello World Kernel Module. The tldp guide I am following said that insmod would not load a module if the init_module function returns a non-zero value. It is working as expected when returning some negative number but while experimenting I noticed that insmod is loading my module even when the return value is positive.

Please explain why?

For example if I return -185, insmod is straight away saying that it cannot load the module.

But when i return 185, it is informing about a suspicious return but still is loading the module. This is the log for "return 185".

[19398.947857] do_init_module: 'hello_1'->init suspiciously returned 185, it should follow 0/-E convention
do_init_module: loading module anyway...
[19398.947859] CPU: 0 PID: 11812 Comm: insmod Tainted: P           OE  3.19.0-15-generic #15-Ubuntu
[19398.947860] Hardware name: innotek GmbH VirtualBox/VirtualBox, BIOS VirtualBox 12/01/2006
[19398.947861]  ffffffffc06c3000 ffff880059347d98 ffffffff817c2205 0000000000000007
[19398.947862]  ffffffffc06c3018 ffff880059347ee8 ffffffff810f9a2d ffffffff810f51d0
[19398.947864]  ffff8800db64ad10 ffff880059347e40 ffffffffc06c3018 ffffffffc0391000
[19398.947865] Call Trace:
[19398.947869]  [<ffffffff817c2205>] dump_stack+0x45/0x57
[19398.947872]  [<ffffffff810f9a2d>] load_module+0x160d/0x1ce0
[19398.947873]  [<ffffffff810f51d0>] ? store_uevent+0x40/0x40
[19398.947875]  [<ffffffff810fa276>] SyS_finit_module+0x86/0xb0
[19398.947877]  [<ffffffff817c934d>] system_call_fastpath+0x16/0x1b

And this gets printed on console for "return -185"

insmod: ERROR: could not insert module hello-1.ko: Unknown error 185

Solution

  • init_module function should return either 0 or negative error code. You can treat returning positive value as leading to undefined behaviour.

    Current kernel interprets positive value as success, but prints warning into system log. This log can be read using dmesg.