通知链描写叙述

大多数内核子系统都是相互独立的,因此某个子系统可能对其他子系统产生的事件感兴趣。

为了满足这个需求,也即是让某个子系统在发生某个事件时通知其他的子系统。Linux内核提供了通知链的机制。通知链表仅仅可以在内核的子系统之间使用,而不可以在内核与用户空间之间进行事件的通知。

    通知链表是一个函数链表,链表上的每个节点都注冊了一个函数。当某个事情发生时,链表上全部节点相应的函数就会被运行。所以对于通知链表来说有一个通知方与一个接收方。

在通知这个事件时所运行的函数由被通知方决定,实际上也即是被通知方注冊了某个函数,在发生某个事件时这些函数就得到运行。

通知链技术能够概括为:事件的被通知者将事件发生时应该运行的操作通过函数指针方式保存在链表(通知链)中,然后当事件发生时通知者依次运行链表中每个元素的回调函数完毕通知。

通知链实现

下面代码来自与linux-3.6.10

/kernel/notifier.c        通知链相关函数的实现

/include/linux/notifier.h 通知链结构、宏等定义

眼下代码实现由四种类型的通知链:

原子通知链(Atomic notifier chains):通知链元素回调函数执行在中断或者原子上下文。不同意堵塞

堵塞通知链(Blocking notifier chains):通知链元素回调函数执行在进程上下文,同意堵塞

原始通知链(Raw notifier chains):通知链元素回调函数执行上下文没有限制。全部锁和保护机制都由调用者维护。

SRCU通知链(SRCU notifier chains):一种堵塞通知链的变种,拥有和堵塞通知链相同的限制,与堵塞通知链的差别在于它使用可睡眠的RCU锁(Sleepable Read-Copy Update)而不是读写信号量来保护通知链。

也就意味着它运行回调函数(也即srcu_notifier_call_chain())的开销是非常小的,由于不须要缓存反弹和内存屏障。作为代价。srcu_notifier_chain_unregister()的开销是相当大的。

所以SRCU通知链适合用在常常通知notifier_blocks在通知链上非常少被去注冊的情况。

struct notifier_block {
int (*notifier_call)(struct notifier_block *, unsigned long, void *);
struct notifier_block __rcu *next;
int priority;
}; struct atomic_notifier_head {
spinlock_t lock;
struct notifier_block __rcu *head;
}; struct blocking_notifier_head {
struct rw_semaphore rwsem;
struct notifier_block __rcu *head;
}; struct raw_notifier_head {
struct notifier_block __rcu *head;
}; struct srcu_notifier_head {
struct mutex mutex;
struct srcu_struct srcu;
struct notifier_block __rcu *head;
};

各种通知链表头的定义

/* srcu_notifier_heads must be initialized and cleaned up dynamically */
extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
#define srcu_cleanup_notifier_head(name) \
cleanup_srcu_struct(&(name)->srcu); #define ATOMIC_NOTIFIER_INIT(name) { \
.lock = __SPIN_LOCK_UNLOCKED(name.lock), \
.head = NULL }
#define BLOCKING_NOTIFIER_INIT(name) { \
.rwsem = __RWSEM_INITIALIZER((name).rwsem), \
.head = NULL }
#define RAW_NOTIFIER_INIT(name) { \
.head = NULL }
/* srcu_notifier_heads cannot be initialized statically */ #define ATOMIC_NOTIFIER_HEAD(name) \
struct atomic_notifier_head name = \
ATOMIC_NOTIFIER_INIT(name)
#define BLOCKING_NOTIFIER_HEAD(name) \
struct blocking_notifier_head name = \
BLOCKING_NOTIFIER_INIT(name)
#define RAW_NOTIFIER_HEAD(name) \
struct raw_notifier_head name = \
RAW_NOTIFIER_INIT(name)

notifier_chain_register()

加入一个元素到通知链,从notifier_chain_register()实现来看,通知链时按优先级从大到小的有序链表。

另外notifier_chain_cond_register()与此不同的是查找是否有同样的通知链元素,假设有则直接返回。

/*
* Notifier chain core routines. The exported routines below
* are layered on top of these, with appropriate locking added.
*/ static int notifier_chain_register(struct notifier_block **nl,
struct notifier_block *n)
{
while ((*nl) != NULL) {
if (n->priority > (*nl)->priority)
break;
nl = &((*nl)->next);
}
n->next = *nl;
rcu_assign_pointer(*nl, n);
return 0;
}

notifier_chain_unregister()

从通知链删除一个元素

static int notifier_chain_unregister(struct notifier_block **nl,
struct notifier_block *n)
{
while ((*nl) != NULL) {
if ((*nl) == n) {
rcu_assign_pointer(*nl, n->next);
return 0;
}
nl = &((*nl)->next);
}
return -ENOENT;
}

notifier_call_chain()

通知已注冊模块相应的事件

/**
* notifier_call_chain - Informs the registered notifiers about an event.
* @nl: Pointer to head of the blocking notifier chain
* @val: Value passed unmodified to notifier function
* @v: Pointer passed unmodified to notifier function
* @nr_to_call: Number of notifier functions to be called. Don't care
* value of this parameter is -1.
* @nr_calls: Records the number of notifications sent. Don't care
* value of this field is NULL.
* @returns: notifier_call_chain returns the value returned by the
* last notifier function called.
*/
static int __kprobes notifier_call_chain(struct notifier_block **nl,
unsigned long val, void *v,
int nr_to_call, int *nr_calls)
{
int ret = NOTIFY_DONE;
struct notifier_block *nb, *next_nb; nb = rcu_dereference_raw(*nl); while (nb && nr_to_call) {
next_nb = rcu_dereference_raw(nb->next); #ifdef CONFIG_DEBUG_NOTIFIERS
if (unlikely(!func_ptr_is_kernel_text(nb->notifier_call))) {
WARN(1, "Invalid notifier called!");
nb = next_nb;
continue;
}
#endif
ret = nb->notifier_call(nb, val, v); if (nr_calls)
(*nr_calls)++; if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
break;
nb = next_nb;
nr_to_call--;
}
return ret;
}

