How does Linux kernel initialize each module skillfully

osc_ lc29nall 2021-01-21 12:35:06
linux kernel initialize module skillfully

I believe a lot of people are studying linux Students of kernel source code , It is often found that the initialization function of some modules cannot find the caller , For example, the initialization function of the following network module :

// net/ipv4/af_inet.c
static int __init inet_init(void)
* Set the IP module up
/* Setup TCP slab cache for open requests. */
/* Setup UDP memory threshold */

Even if you search through the entire kernel code , There's no place to call this function , How is this function called ?

The secret is in a line of code after this function :

fs_initcall( inet_init);

In this line of code ,fs_initcall Is a macro , The specific definitions are as follows :

// include/linux/init.h
#define ___define_initcall(fn, id, __sec) \
static initcall_t __initcall_##fn##id __used \
__attribute__((__p__(#__sec ".init"))) = fn;
#define __define_initcall(fn, id) ___define_initcall(fn, id, .initcall##id)
#define fs_initcall(fn) __define_initcall(fn, 5)

After the macro is expanded , The result of the macro call above , It looks like the following :

static initcall_t __initcall_inet_init5 __attribute__((__p__(".initcall5.init"))) = inet_init;

By the visible on ,fs_initcall The macro ultimately defines a static variable , The type of the variable is initcall_t, Value is the function address represented by the macro parameter .

initcall_t The type is defined as follows :

typedef int (*initcall_t)(void);

By the visible on ,initcall_t Is a function pointer type , It defines a variable that points to a function , The parameter of the function should be null , The return type should be int.

We can look at it again inet_init Method , This method does meet these requirements .

To sum up, we can see that ,fs_initcall A macro defines a variable  __initcall_inet_init5, Its type is initcall_t, Its value is inet_init Address of function .

Here I believe many students will think ,linux The kernel must call through this variable inet_init Functional , Am I right? ?

Yes , It's not right either .

Yes, because the kernel does get it through the memory that the variable points to inet_init Method and call the .

No, it's because the kernel doesn't go through the above __initcall_inet_init5 Variable to access the memory .

You don't need this variable , Is there any other way to access this memory ?

Certainly. , That's exactly what it is. linux The cleverness of kernel design .

Let's take a look at the macro above , Static variables __initcall_inet_init5 The definition of , There is some code in the definition as follows :


This part of the code does not belong to c Language standards , It is gcc Yes c Language extension , It is used to declare that the variable belongs to .initcall5.init This p.

So-called p, We can simply understand it as a layout and planning of the memory area occupied by the program , Like the ones that we see all the time p Yes  .text To store our code ,.data or .bss Used to store our variables .

Through these p The definition of , We can put the related functions in the program into the same memory area , So as to facilitate memory management .

Except for the default ones p outside , We can also go through gcc Of attribute From definition p, So we can put the related functions or variables in the same p It's in .

Like the one above __initcall_inet_init5 Variables belong to .initcall5.init This customization p.

These are defined in the p after , We can tell you in the link script linker, these p What is the location and layout in memory .

about x86 Platform , The link script for the kernel is :


In this script , Yes .initcall5.init Such as these p The relevant definitions are given , The specific logic is as follows :

// include/asm-generic/
#define INIT_CALLS_LEVEL(level) \
__initcall##level##_start = .; \
KEEP(*(.initcall##level##.init)) \
KEEP(*(.initcall##level##s.init)) \

#define INIT_CALLS \
__initcall_start = .; \
KEEP(*(.initcallearly.init)) \
__initcall_end = .;

By the visible on ,initcall dependent p There's a lot of , Let's look at the example above .initcall5.init It's just one of them , In addition to that .initcall0.init,.initcall1.init And so on these p.

these p It's all through macros INIT_CALLS_LEVEL To define its processing rules , identical level Of p Put in the same memory area , Different level Of p The memory area of the level The size is connected in turn .

For the top __initcall_inet_init5 In terms of variables , its p yes .initcall5.init, its level yes 5.

Suppose we have another method calling the macro fs_initcall, The static variable defined by this macro for this method belongs to p It's also .initcall5.init,level It's also 5.

Because the variable and __initcall_inet_init5 The variable belongs to initcall Of level All the same , So they're continuously placed in the same memory area .

in other words , these level by 5 The memory area occupied by static variables of is continuous , And because the types of these variables are initcall_t, So they just make up a type of initcall_t Array of , The starting address of the array is also in the INIT_CALLS_LEVEL Macro defines the , Namely __initcall5_start.

If we want to call these level by 5 Of initcall, Just get it first __initcall5_start Address , Think of it as element type initcall_t The starting address of the array of , Then traverse the elements in the array , Gets the function pointer corresponding to the element , You can call the corresponding function through the pointer .

Take a look at the code :

// init/main.c
extern initcall_entry_t __initcall_start[];
extern initcall_entry_t __initcall0_start[];
extern initcall_entry_t __initcall1_start[];
extern initcall_entry_t __initcall2_start[];
extern initcall_entry_t __initcall3_start[];
extern initcall_entry_t __initcall4_start[];
extern initcall_entry_t __initcall5_start[];
extern initcall_entry_t __initcall6_start[];
extern initcall_entry_t __initcall7_start[];
extern initcall_entry_t __initcall_end[];
static initcall_entry_t *initcall_levels[] __initdata = {
static void __init do_initcall_level(int level)
        initcall_entry_t *fn;
for (fn = initcall_levels[level]; fn < initcall_levels[level+1]; fn++)
static void __init do_initcalls(void)
int level;
for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++)

In the code above ,do_initcalls Method traverses all the legal fields level, For each level,do_initcall_level Method calls the level All functions in the .

Let's look at the example above inet_init The method belongs to level 5, It's also called here .

linux The kernel calls the initialization method of each module in this way , Isn't that clever .

Finally, let's summarize :

1. After the initialization method of each module , Generally, a call similar to fs_initcall(inet_init) The macro , The parameter of the macro is the method name of the initialization method of the module .

2. The result of the macro expansion is to define a static variable , The variable is passed through the gcc Of attribute To declare that it belongs to initcall level Of p, such as inet_init The static variable corresponding to the method belongs to the .initcall5.init This p.

3. stay linux In the script , adopt INIT_CALLS_LEVEL Macro inform linker, Will belong to the same level Put all the static variables in a continuous block of memory , Form an element of type initcall_t Array of , The starting address of the array is placed in a similar location __initcall5_start Variables in .

4. During the initialization of the kernel , By calling do_initcalls Method , Go through each level Function pointers in , Then call the pointer to the method. , The initialization method of each module .

The initialization method of each module is called in this way .

I hope you like it .


Related reading : Explain profound theories in simple language Linux The kernel module

More exciting , All in "Linux Code reading field ", Scan the QR code below for attention

本文为[osc_ lc29nall]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云