linux內核通知鏈原理及應用

一、linux通知鏈介紹

         內核的很多子系統(例如:進程調度、內存管理、虛擬文件系統、路由子系統等)之間具有很強的相互依賴性,因此,其中一個子系統偵測到的或者產生的事件,其他子系統可能都有興趣,爲了完成這種交互需求,linux使用了所謂的通知鏈(notification chain)

        通知鏈表是一個函數鏈表,鏈表上的每一個節點都註冊了一個函數。當某個事情發生時,鏈表上所有節點對應的函數就會被執行。所以對於通知鏈表來說有一個通知方與一個接收方。在通知這個事件時所運行的函數由被通知方決定,實際上也即是被通知方註冊了某個函數,在發生某個事件時這些函數就得到執行。其實和系統調用signal的思想差不多。

注意:通知鏈只在內核子系統之間使用,內核和用戶空間之間的通知信息則是依賴其他機制,例如ioctl。

二、linux內核通知鏈的基本數據結構

       通知鏈列表元素的類型是notifier_block,其定義如下:

 struct notifier_block {
    int (*notifier_call)(struct notifier_block *, unsigned long, void *);
    struct notifier_block __rcu *next;
    int priority;
};

Notifier_call是要執行的函數,next用於鏈接列表的元素,而priority代表的是該函數的優先級,較高優先級的函數會先被執行,但是在實際中,註冊時幾乎都不理會notifier_block定義中的priority,意味着獲取其默認值0,因此,執行的次序僅依賴於註冊次序。

通知鏈有四種類型:

1、  原子通知鏈( Atomicnotifier chains ):通知鏈元素的回調函數(當事件發生時要執行的函數)只能在中斷上下文中運行,不允許阻塞。對應的鏈表頭結構:

structatomic_notifier_head
{
    spinlock_t lock;
    struct notifier_block *head;
};

2、  可阻塞通知鏈(Blocking notifier chains ):通知鏈元素的回調函數在進程上下文中運行,允許阻塞。對應的鏈表頭:

structblocking_notifier_head
{
    struct rw_semaphore rwsem;
    struct notifier_block *head;
};

3、  原始通知鏈( Rawnotifier chains ):對通知鏈元素的回調函數沒有任何限制,所有鎖和保護機制都由調用者維護。對應的鏈表頭:

structraw_notifier_head
{
   struct notifier_block *head;
};

4、  SRCU 通知鏈( SRCU notifier chains ):可阻塞通知鏈的一種變體。對應的鏈表頭:

structsrcu_notifier_head
{
    struct mutex mutex;
    struct srcu_struct srcu;
    struct notifier_block *head;
};

三、linux內核通知鏈註冊、執行相關的函數

通知鏈的運作機制包括兩個角色:

 

被通知者:對某一事件感興趣一方。定義了當事件發生時,相應的處理函數,即回調函數。但需要事先將其註冊到通知鏈中(被通知者註冊的動作就是在通知鏈中增加一項)。


通知者:事件的通知者。當檢測到某事件,或者本身產生事件時,通知所有對該事件感興趣的一方事件發生。他定義了一個通知鏈,其中保存了每一個被通知者對事件的處理函數(回調函數)。通知這個過程實際上就是遍歷通知鏈中的每一項,然後調用相應的事件處理函數。

 

包括以下過程:

通知者定義通知鏈。

被通知者向通知鏈中註冊回調函數。

當事件發生時,通知者發出通知(執行通知鏈中所有元素的回調函數)。


1、通知鏈註冊函數

被通知者調用 notifier_chain_register 函數註冊回調函數,該函數按照優先級將回調函數加入到通知鏈中:

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;
}

2、註銷回調函數

註銷回調函數則使用 notifier_chain_unregister 函數,即將回調函數從通知鏈中刪除

staticint 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;
}

3、通用通知函數

