内核通知链机制的原理及实现
来源:互联网 发布:淘宝内部优惠券去哪领 编辑:程序博客网 时间:2024/05/03 12:27
转自:http://blog.sina.com.cn/s/blog_6683e49d0100tovr.html
一、概念:
二、数据结构:
通知链有四种类型:
- 原子通知链( Atomic notifier chains ):通知链元素的回调函数(当事件发生时要执行的函数)只能在中断上下文中运行,不允许阻塞。对应的链表头结构:
struct atomic_notifier_head
{
};
-
- 可阻塞通知链( Blocking notifier chains ):通知链元素的回调函数在进程上下文中运行,允许阻塞。对应的链表头:
struct blocking_notifier_head
{
};
-
- 原始通知链( Raw notifier chains ):对通知链元素的回调函数没有任何限制,所有锁和保护机制都由调用者维护。对应的链表头:
struct raw_notifier_head
{
};
-
- SRCU 通知链( SRCU notifier chains ):可阻塞通知链的一种变体。对应的链表头:
struct srcu_notifier_head
{
};
通知链的核心结构:
struct notifier_block
{
};
其中notifier_call是通知链要执行的函数指针,next用来连接其它的通知结构,priority是这个通知的优先级,同一条链上的notifier_block{}是按优先级排列的。内核代码中一般把通知链命名为xxx_chain, xxx_nofitier_chain这种形式的变量名。
三、运作机制:
通知链的运作机制包括两个角色:
- 被通知者:对某一事件感兴趣一方。定义了当事件发生时,相应的处理函数,即回调函数。但需要事先将其注册到通知链中(被通知者注册的动作就是在通知链中增加一项)。
- 通知者:事件的通知者。当检测到某事件,或者本身产生事件时,通知所有对该事件感兴趣的一方事件发生。他定义了一个通知链,其中保存了每一个被通知者对事件的处理函数(回调函数)。通知这个过程实际上就是遍历通知链中的每一项,然后调用相应的事件处理函数。
包括以下过程:
- 通知者定义通知链。
- 被通知者向通知链中注册回调函数。
- 当事件发生时,通知者发出通知(执行通知链中所有元素的回调函数)。
被通知者调用 notifier_chain_register 函数注册回调函数,该函数按照优先级将回调函数加入到通知链中:
static int notifier_chain_register(struct notifier_block **nl, struct notifier_block *n)
{
}
注销回调函数则使用 notifier_chain_unregister 函数,即将回调函数从通知链中删除:
static int notifier_chain_unregister(struct notifier_block **nl, struct notifier_block *n)
{
}
通知者调用 notifier_call_chain 函数通知事件的到达,这个函数会遍历通知链中所有的元素,然后依次调用每一个的回调函数(即完成通知动作):
static int __kprobes notifier_call_chain(struct notifier_block **nl, unsigned long val, void *v, int nr_to_call, int *nr_calls)
{
#ifdef CONFIG_DEBUG_NOTIFIERS
#endif
}
- NOTIFY_DONE:表示对相关的事件类型不关心。
- NOTIFY_OK:顺利执行。
- NOTIFY_BAD:执行有错。
- NOTIFY_STOP:停止执行后面的回调函数。
- NOTIFY_STOP_MASK:停止执行的掩码。
四、举例应用:
在这里,写了一个简单的通知链表的代码。实际上,整个通知链的编写也就两个过程:
- 首先是定义自己的通知链的头节点,并将要执行的函数注册到自己的通知链中。
- 其次则是由另外的子系统来通知这个链,让其上面注册的函数运行。
代码1 buildchain.c。它的作用是自定义一个通知链表test_chain,然后再自定义两个函数分别向这个通知链中加入或删除节点,最后再定义一个函数通知这个test_chain链:
#include <asm/uaccess.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/notifier.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
static RAW_NOTIFIER_HEAD(test_chain);
int register_test_notifier(struct notifier_block *nb)
{
}
EXPORT_SYMBOL(register_test_notifier);
int unregister_test_notifier(struct notifier_block *nb)
{
}
EXPORT_SYMBOL(unregister_test_notifier);
int test_notifier_call_chain(unsigned long val, void *v)
{
}
EXPORT_SYMBOL(test_notifier_call_chain);
static int __init init_notifier(void)
{
}
static void __exit exit_notifier(void)
{
}
module_init(init_notifier);
module_exit(exit_notifier);
代码2 regchain.c。该代码的作用是将test_notifier1 test_notifier2 test_notifier3这三个节点加到之前定义的test_chain这个通知链表上,同时每个节点都注册了一个函数:
#include <asm/uaccess.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/notifier.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
extern int register_test_notifier(struct notifier_block*);
extern int unregister_test_notifier(struct notifier_block*);
static int test_event1(struct notifier_block *this, unsigned long event, void *ptr)
{
}
static int test_event2(struct notifier_block *this, unsigned long event, void *ptr)
{
}
static int test_event3(struct notifier_block *this, unsigned long event, void *ptr)
{
}
static struct notifier_block test_notifier1 =
{
};
static struct notifier_block test_notifier2 =
{
};
static struct notifier_block test_notifier3 =
{
};
static int __init reg_notifier(void)
{
}
static void __exit unreg_notifier(void)
{
}
module_init(reg_notifier);
module_exit(unreg_notifier);
代码3 notify.c。该代码的作用就是向test_chain通知链中发送消息,让链中的函数运行:
#include <asm/uaccess.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/notifier.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
extern int test_notifier_call_chain(unsigned long val, void *v);
static int __init call_notifier(void)
{
}
static void __exit uncall_notifier(void)
{
}
module_init(call_notifier);
module_exit(uncall_notifier);
Makefile文件:
obj-m:=buildchain.o regchain.o notify.o
CURRENT_PATH := $(shell pwd)
LINUX_KERNEL := $(shell uname -r)
KERNELDIR := /usr/src/linux-headers-$(LINUX_KERNEL)
all:
make -C $(KERNELDIR) M=$(CURRENT_PATH) modules
clean:
make -C $(KERNELDIR) M=$(CURRENT_PATH) clean
运行(注意insmod要root权限):
make
insmod buildchain.ko
insmod regchain.ko
insmod notify.ko
这样就可以看到通知链运行的效果了:
init_notifier
Begin to register:
register test_notifier1 completed
register test_notifier2 completed
register test_notifier3 completed
Begin to notify:
==============================
In Event 1: Event Number is 1
In Event 2: Event Number is 1
In Event 3: Event Number is 1
==============================
附:
以下是一些宏来初始化各种类型的通知头结构, 一般在程序中使用:
#define ATOMIC_INIT_NOTIFIER_HEAD(name) do {
#define BLOCKING_INIT_NOTIFIER_HEAD(name) do {
#define RAW_INIT_NOTIFIER_HEAD(name) do {
以下这些宏也是用来初始化各种类型的通知头结构, 但是在参数定义时使用(即作为赋值的右半部分,作为等号右边的部分):
#define ATOMIC_NOTIFIER_INIT(name) {
#define BLOCKING_NOTIFIER_INIT(name) {
#define RAW_NOTIFIER_INIT(name)
注意, 没有定义scru通知头结构的初始化, 因为scru是不能静态初始化的.
以下这些宏用来直接定义通知头结构:
#define ATOMIC_NOTIFIER_HEAD(name)
#define BLOCKING_NOTIFIER_HEAD(name)
#define RAW_NOTIFIER_HEAD(name)
4. 扩展的通知块操作
扩展的通知块操作功能和基本通知块类似, 但使用了扩展的结构中的参数保证操作的安全
4.1 原子通知块
4.1.1 登记
// 只是在基本通知登记操作前后加锁解锁进行保护
int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
{
// 加锁
// 解锁
}
EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
4.1.2 撤销
// 只是在基本通知块撤销操作前后加锁解锁进行保护
int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
{
// 加锁
// 解锁
// 同步rcu, 等待一个grace period
}
EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
4.1.3 原子回调
这个函数是在原子操作上下文中调用, 是不能阻塞的
int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh,
{
// 禁止了抢占
// 使用基本通知块回调
// 允许抢占
}
EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
4.2 可阻塞通知块
4.2.1 登记
int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
{
// 这是内核启动时就进行调用了, 虽然可能性很小, 直接执行基本登记函数
// 不用处理信号灯, 因为此时是不能阻塞
// 使用信号灯进行同步, 可能阻塞
// 基本登记函数
}
EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
4.2.2 撤销
该函数是在进程处理过程中调用,可阻塞:
int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
{
// 这是内核启动时就进行调用了, 虽然可能性很小, 直接执行基本撤销函数
// 不用处理信号灯, 因为此时是不能阻塞
// 使用信号灯进行同步, 可能阻塞
// 基本撤销函数
}
EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
4.2.3 回调
在进行上下文中调用, 可以阻塞:
int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
{
// 信号灯同步
// 进行基本回调处理
}
EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
4.3 原始通知块操作
和基本原始块操作完全相同:
int raw_notifier_chain_register(struct raw_notifier_head *nh,
{
}
EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
{
}
EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
int raw_notifier_call_chain(struct raw_notifier_head *nh,
{
}
EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
4.4 SRCU通知块操作
4.4.1 登记
必须在进程的上下文中调用, 和blocking通知类似
int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
{
}
EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
4.4.2 撤销
必须在进程的上下文中调用, 和blocking通知类似
int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
{
}
EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
4.4.3 回调
在进程的上下文中调用, 可以阻塞:
int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
{
// 使用srcu读锁来加锁
}
EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
4.4.4 初始化
因为SRCU通知不能通过宏来初始化, 必须要专门定义一个初始化函数来初始化srcu的通知块参数:
void srcu_init_notifier_head(struct srcu_notifier_head *nh)
{
// 初始化锁
// 初始化scru结构
}
EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
- 内核通知链机制的原理及实现
- Linux内核通知链机制的原理及实现
- 内核通知链机制的原理及实现
- 内核通知链机制的原理及实现(转载)
- 内核通知链机制的原理及实现
- 内核通知链机制的原理及实现
- 内核通知链机制的原理及实现
- 内核通知链机制的原理及实现
- Linux内核通知链机制的原理及实现
- Linux内核通知链机制的原理及实现
- 内核通知链机制的原理及实现
- 内核通知链机制的原理及实现
- Linux内核通知链机制的原理及实现
- Linux内核通知链机制的原理及实现
- Linux内核通知链机制的原理及实现
- Linux内核通知链机制的原理及实现
- 内核通知链原理及机制
- 内核通知链原理及机制
- Burnside引理和polay计数学习小记
- 完全卸载sqlserver
- JAX-RS入门 四: 注入
- OSI模型和TCP/IP模型
- 氪周刊:关注互联网创业
- 内核通知链机制的原理及实现
- 括号匹配问题
- 清华大学信息化技术中心培训部技术分享-字符型数据
- update fabric from 1.3 to 1.8
- ASP.NET获取客户端操作系统类型和IP地址
- JAX-RS入门 五: 自动类型转换
- 汉诺塔
- 案例分析---从“黑掉Github”学Web安全开发
- AX-RS入门 六: 数据处理(1)