> 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()


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://richardweiyang-2.gitbook.io/kernel-exploring/00-index-3/10-list_lru.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
