Linux Kernel中list解读

来源:互联网 发布:鼎尖软件 编辑:程序博客网 时间:2024/04/29 17:42

作者:Sam (甄峰) sam_code@hotmail.com

 

Sam之前看2.4kernel时,常看到List.也仔细看了一下,但现在长期没有看kernel,没有写程序,已经忘记了很多。今天又看一看并记录下来。

 

LinuxKernel中,常常需要使用双向链表。在~/include/linux/list.h中,就定义了双向链表和常用的function.

 

链表头如下:

struct list_head {
 struct list_head *next, *prev;
};

 

1.创建双向链表(doubly linkedlist):

INIT_LIST_HEAD(struct list_head*list)

代码如下:

static inline void INIT_LIST_HEAD(struct list_head *list)
{
 list->next = list;
 list->prev = list;
}
将List的头和尾都指向自身。

 

2. 添加内容到双向链表:

2.1: 平常的添加:

2.1.1:将新项目添加到list的头部(head之后第一个位置)。注意,此处head是指此双向链表头。

void list_add(struct list_head *new, struct list_head *head)

将参数一(new)添加到head之后。它调用

__list_add(new, head,head->next);也就是说,把new添加到head和head->next之间。

static inline void __list_add(structlist_head *new,
        struct list_head *prev,
        struct list_head *next) //它只是将new添加到prev和next之间
{
 next->prev = new;
 new->next = next;
 new->prev = prev;
 prev->next = new;
}

 

2.1.2:将新项目添加双向链表最后一个位置(也就是head的priv)。注意此处head表示list头。

static inline void list_add_tail(structlist_head *new, struct list_head *head)
{
 __list_add(new, head->prev,head);
}

则将new添加到head->prev和head之间了。

 

2.2:读拷贝更新(rcu)模式的添加(smp_wmb())(请看背景知识)

2.2.1: 将新项目加到以知的prev和next之间:

static inline void __list_add_rcu(structlist_head * new,
  struct list_head * prev, structlist_head * next)
{
 new->next = next;
 new->prev = prev;
 smp_wmb();
 next->prev = new;
 prev->next = new;
}//此处注意:smp_wmb();
smp_wmb()防止编译器和CPU优化代码执行的顺序。在这里,smp_wmb保证在它之前的两行代码执行完了之后再执行后两行

 

2.2.2:将新项目添加到list的头部(head之后第一个位置)。注意,此处head是指此双向链表头。

static inline void list_add_rcu(structlist_head *new, struct list_head *head)
{
 __list_add_rcu(new, head,head->next);
}

 

2.2.3:将新项目添加双向链表最后一个位置(也就是head的priv)。注意此处head表示list头。staticinline void list_add_tail_rcu(struct list_head *new,
     structlist_head *head)
{
 __list_add_rcu(new, head->prev,head);
}

 

 

3. 从双向链表删除项目:

3.1:基本删除函数:

static inline void __list_del(structlist_head * prev, struct list_head * next)
{
 next->prev = prev;
 prev->next = next;
}//只是将前一个和后一个互指

 

3.2:删除指定项:

static inline void list_del(structlist_head *entry)
{
 __list_del(entry->prev,entry->next);
 entry->next = LIST_POISON1;
 entry->prev = LIST_POISON2;
}

 

3.3: 安全的删除指定项:

static inline void list_del_rcu(structlist_head *entry)
{
 __list_del(entry->prev,entry->next);
 entry->prev = LIST_POISON2;
}

此处Sam并不很清楚怎么回事。

 

3.4:删除并初始化某一项:

static inline void list_del_init(structlist_head *entry)
{
 __list_del(entry->prev,entry->next);
 INIT_LIST_HEAD(entry);
}

 

4.替换某项:

4.1 使用new 替换 old:

static inline void list_replace(structlist_head *old,
    structlist_head *new)
{
 new->next =old->next;
 new->next->prev =new;
 new->prev =old->prev;
 new->prev->next =new;
}

 

4.2 替换并初始化:

static inline voidlist_replace_init(struct list_head *old,
     structlist_head *new)
{
 list_replace(old, new);
 INIT_LIST_HEAD(old);
}

 

4.3:安全替换:

static inline void list_replace_rcu(structlist_head *old,
    structlist_head *new)
{
 new->next =old->next;
 new->prev =old->prev;
 smp_wmb();
 new->next->prev =new;
 new->prev->next =new;
 old->prev = LIST_POISON2;
}

 

5. 移动项:

5.1移动到头部

static inline void list_move(structlist_head *list, struct list_head *head)
{
 __list_del(list->prev,list->next);
 list_add(list, head);
}

 

 

5.2移动到尾部
static inline void list_move_tail(structlist_head *list,
     struct list_head *head)
{
 __list_del(list->prev,list->next);
 list_add_tail(list, head);
}

6.测试项目是否为最后一项:

static inline int list_is_last(conststruct list_head *list,
    conststruct list_head *head)
{
 return list->next == head;
}

7. 测试list是否为空:

static inline int list_empty(const structlist_head *head)
{
 return head->next == head;
}

8. 两个链表连接起来:

8.1:将list链表连接如head链表头部:

static inline void __list_splice(structlist_head *list,
    struct list_head *head)
{
 struct list_head *first =list->next;
 struct list_head *last =list->prev;
 struct list_head *at =head->next;

 first->prev =head;
 head->next = first;

 last->next = at;
 at->prev = last;
}

8.2:连接

static inline void list_splice(structlist_head *list, struct list_head *head)
{
 if (!list_empty(list))
  __list_splice(list,head);
}

 

8.3:连接并初始化:

将list连接到head头部,再将list初始化:

