> 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/nei-cun-guan-li/00-index/08-numa_balance.md).

# numa balance

在上一节中我们对内存分配策略做了一定的了解，并且留了一个话题 -- numa balance。也就是内核为了在进程迁移过程中保持内存分配策略做的工作。

进程在运行过程中有可能会迁移到不同的cpu上执行，如果被迁移到的cpu和内存之前不满足mempolicy，那么numa balance就会尝试将用符合策略的内存来替换。所以整个过程分为两个步骤：

* 扫描内存是否符合策略，并将不符合的pte设置为pronnone
* 通过numa hinting page fault替换页

## 扫描进程空间

说到扫描就有一个扫描时机，当使用cfs调度时，这个时机由task\_tick\_fair时刻触发。经过一系列的检查后，会插入curr->numa\_work等待执行。

这个numa\_work的回调函数是task\_numa\_work，而这个函数的核心就是扫描符合条件的vma，并通过change\_prot\_numa把对应的pte属性添加上PAGE\_NONE。

## numa hinting page fault


---

# 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:

```
GET https://richardweiyang-2.gitbook.io/kernel-exploring/nei-cun-guan-li/00-index/08-numa_balance.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