通知者調用 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)
{
    int ret = NOTIFY_DONE;
    struct notifier_block *nb, *next_nb;
    
    nb = rcu_dereference(*nl);
    
    while (nb && nr_to_call) 
    {
        next_nb = rcu_dereference(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;
}

參數nl是通知鏈的頭部,val表示事件類型,v用來指向通知鏈上的函數執行時需要用到的參數,一般不同的通知鏈,參數類型也不一樣,例如當通知一個網卡被註冊時,v就指向net_device結構,nr_to_call表示準備最多通知幾個,-1表示整條鏈都通知,nr_calls非空的話,返回通知了多少個。

 

每個被執行的notifier_block回調函數的返回值可能取值爲以下幾個:

NOTIFY_DONE:表示對相關的事件類型不關心。

NOTIFY_OK:順利執行。

NOTIFY_BAD:執行有錯。

NOTIFY_STOP:停止執行後面的回調函數。

NOTIFY_STOP_MASK:停止執行的掩碼。

Notifier_call_chain()把最後一個被調用的回調函數的返回值作爲它的返回值。


四、通知鏈的應用舉例

在這裏寫一個簡單的通知鏈的代碼,通知鏈的編寫分爲兩個過稱:

1、  首先定義自己的通知鏈的頭結點,並將執行的函數註冊到自己的通知鏈中。

2、  其次則是由另外的子系統來通知這個鏈,讓其上面註冊的函數運行。


代碼chain_init.c,它的作用是自定義一個通知鏈表subsystem_chain,然後再自定義兩個函數分別向這個通知鏈表加入或者刪除節點,最後再定義函數通知這個subsystem_chain鏈。

#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/notifier.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/module.h>
#include <asm/uaccess.h>
#include <linux/types.h>

/*
 * * 定義自己的通知鏈頭結點以及註冊和卸載通知鏈的外包函數
 * */

/*
 * * RAW_NOTIFIER_HEAD是定義一個通知鏈的頭部結點,
 * * 通過這個頭部結點可以找到這個鏈中的其它所有的notifier_block
 * */
static RAW_NOTIFIER_HEAD(subsystem_chain);

/*
 * * 自定義的註冊函數,將notifier_block節點加到剛剛定義的subsystem_chain這個鏈表中來
 * * raw_notifier_chain_register會調用notifier_chain_register
 * */
int register_subsystem_notifier(struct notifier_block *nb)
{
  return raw_notifier_chain_register(&subsystem_chain, nb);
}
EXPORT_SYMBOL(register_subsystem_notifier);

int unregister_subsystem_notifier(struct notifier_block *nb)
{
  return raw_notifier_chain_unregister(&subsystem_chain, nb);
}
EXPORT_SYMBOL(unregister_subsystem_notifier);

/*
 * * 自定義的通知鏈表的函數,即通知subsystem_chain指向的鏈表中的所有節點執行相應的函數
 * */
int call_subsystem_notifiers(unsigned long val, void *v)
{
  return raw_notifier_call_chain(&subsystem_chain, val, v);
}
EXPORT_SYMBOL(call_subsystem_notifiers);

/*
 * * init and exit 
 * */
static int __init init_notifier(void)
{
  printk("init_notifier\n");
  return 0;
}

static void __exit exit_notifier(void)
{
    printk("exit_notifier\n");
}

module_init(init_notifier);
module_exit(exit_notifier);
MODULE_LICENSE("GPL");


代碼chain_server.c,該代碼的作用是將subsystem_notifier節點加到之前定義的subsystem_chain鏈上。

#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/notifier.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/module.h>
#include <asm/uaccess.h>
#include <linux/types.h>

#define SUBSYSTEM_CHAIN_INIT 0x90U  

/*
 * * 註冊通知鏈
 * */
extern int register_subsystem_notifier(struct notifier_block*);
extern int unregister_subsystem_notifier(struct notifier_block*);

static int subsystem_event(struct notifier_block *nb, unsigned long event,void *v)  
{  
  switch(event)  
  {  
        case SUBSYSTEM_CHAIN_INIT:  
                printk(KERN_INFO"=====>Get subsystem chain event SUBSYSTEM_CHAIN_INIT\n");  
                break;  
        default:  
                printk(KERN_ERR"Unknown event num %ld\n", event);  
  }  
  return NOTIFY_DONE;  
}  

/*
 * * 事件,該節點執行的函數爲subsystem_event
 * */
static struct notifier_block subsystem_notifier  =
{
    .notifier_call =subsystem_event,
};

/*
 * * 對這些事件進行註冊
 * */
static int __init chain_server_register(void)
{
  int err;
  printk("Begin to register:\n");
  
  err = register_subsystem_notifier(&subsystem_notifier);
  if (err)
  {
    printk("register subsystem_notifier error\n");
    return -1; 
  }
  printk("register subsystem_notifier completed\n");

  return err;
}

/*
 * * 卸載剛剛註冊了的通知鏈
 * */
static void __exit chain_server_unregister(void)
{
  printk("Begin to unregister\n");
  unregister_subsystem_notifier(&subsystem_notifier);
  printk("Unregister finished\n");
}

module_init(chain_server_register);
module_exit(chain_server_unregister);
MODULE_LICENSE("GPL");

代碼chain_client.c,該代碼的作用就是向subsystem_chain通知鏈中發送消息,讓鏈中的函數運行。

#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/notifier.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/module.h>
#include <asm/uaccess.h>
#include <linux/types.h>

#define SUBSYSTEM_CHAIN_INIT 0x90U  

extern int call_subsystem_notifiers(unsigned long val, void *v);

/*
 * * 向通知鏈發送消息以觸發註冊了的函數
 * */
static int __init chain_client_register(void)
{
  int err;
  printk("Begin to notify:\n");

  /*
 *   * 調用自定義的函數,向subsystem_chain鏈發送消息
 *     */
 // printk(" chain client init (subsystem client)\n");  
  err=call_subsystem_notifiers(SUBSYSTEM_CHAIN_INIT, NULL);  
  if (err)
          printk("notifier_call_chain error\n");
  return err;
}

static void __exit chain_client_unregister(void)
{
    printk("End notify\n");
}

module_init(chain_client_register);
module_exit(chain_client_unregister);
MODULE_LICENSE("GPL");

Makefile文件

obj-m:=chain_init.o chain_client.o chain_server.o

KERNEL:=/lib/modules/`uname -r`/build

all:
	make -C $(KERNEL) M=`pwd` modules
install:
	make -C $(KERNEL) M=`pwd` modules_install
clean:
	make -C $(KERNEL) M=`pwd` clean
	rm -rf Module.*


編譯並加載模塊

make

insmod chain_init.c

insmod chain_server.c

insmod chain_client.c


Dmesg查看內核打印




 




發佈了36 篇原創文章 · 獲贊 19 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章