static inline void list_splice_init(structlist_head *list,
       struct list_head *head)
{
 if (!list_empty(list)) {
  __list_splice(list,head);
  INIT_LIST_HEAD(list);
 }
}

 

9.一些有用的宏:

9.1得到 list_entry(ptr, type, member)

简单的讲,这个宏的作用是:通过结构(type)中的某个变量(member)的指针(ptr)获取结构本身的指针.

也就是说,type中包含一个成员变量member.且某个结构体实体中member的指针为ptr.则list_entry()则返回的是:这个结构体实体的指针。至于如何做到的,请看背景知识3---container_of。

 

9.2:list_first_entry(ptr, type,member) 
得到ptr链表中下一个的struct的实体。

 

9.3:  list_for_each(pos,head)

#define list_for_each(pos, head) \
 for (pos = (head)->next;prefetch(pos->next), pos != (head); \
        pos = pos->next)

它其实就是一个for循环,循环双向链表一圈。

prefetch()是档案快取技术,不用深究。

 

下面几个宏与之类似:

__list_for_each(pos, head)  //不用档案快取技术的循环

list_for_each_prev(pos, head) //向前循环

 

9.4: list_for_each_entry(pos,head, member)

这个宏是双向链表中最常用的,也是最有用的。表示从以head为头的双向循环列表中,一个一个拿出包含此list项目的结构体(pos的类型),并放到pos中。

#define list_for_each_entry(pos, head,member)    \
 for (pos =list_entry((head)->next, typeof(*pos),member); \
     prefetch(pos->member.next),&pos->member != (head); \
     pos = list_entry(pos->member.next, typeof(*pos),member))

因为有上面list_entry()的铺垫,所以非常简单。

参数一:pos就是一个结构体指针。这个结构体中会包含成员变量member.

参数二:head就是一个双向链表头。

参数三:pos结构体中的成员变量名。

pos = list_entry((head)->next, typeof(*pos),member):pos得到双向链表中第一个链表被包含的结构体实体。

&pos->member !=(head):此结构体中的链表不是头。

pos = list_entry(pos->member.next, typeof(*pos),member): pos得到双向链表中下一个结构体实体。

 

 

Linux kernel 中双向循环链表的使用:

在Linux内核链表中,需要用链表组织起来的数据通常会包含一个structlist_head成员,结构都通过这个list成员组织在一个链表中。

例如:在hid-core.c中,要组织一个report链表。

 

于是,首先使用

1)

INIT_LIST_HEAD(&device->report_enum[i].report_list)

struct hid_report {
 struct list_head list;
 unsignedid;     
 unsignedtype;     
 struct hid_field*field[HID_MAX_FIELDS]; 
 unsignedmaxfield;    
 unsignedsize;     
 struct hid_device*device;   
};

这就是需要用链表组织起来的数据通常会包含一个struct list_head成员。

 

2)。

list_add_tail(&report->list,&report_enum->report_list);

 将report类型的项目添加到刚才初始化的list中。

 

3).

list_for_each_entry(report,&hid->report_enum[HID_INPUT_REPORT].report_list,list)

遍历 hid->report_enum[HID_INPUT_REPORT].report_list,从其中一个一个得到report.放到report中。

 

 

 

背景知识:

背景知识一:typeof:

typeof不是标准C的运算符,这是gcc的一个扩展.

它与sizeof() 语义类似,sizeof(exp)代表返回exp长度。则typeof(exp)返回的事exp类型。

 

例1:

int a;

typeof(&a) b;

因为a 为int型。所以&a为int*.

也就是说b 为int* 类型。

 

例2:

typedef struct

{

int size;

char t;

} ngate, *pngate;

 

typeof(((ngate *)0)->t) w;

这其实就是表示,w 的类型为:ngate的t的类型。

在这里0并不是真正的变量,可以把它理解为一个替代使用的符号。其意思更可以理解为一个被赋值了的变量,这个数可以不是0,,随便什么数字都可以。


 

背景知识二:offsetof

kernel中定义如下:

#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)

与上面所以类似,(TYPE *)0 表示:0是指向TYPE的指针

则 &(TYPE *)0->MEMBER表示:TYPE类型的实体0的变量MEMBER的地址,因为从0开始,所以它的地址就成为offset.再用size_t强制转换,就是从struct头到成员变量MEMBER的offset.

 

背景知识三:container_of(ptr, type,member)

Kernel中如下定义:

#define container_of(ptr, type, member)({   \
 const typeof( ((type *)0)->member) *__mptr = (ptr); \
 (type *)( (char *)__mptr - offsetof(type,member));})

(type *)0: 表明某个实体为type类型的。

((type *)0)->member表明这个实体的某个成员变量。

typeof(((type *)0)->member) *__mptr表明定了一个指向此成员变量类型的指针。

 

offsetof(type,member)表明成员变量member到结构体类型type头的offset.

(type *)( (char*)__mptr - offsetof(type,member)则表明:返回的是一个指向type的指针,此指针指向一个type类型的实体。而参数ptr则是这个实体中的某一个成员变量位置。

 

 

背景知识四:RCU(Read-CopyUpdate)

RCU是2.5/2.6内核中引入的新技术,它通过延迟写操作来提高同步性能。

系统中数据读取操作远多于写操作,而rwlock机制在smp环境下随着处理机增多性能会迅速下降。针对这一应用背景,IBMLinux技术中心的Paul E.McKenney提出了"读拷贝更新"的技术,并将其应用于Linux内核中。RCU技术的核心是写操作分为写-更新两步,允许读操作在任何时候无阻访问,当系统有写操作时,更新动作一直延迟到对该数据的所有读操作完成为止。