12

I have been trying to port few linux drivers and realized that there is substantial difference between kernel version 2.4 and 2.6 of linux.

In the 2.4 version of kernel, the module programming was as below -

#define MODULE
#include <linux/module.h>
#include <linux/kernel.h>

int init_module(void)      
{ 
printk(KERN_INFO "Hi \n"); 
return 0; 
}

void cleanup_module(void)  
{ 
printk(KERN_INFO "Bye \n"); 
}

But, with the 2.6 version of kernel, the following has to be done for modules -

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

static int hi_init(void)
{
    printk(KERN_ALERT "Hi \n");
    return 0;
}

static void hi_exit(void)
{
    printk(KERN_ALERT "Bye \n");
}

module_init(hi_init);
module_exit(hi_exit);

What is the advantage of such changes in Kernel 2.6 and Why was that change required in kernel 2.6 of linux ?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Karthik Balaguru
  • 7,424
  • 7
  • 48
  • 65
  • The 2.4 declarations imply you could never build the module into the system (as the declarations are not static). – stsquad Jun 13 '12 at 07:55
  • 1
    Here is a very nice explanation to module_init: http://stackoverflow.com/questions/18605653/linux-module-init-vs-core-initcall-vs-early-initcall – user2311046 Jan 06 '14 at 14:45

3 Answers3

8

If you look at the definition of the new functions:

/* Each module must use one module_init(). */
#define module_init(initfn)                 \
static inline initcall_t __inittest(void)       \
{ return initfn; }                  \
int init_module(void) __attribute__((alias(#initfn)));

/* This is only required if you want to be unloadable. */
#define module_exit(exitfn)                 \
static inline exitcall_t __exittest(void)       \
{ return exitfn; }                  \
void cleanup_module(void) __attribute__((alias(#exitfn)));

You'll see it ensures that the right boilerplate is included so these special functions can be correctly treated by the compiler. It's what the internal API of Linux does, it evolves if there are better ways of solving the problem.

chriptus13
  • 705
  • 7
  • 20
stsquad
  • 5,712
  • 3
  • 36
  • 54
  • Yes, but how is this a better way? The directives are just to make the user provided function name to be made as an alias for te init_module() function – Chethan Jun 11 '12 at 09:52
  • Readability, consistency of API, enforcing convention. Generally you want to make the boiler plate bits of API conformance hard to screw up and leave the developer to concentrate on the real problems they need to solve. – stsquad Jun 13 '12 at 07:54
5

What is the advantage of [module_init] in Kernel 2.6

module_init also exited in 2.4, mind you.

It adds the necessary boilerplate to initialize the module and run the entry function when the module file is compiled into the kernel rather than as a module.

user502515
  • 4,346
  • 24
  • 20
1

One advantage is readability. cdrom_init() instantly tells you that it's the init() call for the cdrom driver.

Bandan
  • 596
  • 2
  • 4