如何“固定"Linux 中的页面本身存在(或实际上“固定") [英] How do "pinned" pages in Linux present (or actually "pin") themselves
问题描述
我在 Linux 内核驱动程序中使用 get_user_pages
来固定内存以用于 [硬件] DMA.这一切似乎运行良好 - 但我很难证明固定"是正确的.
I am using get_user_pages
in a Linux kernel driver to pin memory for the purposes of [hardware] DMA. It all seems to work fine - but I am having a hard time proving that the "pinning" is doing the proper thing.
当我在执行 get_user_pages
后检查物理页面上的标志时 - 页面不会出现锁定"(人们可能认为它们应该是).事实上,我认为其他活动"页面的标志与我通过 get_user_pages
固定"的标志之间没有区别.
When I inspect the flags on the physical pages after doing get_user_pages
- the pages don't appear "locked" (as one might think they should be). In fact, I see no difference between the flags of otherwise "active" pages vs. those I have "pinned" via get_user_pages
.
我看到的唯一区别是 get_user_pages 在页面上获取了 refcount.所以我想我的问题是 - 保持这个引用单独足以保证这个页面永远不会被换出、移动,或者我的用户空间的 vaddr 仍然/总是引用相同的底层页面?
The only difference I see is that get_user_pages has taken a refcount on the page. So I guess my question is - is holding this reference alone sufficient to guarantee that this page will never be swapped-out, moved, or that my user-space's vaddr will still/always reference the same underlying page?
我能找到的所有驱动程序源似乎都使用这种机制,文档似乎表明这是正确的方法 - 但我很难证明"这会给我正确、可靠、预期的行为.
All the driver source I can find seems to use this mechanism, and documentation seems to indicate that this is the correct way - but I am having a hard time "proving" that this will give me the correct, reliable, intended behavior.
推荐答案
持有 refcount 看起来足以防止页面被推出、失效或迁移;因此对于 dma 类型的操作是安全的.迁移在 Documentation/vm/page_migrate 中讨论;其他人需要在 vm 代码中进行探索.简而言之,推送页面需要删除所有引用.
Holding a refcount looks sufficient to prevent the page from being pushed out, invalidated or migrated; thus safe for dma-type operations. Migration is discussed in Documentation/vm/page_migrate; the others require spelunking in the vm code. The short version is that to push a page requires getting all of its references dropped.
注意 refcount 和 mapcount 是不同的概念——mapcount 只是意味着某人对它有一个虚拟引用;refcount 意味着他们有一个实际的引用.换出的页面可以有很大的映射数.
Notice that refcount and mapcount are different notions -- mapcount just means somebody has a virtual reference to it; refcount means they have an actual reference to it. A swapped out page can have a large mapcount.
另请注意,截至 https://lkml.org/lkml/2019/11/25/684 有一个不那么晦涩的界面.
Also note that as of https://lkml.org/lkml/2019/11/25/684 there is a less obscure interface for this.
这篇关于如何“固定"Linux 中的页面本身存在(或实际上“固定")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!