下面函数均是对上面三个函数的封装,仅仅只是同步保护机制不同

atomic_notifier_chain_register()

atomic_notifier_chain_unregister()

atomic_notifier_call_chain()

blocking_notifier_chain_register()

blocking_notifier_chain_cond_register()

blocking_notifier_chain_unregister()

blocking_notifier_call_chain()

raw_notifier_chain_register()

raw_notifier_chain_unregister()

raw_notifier_call_chain()

srcu_notifier_chain_register()

srcu_notifier_chain_unregister()

srcu_notifier_call_chain()

Linux内核通知链模块的更多相关文章

  1. Linux 内核通知链随笔【中】

    关于内核通知链不像Netlink那样,既可以用于内核与用户空间的通信,还能用于内核不同子系统之间的通信,通知链只能用于内核不同子系统之间的通信.那么内核通知链到底是怎么工作的?我们如何才能用好通知链? ...

  2. Linux 内核通知链随笔【中】【转】

    转自:http://blog.chinaunix.net/uid-23069658-id-4364171.html 关于内核通知链不像Netlink那样,既可以用于内核与用户空间的通信,还能用于内核不 ...

  3. [Linux] 内核通知链 notifier

    Linux 内核中每个模块之间都是独立的,如果模块需要感知其他模块的事件,就需要用到内核通知链. 最典型的通知链应用就是 LCD 和 TP 之间,TP 需要根据 LCD 的亮灭来控制是否打开关闭触摸功 ...

  4. Linux 内核通知链机制的原理及实现

    一.概念: 大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣.为了满足这个需求,也即是让某个子系统在发生某个事件时通知其它的子 系统,Linux内核提供了通知链的机制.通 ...

  5. Linux内核通知链机制的原理及实现【转】

    转自:http://www.cnblogs.com/armlinux/archive/2011/11/11/2396781.html 一.概念: 大多数内核子系统都是相互独立的,因此某个子系统可能对其 ...

  6. Linux内核调试方法总结之内核通知链

    Linux内核通知链notifier 1.内核通知链表简介(引用网络资料)    大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣.为了满足这个需求,也即是让某个子系统在 ...

  7. 从基本理解到深入探究 Linux kernel 通知链(notifier chain)【转】

    转自:https://blog.csdn.net/u014134180/article/details/86563754 版权声明:本文为博主原创文章,未经博主允许不得转载.——Wu_Being ht ...

  8. Linux内核通知链分析【转】

    转自:http://www.cnblogs.com/jason-lu/articles/2807758.html Linux内核通知链分析 1. 引言 Linux是单内核架构(monolithic k ...

  9. 解析 Linux 内核可装载模块的版本检查机制

    转自:http://www.ibm.com/developerworks/cn/linux/l-cn-kernelmodules/ 为保持 Linux 内核的稳定与可持续发展,内核在发展过程中引进了可 ...

随机推荐

  1. I/O 多路复用之select、poll、epoll详解

    select,poll,epoll都是IO多路复用的机制.I/O多路复用就是通过一种机制,一个进程可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作.但s ...

  2. codevs 1226 倒水问题

    1226 倒水问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold   题目描述 Description 有两个无刻度标志的水壶,分别可装 x 升和 y 升 ( x, ...

  3. Codeforces 798D

    这两天后缀数组整多了整点有意思的,随机算法. 题意给你两个数组,让你求一个排列使得这个排列对应的两个数组前n/2+1个数之和的二倍大于每个序列总和. 下面先贴下这题正解 二维贪心,按a从大到小排,把第 ...

  4. BZOJ 4036: [HAOI2015]按位或 集合幂函数 莫比乌斯变换 莫比乌斯反演

    http://www.lydsy.com/JudgeOnline/problem.php?id=4036 http://blog.csdn.net/lych_cys/article/details/5 ...

  5. JZYZOJ1622 [usaco2009]工作安排 贪心

    和p1123智力大冲浪一样,可以用优先队列写...   每一秒可以做一个工作....因为n个任务只要在限制之前完成就行,所以时间不冲突的话肯定越早做完越好..所以最多的时间是n,当然限定的完成时间中最 ...

  6. bzoj 2938

    收获: 1.AC自动机可以在建立fail时将一些不存在的儿子指针指向对应的位置. 2.判断环时不要想当然地写个这样的版本: bool dfs( int u ) { if( vis[u] ) retur ...

  7. [转]Android x86模拟器Intel Atom x86 System Image配置与使用方法

    Android x86模拟器Intel Atom x86 System Image配置与使用方法 前言: 大家现在开发使用的Android 模拟器模拟的是 ARM 的体系结构(arm-eabi),因此 ...

  8. bzoj 3224 普通平衡树 vactor的妙用

    3224: Tyvj 1728 普通平衡树 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/ ...

  9. luoguoj 1598 垂直柱状图 模拟

    P1598 垂直柱状图 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.luogu.org/problem/show?pid=1598 ...

  10. PAT甲级1049. Counting Ones

    PAT甲级1049. Counting Ones 题意: 任务很简单:给定任何正整数N,你应该计算从1到N的整数的十进制形式的1的总数.例如,给定N为12,在1,10, 11和12. 思路: < ...