Linux 內核提供一套雙向鏈表的實現,你可以在 include/linux/list.h 中找到。我們以雙向鏈表著手開始介紹 Linux 內核中的數據結構 ,因為這個是在 Linux 內核中使用最為廣泛的數據結構。
首先讓我們看一下主要的結構體:
C
struct list_head { struct list_head *next, *prev;};
1
2
3
struct list_head {
struct list_head *next, *prev;
};
你可以看到其與常見的結構體實現有顯著不同,比如 glib 中所使用到的雙向鏈表實現。
C
struct GList { gpointer data; GList *next; GList *prev;};
1
2
3
4
5
struct GList {
gpointer data;
GList *next;
GList *prev;
};
通常來說,鏈表結構體要包括一個指向數據的指針,不過 Linux 內核的鏈表卻不包含此實現。那么首要的疑問:鏈表是用什么方式存儲數據的?。Linux 內核所實現的是一種被稱為侵入式的鏈表(Intrusive list),這種鏈表并不在鏈表結構中包含數據,而僅提供用于維護前向與后向訪問結構的指針。這種實現方式使得鏈表數據結構非常通用,因為它并不需要關注鏈表所維護的具體數據類型。
比如:
C
struct nmi_desc { spinlock_t lock; struct list_head head;};
1
2
3
4
struct nmi_desc {
spinlock_t lock;
struct list_head head;
};
接下來讓我們看一些內核使用 list_head 的具體例子。正如在前文所述的,Linux 內核中諸多模塊都使用了 list_head。這里我們以內核雜項字符設備驅動(miscellaneous character drivers)部分實現為例。驅動的 API 在 drivers/char/misc.c 中,其實現了簡單硬件外設以及虛擬設備的驅動,這個驅動共享主設備號(Major number):
C
#define MISC_MAJOR 10
1
#define MISC_MAJOR??????????????10
每個設備有自己的次設備號,具體可以看這個列子:
ls -l /dev | grep 10crw------- 1 root root 10, 235 Mar 21 12:01 autofsdrwxr-xr-x 10 root root 200 Mar 21 12:01 cpucrw------- 1 root root 10, 62 Mar 21 12:01 cpu_dma_latencycrw------- 1 root root 10, 203 Mar 21 12:01 cusedrwxr-xr-x 2 root root 100 Mar 21 12:01 dricrw-rw-rw- 1 root root 10, 229 Mar 21 12:01 fusecrw------- 1 root root 10, 228 Mar 21 12:01 hpetcrw------- 1 root root 10, 183 Mar 21 12:01 hwrngcrw-rw----+ 1 root kvm 10, 232 Mar 21 12:01 kvmcrw-rw---- 1 root disk 10, 237 Mar 21 12:01 loop-controlcrw------- 1 root root 10, 227 Mar 21 12:01 mcelogcrw------- 1 root root 10, 59 Mar 21 12:01 memory_bandwidthcrw------- 1 root root 10, 61 Mar 21 12:01 network_latencycrw------- 1 root root 10, 60 Mar 21 12:01 network_throughputcrw-r----- 1 root kmem 10, 144 Mar 21 12:01 nvrambrw-rw---- 1 root disk 1, 10 Mar 21 12:01 ram10crw--w---- 1 root tty 4, 10 Mar 21 12:01 tty10crw-rw---- 1 root dialout 4, 74 Mar 21 12:01 ttyS10crw------- 1 root root 10, 63 Mar 21 12:01 vga_arbitercrw------- 1 root root 10, 137 Mar 21 12:01 vhci
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
ls -l /dev |??grep 10
crw-------?? 1 root root???? 10, 235 Mar 21 12:01 autofs
drwxr-xr-x??10 root root???????? 200 Mar 21 12:01 cpu
crw-------?? 1 root root???? 10,??62 Mar 21 12:01 cpu_dma_latency
crw-------?? 1 root root???? 10, 203 Mar 21 12:01 cuse
drwxr-xr-x?? 2 root root???????? 100 Mar 21 12:01 dri
crw-rw-rw-?? 1 root root???? 10, 229 Mar 21 12:01 fuse
crw-------?? 1 root root???? 10, 228 Mar 21 12:01 hpet
crw-------?? 1 root root???? 10, 183 Mar 21 12:01 hwrng
crw-rw----+??1 root kvm??????10, 232 Mar 21 12:01 kvm
crw-rw----?? 1 root disk???? 10, 237 Mar 21 12:01 loop-control
crw-------?? 1 root root???? 10, 227 Mar 21 12:01 mcelog
crw-------?? 1 root root???? 10,??59 Mar 21 12:01 memory_bandwidth
crw-------?? 1 root root???? 10,??61 Mar 21 12:01 network_latency
crw-------?? 1 root root???? 10,??60 Mar 21 12:01 network_throughput
crw-r-----?? 1 root kmem???? 10, 144 Mar 21 12:01 nvram
brw-rw----?? 1 root disk??????1,??10 Mar 21 12:01 ram10
crw--w----?? 1 root tty?????? 4,??10 Mar 21 12:01 tty10
crw-rw----?? 1 root dialout?? 4,??74 Mar 21 12:01 ttyS10
crw-------?? 1 root root???? 10,??63 Mar 21 12:01 vga_arbiter
crw-------?? 1 root root???? 10, 137 Mar 21 12:01 vhci
現在我們看看設備驅動是如何使用鏈表維護設備列表的,首先,我們看一下 miscdevice 的 struct 定義:
C
struct miscdevice{ int minor; const char *name; const struct file_operations *fops; struct list_head list; struct device *parent; struct device *this_device; const char *nodename; mode_t mode;};
1
2
3
4
5
6
7
8
9
10
11
struct miscdevice
{
int minor;
const char *name;
const struct file_operations *fops;
struct list_head list;
struct device *parent;
struct device *this_device;
const char *nodename;
mode_t mode;
};
可以看到 miscdevice 的第四個成員 list ,這個就是用于維護已注冊設備鏈表的結構。在源代碼文的首部,我們可以看到以下定義:
C
static LIST_HEAD(misc_list);
1
static LIST_HEAD(misc_list);
這個定義宏展開,可以看到是用于定義 list_head 類型變量:
C
#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
1
2
#define LIST_HEAD(name)
struct list_head name = LIST_HEAD_INIT(name)
LIST_HEAD_INIT 這個宏用于對定義的變量進行雙向指針的初始化:
C
#define LIST_HEAD_INIT(name) { &(name), &(name) }
1
#define LIST_HEAD_INIT(name) { &(name), &(name) }
現在我看可以看一下函數 misc_register 是如何進行設備注冊的。首先是用 INIT_LIST_HEAD 對 miscdevice->list 成員變量進行初始化:
C
INIT_LIST_HEAD(&misc->list);
1
INIT_LIST_HEAD(&misc->list);
這個操作與 LIST_HEAD_INIT 宏一致:
C
static inline void INIT_LIST_HEAD(struct list_head *list){ list->next = list; list->prev = list;}
1
2
3
4
5
static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
}
接下來,在通過函數 device_create 進行設備創建,同時將設備添加到 Misc 設備列表中:
C
list_add(&misc->list, &misc_list);
1
list_add(&misc->list, &misc_list);
內核的 list.h文件提供向鏈表添加節點的 API,這里是添加操作的實現:
C
static inline void list_add(struct list_head *new, struct list_head *head){ __list_add(new, head, head->next);}
1
2
3
4
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
函數實現很簡單,就是入參轉換為三個參數后調用內部 __list_add :
new – 新節點;
head – 新節點插入的雙向鏈表頭;
head->next – 鏈表頭的下一個節點;
_list_add 函數的實現更加簡單:
C
static inline void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next){ next->prev = new; new->next = next; new->prev = prev; prev->next = new;}
1
2
3
4
5
6
7
8
9
static inline void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
這里設置了新添加結點的 prev 與 next 指針,通過這些操作,就將先前使用 LIST_HEAD_INIT 所定義的 misc 鏈表的雙向指針與 miscdevice->list 結構關聯起來。
這里還有一個問題,就是如何獲取鏈表中的數據,list_head 提供了一個特殊的宏用于獲取數據指針。
C
#define list_entry(ptr, type, member) container_of(ptr, type, member)
1
2
#define list_entry(ptr, type, member)
container_of(ptr, type, member)
這里有三個參數
ptr:list_head 結構指針
type:數據對應的 struct 類型
member:數據中 list_head 成員對應的成員變量名
舉例如下:
C
const struct miscdevice *p = list_entry(v, struct miscdevice, list)
1
const struct miscdevice *p = list_entry(v, struct miscdevice, list)
接下來我們就夠訪問 miscdevice 的各個成員,如 p->minor、p->name 等等,我們看一下 list_entry 的實現:
C
#define list_entry(ptr, type, member) container_of(ptr, type, member)
1
2
#define list_entry(ptr, type, member)
container_of(ptr, type, member)
其實現非常簡單,就是使用入參調用 container_of 宏,宏的實現如下:
C
#define container_of(ptr, type, member) ({ const typeof( ((type *)0)->member ) *__mptr = (ptr); (type *)( (char *)__mptr - offsetof(type,member) );})
1
2
3
#define container_of(ptr, type, member) ({??????????????????????
const typeof( ((type *)0)->member ) *__mptr = (ptr);????
(type *)( (char *)__mptr - offsetof(type,member) );})
注意,宏使用了大括號表達式,對于大括號表達式,編譯器會展開所有表達式,同時使用最后一個表達式的結果進行返回。
舉個例子:
C
#include int main() { int i = 0; printf("i = %dn", ({++i; ++i;})); return 0;}
1
2
3
4
5
6
7
#include
int main() {
int i = 0;
printf("i = %dn", ({++i; ++i;}));
return 0;
}
輸出結果為 2 。
另一個關鍵是 typeof 關鍵字,這個非常簡單,這個正如它的名字一樣,這個關鍵字返回的結果是變量的類型。當我第一次看到這個宏時,最讓我覺得奇怪的是表達式 ((type*)0) 中的 0 值,實際上,使用 0 值作為地址這個是成員變量取得 struct 內相對偏移地址的巧妙實現,我們再來看個例子:
C
#include struct s { int field1; char field2; char field3;};int main() { printf("%pn", &((struct s*)0)->field3); return 0;}
1
2
3
4
5
6
7
8
9
10
11
12
#include
struct s {
int field1;
char field2;
char field3;
};
int main() {
printf("%pn", &((struct s*)0)->field3);
return 0;
}
輸出結果為 0x5 。
還有一個專門用于獲取結構體中某個成員變量偏移的宏,其實現與前面提到的宏非常類似:
C
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
1
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
這里對 container_of 宏做個綜述,container_of 宏通過 struct 中的 list_head 成員返回 struct 對應數據的內存地址。在宏的第一行定義了指向 list_head 成員的指針 __mptr ,并將 ptr 地址賦給 __mptr 。從技術實現的角度來看,實際并不需要這一行定義,但這個對于類型檢查而言非常有意義。這一行代碼確保結構體( type )中存在 member 對應的成員。第二行使用 offsetoff 宏計算出包含 member 的結構體所對應的內存地址,就是這么簡單。
當然 list_add 與 list_entry 并非是 中的全部函數,對于雙向鏈表 list_head ,內核還提供了以下的接口:
list_add
list_add_tail
list_del
list_replace
list_move
list_is_last
list_empty
list_cut_position
list_splice
未了,需要說的是,內核代碼中并不僅僅只有上述這些接口。
?
評論