> For the complete documentation index, see [llms.txt](https://richardweiyang-2.gitbook.io/kernel-exploring/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://richardweiyang-2.gitbook.io/kernel-exploring/00-index-3/10-list_lru.md).

# list\_lru

## 长什么样

```
   struct list_lru
   +-----------------------------+
   |node                         |  这其实是一个数组，__list_lru_init中用kzalloc_objs()分配
   |    (struct list_lru_node*)  |
   |   +-------------------------+
   |   |nr_items                 |  这个比较tricky，如果是memcg上的，也会算到这里
   |   |    (atomic_long_t)      |  所以这是整个node上，链表中个数
   |   |lru                      |
   |   |    (struct list_lru_one)|
   |   |    +--------------------+
   |   |    |list                |
   |   |    |  (struct list_head)|
   |   |    |nr_items            |  list中成员的个数
   |   |    |  (long)            |
   |   |    |lock                |  保护lru的锁
   |   |    |  (spinlock_t)      |
   +---+----+--------------------+
   |list                         |  这部分只有CONFIG_MEMCG时才有
   |    (struct list_head)       |
   |shrinker_id                  |
   |    (int)                    |
   |memcg_aware                  |
   |    (bool)                   |
   |xa                           |---->struct list_lru_memcg
   |    (struct xarray)          |     +------------------------------+
   +-----------------------------+     |node[]                        |
                                       |    (struct list_lru_one)     |
                                       |    +-------------------------+
                                       |    |list                     |
                                       |    |  (struct list_head)     |
                                       |    |nr_items                 |
                                       |    |  (long)                 |
                                       |    |lock                     |
                                       |    |  (spinlock_t)           |
                                       +----+-------------------------+
```

## 使用方法

### 初始化

初始化一共有两个list\_lru\_init()和list\_lru\_init\_memcg()。但最后都走到了\_\_list\_lru\_init()。

其中值得注意的是node这个字段是一个数组，在\_\_list\_lru\_init()中用kzalloc\_objs()给每一个node分配，并通过init\_one\_lru()初始化。

如果配置了CONFIG\_MEMCG，还可能将这个lru\_list注册到memcg\_list\_lrus上。

### 添加/删除

添加删除分别是list\_lru\_add()和list\_lru\_del()。

### 遍历

list\_lru\_walk()
