Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it a convention to use static functions in a LKM

I've been researching this a lot lately and have looked into various articles and stackoverflow posts but I can't seem to find a straight answer. When creating a kernel module I have seen most code look like this:

#include <linux/init.h>
static int test_init(void) {return 0;}
static void test_exit(void) {;}
module_init(test_init);
module_exit(test_exit);
  • One possible reason I have found is that doing this increases the difficulty of injecting malicious code into a running module.

  • Another is less cluttering of the namespace but wouldn't that only be an issue in the context of the kernel module you are linking and compiling and nothing else? If insmod actually links the code into the kernel like ld would then I can see how name clashes would mess up the system. Is this the reason?

I cannot think of any other reasons and I would like this to be clarified before I blindly start using conventions.

Thank you in advance

like image 305
edaniels Avatar asked Feb 03 '26 17:02

edaniels


1 Answers

If a function isn't needed outside of a .c file, it should be declared static within that .c file.

That's just good encapsulation.

It avoids name collisions and lets the reader know your intent.

like image 85
Charlie Burns Avatar answered Feb 06 '26 08:02

Charlie Burns